]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CacheCollectionResult.java
Multiple readers and variable optimization
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / CacheCollectionResult.java
1 package org.simantics.db.impl.query;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5
6 public class CacheCollectionResult {
7         public static final int LEVELS = 10;
8         private int level = 0;
9         private int position = -1;
10         private int currentLength = 0;
11         private int size = 0;
12         private ArrayList<CacheEntryBase> currentLevel;
13         public ArrayList<CacheEntryBase> levels[] = new ArrayList[LEVELS];
14         public CacheCollectionResult() {
15                 for(int i=0;i<LEVELS;i++) {
16                         levels[i] = new ArrayList<CacheEntryBase>();
17                 }
18         }
19         public void add(CacheEntryBase entry) {
20                 short level = entry.getLevel();
21                 if(level < (LEVELS-1)) levels[level].add(entry);
22                 else levels[(LEVELS-1)].add(entry);
23                 size++;
24         }
25         public Collection<CacheEntryBase> toCollection() {
26                 ArrayList<CacheEntryBase> result = new ArrayList<CacheEntryBase>(size());
27                 for(int i=0;i<LEVELS;i++)
28                         result.addAll(levels[i]);
29                 return result;
30         }
31         public int size() {
32                 return size;
33         }
34         public boolean isAtStart() {
35                 return level == 0 && position == -1;
36         }
37         public void restart() {
38                 level = 0;
39                 position = -1;
40                 currentLevel = levels[0];
41                 currentLength = currentLevel.size();
42         }
43         public CacheEntryBase next(int maxLevel) {
44                 //System.err.println("next " + position + " " + level);
45                 position++;
46                 if(position >= currentLength) {
47                         if(level == (LEVELS-1)) return null;
48                         if(level == maxLevel) return null;
49                         level++;
50                         position=-1;
51                         currentLevel = levels[level];
52                         currentLength = currentLevel.size();
53                         return next(maxLevel);
54                 }
55                 return currentLevel.get(position);
56         }
57         public void remove() {
58                 int lastPosition = currentLevel.size()-1;
59                 CacheEntryBase last = currentLevel.remove(lastPosition);
60                 if(lastPosition > position) {
61                         currentLevel.set(position, last);
62                 }
63                 currentLength--;
64                 position--;
65                 size--;
66         }
67         public void setLevel(CacheEntryBase current, int level) {
68                 if(level == current.level) return;
69                 current.level = (short)level;
70                 remove();
71                 add(current);
72         }
73 }