Failing test for sorted subspans

Change-Id: Ic37621e0dd3b68c8a3e050cc0c9dc5be5fb735ff
diff --git a/src/main/java/de/ids_mannheim/korap/query/SpanSubspanQuery.java b/src/main/java/de/ids_mannheim/korap/query/SpanSubspanQuery.java
index 84f589d..1a1ab0a 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SpanSubspanQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SpanSubspanQuery.java
@@ -34,8 +34,7 @@
  * 
  * In this example, the SpanSubspanQuery creates subspans, that are
  * the first
- * two tokens of all sentences. It also collects all payloads from the
- * {@link ElementSpans} for the SubSpans.
+ * two tokens of all sentences.
  * 
  * @author margaretha
  * */
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/SubSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/SubSpans.java
index 64c2355..ca01080 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/SubSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/SubSpans.java
@@ -2,6 +2,7 @@
 
 import java.io.IOException;
 import java.util.Map;
+import java.util.ArrayList;
 
 import org.apache.lucene.index.LeafReaderContext;
 import org.apache.lucene.index.Term;
@@ -10,6 +11,9 @@
 
 import de.ids_mannheim.korap.query.SpanSubspanQuery;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /**
  * Enumeration of SubSpans, which are parts of another Spans. The
  * SubSpans are specified with a start offset relative to the original
@@ -17,12 +21,18 @@
  * position of the subspans is the same as that of the original spans.
  * 
  * @author margaretha
+ * @author diewald
  * 
  */
 public class SubSpans extends SimpleSpans {
 
-    private int startOffset, length;
+    // Logger
+    private final Logger log = LoggerFactory.getLogger(SubSpans.class);
 
+    // This advices the java compiler to ignore all loggings
+    public static final boolean DEBUG = false;
+
+    private int startOffset, length;
 
     /**
      * Constructs SubSpans for the given {@link SpanSubspanQuery}
@@ -41,6 +51,11 @@
         super(subspanQuery, context, acceptDocs, termContexts);
         this.startOffset = subspanQuery.getStartOffset();
         this.length = subspanQuery.getLength();
+        this.matchPayload = new ArrayList<byte[]>(6);
+
+        if (DEBUG) {
+            log.trace("Init SubSpan at {} with length {}", this.startOffset, this.length);
+        };
         hasMoreSpans = firstSpans.next();
     }
 
@@ -77,12 +92,15 @@
      * @throws IOException
      */
     public boolean findMatch () throws IOException {
+
+        // Check at span ending
         if (this.startOffset < 0) {
             matchStartPosition = firstSpans.end() + startOffset;
             if (matchStartPosition < firstSpans.start()) {
                 matchStartPosition = firstSpans.start();
-            }
+            };
         }
+        // Check at span beginning
         else {
             matchStartPosition = firstSpans.start() + startOffset;
             if (matchStartPosition >= firstSpans.end()) {
@@ -90,6 +108,7 @@
             }
         }
 
+        // Find end position of span
         if (this.length > 0) {
             matchEndPosition = matchStartPosition + this.length;
             if (matchEndPosition > firstSpans.end()) {
@@ -99,8 +118,27 @@
         else {
             matchEndPosition = firstSpans.end();
         }
-        matchPayload = firstSpans.getPayload();
+
+        matchPayload.clear();
+
+        // Remove element payloads
+        for (byte[] payload : firstSpans.getPayload()) {
+            if (payload[0] == (byte) 64) {
+                continue;
+            };
+            
+            matchPayload.add(payload.clone());
+        };
+
         matchDocNumber = firstSpans.doc();
+
+        if (DEBUG) {
+            log.trace("Start at absolute position {} " +
+                      "and end at absolute position {}",
+                      matchStartPosition,
+                      matchEndPosition);
+        };
+
         return true;
     }
 
diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties
index 78bf62e..1945bdb 100644
--- a/src/main/resources/log4j.properties
+++ b/src/main/resources/log4j.properties
@@ -1,4 +1,4 @@
-log4j.rootLogger = ERROR, stdout
+# log4j.rootLogger = ERROR, stdout
 
 # Queries:
 # log4j.logger.de.ids_mannheim.korap.query.SpanNextQuery = TRACE, stdout
@@ -6,6 +6,7 @@
 # Spans:
 # log4j.logger.de.ids_mannheim.korap.query.spans.ElementSpans = TRACE, stdout
 # log4j.logger.de.ids_mannheim.korap.query.spans.AttributeSpans = TRACE, stdout
+log4j.logger.de.ids_mannheim.korap.query.spans.SubSpans = TRACE, stdout
 # log4j.logger.de.ids_mannheim.korap.query.spans.ElementAttributeSpans = TRACE, stdout
 # log4j.logger.de.ids_mannheim.korap.query.spans.KorapTermSpan = TRACE, stdout
 # log4j.logger.de.ids_mannheim.korap.query.spans.WithinSpans = TRACE, stdout