2017-06-26 12 views
0

私はクロニクルキューから読み取るために、次のコード(それはKotlinで書かれていますが、それは問題ではありません)があります。どのように私は、キューから前の10件のレコードを読み、続行するために、コメントコードを変更することができますChronicle Queueから最後のNレコードを読み取るには?

val queue = ChronicleQueueBuilder.single(path).build() 
val tailer = queue.createTailer() 

tailer.toEnd() 

// // This code is wrong 
// val lastIndex = tailer.index() 
// 
// val shift = lastIndex - 10 
// if (shift > 0) { 
//  tailer.moveToIndex(lastIndex) 
// } 

while (true) { 
    val text = await(tailer) 

    if (prefix == null) { 
     println(text) 
    } else { 
     if (text.startsWith(prefix)) { 
      // Would be nice without additional allocation ... 
      println(text.substring(prefix.length + 1)) 
     } 
    } 
} 

を?

理由:理由は、ログを表示するためにキューを使用する場合に便利です。以前のいくつかのロギングステートメントを見て、新しいロギングステートメントが来るのを見たいと思っています。

+0

コメントしたコードの問題は何か?通常は元に戻す必要があります。 – Krishas

+0

もう一度テストします。それは私が考えるいくつかの例外を投げた。私はインデックスが正確に何であるかわからないので、それがうまくいくかどうかはわかりません。バイナリデータへのポインタですか?それは私の記録の論理的なインデックスですか?私はこれをよりよく理解するためにプロジェクトのテストを見ていきます。 –

+0

私はコミッタではありませんが、自分の経験では、新しいドキュメントごとにインデックスが増え、インデックスを10だけ引いてそこから読み取るだけで、最新の10ドキュメントが得られます。また、インデックスにはポインタとサイクル数が含まれている必要があります。 – Krishas

答えて

1

私はあなたのためにテストを書いています。同じように動作させてください。さらに、直接インデックスを使用するには

public class ChronicleTest { 

private String chroniclePath = "/tmp/chronicle-test"; 

private int msgCount = 10; 

private int i = 0; 

    @Test 
    public void writeToQ() { 
     ChronicleQueue queue = ChronicleQueueBuilder.single(chroniclePath).build(); 
     ExcerptAppender appender = queue.acquireAppender(); 
     for (i = 1; i <= msgCount; i++) { 
      appender.writeBytes(b -> { 
       b.writeInt(i); 
      }); 
     } 
     ExcerptTailer tailer = queue.createTailer(); 
     tailer.toEnd(); 
     long lastIndex = tailer.index(); 
     tailer.moveToIndex(lastIndex - 5); 

     while (tailer.readBytes(b -> { 
      int value = b.readInt(); 
      System.out.println("Received:" + value); 
     })) 
      System.out.println("Completed"); 
    } 
} 
+0

私の問題は、4.5.5バージョンのバグでした。私のコードは4.6.6で動作します。ありがとう! –

0

、あなたはExcerptTailerの方向プロパティを使用できます。

final SingleChronicleQueue queue = createQueue(); 

    final int totalRecords = 20; 
    final int tailRecords = 10; 

    final ExcerptAppender appender = queue.acquireAppender(); 
    for (int i = 0; i < totalRecords; i++) { 
     try(final DocumentContext ctx = appender.writingDocument()) { 
      ctx.wire().writeText(Integer.toString(i)); 
     } 
    } 

    final ExcerptTailer tailer = queue.createTailer(); 
    tailer.direction(TailerDirection.BACKWARD).toEnd(); 

    int rewind = tailRecords; 
    final int endCycle = tailer.cycle(); 
    while(--rewind != 0) { 
     try(final DocumentContext ctx = tailer.readingDocument()) { 
      if (!ctx.isPresent()) { 
       break; 
      } 

      if (endCycle != tailer.cycle()) { 
       System.out.println("Rewound past beginning of cycle"); 
      } 
     } 
    } 

    tailer.direction(TailerDirection.FORWARD); 

    for (int i = 0; i < tailRecords; i++) { 
     try(final DocumentContext ctx = tailer.readingDocument()) { 
      if (!ctx.isPresent()) { 
       break; 
      } 

      System.out.println(ctx.wire().readText()); 
     } 
    } 
関連する問題