chronicle-bytes shared DirectBytesStores

爷,独闯天下 提交于 2019-12-11 03:15:37

问题


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:


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


来源:https://stackoverflow.com/questions/52848025/chronicle-bytes-shared-directbytesstores

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!