use of org.apache.jena.dboe.DBOpEnvException in project jena by apache.
the class FilenameUtils method scanForDirByPattern.
/**
* Find the files in this directory that have namebase as a prefix and
* are then numbered.
* <p>
* Returns a sorted list from, low to high index.
*/
public static List<Path> scanForDirByPattern(Path directory, String namebase, String nameSep) {
Pattern pattern = Pattern.compile(Pattern.quote(namebase) + Pattern.quote(nameSep) + "[\\d]+");
List<Path> paths = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory, namebase + nameSep + "*")) {
for (Path entry : stream) {
if (!pattern.matcher(entry.getFileName().toString()).matches()) {
throw new DBOpEnvException("Invalid filename for matching: " + entry.getFileName());
// Alternative: Skip bad trailing parts but more likely there is a naming problem.
// LOG.warn("Invalid filename for matching: {} skipped", entry.getFileName());
// continue;
}
// Follows symbolic links.
if (!Files.isDirectory(entry))
throw new DBOpEnvException("Not a directory: " + entry);
paths.add(entry);
}
} catch (IOException ex) {
FmtLog.warn(LOG, "Can't inspect directory: (%s, %s)", directory, namebase);
throw new DBOpEnvException(ex);
}
Comparator<Path> comp = (f1, f2) -> {
int num1 = extractIndex(f1.getFileName().toString(), namebase, nameSep);
int num2 = extractIndex(f2.getFileName().toString(), namebase, nameSep);
return Integer.compare(num1, num2);
};
paths.sort(comp);
// indexes.sort(Long::compareTo);
return paths;
}
use of org.apache.jena.dboe.DBOpEnvException in project jena by apache.
the class IO_DB method scanForDirByPattern.
/**
* Find the files in this directory that have namebase as a prefix and
* are then numbered.
* <p>
* Returns a sorted list from, low to high index.
*/
public static List<Path> scanForDirByPattern(Path directory, String namebase, String nameSep) {
Pattern pattern = Pattern.compile(Pattern.quote(namebase) + Pattern.quote(nameSep) + "[\\d]+");
List<Path> paths = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory, namebase + "*")) {
for (Path entry : stream) {
if (!pattern.matcher(entry.getFileName().toString()).matches()) {
throw new DBOpEnvException("Invalid filename for matching: " + entry.getFileName());
// Alternative: Skip bad trailing parts but more likely there is a naming problem.
// LOG.warn("Invalid filename for matching: {} skipped", entry.getFileName());
// continue;
}
// Follows symbolic links.
if (!Files.isDirectory(entry))
throw new DBOpEnvException("Not a directory: " + entry);
paths.add(entry);
}
} catch (IOException ex) {
FmtLog.warn(IO_DB.class, "Can't inspect directory: (%s, %s)", directory, namebase);
throw new DBOpEnvException(ex);
}
Comparator<Path> comp = (f1, f2) -> {
int num1 = extractIndex(f1.getFileName().toString(), namebase, nameSep);
int num2 = extractIndex(f2.getFileName().toString(), namebase, nameSep);
return Integer.compare(num1, num2);
};
paths.sort(comp);
// indexes.sort(Long::compareTo);
return paths;
}
use of org.apache.jena.dboe.DBOpEnvException in project jena by apache.
the class ProcessFileLock method lockOperation.
/**
* Take the lock.
* <p>
* Write our PID into the file and return true if it succeeds.
* <p>
* Try to get the existing PID if it fails.
*/
private boolean lockOperation(NoLockAction action) {
synchronized (sync) {
if (fileLock != null)
throw new AlreadyLocked("Failed to get a lock: file='" + filepath + "': Lock already held");
try {
fileLock = (action != NoLockAction.WAIT) ? fileChannel.tryLock() : fileChannel.lock();
if (fileLock == null) {
switch(action) {
case EXCEPTION:
{
// Read without the lock.
// This isn't perfect (synchronization issues across multiple processes)
// but it is only providing helpful information.
int pid = readProcessId(-99);
if (pid >= 0)
throw new DBOpEnvException("Failed to get a lock: file='" + filepath + "': held by process " + pid);
throw new DBOpEnvException("Failed to get a lock: file='" + filepath + "': failed to get the holder's process id");
}
case RETURN:
return false;
case WAIT:
throw new InternalError("FileChannel.lock returned null");
}
}
// Got the lock. Record our process id.
int pid = ProcessUtils.getPid(-1);
writeProcessId(pid);
return true;
} catch (IOException ex) {
if (action == NoLockAction.RETURN)
return false;
throw new DBOpEnvException("Failed to get a lock: file='" + filepath + "'", ex);
}
}
}
use of org.apache.jena.dboe.DBOpEnvException in project jena by apache.
the class BPlusTreeFactory method createEmptyBPT.
/**
* Allocate root node space. The root is a Node with a Records block.
* @param stateMgr
*/
private static int createEmptyBPT(BPTStateMgr stateMgr, BPTreeNodeMgr nodeManager, BPTreeRecordsMgr recordsMgr) {
// Create an empty records block.
nodeManager.startUpdate();
recordsMgr.startUpdate();
// Empty tree.
stateMgr.setState(0, 0, 0);
try {
BPTreeRecords recordsPage = recordsMgr.create();
if (recordsPage.getId() != BPlusTreeParams.RootId)
throw new DBOpEnvException("Root blocks must be at position zero (got " + recordsPage.getId() + ")");
// Empty data block.
recordsMgr.write(recordsPage);
recordsMgr.release(recordsPage);
// Not this - we haven't full initialized and the BPTreeRecords has null BPTreeRecordsMgr
// recordsPage.write();
// recordsPage.release();
BPTreeNode n = nodeManager.createNode(BPlusTreeParams.RootParent);
// n.ptrs is currently invalid. count was 0 so thinks it has a pointer.
// Force to right layout.
// No pointers
n.ptrs.setSize(0);
// Add the page below
n.ptrs.add(recordsPage.getId());
// n.ptrs.set(0, page.getId()); // This is the same as the size is one.
n.setIsLeaf(true);
// Count is count of records.
n.setCount(0);
int rootId = n.getId();
nodeManager.write(n);
nodeManager.release(n);
// This makes the next blocks alocated 1 and 1 - just like a transaction.
// Blocks 0/0 are always an empty tree unless the bulk loader built the tree.
stateMgr.setState(0, 1, 1);
return rootId;
} finally {
recordsMgr.finishUpdate();
nodeManager.finishUpdate();
}
// stateMgr.setState(0, 1, 1);
}
use of org.apache.jena.dboe.DBOpEnvException in project jena by apache.
the class BPlusTreeParams method readMeta.
public static BPlusTreeParams readMeta(MetaFile mf) {
try {
int pOrder = mf.getPropertyAsInteger(ParamOrder);
int pKeyLen = mf.getPropertyAsInteger(ParamKeyLength);
int pRecLen = mf.getPropertyAsInteger(ParamValueLength);
return new BPlusTreeParams(pOrder, pKeyLen, pRecLen);
} catch (NumberFormatException ex) {
Log.error(BPlusTreeParams.class, "Badly formed metadata for B+Tree");
throw new DBOpEnvException("Failed to read metadata");
}
}
Aggregations