Skip to content

Commit ed034f2

Browse files
committed
some clearing up in chunk cache.
1 parent 504150d commit ed034f2

File tree

9 files changed

+106
-43
lines changed

9 files changed

+106
-43
lines changed

src/net/sharksystem/aasp/AASPChunkFS.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class AASPChunkFS implements AASPChunk {
4545
this.era = era;
4646
this.sender = sender;
4747

48-
String trunkName = this.storage.getFullFileName(era, targetUri);
48+
String trunkName = this.storage.setupChunkFolder(era, targetUri);
4949

5050
// init
5151
this.initFiles(trunkName);
@@ -57,7 +57,7 @@ class AASPChunkFS implements AASPChunk {
5757

5858
this.initFiles(trunkName);
5959
}
60-
60+
6161
private void initFiles(String trunkName) throws IOException {
6262
String messageFileName = trunkName + "." + DATA_EXTENSION;
6363
String metaFileName = trunkName + "." + META_DATA_EXTENSION;

src/net/sharksystem/aasp/AASPChunkStorage.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ public interface AASPChunkStorage {
1717

1818
public AASPChunk getChunk(CharSequence uri, int era) throws IOException;
1919

20+
public boolean existsChunk(CharSequence uri, int era) throws IOException;
21+
2022
public List<AASPChunk> getChunks(int era) throws IOException;
2123

2224
public void dropChunks(int era) throws IOException;
2325

2426
/**
2527
*
2628
* @param uri chunk storage uri
27-
* @param fromEra oldest era
28-
* @param toEra youngest era
29+
* @param toEra newest era
2930
* @return a chunk cache which hides details of era
3031
* @throws IOException
3132
*/
32-
public AASPChunkCache getAASPChunkCache(CharSequence uri, int fromEra,
33-
int toEra) throws IOException;
33+
public AASPChunkCache getAASPChunkCache(CharSequence uri, int toEra) throws IOException;
3434
}

src/net/sharksystem/aasp/AASPChunkStorageFS.java

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,22 @@ class AASPChunkStorageFS implements AASPChunkStorage {
2323
public AASPChunk getChunk(CharSequence uriTarget, int era) throws IOException {
2424
return new AASPChunkFS(this, (String) uriTarget, era);
2525
}
26-
26+
27+
@Override
28+
public boolean existsChunk(CharSequence uri, int era) throws IOException {
29+
String fullContentFileName = this.getChunkContentFilename(era, uri);
30+
31+
return(new File(fullContentFileName).exists());
32+
}
33+
34+
String getChunkContentFilename(int era, CharSequence uri) {
35+
return this.getChunkFileTrunkname(era, uri.toString()) + "." + DATA_EXTENSION;
36+
}
37+
38+
String getChunkFileTrunkname(int era, String uri) {
39+
return this.getPath(era) + "/" + this.url2FileName(uri);
40+
}
41+
2742
String url2FileName(String url) {
2843
// escape:
2944
/*
@@ -52,8 +67,8 @@ String url2FileName(String url) {
5267
* @return full name (path/name) of that given url and target. Directories
5368
* are created if necessary.
5469
*/
55-
String getFullFileName(int era, String targetUrl) {
56-
String eraFolderString = this.getRootPath() + "/" + Integer.toString(era);
70+
String setupChunkFolder(int era, String targetUrl) {
71+
String eraFolderString = this.getPath(era);
5772
File eraFolder = new File(eraFolderString);
5873
if(!eraFolder.exists()) {
5974
eraFolder.mkdirs();
@@ -62,15 +77,15 @@ String getFullFileName(int era, String targetUrl) {
6277
String fileName = eraFolderString + "/" + this.url2FileName(targetUrl);
6378
return fileName;
6479
}
65-
80+
6681
/**
6782
*
6883
* @param era
6984
* @return full name (path/name) of that given url and target. Directories
7085
* are expected to be existent
7186
*/
72-
String getFullFileNameByChunkName(int era, String contentName) {
73-
return this.getPath(era) + "/" + contentName;
87+
String getFileNameByUri(int era, String uri) {
88+
return this.getPath(era) + "/" + uri;
7489
}
7590

7691
private String getPath(int era) {
@@ -99,7 +114,7 @@ public boolean accept(File dir, String fileName) {
99114
int index = name.lastIndexOf('.');
100115
if(index != -1) {
101116
String chunkName = name.substring(0, index);
102-
String fName = this.getFullFileNameByChunkName(era, chunkName);
117+
String fName = this.getFileNameByUri(era, chunkName);
103118
chunkList.add(new AASPChunkFS(this, fName));
104119
}
105120
}
@@ -117,13 +132,18 @@ public void dropChunks(int era) throws IOException {
117132

118133
}
119134

120-
String getRootPath() {
121-
return this.rootDirectory;
122-
}
123-
124135
@Override
125-
public AASPChunkCache getAASPChunkCache(CharSequence uri, int fromEra,
126-
int toEra) throws IOException {
127-
return new AASPInMemoChunkCache(this, uri, fromEra, toEra);
136+
public AASPChunkCache getAASPChunkCache(CharSequence uri, int toEra) throws IOException {
137+
// go back 1000 eras
138+
int fromEra = toEra;
139+
for(int i = 0; i < 1000; i++) {
140+
fromEra = AASPEngine.previousEra(fromEra);
141+
}
142+
143+
return new AASPInMemoChunkCache(this,
144+
uri,
145+
fromEra, // set starting era
146+
toEra // anything before
147+
);
128148
}
129149
}

src/net/sharksystem/aasp/AASPEngine.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* See ASPChunkStorage for details.
1515
*
1616
* @see AASPStorage
17-
* @see AASPReader
1817
* @author thsc
1918
*/
2019
public abstract class AASPEngine implements AASPStorage, AASPProtocolEngine {
@@ -37,6 +36,7 @@ public abstract class AASPEngine implements AASPStorage, AASPProtocolEngine {
3736
protected AASPMemento memento = null;
3837

3938
/* private */ final private AASPChunkStorage chunkStorage;
39+
private boolean dropDeliveredChunks = false;
4040

4141
protected AASPEngine(AASPChunkStorage chunkStorage)
4242
throws AASPException, IOException {
@@ -87,7 +87,7 @@ public void add(CharSequence urlTarget, CharSequence message) throws IOException
8787

8888
private String getLogStart() {
8989
StringBuilder b = new StringBuilder();
90-
b.append("ASP3Engine (");
90+
b.append("AASPEngine (");
9191
b.append(this.owner);
9292
b.append(") ");
9393

@@ -208,10 +208,10 @@ public void handleConnection(InputStream is, OutputStream os,
208208
//>>>>>>>>>>>>>>>>>>>debug
209209

210210
// is not a public chunk
211-
if(!this.isPublic(chunk)) {
211+
if (!this.isPublic(chunk)) {
212212
List<CharSequence> recipients = chunk.getRecipients();
213213

214-
if(!recipients.contains(peer)) {
214+
if (!recipients.contains(peer)) {
215215
continue;
216216
}
217217
}
@@ -234,13 +234,20 @@ public void handleConnection(InputStream is, OutputStream os,
234234
System.out.println(b.toString());
235235
//>>>>>>>>>>>>>>>>>>>debug
236236
// empty?
237-
if(chunk.getRecipients().isEmpty()) {
238-
chunk.drop();
239-
//<<<<<<<<<<<<<<<<<<debug
240-
b = new StringBuilder();
241-
b.append(this.getLogStart());
242-
b.append("chunk dropped");
243-
System.out.println(b.toString());
237+
if (chunk.getRecipients().isEmpty()) {
238+
if (this.isDropDeliveredChunks()) {
239+
chunk.drop();
240+
//<<<<<<<<<<<<<<<<<<debug
241+
b = new StringBuilder();
242+
b.append(this.getLogStart());
243+
b.append("chunk dropped");
244+
System.out.println(b.toString());
245+
} else {
246+
b = new StringBuilder();
247+
b.append(this.getLogStart());
248+
b.append("drop flag set false - engine does not remove delivered chunks");
249+
System.out.println(b.toString());
250+
}
244251
}
245252
}
246253

@@ -297,6 +304,14 @@ public void handleConnection(InputStream is, OutputStream os,
297304
// }
298305
}
299306

307+
private boolean isDropDeliveredChunks() {
308+
return this.dropDeliveredChunks;
309+
}
310+
311+
public void setDropDeliveredChunks(boolean drop) {
312+
this.dropDeliveredChunks = drop;
313+
}
314+
300315
static int nextEra(int workingEra) {
301316
if(workingEra == Integer.MAX_VALUE) {
302317
return 0;
@@ -375,7 +390,10 @@ private synchronized void incrementEra() throws IOException {
375390
this.era = this.getNextEra(this.era);
376391

377392
// drop very very old chunks - if available
378-
this.chunkStorage.dropChunks(this.era);
393+
this.chunkStorage.dropChunks(this.era);
394+
395+
// persistent values
396+
if(this.memento != null) this.memento.save(this);
379397
}
380398

381399
/**

src/net/sharksystem/aasp/AASPEngineFS.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,21 @@ public static AASPEngine getAASPEngine(String owner, String rootDirectory)
6666

6767
// reset owner?
6868
if(owner != null && engine.owner != null
69-
&& !engine.owner.equalsIgnoreCase(AASPEngine.ANONYMOUS_OWNER)
69+
&& !engine.owner.equalsIgnoreCase(AASPEngine.ANONYMOUS_OWNER)
70+
&& !engine.owner.equalsIgnoreCase(AASPEngine.DEFAULT_OWNER)
71+
&& !owner.equalsIgnoreCase(AASPEngine.ANONYMOUS_OWNER)
72+
&& !owner.equalsIgnoreCase(AASPEngine.DEFAULT_OWNER)
7073
&& !owner.equalsIgnoreCase(engine.owner)) {
7174

7275
throw new AASPException("cannot overwrite folder of a "
7376
+ "a non-anonymous different owner: " + engine.owner);
7477
}
7578

7679
// replacing owner could be done
77-
if(owner != null) {
80+
if(owner != null
81+
&& !owner.equalsIgnoreCase(AASPEngine.ANONYMOUS_OWNER)
82+
&& !owner.equalsIgnoreCase(AASPEngine.DEFAULT_OWNER)
83+
) {
7884
engine.owner = owner;
7985
mementoFS.save(engine);
8086
}

src/net/sharksystem/aasp/AASPInMemoChunkCache.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,14 @@ private void syncChunkList() throws IOException {
6868
this.chunkList = new ArrayList<>();
6969

7070
do {
71-
AASPChunk chunk = this.chunkStorage.getChunk(this.uri, thisEra);
72-
this.chunkList.add(chunk);
73-
this.numberOfMessages += chunk.getNumberMessage();
74-
71+
// check if chunk exists - don't create on
72+
if(this.chunkStorage.existsChunk(this.uri, thisEra)) {
73+
// is there - get it
74+
AASPChunk chunk = this.chunkStorage.getChunk(this.uri, thisEra);
75+
this.chunkList.add(chunk);
76+
this.numberOfMessages += chunk.getNumberMessage();
77+
}
78+
7579
if (anotherLoop) {
7680
if (finalLoop) {
7781
anotherLoop = false;
@@ -81,7 +85,6 @@ private void syncChunkList() throws IOException {
8185
}
8286
}
8387
} while (anotherLoop);
84-
8588
}
8689

8790
@Override

src/net/sharksystem/aasp/AASPProtocolEngine.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,15 @@
1111
interface AASPProtocolEngine {
1212
public void handleConnection(InputStream is, OutputStream os,
1313
AASPReceivedChunkListener listener) throws IOException;
14+
15+
/**
16+
* Chunks are (tried to be) delivered to their recipients during each encounter
17+
* with another peer. After successful delivery, recipient is withdrawn from recipient
18+
* list to prevent mutliple delivery.
19+
*
20+
* If this flag is set, chunk are removed permanently if their are delivered
21+
* to all their recipients. If no set, they remain intact.
22+
* @param drop
23+
*/
24+
void setDropDeliveredChunks(boolean drop);
1425
}

test/net/sharksystem/aasp/BasicTests.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ public void androidUsage() throws IOException, AASPException, InterruptedExcepti
2929

3030
// alice writes a message into chunkStorage
3131
AASPStorage aliceStorage =
32-
AASPEngineFS.getAASPChunkStorage(ALICE_FOLDER);
32+
AASPEngineFS.getAASPChunkStorage(ALICE, ALICE_FOLDER);
3333

3434
aliceStorage.add(ALICE_BOB_CHAT_URL, ALICE2BOB_MESSAGE);
3535

3636
// bob does the same
3737
AASPStorage bobStorage =
38-
AASPEngineFS.getAASPChunkStorage(BOB_FOLDER);
38+
AASPEngineFS.getAASPChunkStorage(BOB, BOB_FOLDER);
3939

4040
bobStorage.add(ALICE_BOB_CHAT_URL, BOB2ALICE_MESSAGE);
4141

@@ -71,7 +71,7 @@ public void androidUsage() throws IOException, AASPException, InterruptedExcepti
7171
bobChannel.getOutputStream(), bobListener);
7272

7373
// wait until communication probably ends
74-
Thread.sleep(10000);
74+
Thread.sleep(5000);
7575

7676
// close connections: note AASPEngine does NOT close any connection!!
7777
aliceChannel.close();
@@ -114,5 +114,10 @@ public void androidUsage() throws IOException, AASPException, InterruptedExcepti
114114
// expect bob
115115
Assert.assertEquals(1, senderList.size());
116116
Assert.assertTrue(BOB.equalsIgnoreCase(senderList.get(0).toString()));
117+
118+
// simulate a sync
119+
bobStorage = AASPEngineFS.getAASPChunkStorage(BOB, BOB_FOLDER);
120+
Assert.assertEquals(1, bobStorage.getEra());
121+
Assert.assertEquals(bobEngine.getEra(), bobStorage.getEra());
117122
}
118123
}

test/net/sharksystem/aasp/ChunkCacheTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void chunkTest1() throws IOException, AASPException {
4343

4444
chunk.add(MESSAGE_FOUR);
4545

46-
AASPChunkCache aaspChunkCache = chunkStorage.getAASPChunkCache(TEST_URI, era, newEra);
46+
AASPChunkCache aaspChunkCache = chunkStorage.getAASPChunkCache(TEST_URI, storage.getEra());
4747

4848
// add message after getting cache
4949

0 commit comments

Comments
 (0)