At last the EntityBatchingProducerNode is working. This will replace the old
[mir.git] / source / mir / entity / EntityBrowser.java
index 5a05e18..a320dde 100755 (executable)
@@ -11,47 +11,59 @@ public class EntityBrowser implements RewindableIterator {
   private String whereClause;\r
   private String orderByClause;\r
   private int batchSize;\r
-  private int position=0;\r
-\r
+  private int toFetch;\r
   private EntityList currentBatch;\r
 \r
-  public EntityBrowser(StorageObject aStorage,\r
-          String aWhereClause, String anOrderByClause,\r
-          int aBatchSize) throws StorageObjectException {\r
+  private int skip;\r
+  private int limit;\r
+\r
+  private int batchPosition;\r
+  private int positionInBatch;\r
+\r
+  public EntityBrowser(StorageObject aStorage, String aWhereClause, String anOrderByClause,\r
+                       int aBatchSize, int aLimit, int aSkip) throws StorageObjectException {\r
 \r
     storage=aStorage;\r
     whereClause=aWhereClause;\r
     orderByClause=anOrderByClause;\r
     batchSize=aBatchSize;\r
+    skip=aSkip;\r
+    limit=aLimit;\r
 \r
     rewind();\r
   }\r
 \r
+  public EntityBrowser(StorageObject aStorage,\r
+          String aWhereClause, String anOrderByClause,\r
+          int aBatchSize) throws StorageObjectException {\r
+    this(aStorage, aWhereClause, anOrderByClause, aBatchSize, -1, 0);\r
+  }\r
+\r
+  public void readCurrentBatch(int aSkip) throws StorageObjectException {\r
+    currentBatch = storage.selectByWhereClause(whereClause, orderByClause, aSkip, batchSize);\r
+    batchPosition = aSkip;\r
+    positionInBatch = 0;\r
+  }\r
+\r
   public void rewind() {\r
     try {\r
-      currentBatch = storage.selectByWhereClause(whereClause, orderByClause, 0, batchSize);\r
-      position=0;\r
+      readCurrentBatch(skip);\r
     }\r
     catch (Throwable t) {\r
       throw new RuntimeException(t.getMessage());\r
     }\r
   }\r
 \r
-  private void readNextBatch() throws StorageObjectException  {\r
-    if (currentBatch.hasNextBatch()) {\r
-      currentBatch = storage.selectByWhereClause(whereClause, orderByClause,\r
-            currentBatch.getNextBatch(), batchSize);\r
-      position=0;\r
-    }\r
-  }\r
-\r
   public boolean hasNext() {\r
     try {\r
-      if (position>=currentBatch.size() && currentBatch.hasNextBatch()) {\r
-        readNextBatch();\r
+      if (limit>-1 && batchPosition+positionInBatch>=skip+limit)\r
+        return false;\r
+\r
+      if (positionInBatch>=currentBatch.size() && currentBatch.hasNextBatch()) {\r
+        readCurrentBatch(batchPosition+positionInBatch);\r
       }\r
 \r
-      return (position<currentBatch.size());\r
+      return (positionInBatch<currentBatch.size());\r
     }\r
     catch (Throwable t) {\r
       throw new RuntimeException(t.getMessage());\r
@@ -61,8 +73,8 @@ public class EntityBrowser implements RewindableIterator {
   public Object next() {\r
     try {\r
       if (hasNext()) {\r
-        Entity result = currentBatch.elementAt(position);\r
-        position=position+1;\r
+        Entity result = currentBatch.elementAt(positionInBatch);\r
+        positionInBatch=positionInBatch+1;\r
 \r
         return result;\r
       }\r