Skip to content

Commit 923eaaf

Browse files
committed
Allow DataStore to wrap an IndexChronicle not just an InProcessChronicleSink
1 parent 68e51c4 commit 923eaaf

File tree

2 files changed

+43
-24
lines changed

2 files changed

+43
-24
lines changed

chronicle/src/main/java/com/higherfrequencytrading/chronicle/datamodel/DataStore.java

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.higherfrequencytrading.chronicle.Chronicle;
2020
import com.higherfrequencytrading.chronicle.Excerpt;
2121
import com.higherfrequencytrading.chronicle.ExcerptMarshallable;
22+
import com.higherfrequencytrading.chronicle.tcp.InProcessChronicleSink;
2223
import com.higherfrequencytrading.chronicle.tools.ChronicleTools;
2324

2425
import java.io.Closeable;
@@ -57,14 +58,17 @@ public DataStore(final Chronicle chronicle, ModelMode mode) {
5758

5859
case READ_ONLY:
5960
final String name = chronicle.name();
60-
updater = Executors.newSingleThreadExecutor(new ThreadFactory() {
61-
@Override
62-
public Thread newThread(Runnable r) {
63-
Thread t = new Thread(r, name + "data store updater");
64-
t.setDaemon(true);
65-
return t;
66-
}
67-
});
61+
if (chronicle instanceof InProcessChronicleSink)
62+
updater = Executors.newSingleThreadExecutor(new ThreadFactory() {
63+
@Override
64+
public Thread newThread(Runnable r) {
65+
Thread t = new Thread(r, name + "data store updater");
66+
t.setDaemon(true);
67+
return t;
68+
}
69+
});
70+
else
71+
updater = null;
6872
break;
6973

7074
default:
@@ -157,24 +161,25 @@ public void start(final long lastEvent) {
157161
break;
158162

159163
case READ_ONLY:
160-
updater.submit(new Runnable() {
161-
@Override
162-
public void run() {
163-
excerpt = chronicle.createExcerpt();
164-
while (!closed) {
165-
boolean found = excerpt.nextIndex();
166-
if (found) {
167-
processNextEvent(excerpt.index() <= lastEvent);
168-
169-
} else {
170-
for (Wrapper wrapper : wrappersArray) {
171-
wrapper.notifyOff(false);
172-
wrapper.inSync();
164+
if (updater != null)
165+
updater.submit(new Runnable() {
166+
@Override
167+
public void run() {
168+
excerpt = chronicle.createExcerpt();
169+
while (!closed) {
170+
boolean found = excerpt.nextIndex();
171+
if (found) {
172+
processNextEvent(excerpt.index() <= lastEvent);
173+
174+
} else {
175+
for (Wrapper wrapper : wrappersArray) {
176+
wrapper.notifyOff(false);
177+
wrapper.inSync();
178+
}
173179
}
174180
}
175181
}
176-
}
177-
});
182+
});
178183
break;
179184

180185
default:
@@ -233,4 +238,18 @@ public void close() {
233238
updater.shutdown();
234239
chronicle.close();
235240
}
241+
242+
/**
243+
* Should only be used for plain IndexChronicle.
244+
*
245+
* @return was a new entry found.
246+
*/
247+
public boolean nextEvent() {
248+
assert updater == null;
249+
if (excerpt.nextIndex()) {
250+
processNextEvent(false);
251+
return true;
252+
}
253+
return false;
254+
}
236255
}

chronicle/src/test/java/com/higherfrequencytrading/chronicle/examples/ExampleKeyedExcerptMain.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static void main(String... ignored) throws IOException {
6767
ExampleKeyedExcerptMain map = new ExampleKeyedExcerptMain(basePath);
6868
map.load();
6969
long start = System.nanoTime();
70-
int keys = 1000000;
70+
int keys = 10000000;
7171
for (int i = 0; i < keys; i++) {
7272
Map<String, String> props = new LinkedHashMap<String, String>();
7373
props.put("a", Integer.toString(i)); // an int.

0 commit comments

Comments
 (0)