2

I've created a MappedBytes instance to a file that I'm using as shared cache between different Java processes.

I would like to be able to split out additional MappedByte instances (or ByteBuffer or any other instance) from the original that provide direct read/write access to a subset of the underlying file.

I've spent today experimenting with different methods but options like subBytes(), rawCopy() and copyTo() all seem to create local copies of the underlying file, rather than accessing the file directly.

For example:

File tmpFile = new File(System.getProperty("java.io.tmpdir"), "data.dat");
MappedFile mappedFile = MappedFile.mappedFile(tmpfile, 1000, 100, 10, false);
MappedBytes original = MappedBytes.mappedBytes(mappedFile);
original.zeroOut(0, 1000);

original.writeInt(0, 1234);
BytesStore copy = original.bytesStore().subBytes(0, 200);

// Print out the int in the two BytesStores.
// This shows that the copy has the same contents of the original.
System.out.println("Original(0): " + original.readInt(0));
System.out.println("Copy(0): " + copy.readInt(0));

// Now modify the copy and print out the new int in the two BytesStores again.
copy.writeInt(50, 4321);
System.out.println("Original(50): " + original.readInt(50));
System.out.println("Copy(50): " + copy.readInt(50));

Produces the output:

Original(0): 1234
Copy(0): 1234
Original(50): 0
Copy(50): 4321

The copy has been modified but not the original. I would like the original to be modified, can chronicle-bytes do this?

Thanks for your help, Josh.

1 Answers1

0

This is a self-contained test which I think behaves the way you need.

@Test
public void multiBytes() throws FileNotFoundException {
    String tmpfile = OS.TMP +  "/data.dat";
    MappedFile mappedFile = MappedFile.mappedFile(new File(tmpfile), 64 << 10);
    MappedBytes original = MappedBytes.mappedBytes(mappedFile);
    original.zeroOut(0, 1000);

    original.writeInt(0, 1234);

    PointerBytesStore pbs = new PointerBytesStore();
    pbs.set(original.addressForRead(50), 100);

    // Print out the int in the two BytesStores.
    // This shows that the copy has the same contents of the original.
    System.out.println("Original(0): " + original.readInt(0));
    System.out.println("PBS(0): " + pbs.readInt(0));

    // Now modify the copy and print out the new int in the two BytesStores again.
    pbs.writeInt(0, 4321);
    System.out.println("Original(50): " + original.readInt(50));
    System.out.println("PBS(0): " + pbs.readInt(0));
    original.writeInt(54, 12345678);
    System.out.println("Original(54): " + original.readInt(54));
    System.out.println("PBS(4): " + pbs.readInt(4));
}

prints

Original(0): 1234
PBS(0): 0
Original(50): 4321
PBS(0): 4321
Original(54): 12345678
PBS(4): 12345678
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    Hi @Peter thanks for the fast response. This code looks like it achieves most of what I want. However it still provides the copy with full access to the entire MappedFile. I was hoping for something that would provide access to a subset of the file. Last night I did some more investigations and found the `sliceAsByteBuffer(ByteBuffer)` method which is closer to what I want. However, it gives full access to the buffer from an offset position, ideally I would like something like `sliceAsByteBuffer(ByteBuffer buf, long offset, long length)`. – BackroomGibbon Oct 18 '18 at 00:40
  • 1
    It looks like the openhft.java-lang library hasn't been updated for more than 2 years and doesn't seem to work with java 10 (Not a problem for me since I'm in Java 8). Is that library still ok to use? – BackroomGibbon Oct 18 '18 at 00:44
  • @BackroomGibbon I would avoid using Java Lang as we don't support it. – Peter Lawrey Oct 18 '18 at 06:55
  • @BackroomGibbon see my updated example, this has two BytesStore which uses the same memory but over different ranges. – Peter Lawrey Oct 18 '18 at 07:03
  • BTW we only support Java 8 and 11 (Java 10 is EOL) – Peter Lawrey Oct 18 '18 at 07:05
  • Thanks @Peter, that's exactly what I want. Thanks for your help. – BackroomGibbon Oct 18 '18 at 22:18