At last the EntityBatchingProducerNode is working. This will replace the old
[mir.git] / source / mir / entity / EntityBrowser.java
1 package mir.entity;\r
2 \r
3 import java.util.*;\r
4 import mir.util.*;\r
5 import mir.storage.*;\r
6 import mir.entity.*;\r
7 \r
8 public class EntityBrowser implements RewindableIterator {\r
9 \r
10   private StorageObject storage;\r
11   private String whereClause;\r
12   private String orderByClause;\r
13   private int batchSize;\r
14   private int toFetch;\r
15   private EntityList currentBatch;\r
16 \r
17   private int skip;\r
18   private int limit;\r
19 \r
20   private int batchPosition;\r
21   private int positionInBatch;\r
22 \r
23   public EntityBrowser(StorageObject aStorage, String aWhereClause, String anOrderByClause,\r
24                        int aBatchSize, int aLimit, int aSkip) throws StorageObjectException {\r
25 \r
26     storage=aStorage;\r
27     whereClause=aWhereClause;\r
28     orderByClause=anOrderByClause;\r
29     batchSize=aBatchSize;\r
30     skip=aSkip;\r
31     limit=aLimit;\r
32 \r
33     rewind();\r
34   }\r
35 \r
36   public EntityBrowser(StorageObject aStorage,\r
37           String aWhereClause, String anOrderByClause,\r
38           int aBatchSize) throws StorageObjectException {\r
39     this(aStorage, aWhereClause, anOrderByClause, aBatchSize, -1, 0);\r
40   }\r
41 \r
42   public void readCurrentBatch(int aSkip) throws StorageObjectException {\r
43     currentBatch = storage.selectByWhereClause(whereClause, orderByClause, aSkip, batchSize);\r
44     batchPosition = aSkip;\r
45     positionInBatch = 0;\r
46   }\r
47 \r
48   public void rewind() {\r
49     try {\r
50       readCurrentBatch(skip);\r
51     }\r
52     catch (Throwable t) {\r
53       throw new RuntimeException(t.getMessage());\r
54     }\r
55   }\r
56 \r
57   public boolean hasNext() {\r
58     try {\r
59       if (limit>-1 && batchPosition+positionInBatch>=skip+limit)\r
60         return false;\r
61 \r
62       if (positionInBatch>=currentBatch.size() && currentBatch.hasNextBatch()) {\r
63         readCurrentBatch(batchPosition+positionInBatch);\r
64       }\r
65 \r
66       return (positionInBatch<currentBatch.size());\r
67     }\r
68     catch (Throwable t) {\r
69       throw new RuntimeException(t.getMessage());\r
70     }\r
71   }\r
72 \r
73   public Object next() {\r
74     try {\r
75       if (hasNext()) {\r
76         Entity result = currentBatch.elementAt(positionInBatch);\r
77         positionInBatch=positionInBatch+1;\r
78 \r
79         return result;\r
80       }\r
81       else {\r
82         return null;\r
83       }\r
84     }\r
85     catch (Throwable t) {\r
86       throw new RuntimeException(t.getMessage());\r
87     }\r
88   }\r
89 \r
90   public void remove() {\r
91     throw new UnsupportedOperationException();\r
92   }\r
93 }