2015-11-05 53 views
-1

这个程序,使用net.openhft:chronicle:3.5.3,说明了我在使用Chronicle时遇到的以下问题。编年史问题?难道我做错了什么?

1香草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"); 
    } 
} 
+0

欢迎来到StackOverflow。请阅读并遵循 [问](http://stackoverflow.com/help/how-to-ask) 准则。这里需要[MCVE](http://stackoverflow.com/help/mcve),还有[标签](http://stackoverflow.com/help/tagging),以及更详细的描述你面临的问题以及你所面临的问题有尝试和认为做错了。 – jvdm

回答

0

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

如果你在GitHub上打开一个测试失败的问题会很好。公关将受到欢迎。

关于VanillaChronicle中的疯狂值请注意,索引不像IndexedChronicle那样工作,因为香草指数也包含关于您正在使用的循环的信息,撰写摘录等的线索,因此您看到的值是正确的。