Skip to content

Commit f187c7c

Browse files
committed
added a sync method which was urgently required from Android apps.
1 parent eb6cec0 commit f187c7c

File tree

7 files changed

+47
-18
lines changed

7 files changed

+47
-18
lines changed

.idea/artifacts/aaspJava_jar.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

META-INF/MANIFEST.MF

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Manifest-Version: 1.0
2+

src/net/sharksystem/aasp/AASPChunkCache.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,14 @@ CharSequence getMessage(int position, boolean chronologically)
5454
throws AASPException, IOException;
5555

5656
/**
57-
* Add a message. Implementations are free to simply delegate that method to
58-
* the underlying chunk storage. They can also decide to keep the cache in
59-
* sync. That could be useful if only one application uses the chunk storage.
60-
* It's up to implementation and using application to clarify semantics
61-
* of this method.
62-
*
63-
* @param message
64-
* @throws IOException
57+
* Synchronizes cache. It is up to real implemenations what really
58+
* happens. That method is meant to be called whenever the underlying
59+
* chunk storage has been changed. After that call, cache and storage are
60+
* in sync agin. Implemenations whoch don't have an internal cache don't
61+
* have to do anything.
62+
*
63+
* @throws IOException
6564
*/
66-
void add(CharSequence message) throws IOException;
65+
void sync() throws IOException;
6766

6867
}

src/net/sharksystem/aasp/AASPChunkFS.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ class AASPChunkFS implements AASPChunk {
3939

4040
AASPChunkFS(AASPChunkStorageFS storage, String targetUri, int era, String sender) throws IOException {
4141
this.storage = storage;
42-
this.uri = targetUri;
42+
if(targetUri != null) {
43+
this.uri = targetUri;
44+
}
4345
this.era = era;
4446
this.sender = sender;
4547

src/net/sharksystem/aasp/AASPChunkStorageFS.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ String url2FileName(String url) {
3030
see https://en.wikipedia.org/wiki/Percent-encoding
3131
\ - %5C, / - %2F, : - %3A, ? - %3F," - %22,< - %3C,> - %3E,| - %7C
3232
*/
33+
34+
if(url == null) return null; // to be safe
3335

3436
String newString = url.replace("\\", "%5C");
3537
newString = newString.replace("/", "%2F");

src/net/sharksystem/aasp/AASPInMemoChunkCache.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,18 @@ public CharSequence getMessage(int position, boolean chronologically)
204204
return this.getMessage(position, true);
205205
}
206206

207-
@Override
208-
public void add(CharSequence message) throws IOException {
209-
// TODO
210-
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
207+
public void sync() throws IOException {
208+
this.initialized = false;
209+
this.firstIndexMessageCache = -1;
210+
this.lastIndexMessageCache = -1;
211+
this.numberOfMessages = 0;
212+
this.messageCache = null;
211213
}
212214

215+
//////////////////////////////////////////////////////////////////////////////////////////
216+
// helper: message iterator implementation //
217+
//////////////////////////////////////////////////////////////////////////////////////////
218+
213219
private class ChunkListMessageIterator implements Iterator<CharSequence> {
214220

215221
private final List<AASPChunk> chunkList;

test/net/sharksystem/aasp/ChunkCacheTests.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class ChunkCacheTests {
1212
private static final String MESSAGE_TWO = "message two";
1313
private static final String MESSAGE_THREE = "message three";
1414
private static final String MESSAGE_FOUR = "message four";
15+
private static final String MESSAGE_FIVE = "message five";
1516

1617
@Test
1718
public void chunkTest1() throws IOException, AASPException {
@@ -30,21 +31,22 @@ public void chunkTest1() throws IOException, AASPException {
3031
AASPChunk chunk = chunkStorage.getChunk(TEST_URI, era);
3132

3233
chunk.add(MESSAGE_ONE);
33-
storage.newEra();
34+
storage.newEra(); // finish chunk one
3435
int newEra = storage.getEra();
3536

37+
// start chunk two
3638
Assert.assertEquals(AASPEngine.nextEra(era), newEra);
3739
chunk = chunkStorage.getChunk(TEST_URI, newEra);
3840
chunk.add(MESSAGE_TWO);
3941

40-
Assert.assertEquals(AASPEngine.nextEra(era), newEra);
41-
chunk = chunkStorage.getChunk(TEST_URI, newEra);
4242
chunk.add(MESSAGE_THREE);
43-
chunk = chunkStorage.getChunk(TEST_URI, newEra);
43+
4444
chunk.add(MESSAGE_FOUR);
4545

4646
AASPChunkCache aaspChunkCache = chunkStorage.getAASPChunkCache(TEST_URI, era, newEra);
4747

48+
// add message after getting cache
49+
4850
// position test - chronological order
4951
CharSequence message = aaspChunkCache.getMessage(0, true);
5052
Assert.assertTrue(message.toString().equalsIgnoreCase(MESSAGE_ONE));
@@ -59,6 +61,12 @@ public void chunkTest1() throws IOException, AASPException {
5961
message = aaspChunkCache.getMessage(1, false);
6062
Assert.assertTrue(message.toString().equalsIgnoreCase(MESSAGE_THREE));
6163

64+
chunk.add(MESSAGE_FIVE);
65+
66+
aaspChunkCache.sync();
67+
message = aaspChunkCache.getMessage(4, true);
68+
Assert.assertTrue(message.toString().equalsIgnoreCase(MESSAGE_FIVE));
69+
6270
Iterator<CharSequence> messages = aaspChunkCache.getMessages();
6371

6472
// Iterator test
@@ -71,6 +79,8 @@ public void chunkTest1() throws IOException, AASPException {
7179
Assert.assertTrue(message.toString().equalsIgnoreCase(MESSAGE_THREE));
7280
message = messages.next();
7381
Assert.assertTrue(message.toString().equalsIgnoreCase(MESSAGE_FOUR));
82+
message = messages.next();
83+
Assert.assertTrue(message.toString().equalsIgnoreCase(MESSAGE_FIVE));
7484

7585
Assert.assertFalse(messages.hasNext());
7686

0 commit comments

Comments
 (0)