编年史问题?难道我做错了什么?

时间:2015-11-05 00:46:45

标签: chronicle chronicle-queue

这个程序,使用net.openhft:chronicle:3.5.3,说明了我对Chronicle的以下问题。

1 Vanilla Chronicle.size()返回疯狂值

2 ExcerptTailer.toStart不适用于Indexed Chronicle

3 ExcerptTailer.index(-1)不适用于Vanilla Chronicle

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");
    }
}

1 个答案:

答案 0 :(得分:0)

正如编年史小组所述,toStart问题看起来像一个错误,我需要检查。

如果你在GitHub上用失败的测试打开一个问题会很好。欢迎PR。

关于VanillaChronicle中的疯狂值请注意,索引不像IndexedChronicle那样工作,因为vanilla索引还包含有关您所处的循环的信息,编写摘录的线程等,因此您看到的值是正确的。