Search in sources :

Example 1 with PtrBuffer

use of org.apache.jena.dboe.base.buffer.PtrBuffer in project jena by apache.

the class BPTreeNodeBuilder method hasNext.

@Override
public boolean hasNext() {
    if (slot != null)
        return true;
    if (iter == null)
        return false;
    if (!iter.hasNext()) {
        // Start of next block, no more input.
        iter = null;
        return false;
    }
    // At least one element to put in a new node.
    // Unknown parent. Does not matter (parent is only in-memory)
    BPTreeNode bptNode = mgr.createNode(-1);
    bptNode.setIsLeaf(leafLayer);
    RecordBuffer recBuff = bptNode.getRecordBuffer();
    PtrBuffer ptrBuff = bptNode.getPtrBuffer();
    recBuff.setSize(0);
    // Creation leaves this junk.
    ptrBuff.setSize(0);
    final boolean debug = false;
    int rMax = recBuff.maxSize();
    int pMax = ptrBuff.maxSize();
    if (debug)
        System.out.printf("Max: (%d, %d)\n", rMax, pMax);
    for (; iter.hasNext(); ) {
        int X = bptNode.getCount();
        int X2 = bptNode.getMaxSize();
        int P = ptrBuff.size();
        int P2 = ptrBuff.maxSize();
        int R = recBuff.size();
        int R2 = recBuff.maxSize();
        // bptNode.getMaxSize() is drivel
        // System.out.printf("N: %d/%d : P %d/%d : R %d/%d\n", X, X2, P, P2,
        // R, R2);
        Pair<Integer, Record> pair = iter.next();
        if (debug)
            System.out.println("** Item: " + pair);
        Record r = pair.cdr();
        // The record is key-only (which is correct) but until FREC fixed,
        // we need key,value
        r = recordFactory.create(r.getKey());
        // There is always one more ptr than record in a B+Tree node.
        if (ptrBuff.isFull())
            Log.error(this, "PtrBuffer is full");
        // Add pointer.
        ptrBuff.add(pair.car());
        if (ptrBuff.isFull()) {
            // Internal consistency check.
            if (!ptrBuff.isFull())
                Log.error(this, "PtrBuffer is not full");
            // The split point for the next level up.
            slot = new Pair<>(bptNode.getId(), pair.cdr());
            if (debug)
                System.out.printf("Write(1): %d\n", bptNode.getId());
            if (debug)
                System.out.println(bptNode);
            if (debug)
                System.out.println("Slot = " + slot);
            mgr.put(bptNode);
            // No count increment needed.
            return true;
        }
        recBuff.add(r);
        bptNode.setCount(bptNode.getCount() + 1);
    }
    // If we get here, the input stream ran out before we finished a
    // complete block.
    // Fix up block (remove the last record)
    Record r = recBuff.getHigh();
    recBuff.removeTop();
    bptNode.setCount(bptNode.getCount() - 1);
    slot = new Pair<>(bptNode.getId(), r);
    if (debug)
        System.out.printf("Write(2): %d\n", bptNode.getId());
    if (debug)
        System.out.println(bptNode);
    if (debug)
        System.out.println("Slot = " + slot);
    mgr.put(bptNode);
    return true;
}
Also used : BPTreeNode(org.apache.jena.dboe.trans.bplustree.BPTreeNode) Record(org.apache.jena.dboe.base.record.Record) RecordBuffer(org.apache.jena.dboe.base.buffer.RecordBuffer) PtrBuffer(org.apache.jena.dboe.base.buffer.PtrBuffer)

Example 2 with PtrBuffer

use of org.apache.jena.dboe.base.buffer.PtrBuffer in project jena by apache.

the class BPTreeNodeMgr method formatBPTreeNode.

static void formatBPTreeNode(BPTreeNode n, BPlusTree bpTree, Block block, boolean leaf, int parent, int count) {
    BPlusTreeParams params = bpTree.getParams();
    int ptrBuffLen = params.MaxPtr * params.getPtrLength();
    // Allocate space for record, key and value, despite slight over allocation.
    int recBuffLen = params.MaxRec * params.getRecordLength();
    n.id = block.getId().intValue();
    n.setParent(parent);
    n.setCount(count);
    n.setIsLeaf(leaf);
    int header = BPlusTreeParams.BlockHeaderSize;
    int rStart = header;
    int pStart = header + recBuffLen;
    // Find the number of pointers.
    int numPtrs = -1;
    // Junk when creating a new new node.
    if (n.getCount() < 0) {
        numPtrs = 0;
        n.setCount(decodeCount(n.getCount()));
    } else
        numPtrs = n.getCount() + 1;
    // Block dependent
    n.block = block;
    ByteBuffer byteBuffer = block.getByteBuffer();
    // -- Records area
    byteBuffer.position(rStart);
    byteBuffer.limit(rStart + recBuffLen);
    ByteBuffer bbr = byteBuffer.slice();
    // bbr.limit(recBuffLen);
    n.setRecordBuffer(new RecordBuffer(bbr, n.params.keyFactory, n.getCount()));
    // -- Pointers area
    byteBuffer.position(pStart);
    byteBuffer.limit(pStart + ptrBuffLen);
    ByteBuffer bbi = byteBuffer.slice();
    // bbi.limit(ptrBuffLen);
    n.setPtrBuffer(new PtrBuffer(bbi, numPtrs));
    // Reset
    byteBuffer.rewind();
}
Also used : RecordBuffer(org.apache.jena.dboe.base.buffer.RecordBuffer) ByteBuffer(java.nio.ByteBuffer) PtrBuffer(org.apache.jena.dboe.base.buffer.PtrBuffer)

Example 3 with PtrBuffer

use of org.apache.jena.dboe.base.buffer.PtrBuffer in project jena by apache.

the class BPlusTreeRewriter method copyBPTreeNode.

private static void copyBPTreeNode(BPTreeNode nodeSrc, BPTreeNode nodeDst, BPlusTree bpt2) {
    PtrBuffer pBuff = nodeSrc.getPtrBuffer();
    pBuff.copy(0, nodeDst.getPtrBuffer(), 0, pBuff.getSize());
    RecordBuffer rBuff = nodeSrc.getRecordBuffer();
    rBuff.copy(0, nodeDst.getRecordBuffer(), 0, rBuff.getSize());
    nodeDst.setCount(nodeSrc.getCount());
    nodeDst.setIsLeaf(nodeSrc.isLeaf());
    bpt2.getNodeManager().put(nodeDst);
}
Also used : RecordBuffer(org.apache.jena.dboe.base.buffer.RecordBuffer) PtrBuffer(org.apache.jena.dboe.base.buffer.PtrBuffer)

Aggregations

PtrBuffer (org.apache.jena.dboe.base.buffer.PtrBuffer)3 RecordBuffer (org.apache.jena.dboe.base.buffer.RecordBuffer)3 ByteBuffer (java.nio.ByteBuffer)1 Record (org.apache.jena.dboe.base.record.Record)1 BPTreeNode (org.apache.jena.dboe.trans.bplustree.BPTreeNode)1