Search in sources :

Example 1 with CompoundByteIterable

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;
}
Also used : CompoundByteIterable(jetbrains.exodus.CompoundByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) CompoundByteIterable(jetbrains.exodus.CompoundByteIterable)

Example 2 with CompoundByteIterable

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)));
}
Also used : Log(jetbrains.exodus.log.Log) TooBigLoggableException(jetbrains.exodus.log.TooBigLoggableException) CompoundByteIterable(jetbrains.exodus.CompoundByteIterable) CompressedUnsignedLongByteIterable(jetbrains.exodus.log.CompressedUnsignedLongByteIterable) CompressedUnsignedLongArrayByteIterable(jetbrains.exodus.bindings.CompressedUnsignedLongArrayByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) CompoundByteIterable(jetbrains.exodus.CompoundByteIterable)

Example 3 with CompoundByteIterable

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));
}
Also used : CompoundByteIterable(jetbrains.exodus.CompoundByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) CompoundByteIterable(jetbrains.exodus.CompoundByteIterable)

Aggregations

ByteIterable (jetbrains.exodus.ByteIterable)3 CompoundByteIterable (jetbrains.exodus.CompoundByteIterable)3 CompressedUnsignedLongArrayByteIterable (jetbrains.exodus.bindings.CompressedUnsignedLongArrayByteIterable)1 CompressedUnsignedLongByteIterable (jetbrains.exodus.log.CompressedUnsignedLongByteIterable)1 Log (jetbrains.exodus.log.Log)1 TooBigLoggableException (jetbrains.exodus.log.TooBigLoggableException)1