@@ -14,9 +14,16 @@ class AASPInMemoChunkCache implements AASPChunkCache {
1414 private final AASPChunkStorageFS chunkStorage ;
1515 private final int fromEra ;
1616 private final int toEra ;
17-
17+
1818 private List <AASPChunk > chunkList ;
19- private int size = 0 ;
19+
20+ /** the internal message Cache */
21+ private List <CharSequence > messageCache ;
22+ private int firstIndexMessageCache = -1 ;
23+ private int lastIndexMessageCache = -1 ;
24+ private int maxCacheLen = 1000 ;
25+
26+ private int numberOfMessages = 0 ;
2027
2128 public AASPInMemoChunkCache (AASPChunkStorageFS chunkStorage ,
2229 CharSequence uri , int fromEra , int toEra ) {
@@ -32,9 +39,8 @@ public AASPInMemoChunkCache(AASPChunkStorageFS chunkStorage,
3239 private void initialize () throws IOException {
3340 if (!initialized ) {
3441 this .syncChunkList ();
42+ this .initialized = true ;
3543 }
36-
37- this .initialized = true ;
3844 }
3945
4046 private void syncChunkList () throws IOException {
@@ -55,7 +61,7 @@ private void syncChunkList() throws IOException {
5561 do {
5662 AASPChunk chunk = this .chunkStorage .getChunk (this .uri , thisEra );
5763 this .chunkList .add (chunk );
58- this .size += chunk .getNumberMessage ();
64+ this .numberOfMessages += chunk .getNumberMessage ();
5965
6066 if (anotherLoop ) {
6167 if (finalLoop ) {
@@ -72,7 +78,7 @@ private void syncChunkList() throws IOException {
7278 @ Override
7379 public int getNumberMessage () throws IOException {
7480 this .initialize ();
75- return this .size ;
81+ return this .numberOfMessages ;
7682 }
7783
7884 @ Override
@@ -82,6 +88,8 @@ public CharSequence getURI() {
8288
8389 @ Override
8490 public Iterator <CharSequence > getMessages (boolean chronologically ) throws IOException {
91+ this .initialize ();
92+
8593 List <CharSequence > dummyList = new ArrayList <>();
8694
8795 dummyList .add ("dummy entry 1" );
@@ -93,49 +101,65 @@ public Iterator<CharSequence> getMessages(boolean chronologically) throws IOExce
93101 @ Override
94102 public CharSequence getMessage (int position , boolean chronologically )
95103 throws AASPException , IOException {
96-
97- if (position > this .size )
104+
105+ this .initialize ();
106+
107+ if (position > this .numberOfMessages )
98108 throw new AASPException ("Position exceeds number of message" );
99109
100- // we want to turn around message list - newest first
101- position = this .size -1 - position ; //
110+ if (!chronologically ) {
111+ // invert position - first becomes last etc.
112+ position = this .numberOfMessages - 1 - position ;
113+ }
102114
103- /*
104- if(bubbleList != null && position >= cachedFirstIndex && position <= cachedLastIndex) {
105- return this.bubbleList.get(position - cachedFirstIndex); // TODO calculation correct?
115+ if (this .messageCache != null && position >= this .firstIndexMessageCache && position <= this .lastIndexMessageCache ) {
116+ return this .messageCache .get (position - this .firstIndexMessageCache ); // TODO calculation correct?
106117 }
107118
108- // not yet in cache - find chunk
109- int cachedFirstIndex = 0;
119+ // not yet in cache - find chunk with required message
120+ int firstIndex = 0 ; // absolut index of first message in current chunk
121+ int lastIndex = 0 ; // absolut index of last message in current chunk
110122
111- // TODO assumed temporal sorted list
112123 boolean found = false ;
113124 AASPChunk fittingChunk = null ;
125+ int fittingChunkIndex = 0 ;
126+
114127 for (AASPChunk chunk : this .chunkList ) {
115- this.cachedLastIndex = this.cachedFirstIndex + chunk.getNumberMessage() - 1 ;
128+ lastIndex = firstIndex + chunk .getNumberMessage ();
116129
117- if(position >= cachedFirstIndex && position <= cachedLastIndex) {
130+ if (position >= firstIndex && position <= lastIndex ) {
131+ // we have got our chunk
118132 fittingChunk = chunk ;
119133 break ;
120134 }
121135
122- this.cachedFirstIndex += chunk.getNumberMessage() - 1;
136+ fittingChunkIndex ++;
137+ }
138+
139+ if (fittingChunk == null ) {
140+ throw new AASPException ("internal failure - wrong calculation in chunk cache" );
123141 }
124142
125- // chunk found - copy to memory
126- this.bubbleList = new ArrayList<>();
127- Iterator<CharSequence> messageIter = fittingChunk.getMessages();
128- while(messageIter.hasNext()) {
129- CharSequence message = messageIter.next();
143+ // we can fill our cache right now
130144
131- BubbleMessageInMemo bubbleMessage = new BubbleMessageInMemo(this.topic, message);
132- this.bubbleList.add(bubbleMessage);
145+ // reset cache
146+ this .messageCache = new ArrayList <>();
147+
148+ // simple approach in that first implementation ... we keep fitting chunk in memory
149+ Iterator <CharSequence > messages = fittingChunk .getMessages ();
150+ this .firstIndexMessageCache = firstIndex ;
151+
152+ int counter = 0 ;
153+ while (messages .hasNext ()) {
154+ this .messageCache .add (messages .next ());
155+ counter ++;
156+ if (counter > this .maxCacheLen ) break ;
133157 }
134158
135- // cache filled - try again
136- return this.getMessage(position);
137- */
138- return "dummy" ;
159+ this . lastIndexMessageCache = this . firstIndexMessageCache + counter - 1 ;
160+
161+ // cache filled - call again
162+ return this . getMessage ( position , chronologically ) ;
139163 }
140164
141165 @ Override
@@ -144,3 +168,4 @@ public void add(CharSequence message) throws IOException {
144168 //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
145169 }
146170}
171+
0 commit comments