use of jetbrains.exodus.CompoundByteIterable in project xodus by JetBrains.
the class BTreeDupMutable method save.
@Override
public long save() {
if (address != Loggable.NULL_ADDRESS) {
throw new IllegalStateException("Duplicates sub-tree already saved");
}
final BasePageMutable rootPage = getRoot();
final byte type = rootPage.isBottom() ? BTreeBase.LEAF_DUP_BOTTOM_ROOT : BTreeBase.LEAF_DUP_INTERNAL_ROOT;
final ByteIterable keyIterable = CompressedUnsignedLongByteIterable.getIterable(key.getLength());
ByteIterable sizeIterable;
// remember high address before saving the data
long startAddress = log.getWrittenHighAddress();
final ByteIterable rootDataIterable = rootPage.getData();
ByteIterable[] iterables;
long result;
final boolean canRetry;
if (log.isLastWrittenFileAddress(startAddress)) {
sizeIterable = CompressedUnsignedLongByteIterable.getIterable(size << 1);
iterables = new ByteIterable[] { keyIterable, key, sizeIterable, rootDataIterable };
result = log.tryWrite(type, structureId, new CompoundByteIterable(iterables));
if (result >= 0) {
address = result;
return result;
} else {
canRetry = false;
}
} else {
canRetry = true;
}
if (!log.isLastWrittenFileAddress(startAddress)) {
final byte writtenType = log.getWrittenLoggableType(startAddress, BTreeBase.DUP_LEAF);
if (NullLoggable.isNullLoggable(writtenType)) {
final long lengthBound = log.getFileLengthBound();
final long alignment = startAddress % lengthBound;
startAddress += (lengthBound - alignment);
if (log.getWrittenHighAddress() < startAddress) {
throw new IllegalStateException("Address alignment underflow: start address " + startAddress + ", alignment " + alignment);
}
}
}
sizeIterable = CompressedUnsignedLongByteIterable.getIterable((size << 1) + 1);
final ByteIterable offsetIterable = CompressedUnsignedLongByteIterable.getIterable(log.getWrittenHighAddress() - startAddress);
iterables = new ByteIterable[] { keyIterable, key, sizeIterable, offsetIterable, rootDataIterable };
final ByteIterable data = new CompoundByteIterable(iterables);
result = canRetry ? log.tryWrite(type, structureId, data) : log.writeContinuously(type, structureId, data);
if (result < 0) {
if (canRetry) {
iterables[3] = CompressedUnsignedLongByteIterable.getIterable(log.getWrittenHighAddress() - startAddress);
result = log.writeContinuously(type, structureId, new CompoundByteIterable(iterables));
if (result < 0) {
throw new TooBigLoggableException();
}
} else {
throw new TooBigLoggableException();
}
}
address = result;
return result;
}
use of jetbrains.exodus.CompoundByteIterable in project xodus by JetBrains.
the class BasePageMutable method save.
/**
* Save page to log
*
* @return address of this page after save
*/
protected long save() {
// save leaf nodes
ReclaimFlag flag = saveChildren();
// save self. complementary to {@link load()}
final byte type = getType();
final BTreeBase tree = getTree();
final int structureId = tree.structureId;
final Log log = tree.log;
if (flag == ReclaimFlag.PRESERVE) {
// there is a chance to update the flag to RECLAIM
if (log.getWrittenHighAddress() % log.getFileLengthBound() == 0) {
// page will be exactly on file border
flag = ReclaimFlag.RECLAIM;
} else {
final ByteIterable[] iterables = getByteIterables(flag);
long result = log.tryWrite(type, structureId, new CompoundByteIterable(iterables));
if (result < 0) {
iterables[0] = CompressedUnsignedLongByteIterable.getIterable((size << 1) + ReclaimFlag.RECLAIM.value);
result = log.writeContinuously(type, structureId, new CompoundByteIterable(iterables));
if (result < 0) {
throw new TooBigLoggableException();
}
}
return result;
}
}
return log.write(type, structureId, new CompoundByteIterable(getByteIterables(flag)));
}
use of jetbrains.exodus.CompoundByteIterable in project xodus by JetBrains.
the class BTreeMutable method save.
@Override
public long save() {
// dfs, save leafs, then bottoms, then internals, then root
final byte type = root.isBottom() ? BOTTOM_ROOT : INTERNAL_ROOT;
final Log log = getLog();
final ByteIterable savedData = root.getData();
final ByteIterable[] iterables = { CompressedUnsignedLongByteIterable.getIterable(size), savedData };
return log.write(type, structureId, new CompoundByteIterable(iterables));
}
Aggregations