该程序使用 net.openhft:chronicle:3.5.3 说明了我在使用 Chronicle 时遇到的以下问题。
1 Vanilla Chronicle.size() 返回疯狂的值
2 ExcerptTailer.toStart 不适用于索引编年史
3 ExcerptTailer.index(-1) 不适用于香草编年史
import net.openhft.chronicle.Chronicle;
import net.openhft.chronicle.ChronicleQueueBuilder;
import net.openhft.chronicle.ExcerptAppender;
import net.openhft.chronicle.ExcerptTailer;
import org.apache.commons.io.FileUtils;
import java.io.File;
/*
* These functions demonstrate a number of issues I've come across.
* */
public class Problems {
public static void main(String[] a) throws Exception {
sizeAndLastIndexWrittenProblem();
//indexedToStartDoesntWork();
//vanillaToIndexDoesntWork();
}
static void sizeAndLastIndexWrittenProblem() throws Exception {
File f = new File("cron");
FileUtils.deleteDirectory(f);
Chronicle chronicle = ChronicleQueueBuilder.vanilla(new File(f, "chronicle")).build();
ExcerptAppender ap = chronicle.createAppender();
// vanilla provides sensible values first of all
Assert(chronicle.size() == 0);
Assert(chronicle.lastWrittenIndex() == -1);
ap.startExcerpt();
ap.writeInt(1);
ap.finish();
// but after appending to a vanilla then these lines print insane values eg
// size = 18410222695481345
// lastWrittenIndex = 18410222695481344
System.out.println("size = " + chronicle.size());
System.out.println("lastWrittenIndex = " + chronicle.lastWrittenIndex());
Assert(chronicle.size() == 1); // FAILS HERE
Assert(chronicle.lastWrittenIndex() == 0);
// note that the same code using an indexed chronicla passes ok
}
static void indexedToStartDoesntWork() throws Exception {
File f = new File("cron");
FileUtils.deleteDirectory(f);
Chronicle chronicle = ChronicleQueueBuilder.indexed(new File(f,"chronicle")).build();
ExcerptAppender ap = chronicle.createAppender();
ExcerptTailer t = chronicle.createTailer();
ap.startExcerpt();
ap.writeInt(1);
ap.finish();
Assert(t.nextIndex()); // ok
Assert(t.readInt() == 1); // ok
t.finish();
ap.startExcerpt();
ap.writeInt(2);
ap.finish();
Assert(t.nextIndex()); // ok
Assert(t.readInt() == 2); // ok
t.finish();
/*
On an indexed chronicle toStart does not rewind to start so the next read fails.
I found that if using indexed then one must use index(-1) not toStart
*/
t.toStart(); // DOESN'T REWIND US TO START
Assert(t.nextIndex()); // FAILS HERE IF USING toStart BUT OK IF USING index(-1)
Assert(t.readInt() == 1);
}
static void vanillaToIndexDoesntWork() throws Exception {
File f = new File("cron");
FileUtils.deleteDirectory(f);
Chronicle chronicle = ChronicleQueueBuilder.vanilla(new File(f,"chronicle")).build();
ExcerptAppender ap = chronicle.createAppender();
ExcerptTailer t = chronicle.createTailer();
ap.startExcerpt();
ap.writeInt(1);
ap.finish();
Assert(t.nextIndex()); // ok
Assert(t.readInt() == 1); // ok
t.finish();
ap.startExcerpt();
ap.writeInt(2);
ap.finish();
Assert(t.nextIndex()); // ok
Assert(t.readInt() == 2); // ok
t.finish();
/*
On an vanilla chronicle index(-1) does not rewind to start so the next read fails.
I found that if using vanilla then one must use toStart not index(-1)
*/
t.index(-1); // DOESN'T REWIND US TO START
Assert(t.nextIndex()); // FAILS HERE IF USING index(-1) BUT OK IF USING toStart()
Assert(t.readInt() == 1);
}
static void Assert(boolean b) {
if (!b) throw new RuntimeException("bang");
}
}