-1

该程序使用 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");
    }
}
4

1 回答 1

0

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

如果您在 GitHub 上打开一个测试失败的问题,那就太好了。公关将是受欢迎的。

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

于 2015-11-05T07:07:06.133 回答