To store a CharacterLongObject, the code is very straightforward:
Cu cu = Cu.alloc().append("my prefix");
CharacterLongObjectWriter w = new CharacterLongObjectWriter(db, cu);
w.setKeepResourcesOnClose(true); // Optional, for reusability
Random random = new Random(1234); // seed.
for (int i = 0; i < 1000000; i++) {
w.write((random.nextInt() % 26) + 'A');
}
w.close(); // Flushes. Internal buffer and Cu preserved for speed
To read it back again the code is also straightforward:
CharacterLongObjectReader r = new CharacterLongObjectReader(db, cu);
r.setKeepResourcesOnClose(true); // Optional, for reusability
for (int c; (c = r.read()) != -1;) {
// use c
}
r.close(); // Internal Cu preserved for speed
Storage of BinaryLongObjects is essentially the same.
Note also that LongObject Writers, Readers, OutputStreams, and InputStreams
are very lightweight, and can be used again and again without releasing internal
resources. Use setSpaceAndPrefix(ItemSpace space, Cu cuPrefix)
to start over again after closing, assuming
setKeepResourcesOnClose(boolean keepResourcesOnClose) has been
set true.
"My BLOB's unique prefix" [997] {21 37 0.... 0 61}
Where the component in brackets is the index component identifying a block, and
the single component in braces is the data 'block' as a byte array component.
The last block may be shorter than 1024 bytes or chars. For a CLOB, the Item
looks like:
"My CLOB's unique prefix" [997] {" ... "}
There is no character encoding issue with InfinityDB CLOBs because they are stored in native Java char format as two bytes without being encoded into single bytes by a platform- or encoding-specific character set as they are by WriterOutputStream. Thus there is only one storage format. The inefficiency of the two-byte char is overcome transparently by the underlying universal InfinityDB compression instead.