Skip to content

Commit 84faa77

Browse files
committed
asap cache / channel setup is much faster - check what era present instead of guessing.
1 parent fe5b007 commit 84faa77

File tree

3 files changed

+94
-14
lines changed

3 files changed

+94
-14
lines changed

src/net/sharksystem/Utils.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package net.sharksystem;
22

3+
import net.sharksystem.asap.ASAP;
4+
5+
import java.io.File;
6+
import java.util.ArrayList;
7+
import java.util.Collection;
8+
import java.util.HashSet;
9+
310
public class Utils {
411
public static String url2FileName(String url) {
512
// escape:
@@ -21,4 +28,60 @@ public static String url2FileName(String url) {
2128

2229
return newString;
2330
}
31+
32+
/**
33+
*
34+
* @param rootFolder
35+
* @return collection of integer values depicting era present in that folder
36+
*/
37+
public static Collection<Integer> getErasInFolder(String rootFolder) {
38+
Collection<Integer> eras = new HashSet<>();
39+
File dir = new File(rootFolder);
40+
String[] dirEntries = dir.list();
41+
if (dirEntries != null) {
42+
for (String fileName : dirEntries) {
43+
// era folder?
44+
try {
45+
int era = Integer.parseInt(fileName);
46+
// It is an era folder
47+
eras.add(era);
48+
} catch (NumberFormatException e) {
49+
// no number - no problem - go ahead!
50+
}
51+
}
52+
}
53+
return eras;
54+
}
55+
56+
/**
57+
*
58+
* @param searchSpace list of possible eras
59+
* @param fromEra lowest era
60+
* @param toEra highest era
61+
* @return list of era which are within from and to and also in search space
62+
*/
63+
public static Collection<Integer> getErasInRange(Collection<Integer> searchSpace,
64+
int fromEra, int toEra) {
65+
66+
Collection<Integer> eras = new ArrayList<>();
67+
68+
// the only trick is to be aware of the cyclic nature of era numbers
69+
boolean wrapped = fromEra > toEra; // it reached the era end and started new
70+
71+
for(Integer era : searchSpace) {
72+
if(!wrapped) {
73+
//INIT ---- from-> +++++++++++++ <-to ----- MAX (+ fits)
74+
if(era >= fromEra && era <= toEra) eras.add(era);
75+
} else {
76+
// INIT+++++++++<-to ------ from->++++++MAX
77+
if(era <= toEra && era >= ASAP.INITIAL_ERA
78+
|| era >= fromEra && era <= ASAP.MAX_ERA
79+
) eras.add(era);
80+
}
81+
}
82+
83+
return eras;
84+
85+
}
86+
2487
}

src/net/sharksystem/asap/ASAPChunkStorageFS.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,23 @@ public void dropChunks(int era) throws IOException {
128128

129129
@Override
130130
public ASAPMessages getASAPChunkCache(CharSequence uri, int toEra) throws IOException {
131+
// INIT ++++++++++++++++++++++ toEra +++++++++++++++++++++ MAX
132+
133+
int fromEra = ASAP.nextEra(toEra); // the whole cycle
134+
/*
131135
// go back 1000 eras
132136
int fromEra = toEra;
133137
for(int i = 0; i < 1000; i++) {
134138
fromEra = ASAP.previousEra(fromEra);
135139
}
136-
140+
*/
137141
return this.getASAPChunkCache(uri, fromEra, toEra);
138142
}
139143

140144
@Override
141145
public ASAPMessages getASAPChunkCache(CharSequence uri, int fromEra, int toEra) throws IOException {
142146
Log.writeLog(this, "create ASAPInMemoMessages");
143147

144-
Log.writeLogErr(this, "PLEASE: Implement a method getValidEra() which delivers set of all era which are present int that store - would increase performance!");
145-
146148
return new ASAPInMemoMessages(
147149
this,
148150
this.getFormat(),

src/net/sharksystem/asap/ASAPInMemoMessages.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package net.sharksystem.asap;
22

3+
import net.sharksystem.Utils;
34
import net.sharksystem.asap.apps.ASAPMessages;
45
import net.sharksystem.asap.util.Log;
56

67
import java.io.IOException;
7-
import java.util.ArrayList;
8-
import java.util.Iterator;
9-
import java.util.List;
10-
import java.util.NoSuchElementException;
8+
import java.util.*;
119

1210
/**
1311
* @author thsc
@@ -40,9 +38,13 @@ public ASAPInMemoMessages(ASAPChunkStorageFS chunkStorage,
4038
this.toEra = toEra;
4139
this.maxCacheLen = maxCacheLen;
4240

43-
Log.writeLog(this, "format: " + format + " | uri: " + uri
41+
Log.writeLog(this, this.toString());
42+
}
43+
44+
public String toString() {
45+
return "format: " + format + " | uri: " + uri
4446
+ " | rootDir: " + chunkStorage.getRootDirectory()
45-
+ " | fromEra: " + fromEra+ " | toEra: " + toEra);
47+
+ " | fromEra: " + fromEra+ " | toEra: " + toEra;
4648
}
4749

4850
public ASAPInMemoMessages(ASAPChunkStorageFS chunkStorage,
@@ -59,31 +61,42 @@ private void initialize() throws IOException {
5961
this.initialized = true;
6062
}
6163
}
62-
64+
6365
private void syncChunkList() throws IOException {
6466
// get all chunks in chronological order
65-
67+
Collection<Integer> erasInFolder = Utils.getErasInFolder(this.chunkStorage.getRootDirectory());
68+
if(erasInFolder.isEmpty()) return;
69+
70+
Collection<Integer> erasToUse = Utils.getErasInRange(erasInFolder, this.fromEra, this.toEra);
71+
if(erasToUse.isEmpty()) return;
72+
73+
/*
6674
// current era in following loop
6775
int thisEra = this.fromEra;
68-
76+
6977
// do we need more than one loop?
7078
boolean anotherLoop = this.fromEra != this.toEra;
7179
7280
// are we in the final loop?
7381
boolean finalLoop = !anotherLoop;
82+
*/
7483

7584
// drop old vunk list - if any
7685
this.chunkList = new ArrayList<>();
7786

78-
do {
87+
// do {
88+
for(Integer thisEra : erasToUse) {
7989
// check if chunk exists - don't create on
80-
if(this.chunkStorage.existsChunk(this.uri, thisEra)) {
90+
Log.writeLog(this, "reached era: " + thisEra);
91+
if (erasInFolder.contains(thisEra) && this.chunkStorage.existsChunk(this.uri, thisEra)) {
8192
// is there - get it
93+
Log.writeLog(this, "getChunk with era: " + thisEra);
8294
ASAPChunk chunk = this.chunkStorage.getChunk(this.uri, thisEra);
8395
this.chunkList.add(chunk);
8496
this.numberOfMessages += chunk.getNumberMessage();
8597
}
8698

99+
/*
87100
if (anotherLoop) {
88101
if (finalLoop) {
89102
anotherLoop = false;
@@ -93,6 +106,8 @@ private void syncChunkList() throws IOException {
93106
}
94107
}
95108
} while (anotherLoop);
109+
*/
110+
}
96111
}
97112

98113
public int size() throws IOException {

0 commit comments

Comments
 (0)