Search in sources :

Example 91 with Record

use of org.apache.jena.tdb.base.record.Record in project jena by apache.

the class TupleIndexRecord method findWorker.

private Iterator<Tuple<NodeId>> findWorker(Tuple<NodeId> patternNaturalOrder, boolean partialScanAllowed, boolean fullScanAllowed) {
    if (Check) {
        if (tupleLength != patternNaturalOrder.len())
            throw new TDBException(String.format("Mismatch: tuple length %d / index for length %d", patternNaturalOrder.len(), tupleLength));
    }
    // Convert to index order.
    Tuple<NodeId> pattern = colMap.map(patternNaturalOrder);
    // Canonical form.
    int numSlots = 0;
    // Index of last leading pattern NodeId. Start less
    int leadingIdx = -2;
    // than numSlots-1
    boolean leading = true;
    // Records.
    Record minRec = factory.createKeyOnly();
    Record maxRec = factory.createKeyOnly();
    // Set the prefixes.
    for (int i = 0; i < pattern.len(); i++) {
        NodeId X = pattern.get(i);
        if (NodeId.isAny(X)) {
            X = null;
            // No longer setting leading key slots.
            leading = false;
            continue;
        }
        numSlots++;
        if (leading) {
            leadingIdx = i;
            Bytes.setLong(X.getId(), minRec.getKey(), i * SizeOfNodeId);
            Bytes.setLong(X.getId(), maxRec.getKey(), i * SizeOfNodeId);
        }
    }
    // Is it a simple existence test?
    if (numSlots == pattern.len()) {
        if (index.contains(minRec))
            return new SingletonIterator<>(pattern);
        else
            return new NullIterator<>();
    }
    Iterator<Record> iter = null;
    if (leadingIdx < 0) {
        if (!fullScanAllowed)
            return null;
        // System.out.println("Full scan") ;
        // Full scan necessary
        iter = index.iterator();
    } else {
        // Adjust the maxRec.
        NodeId X = pattern.get(leadingIdx);
        // Set the max Record to the leading NodeIds, +1.
        // Example, SP? inclusive to S(P+1)? exclusive where ? is zero.
        Bytes.setLong(X.getId() + 1, maxRec.getKey(), leadingIdx * SizeOfNodeId);
        iter = index.iterator(minRec, maxRec);
    }
    Iterator<Tuple<NodeId>> tuples = Iter.map(iter, item -> TupleLib.tuple(item, colMap));
    if (leadingIdx < numSlots - 1) {
        if (!partialScanAllowed)
            return null;
        // Didn't match all defined slots in request.
        // Partial or full scan needed.
        // pattern.unmap(colMap) ;
        tuples = TupleIndex.scan(tuples, patternNaturalOrder);
    }
    return tuples;
}
Also used : TDBException(org.apache.jena.tdb.TDBException) SizeOfNodeId(org.apache.jena.tdb.sys.SystemTDB.SizeOfNodeId) NodeId(org.apache.jena.tdb.store.NodeId) Record(org.apache.jena.tdb.base.record.Record) Tuple(org.apache.jena.atlas.lib.tuple.Tuple)

Example 92 with Record

use of org.apache.jena.tdb.base.record.Record in project jena by apache.

the class DebugTDB method dumpIndex.

public static void dumpIndex(TupleIndex idx) {
    Iterator<Tuple<NodeId>> iter = idx.all();
    while (iter.hasNext()) {
        Tuple<NodeId> tuple = iter.next();
        System.out.println(tuple);
    }
    if (false) {
        // Dump raw
        TupleIndexRecord tir = (TupleIndexRecord) idx;
        RangeIndex rIdx = tir.getRangeIndex();
        for (Record aRIdx : rIdx) {
            System.out.println(aRIdx);
        }
    }
}
Also used : TupleIndexRecord(org.apache.jena.tdb.store.tupletable.TupleIndexRecord) NodeId(org.apache.jena.tdb.store.NodeId) Record(org.apache.jena.tdb.base.record.Record) TupleIndexRecord(org.apache.jena.tdb.store.tupletable.TupleIndexRecord) RangeIndex(org.apache.jena.tdb.index.RangeIndex) Tuple(org.apache.jena.atlas.lib.tuple.Tuple)

Example 93 with Record

use of org.apache.jena.tdb.base.record.Record in project jena by apache.

the class NodeTableNative method accessIndex.

private final NodeId accessIndex(Node node, boolean create) {
    Hash hash = new Hash(nodeHashToId.getRecordFactory().keyLength());
    setHash(hash, node);
    byte[] k = hash.getBytes();
    // Key only.
    Record r = nodeHashToId.getRecordFactory().create(k);
    synchronized (// Pair to readNodeFromTable.
    this) {
        // Key and value, or null
        Record r2 = nodeHashToId.find(r);
        if (r2 != null) {
            // Found.  Get the NodeId.
            NodeId id = NodeId.create(r2.getValue(), 0);
            return id;
        }
        // Not found.
        if (!create)
            return NodeId.NodeDoesNotExist;
        // Write the node, which allocates an id for it.
        NodeId id = writeNodeToTable(node);
        // Update the r record with the new id.
        // r.value := id bytes ;
        id.toBytes(r.getValue(), 0);
        // Put in index - may appear because of concurrency
        if (!nodeHashToId.add(r))
            throw new TDBException("NodeTableBase::nodeToId - record mysteriously appeared");
        return id;
    }
}
Also used : TDBException(org.apache.jena.tdb.TDBException) NodeId(org.apache.jena.tdb.store.NodeId) Record(org.apache.jena.tdb.base.record.Record) NodeLib.setHash(org.apache.jena.tdb.lib.NodeLib.setHash) Hash(org.apache.jena.tdb.store.Hash)

Example 94 with Record

use of org.apache.jena.tdb.base.record.Record in project jena by apache.

the class RecordBufferPagePacker method hasNext.

@Override
public boolean hasNext() {
    if (recordBufferPage == null) {
        if (records == null)
            return false;
        if (!records.hasNext()) {
            records = null;
            return false;
        }
        // At least one record to be processed.
        // No pending RecordBufferPage
        // ==> There will be a RecordBufferPage to yield.
        // int id = rbMgr.allocateId() ;
        // //System.out.println("Allocate : "+id) ;
        recordBufferPage = rbMgr.create();
        RecordBuffer rb = recordBufferPage.getRecordBuffer();
        while (!rb.isFull() && records.hasNext()) {
            Record r = records.next();
            rb.add(r);
        }
        if (!records.hasNext())
            records = null;
        return true;
    }
    return true;
}
Also used : Record(org.apache.jena.tdb.base.record.Record) RecordBuffer(org.apache.jena.tdb.base.buffer.RecordBuffer)

Example 95 with Record

use of org.apache.jena.tdb.base.record.Record in project jena by apache.

the class dumpbpt method exec.

@Override
protected void exec() {
    List<String> tripleIndexes = Arrays.asList(Names.tripleIndexes);
    List<String> quadIndexes = Arrays.asList(Names.quadIndexes);
    Location loc = modLocation.getLocation();
    // The name is the order.
    for (String indexName : super.getPositional()) {
        String primary;
        if (indexName.length() == 3) {
            primary = Names.primaryIndexTriples;
        } else if (indexName.length() == 4) {
            primary = Names.primaryIndexQuads;
        } else if (Objects.equals(indexName, Names.indexNode2Id)) {
            primary = Names.indexNode2Id;
        } else {
            cmdError("Wrong length: " + indexName);
            primary = null;
        }
        // prefix2id
        // prefixIdx : GPU
        int keySubLen = SystemTDB.SizeOfNodeId;
        int keyUnitLen = indexName.length();
        int keyLength = keySubLen * keyUnitLen;
        int valueLength = 0;
        // Node table indexes.
        if (Objects.equals(indexName, Names.indexNode2Id) || Objects.equals(indexName, Names.prefixNode2Id)) {
            keySubLen = SystemTDB.LenNodeHash;
            keyUnitLen = 1;
            keyLength = SystemTDB.LenNodeHash;
            valueLength = SystemTDB.SizeOfNodeId;
        }
        // Prefixes
        if (Objects.equals(indexName, Names.indexPrefix)) {
            primary = Names.primaryIndexPrefix;
        }
        RecordFactory rf = new RecordFactory(keyLength, valueLength);
        RangeIndex rIndex = IndexFactory.buildRangeIndex(loc, indexName, rf);
        BPlusTree bpt = (BPlusTree) rIndex;
        if (false) {
            System.out.println("---- Index structure");
            bpt.dump();
        }
        if (true) {
            System.out.println("---- Index contents");
            Iterator<Record> iter = bpt.iterator();
            if (!iter.hasNext())
                System.out.println("<<Empty>>");
            for (; iter.hasNext(); ) {
                Record r = iter.next();
                printRecord("", System.out, r, keyUnitLen);
            }
        }
        // Check.
        Iterator<Record> iterCheck = bpt.iterator();
        Record r1 = null;
        int i = 0;
        for (; iterCheck.hasNext(); ) {
            Record r2 = iterCheck.next();
            i++;
            if (r1 != null) {
                if (!Record.keyLT(r1, r2)) {
                    System.err.println("key error@ " + i);
                    printRecord("  ", System.err, r1, keyUnitLen);
                    printRecord("  ", System.err, r2, keyUnitLen);
                }
            }
            r1 = r2;
        }
        if (false) {
            // Dump in tuple order.
            TupleIndex tupleIndex = new TupleIndexRecord(primary.length(), new ColumnMap(primary, indexName), indexName, rIndex.getRecordFactory(), rIndex);
            if (true) {
                System.out.println("---- Tuple contents");
                Iterator<Tuple<NodeId>> iter2 = tupleIndex.all();
                if (!iter2.hasNext())
                    System.out.println("<<Empty>>");
                for (; iter2.hasNext(); ) {
                    Tuple<NodeId> row = iter2.next();
                    System.out.println(row);
                }
            }
        }
    }
}
Also used : ColumnMap(org.apache.jena.tdb.lib.ColumnMap) TupleIndexRecord(org.apache.jena.tdb.store.tupletable.TupleIndexRecord) RecordFactory(org.apache.jena.tdb.base.record.RecordFactory) NodeId(org.apache.jena.tdb.store.NodeId) Record(org.apache.jena.tdb.base.record.Record) TupleIndexRecord(org.apache.jena.tdb.store.tupletable.TupleIndexRecord) RangeIndex(org.apache.jena.tdb.index.RangeIndex) TupleIndex(org.apache.jena.tdb.store.tupletable.TupleIndex) BPlusTree(org.apache.jena.tdb.index.bplustree.BPlusTree) Tuple(org.apache.jena.atlas.lib.tuple.Tuple) Location(org.apache.jena.tdb.base.file.Location) ModLocation(tdb.cmdline.ModLocation)

Aggregations

Record (org.apache.jena.tdb.base.record.Record)95 Test (org.junit.Test)20 RecordLib.intToRecord (org.apache.jena.tdb.base.record.RecordLib.intToRecord)15 RecordBuffer (org.apache.jena.tdb.base.buffer.RecordBuffer)14 BaseTest (org.apache.jena.atlas.junit.BaseTest)10 RangeIndex (org.apache.jena.tdb.index.RangeIndex)8 BlockMgr (org.apache.jena.tdb.base.block.BlockMgr)6 RecordFactory (org.apache.jena.tdb.base.record.RecordFactory)6 NoSuchElementException (java.util.NoSuchElementException)5 FileSet (org.apache.jena.tdb.base.file.FileSet)5 BPlusTree (org.apache.jena.tdb.index.bplustree.BPlusTree)5 NodeId (org.apache.jena.tdb.store.NodeId)5 Pair (org.apache.jena.atlas.lib.Pair)4 Location (org.apache.jena.tdb.base.file.Location)4 BPlusTreeParams (org.apache.jena.tdb.index.bplustree.BPlusTreeParams)4 Tuple (org.apache.jena.atlas.lib.tuple.Tuple)3 StorageException (org.apache.jena.tdb.base.StorageException)3 RecordBufferPage (org.apache.jena.tdb.base.recordbuffer.RecordBufferPage)3 ColumnMap (org.apache.jena.tdb.lib.ColumnMap)3 TupleIndexRecord (org.apache.jena.tdb.store.tupletable.TupleIndexRecord)3