Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExcerptTailer.read(ByteBuffer) fails #112

Closed
jhalterman opened this issue Dec 17, 2014 · 8 comments
Closed

ExcerptTailer.read(ByteBuffer) fails #112

jhalterman opened this issue Dec 17, 2014 · 8 comments

Comments

@jhalterman
Copy link

When writing a simple ByteBuffer out and attempting to read it back via ExcerptTailer.read, the ByteBuffer is never read.

String path = new File(System.getProperty("java.io.tmpdir"), "foo").getAbsolutePath() + "/" + UUID.randomUUID().toString();
try (IndexedChronicle chronicle = new IndexedChronicle(path, ChronicleConfig.TEST)) {
  ExcerptAppender appender = chronicle.createAppender();
  appender.startExcerpt(100);
  ByteBuffer entry = ByteBuffer.wrap("1".getBytes());
  appender.write(entry);
  appender.finish();

  ExcerptTailer tailer = chronicle.createTailer();
  ByteBuffer input = ByteBuffer.allocate(1);
  tailer.read(input);

  // Fails
  assert input.equals(entry);
}

This appears to be because ExcerptTailer.remaining() is 0, which causes the read to do nothing.

@jhalterman jhalterman changed the title ExcerptTailer.read(ByteBuffer) is often a no-op ExcerptTailer.read(ByteBuffer) fails Dec 17, 2014
@peter-lawrey
Copy link
Member

You need to use nextIndex () on the Tailer first.
On 17/12/2014 9:20 PM, "Jonathan Halterman" notifications@github.com
wrote:

When writing a simple ByteBuffer out and attempting to read it back via
ExcerptTailer.read, the ByteBuffer is never read.

String path = new File(System.getProperty("java.io.tmpdir"), "foo").getAbsolutePath() + "/" + UUID.randomUUID().toString();
try (IndexedChronicle chronicle = new IndexedChronicle(path, ChronicleConfig.TEST)) {
ExcerptAppender appender = chronicle.createAppender();
appender.startExcerpt(100);
ByteBuffer entry = ByteBuffer.wrap("1".getBytes());
appender.write(entry);
appender.finish();

ExcerptTailer tailer = chronicle.createTailer();
ByteBuffer input = ByteBuffer.allocate(1);
tailer.read(input);

assertEquals(new String(input.array()), new String(entry.array()));
}

This appears to be because ExcerptTailer.remaining() is 0, which causes
the read to do nothing.


Reply to this email directly or view it on GitHub
#112.

@jhalterman
Copy link
Author

Thanks - I've gone through a few variations of this seeing what works and what doesn't. Using nextIndex(), here's a similar example that fails:

String path = new File(System.getProperty("java.io.tmpdir"), "foo").getAbsolutePath() + "/"
  + UUID.randomUUID().toString();
try (IndexedChronicle chronicle = new IndexedChronicle(path, ChronicleConfig.TEST)) {
  ExcerptAppender appender = chronicle.createAppender();
  appender.startExcerpt(100);
  ByteBuffer entry = ByteBuffer.allocate(4).putInt(5);
  appender.write(entry);
  appender.finish();

  ExcerptTailer tailer = chronicle.createTailer();
  if (tailer.nextIndex()) {
    ByteBuffer input = ByteBuffer.allocate(4);
    tailer.read(input);

    // Fails
    assert input.equals(entry);
  }
}

@peter-lawrey
Copy link
Member

After you write to a ByteBuffer you need to flip() it. i.e. this leaves
you with a a ByteBuffer which is entry.remaining() == 0.

ByteBuffer entry = ByteBuffer.allocate(4).putInt(5);

Instead of using ByteBuffers can you not write directly to/from the
chronicle.

File path = File.createTempFile("foo", "");
ChronicleTools.deleteOnExit(path.toString());
try (Chronicle chronicle = ChronicleQueueBuilder.indexed(path).build()) {
ExcerptAppender appender = chronicle.createAppender();
appender.startExcerpt();
appender.writeInt(5);
appender.finish();

ExcerptTailer tailer = chronicle.createTailer();
assertTrue(tailer.nextIndex());
int x = tailer.readInt();

// Fails
assertEquals(5, x);

}

On 17 December 2014 at 22:10, Jonathan Halterman notifications@github.com
wrote:

Thanks - I've gone through a few variations of this seeing what works and
what doesn't. Using nextIndex(), here's a similar example that fails:

String path = new File(System.getProperty("java.io.tmpdir"), "foo").getAbsolutePath() + "/"
  + UUID.randomUUID().toString();
try (IndexedChronicle chronicle = new IndexedChronicle(path, ChronicleConfig.TEST)) {
  ExcerptAppender appender = chronicle.createAppender();
  appender.startExcerpt(100);
  ByteBuffer entry = ByteBuffer.allocate(4).putInt(5);
  appender.write(entry);
  appender.finish();

  ExcerptTailer tailer = chronicle.createTailer();
  if (tailer.nextIndex()) {
    ByteBuffer input = ByteBuffer.allocate(4);
    tailer.read(input);

    // Fails
    assert input.equals(entry);
  }
}


Reply to this email directly or view it on GitHub
#112 (comment)
.

@peter-lawrey
Copy link
Member

Oops this example doesn't fail. Ignore the comment.

On 17 December 2014 at 22:23, Peter Lawrey <
peter.lawrey@higherfrequencytrading.com> wrote:

After you write to a ByteBuffer you need to flip() it. i.e. this leaves
you with a a ByteBuffer which is entry.remaining() == 0.

ByteBuffer entry = ByteBuffer.allocate(4).putInt(5);

Instead of using ByteBuffers can you not write directly to/from the
chronicle.

File path = File.createTempFile("foo", "");
ChronicleTools.deleteOnExit(path.toString());
try (Chronicle chronicle = ChronicleQueueBuilder.indexed(path).build()) {
ExcerptAppender appender = chronicle.createAppender();
appender.startExcerpt();
appender.writeInt(5);
appender.finish();

ExcerptTailer tailer = chronicle.createTailer();
assertTrue(tailer.nextIndex());
int x = tailer.readInt();

// Fails
assertEquals(5, x);

}

On 17 December 2014 at 22:10, Jonathan Halterman <notifications@github.com

wrote:

Thanks - I've gone through a few variations of this seeing what works and
what doesn't. Using nextIndex(), here's a similar example that fails:

String path = new File(System.getProperty("java.io.tmpdir"), "foo").getAbsolutePath() + "/"
  + UUID.randomUUID().toString();
try (IndexedChronicle chronicle = new IndexedChronicle(path, ChronicleConfig.TEST)) {
  ExcerptAppender appender = chronicle.createAppender();
  appender.startExcerpt(100);
  ByteBuffer entry = ByteBuffer.allocate(4).putInt(5);
  appender.write(entry);
  appender.finish();

  ExcerptTailer tailer = chronicle.createTailer();
  if (tailer.nextIndex()) {
    ByteBuffer input = ByteBuffer.allocate(4);
    tailer.read(input);

    // Fails
    assert input.equals(entry);
  }
}


Reply to this email directly or view it on GitHub
#112 (comment)
.

@jhalterman
Copy link
Author

We're exposing an API that allows ByteBuffers to be written through to a Chronicle and read back later - so we don't have control over the state of the ByteBuffer or its content when we receive it.

Do you have an opinion on whether this is generally a good or bad idea?

@peter-lawrey
Copy link
Member

You need to flip() it and pass the original ByteBuffer to the
write(ByteBuffer) method or you can cast it.

ByteBuffer is a built in library so we have to support how it works
currently. Or we can keep suggestion you use our API instead ;)

On 17 December 2014 at 22:29, Jonathan Halterman notifications@github.com
wrote:

Re: flipping the ByteBuffer, flip() returns Buffer, which cannot be
written via ExcerptAppender.


Reply to this email directly or view it on GitHub
#112 (comment)
.

@lburgazzoli
Copy link
Contributor

A new tailer should point to the end of a chronicle-queue so if you want to read the data written before the tailer was created you should move it to the beginning - with toStart() - or set the index from which you want to start from - with index(..)

@jhalterman
Copy link
Author

Thanks - We'll use flip() to try and normalize the buffers we receive before attempting to write them out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants