2015-11-05 52 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那樣工作,因爲香草指數也包含關於您正在使用的循環的信息,撰寫摘錄等的線索,因此您看到的值是正確的。