Search in sources :

Example 1 with DBOpEnvException

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;
}
Also used : Path(java.nio.file.Path) DirectoryStream(java.nio.file.DirectoryStream) DBOpEnvException(org.apache.jena.dboe.DBOpEnvException) List(java.util.List) Logger(org.slf4j.Logger) Files(java.nio.file.Files) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) Pattern(java.util.regex.Pattern) Comparator(java.util.Comparator) Path(java.nio.file.Path) FmtLog(org.apache.jena.atlas.logging.FmtLog) ArrayList(java.util.ArrayList) Pattern(java.util.regex.Pattern) DBOpEnvException(org.apache.jena.dboe.DBOpEnvException) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 2 with DBOpEnvException

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;
}
Also used : Path(java.nio.file.Path) RuntimeIOException(org.apache.jena.atlas.RuntimeIOException) DBOpEnvException(org.apache.jena.dboe.DBOpEnvException) Files(java.nio.file.Files) IOException(java.io.IOException) FmtLog(org.apache.jena.atlas.logging.FmtLog) File(java.io.File) ArrayList(java.util.ArrayList) Objects(java.util.Objects) DirectoryStream(java.nio.file.DirectoryStream) List(java.util.List) Location(org.apache.jena.dboe.base.file.Location) Paths(java.nio.file.Paths) Pattern(java.util.regex.Pattern) Comparator(java.util.Comparator) Path(java.nio.file.Path) Pattern(java.util.regex.Pattern) DBOpEnvException(org.apache.jena.dboe.DBOpEnvException) ArrayList(java.util.ArrayList) RuntimeIOException(org.apache.jena.atlas.RuntimeIOException) IOException(java.io.IOException)

Example 3 with DBOpEnvException

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);
        }
    }
}
Also used : DBOpEnvException(org.apache.jena.dboe.DBOpEnvException) RuntimeIOException(org.apache.jena.atlas.RuntimeIOException) IOException(java.io.IOException)

Example 4 with DBOpEnvException

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);
}
Also used : DBOpEnvException(org.apache.jena.dboe.DBOpEnvException)

Example 5 with DBOpEnvException

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");
    }
}
Also used : DBOpEnvException(org.apache.jena.dboe.DBOpEnvException)

Aggregations

DBOpEnvException (org.apache.jena.dboe.DBOpEnvException)5 IOException (java.io.IOException)3 DirectoryStream (java.nio.file.DirectoryStream)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 Comparator (java.util.Comparator)2 List (java.util.List)2 Pattern (java.util.regex.Pattern)2 RuntimeIOException (org.apache.jena.atlas.RuntimeIOException)2 FmtLog (org.apache.jena.atlas.logging.FmtLog)2 File (java.io.File)1 Paths (java.nio.file.Paths)1 Objects (java.util.Objects)1 Location (org.apache.jena.dboe.base.file.Location)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1