use of org.apache.jena.tdb2.TDBException 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 = tupleMap.map(patternNaturalOrder);
// Canonical form.
int numSlots = 0;
// Index of last leading pattern NodeId. Start less than numSlots-1
int leadingIdx = -2;
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 seting leading key slots.
leading = false;
continue;
}
// if ( NodeId.isDoesNotExist(X) )
// return Iter.nullIterator();
numSlots++;
if (leading) {
leadingIdx = i;
NodeIdFactory.set(X, minRec.getKey(), i * SizeOfNodeId);
NodeIdFactory.set(X, 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<>();
}
if (true) {
Iterator<Tuple<NodeId>> tuples;
if (leadingIdx < 0) {
if (!fullScanAllowed)
return null;
// Full scan necessary
tuples = index.iterator(null, null, recordMapper);
} 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.
NodeIdFactory.setNext(X, maxRec.getKey(), leadingIdx * SizeOfNodeId);
tuples = index.iterator(minRec, maxRec, recordMapper);
}
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 = scan(tuples, patternNaturalOrder);
}
return tuples;
}
Iterator<Record> iter = null;
if (leadingIdx < 0) {
if (!fullScanAllowed)
return null;
// 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.
NodeIdFactory.setNext(X, maxRec.getKey(), leadingIdx * SizeOfNodeId);
iter = index.iterator(minRec, maxRec);
}
Iterator<Tuple<NodeId>> tuples = Iter.map(iter, item -> TupleLib.tuple(item, tupleMap));
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 = scan(tuples, patternNaturalOrder);
}
return tuples;
}
use of org.apache.jena.tdb2.TDBException in project jena by apache.
the class TupleTable method find.
/**
* Find all matching tuples - a slot of NodeId.NodeIdAny means match any
*/
public Iterator<Tuple<NodeId>> find(Tuple<NodeId> pattern) {
if (tupleLen != pattern.len())
throw new TDBException(format("Mismatch: finding tuple of length %d in a table of tuples of length %d", pattern.len(), tupleLen));
int numSlots = 0;
// Canonical form.
for (int i = 0; i < tupleLen; i++) {
NodeId x = pattern.get(i);
if (!NodeId.isAny(x))
numSlots++;
if (NodeId.isDoesNotExist(x))
return Iter.nullIterator();
}
if (numSlots == 0)
return scanAllIndex.all();
int indexNumSlots = 0;
TupleIndex index = null;
for (TupleIndex idx : indexes) {
if (idx != null) {
int w = idx.weight(pattern);
if (w > indexNumSlots) {
indexNumSlots = w;
index = idx;
}
}
}
if (index == null)
// No index at all. Scan.
index = indexes[0];
return index.find(pattern);
}
use of org.apache.jena.tdb2.TDBException in project jena by apache.
the class TupleTable method add.
/**
* Insert a tuple
*/
public void add(Tuple<NodeId> t) {
// the indexes when the triple is already present.
if (tupleLen != t.len())
throw new TDBException(format("Mismatch: inserting tuple of length %d into a table of tuples of length %d", t.len(), tupleLen));
for (int i = 0; i < indexes.length; i++) {
if (indexes[i] == null)
continue;
indexes[i].add(t);
syncNeeded = true;
}
}
use of org.apache.jena.tdb2.TDBException in project jena by apache.
the class DatabaseOps method compact.
// XXX Later - switch in a recording dataset, not block writers, and reply after
// switch over before releasing the new dataset to the container.
// Maybe copy indexes and switch the DSG over (drop switchable).
/**
* Copy the latest version from one location to another.
*/
private static void compact(DatasetGraphSwitchable container, Location loc1, Location loc2) {
if (loc1.isMem() || loc2.isMem())
throw new TDBException("Compact involves a memory location: " + loc1 + " : " + loc2);
copyFiles(loc1, loc2);
StoreConnection srcConn = StoreConnection.connectExisting(loc1);
if (srcConn == null)
throw new TDBException("No database at location : " + loc1);
if (!(container.get() instanceof DatasetGraphTDB))
throw new TDBException("Not a TDB2 database in DatasetGraphSwitchable");
DatasetGraphTDB dsgCurrent = (DatasetGraphTDB) container.get();
if (!dsgCurrent.getLocation().equals(loc1))
throw new TDBException("Inconsistent locations for base : " + dsgCurrent.getLocation() + " , " + dsgCurrent.getLocation());
DatasetGraphTDB dsgBase = srcConn.getDatasetGraphTDB();
if (dsgBase != dsgCurrent)
throw new TDBException("Inconsistent datasets : " + dsgCurrent.getLocation() + " , " + dsgBase.getLocation());
TransactionalSystem txnSystem = dsgBase.getTxnSystem();
TransactionCoordinator txnMgr = dsgBase.getTxnSystem().getTxnMgr();
// Stop update. On exit there are no writers and none will start until switched over.
txnMgr.tryBlockWriters();
// txnMgr.begin(WRITE, false) will now bounce.
// Copy the latest generation.
DatasetGraphTDB dsgCompact = StoreConnection.connectCreate(loc2).getDatasetGraphTDB();
CopyDSG.copy(dsgBase, dsgCompact);
TransactionCoordinator txnMgr2 = dsgCompact.getTxnSystem().getTxnMgr();
txnMgr2.startExclusiveMode();
txnMgr.startExclusiveMode();
// Switch.
if (!container.change(dsgCurrent, dsgCompact)) {
Log.warn(DatabaseOps.class, "Inconistent: old datasetgraph not as expected");
container.set(dsgCompact);
}
txnMgr2.finishExclusiveMode();
// New database running.
// Clean-up.
// txnMgr.finishExclusiveMode();
// Don't call : txnMgr.startWriters();
StoreConnection.release(dsgBase.getLocation());
}
use of org.apache.jena.tdb2.TDBException in project jena by apache.
the class DatabaseOps method compact.
public static void compact(DatasetGraphSwitchable container, boolean shouldDeleteOld) {
checkSupportsAdmin(container);
synchronized (compactionLock) {
Path base = container.getContainerPath();
Path db1 = findLocation(base, dbPrefix);
if (db1 == null)
throw new TDBException("No location: (" + base + ", " + dbPrefix + ")");
Location loc1 = IO_DB.asLocation(db1);
// -- Checks
Location loc1a = ((DatasetGraphTDB) container.get()).getLocation();
if (loc1a.isMem()) {
}
if (!loc1a.exists())
throw new TDBException("No such location: " + loc1a);
// Is this the same database location?
if (!loc1.equals(loc1a))
throw new TDBException("Inconsistent (not latest?) : " + loc1a + " : " + loc1);
// -- Checks
// Version
int v = IO_DB.extractIndex(db1.getFileName().toString(), dbPrefix, SEP);
String next = FilenameUtils.filename(dbPrefix, SEP, v + 1);
Path db2 = db1.getParent().resolve(next);
IOX.createDirectory(db2);
Location loc2 = IO_DB.asLocation(db2);
LOG.debug(String.format("Compact %s -> %s\n", db1.getFileName(), db2.getFileName()));
compact(container, loc1, loc2);
if (shouldDeleteOld) {
Path loc1Path = IO_DB.asPath(loc1);
LOG.debug("Deleting old database after successful compaction (old db path='" + loc1Path + "')...");
try (Stream<Path> walk = Files.walk(loc1Path)) {
walk.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
} catch (IOException ex) {
throw IOX.exception(ex);
}
}
}
}
Aggregations