Added the payload type idenfifiers to attributes.

Change-Id: Iec4231e5312ed408d9f49fbf8fba7889c31f70f7
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/AttributeSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/AttributeSpans.java
index 91b85bf..9dcdbd5 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/AttributeSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/AttributeSpans.java
@@ -50,6 +50,16 @@
     private int currentDoc, currentPosition;
     private boolean isFinish;
 
+	public static enum PayloadTypeIdentifier {
+		TERM_ATTRIBUTE(16), ELEMENT_ATTRIBUTE(17), RELATION_ATTRIBUTE(18);
+
+		private int value;
+
+		private PayloadTypeIdentifier(int value) {
+			this.value = value;
+		}
+	}
+
     protected Logger logger = LoggerFactory.getLogger(AttributeSpans.class);
 
 
@@ -154,24 +164,25 @@
      */
     private CandidateAttributeSpan createCandidateSpan () throws IOException {
         List<byte[]> payload = (List<byte[]>) firstSpans.getPayload();
-        ByteBuffer wrapper = ByteBuffer.wrap(payload.get(0));
+		ByteBuffer payloadBuffer = ByteBuffer.wrap(payload.get(0));
 
-        short spanId;
-        int start = 0, end;
+		byte payloadTypeIdentifier = payloadBuffer.get(0);
+		short spanId = payloadBuffer.getShort(5);
+		// if (payload.get(0).length == 6) {
+		int end = payloadBuffer.getInt(1);
 
-        if (payload.get(0).length == 6) {
-            end = wrapper.getInt(0);
-            spanId = wrapper.getShort(4);
-            return new CandidateAttributeSpan(firstSpans, spanId, end);
-        }
-        else if (payload.get(0).length == 10) {
-            start = wrapper.getInt(0);
-            end = wrapper.getInt(4);
-            spanId = wrapper.getShort(8);
-            return new CandidateAttributeSpan(firstSpans, spanId, start, end);
-        }
+		return new CandidateAttributeSpan(firstSpans, payloadTypeIdentifier,
+				spanId, end);
 
-        throw new NullPointerException("Missing element end in payloads.");
+		// }
+		// else if (payload.get(0).length == 10) {
+		// start = wrapper.getInt(0);
+		// end = wrapper.getInt(4);
+		// spanId = wrapper.getShort(8);
+		// return new CandidateAttributeSpan(firstSpans, spanId, start, end);
+		// }
+
+		// throw new NullPointerException("Missing element end in payloads.");
     }
 
 
@@ -235,9 +246,6 @@
     class CandidateAttributeSpan extends CandidateSpan implements
             Comparable<CandidateSpan> {
 
-        private short spanId;
-
-
         /**
          * Construct a CandidateAttributeSpan based on the given span,
          * spanId,
@@ -258,32 +266,23 @@
          *            belongs to.
          * @throws IOException
          */
-        public CandidateAttributeSpan (Spans span, short spanId, int elementEnd)
+		public CandidateAttributeSpan(Spans span, byte payloadTypeIdenfitier,
+				short spanId, int elementEnd)
                 throws IOException {
             super(span);
-            setSpanId(spanId);
+			this.spanId = spanId;
             this.end = elementEnd;
+			this.payloadTypeIdentifier = payloadTypeIdenfitier;
         }
 
 
-        public CandidateAttributeSpan (Spans span, short spanId, int start,
-                                       int end) throws IOException {
-            super(span);
-            setSpanId(spanId);
-            this.start = start;
-            this.end = end;
-        }
-
-
-        public void setSpanId (short spanId) {
-            this.spanId = spanId;
-        }
-
-
-        public short getSpanId () {
-            return spanId;
-        }
-
+		// public CandidateAttributeSpan (Spans span, short spanId, int start,
+		// int end) throws IOException {
+		// super(span);
+		// setSpanId(spanId);
+		// this.start = start;
+		// this.end = end;
+		// }
 
         @Override
         public int compareTo (CandidateSpan o) {
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/CandidateSpan.java b/src/main/java/de/ids_mannheim/korap/query/spans/CandidateSpan.java
index 8198195..1411f60 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/CandidateSpan.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/CandidateSpan.java
@@ -28,6 +28,7 @@
     private int leftStart, leftEnd;
     private int rightStart, rightEnd;
 
+	protected byte payloadTypeIdentifier;
 
     /**
      * Constructs a CandidateSpan for the given Span.
@@ -359,7 +360,15 @@
     }
 
 
-    @Override
+	public byte getPayloadTypeIdentifier() {
+		return payloadTypeIdentifier;
+	}
+
+	public void setPayloadTypeIdentifier(byte payloadTypeIdentifier) {
+		this.payloadTypeIdentifier = payloadTypeIdentifier;
+	}
+
+	@Override
     public int compareTo (CandidateSpan o) {
         if (this.doc == o.doc) {
             if (this.getStart() == o.getStart()) {
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java
index 49ac41e..597f322 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java
@@ -80,6 +80,9 @@
 
     @Override
     public boolean next () throws IOException {
+        if (!hasMoreSpans || !(hasMoreSpans = termSpans.next()))
+            return false;
+        
 		isStartEnumeration = false;
 		this.matchPayload = null;
 		matchEndPosition = -1;
@@ -87,8 +90,7 @@
 	};
 
 	private boolean advance() throws IOException {
-		if (!hasMoreSpans || !(hasMoreSpans = termSpans.next()))
-			return false;
+		
 
 		this.matchStartPosition = termSpans.start();
 		this.matchDocNumber = termSpans.doc();		
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestAttributeIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestAttributeIndex.java
index ceee21d..9ec7b4d 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestAttributeIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestAttributeIndex.java
@@ -36,33 +36,33 @@
         fd.addTV(
                 "base",
                 "bcbabd",
-				"[(0-1)s:a|_1#0-1|"						
+				"[(0-1)s:a|_1$<i>0<i>1|"
 				+ "<>:div$<b>65<i>0<i>2<i>2<b>0<s>2|"
 				+ "<>:div$<b>65<i>0<i>3<i>3<b>0<s>1|"
 				+ "<>:s$<b>65<i>0<i>5<i>5<b>0<s>3|"
-				+ "@:class=header$<i>3<s>1|@:class=header$<i>2<s>2]"
+						+ "@:class=header$<b>17<i>3<s>1|@:class=header$<b>17<i>2<s>2]"
 
-				+ "[(1-2)s:e|_2#1-2|"
-				+ "<>:a$<b>65<i>1<i>2<i>2<b>0<s>1|@:class=header$<i>2<s>1]"
+						+ "[(1-2)s:e|_2$<i>1<i>2|"
+						+ "<>:a$<b>65<i>1<i>2<i>2<b>0<s>1|@:class=header$<b>17<i>2<s>1]"
   
-				+ "[(2-3)s:e|_3#2-3|"
-				+ "<>:div$<b>65<i>2<i>5<i>5<b>0<s>1|@:class=time$<i>5<s>1]"
+						+ "[(2-3)s:e|_3$<i>2<i>3|"
+						+ "<>:div$<b>65<i>2<i>5<i>5<b>0<s>1|@:class=time$<b>17<i>5<s>1]"
   
-				+ "[(3-4)s:a|_4#3-4|"
-				+ "<>:div$<b>65<i>3<i>5<i>5<b>0<s>1|@:class=header$<i>5<s>1]"
+						+ "[(3-4)s:a|_4$<i>3<i>4|"
+						+ "<>:div$<b>65<i>3<i>5<i>5<b>0<s>1|@:class=header$<b>17<i>5<s>1]"
   
-				+ "[(4-5)s:b|_5#4-5|"
+						+ "[(4-5)s:b|_5$<i>4<i>5|"
 				+ "<>:div$<b>65<i>4<i>5<i>5<b>0<s>1|"
-				+ "<>:a$<b>65<i>4<i>5<i>5<b>0<s>2|@:class=header$<i>5<s>2]"
+						+ "<>:a$<b>65<i>4<i>5<i>5<b>0<s>2|@:class=header$<b>17<i>5<s>2]"
 		  
-				+ "[(5-6)s:d|_6#5-6|"
+						+ "[(5-6)s:d|_6$<i>5<i>6|"
 				+ "<>:s$<b>65<i>5<i>6<i>6<b>0<s>1|"
-				+ "<>:div$<b>65<i>5<i>6<i>6<b>0<s>2|@:class=header$<i>6<s>1]"
+						+ "<>:div$<b>65<i>5<i>6<i>6<b>0<s>2|@:class=header$<b>17<i>6<s>1]"
   
-				+ "[(6-7)s:d|_7#6-7|"
+						+ "[(6-7)s:d|_7$<i>6<i>7|"
 				+ "<>:div$<b>65<i>6<i>7<i>7<b>0<s>1"
 				+ "<>:s$<b>65<i>6<i>7<i>7<b>0<s>2|"
-				+ "|@:class=header$<i>7<s>1|@:class=header$<i>7<s>2]");
+						+ "|@:class=header$<b>17<i>7<s>1|@:class=header$<b>17<i>7<s>2]");
 
         return fd;
     }
@@ -74,24 +74,27 @@
         fd.addTV(
                 "base",
                 "bcbabd",
-				"[(0-1)s:b|_1#0-1|"
-						+ "<>:div$<b>65<i>0<i>3<i>3<b>0<s>1|@:class=header$<i>3<s>1|@:class=title$<i>3<s>1|@:class=book$<i>3<s>1]"
+				"[(0-1)s:b|_1$<i>0<i>1|"
+						+ "<>:div$<b>65<i>0<i>3<i>3<b>0<s>1|"
 						+ "<>:s<b>65<i>0<i>5<i>5<b>0<s>2|"
-						+ "[(1-2)s:c|_2#1-2|"
-						+ "<>:div$<b>65<i>1<i>2<i>2<b>0<s>1|@:class=header$<i>2<s>1|@:class=title$<i>2<s>1]"
-						+ "[(2-3)s:b|_3#2-3|"
-						+ "<>:div$<b>65<i>2<i>3<i>5<b>0<s>1|@:class=book$<i>5<s>1]"
-						+ "[(3-4)s:a|_4#3-4|"
-						+ "<>:div$<b>65<i>3<i>5<i>5<b>0<s>1|@:class=title$<i>5<s>1]"
-						+ "[(4-5)s:b|_5#4-5|"
-						+ "<>:div$<b>65<i>4<i>5<i>5<b>0<s>1|@:class=header$<i>5<s>1|@:class=book$<i>5<s>1|@:class=title$<i>5<s>1]"
-						+ "[(5-6)s:d|_6#5-6|"						
-						+ "<>:div$<b>65<i>5<i>6<i>6<b>0<s>1|@:class=header$<i>6<s>1]"
+						+ "@:class=header$<b>17<i>3<s>1|@:class=title$<b>17<i>3<s>1|@:class=book$<b>17<i>3<s>1]"
+						+ "[(1-2)s:c|_2$<i>1<i>2|"
+						+ "<>:div$<b>65<i>1<i>2<i>2<b>0<s>1|"
+						+ "@:class=header$<b>17<i>2<s>1|@:class=title$<b>17<i>2<s>1]"
+						+ "[(2-3)s:b|_3$<i>2<i>3|"
+						+ "<>:div$<b>65<i>2<i>5<i>5<b>0<s>1|@:class=book$<b>17<i>5<s>1]"
+						+ "[(3-4)s:a|_4$<i>3<i>4|"
+						+ "<>:div$<b>65<i>3<i>5<i>5<b>0<s>1|@:class=title$<b>17<i>5<s>1]"
+						+ "[(4-5)s:b|_5$<i>4<i>5|"
+						+ "<>:div$<b>65<i>4<i>5<i>5<b>0<s>1|"
+						+ "@:class=header$<b>17<i>5<s>1|@:class=book$<b>17<i>5<s>1|@:class=title$<b>17<i>5<s>1]"
+						+ "[(5-6)s:d|_6$<i>5<i>6|"
+						+ "<>:div$<b>65<i>5<i>6<i>6<b>0<s>1|@:class=header$<b>17<i>6<s>1]"
 						+ "<>:s$<b>65<i>5<i>6<i>6<b>0<s>2|"
-						+ "[(6-7)s:d|_7#6-7|"
+						+ "[(6-7)s:d|_7$<i>6<i>7|"
 						+ "<>:div$<b>65<i>6<i>7<i>7<b>0<s>1|"
 						+ "<>:s$<b>65<i>6<i>7<i>7<b>0<s>2|"
-						+ "@:class=header$<i>7<s>1|@:class=title$<i>7<s>1]");
+						+ "@:class=header$<b>17<i>7<s>1|@:class=title$<b>17<i>7<s>1]");
 
         return fd;
     }
@@ -103,26 +106,27 @@
         fd.addTV(
                 "base",
                 "bcbabd",
-				"[(0-1)s:b|_1#0-1|"
+				"[(0-1)s:b|_1$<i>0<i>1|"
 						+ "<>:s$<b>65<i>0<i>5<i>5<b>0<s>1|"
-						+ "<>:div$<b>65<i>0<i>3<i>3<b>0<s>2|@:class=header$<i>3<s>2|@:class=book$<i>5<s>1|@:class=book$<i>3<s>2]"
-						+ "[(1-2)s:e|_2#1-2|"
+						+ "<>:div$<b>65<i>0<i>3<i>3<b>0<s>2|"
+						+ "@:class=header$<b>17<i>3<s>2|@:class=book$<b>17<i>5<s>1|@:class=book$<b>17<i>3<s>2]"
+						+ "[(1-2)s:e|_2$<i>1<i>2|"
 						+ "<>:div$<b>65<i>1<i>2<i>2<b>0<s>1|"
-						+ "<>:a$<b>65<i>1<i>2<i>2<b>0<s>2|@:class=book$<i>2<s>2|@:class=header$<i>2<s>1]"
-						+ "[(2-3)s:b|_3#2-3|"
+						+ "<>:a$<b>65<i>1<i>2<i>2<b>0<s>2|@:class=book$<b>17<i>2<s>2|@:class=header$<b>17<i>2<s>1]"
+						+ "[(2-3)s:b|_3$<i>2<i>3|"
 						+ "<>:div$<b>65<i>2<i>3<i>5<b>0<s>1|"
-						+ "<>:a$<b>65<i>1<i>2<i>2<b>0<s>2|@:class=header$<i>2<s>2|@:class=book$<i>5<s>1]"
-						+ "[(3-4)s:a|_4#3-4|"
-						+ "<>:div$<b>65<i>3<i>5<i>5<b>0<s>1|@:class=title$<i>5<s>1]"
-						+ "[(4-5)s:b|_5#4-5|"
-						+ "<>:div$<b>65<i>4<i>5<i>5<b>0<s>1|@:class=header$<i>5<s>1|@:class=book$<i>5<s>1]"
-						+ "[(5-6)s:d|_6#5-6|"
+						+ "<>:a$<b>65<i>1<i>2<i>2<b>0<s>2|@:class=header$<b>17<i>2<s>2|@:class=book$<b>17<i>5<s>1]"
+						+ "[(3-4)s:a|_4$<i>3<i>4|"
+						+ "<>:div$<b>65<i>3<i>5<i>5<b>0<s>1|@:class=title$<b>17<i>5<s>1]"
+						+ "[(4-5)s:b|_5$<i>4<i>5|"
+						+ "<>:div$<b>65<i>4<i>5<i>5<b>0<s>1|@:class=header$<b>17<i>5<s>1|@:class=book$<b>17<i>5<s>1]"
+						+ "[(5-6)s:d|_6$<i>5<i>6|"
 						+ "<>:s$<b>65<i>5<i>6<i>6<b>0<s>1|"
-						+ "<>:div$<b>65<i>5<i>6<i>6<b>0<s>1|@:class=header$<i>6<s>1]"
-						+ "[(6-7)s:d|_7#6-7|"
+						+ "<>:div$<b>65<i>5<i>6<i>6<b>0<s>1|@:class=header$<b>17<i>6<s>1]"
+						+ "[(6-7)s:d|_7$<i>6<i>7|"
 						+ "<>:div$<b>65<i>6<i>7<i>7<b>0<s>1|"
 						+ "<>:s$<b>65<i>6<i>7<i>7<b>0<s>2|"
-						+ "@:class=header$<i>7<s>1|@:class=book$<i>7<s>2]");
+						+ "@:class=header$<b>17<i>7<s>1|@:class=book$<b>17<i>7<s>2]");
 
         return fd;
     }
@@ -317,7 +321,13 @@
                 new SpanElementQuery("base", "div"), sql, true);
 
         kr = ki.search(sq, (short) 10);
-        assertEquals((long) 6, kr.getTotalResults());
+
+        // for (int i = 0; i < kr.getTotalResults(); i++) {
+        // System.out.println(kr.getMatch(i).getLocalDocID() + " "
+        // + kr.getMatch(i).startPos + " " + kr.getMatch(i).endPos);
+        // }
+
+		assertEquals((long) 6, kr.getTotalResults());
 
         SpanNextQuery snq = new SpanNextQuery(new SpanTermQuery(new Term(
                 "base", "s:e")), sq);
@@ -383,10 +393,6 @@
         assertEquals(4, kr.getMatch(1).getStartPos());
         assertEquals(5, kr.getMatch(1).getEndPos());
 
-        //		for (int i = 0; i < kr.getTotalResults(); i++) {
-        //			System.out.println(kr.getMatch(i).getLocalDocID() + " "
-        //					+ kr.getMatch(i).startPos + " " + kr.getMatch(i).endPos);
-        //		}
     }