Skip to content

Commit 248a922

Browse files
committed
working on chunk cache implementation
1 parent c6bc2da commit 248a922

File tree

2 files changed

+65
-31
lines changed

2 files changed

+65
-31
lines changed

src/net/sharksystem/aasp/AASPInMemoChunkCache.java

100644100755
Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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+

test/net/sharksystem/aasp/ChunkCacheTests.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,23 @@ public void chunkTest1() throws IOException, AASPException {
3838

3939
AASPChunkCache aaspChunkCache = chunkStorage.getAASPChunkCache(TEST_URI, era, newEra);
4040

41+
// position test
42+
CharSequence message = aaspChunkCache.getMessage(0, false);
43+
Assert.assertTrue(message.toString().equalsIgnoreCase(MESSAGE_TWO));
44+
45+
message = aaspChunkCache.getMessage(1, false);
46+
Assert.assertTrue(message.toString().equalsIgnoreCase(MESSAGE_ONE));
47+
4148
Iterator<CharSequence> messages = aaspChunkCache.getMessages(true);
4249

50+
// Iterator test
4351
Assert.assertTrue(messages.hasNext());
44-
CharSequence message = messages.next();
52+
message = messages.next();
4553
Assert.assertTrue(message.toString().equalsIgnoreCase(MESSAGE_ONE));
4654

4755
message = messages.next();
4856
Assert.assertTrue(message.toString().equalsIgnoreCase(MESSAGE_TWO));
4957

58+
5059
}
5160
}

0 commit comments

Comments
 (0)