Search in sources :

Example 6 with StringLogger

use of org.neo4j.kernel.impl.util.StringLogger in project graphdb by neo4j-attic.

the class AbstractDynamicStore method rebuildIdGenerator.

/**
     * Rebuilds the internal id generator keeping track of what blocks are free
     * or taken.
     * 
     * @throws IOException
     *             If unable to rebuild the id generator
     */
protected void rebuildIdGenerator() {
    if (getBlockSize() <= 0) {
        throw new InvalidRecordException("Illegal blockSize: " + getBlockSize());
    }
    logger.fine("Rebuilding id generator for[" + getStorageFileName() + "] ...");
    closeIdGenerator();
    File file = new File(getStorageFileName() + ".id");
    if (file.exists()) {
        boolean success = file.delete();
        assert success;
    }
    createIdGenerator(getStorageFileName() + ".id");
    openIdGenerator();
    //        nextBlockId(); // reserved first block containing blockSize
    setHighId(1);
    FileChannel fileChannel = getFileChannel();
    long highId = 0;
    long defraggedCount = 0;
    try {
        long fileSize = fileChannel.size();
        boolean fullRebuild = true;
        if (getConfig() != null) {
            String mode = (String) getConfig().get("rebuild_idgenerators_fast");
            if (mode != null && mode.toLowerCase().equals("true")) {
                fullRebuild = false;
                highId = findHighIdBackwards();
            }
        }
        ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[1]);
        LinkedList<Long> freeIdList = new LinkedList<Long>();
        if (fullRebuild) {
            for (long i = 1; i * getBlockSize() < fileSize; i++) {
                fileChannel.position(i * getBlockSize());
                fileChannel.read(byteBuffer);
                byteBuffer.flip();
                byte inUse = byteBuffer.get();
                byteBuffer.flip();
                nextBlockId();
                if (inUse == Record.NOT_IN_USE.byteValue()) {
                    freeIdList.add(i);
                } else {
                    highId = i;
                    while (!freeIdList.isEmpty()) {
                        freeBlockId(freeIdList.removeFirst());
                        defraggedCount++;
                    }
                }
            }
        }
    } catch (IOException e) {
        throw new UnderlyingStorageException("Unable to rebuild id generator " + getStorageFileName(), e);
    }
    setHighId(highId + 1);
    logger.fine("[" + getStorageFileName() + "] high id=" + getHighId() + " (defragged=" + defraggedCount + ")");
    if (getConfig() != null) {
        String storeDir = (String) getConfig().get("store_dir");
        StringLogger msgLog = StringLogger.getLogger(storeDir);
        msgLog.logMessage(getStorageFileName() + " rebuild id generator, highId=" + getHighId() + " defragged count=" + defraggedCount, true);
    }
    closeIdGenerator();
    openIdGenerator();
}
Also used : FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) LinkedList(java.util.LinkedList) File(java.io.File) StringLogger(org.neo4j.kernel.impl.util.StringLogger)

Example 7 with StringLogger

use of org.neo4j.kernel.impl.util.StringLogger in project graphdb by neo4j-attic.

the class AbstractDynamicStore method loadStorage.

//    public AbstractDynamicStore( String fileName )
//    {
//        super( fileName );
//    }
/**
     * Loads this store validating version and id generator. Also the block size
     * is loaded (contained in first block)
     */
protected void loadStorage() {
    try {
        long fileSize = getFileChannel().size();
        String expectedVersion = getTypeAndVersionDescriptor();
        byte[] version = new byte[UTF8.encode(expectedVersion).length];
        ByteBuffer buffer = ByteBuffer.wrap(version);
        getFileChannel().position(fileSize - version.length);
        getFileChannel().read(buffer);
        buffer = ByteBuffer.allocate(4);
        getFileChannel().position(0);
        getFileChannel().read(buffer);
        buffer.flip();
        blockSize = buffer.getInt();
        if (blockSize <= 0) {
            throw new InvalidRecordException("Illegal block size: " + blockSize + " in " + getStorageFileName());
        }
        if (!expectedVersion.equals(UTF8.decode(version))) {
            if (!versionFound(UTF8.decode(version)) && !isReadOnly()) {
                setStoreNotOk();
            }
        }
        if ((fileSize - version.length) % blockSize != 0 && !isReadOnly()) {
            setStoreNotOk();
        }
        if (getStoreOk() && !isReadOnly()) {
            getFileChannel().truncate(fileSize - version.length);
        }
    } catch (IOException e) {
        throw new UnderlyingStorageException("Unable to load storage " + getStorageFileName(), e);
    }
    try {
        if (!isReadOnly() || isBackupSlave()) {
            openIdGenerator();
        } else {
            openReadOnlyIdGenerator(getBlockSize());
        }
    } catch (InvalidIdGeneratorException e) {
        setStoreNotOk();
    } finally {
        if (!getStoreOk()) {
            if (getConfig() != null) {
                String storeDir = (String) getConfig().get("store_dir");
                StringLogger msgLog = StringLogger.getLogger(storeDir);
                msgLog.logMessage(getStorageFileName() + " non clean shutdown detected", true);
            }
        }
    }
    setWindowPool(new PersistenceWindowPool(getStorageFileName(), getBlockSize(), getFileChannel(), getMappedMem(), getIfMemoryMapped(), isReadOnly() && !isBackupSlave()));
}
Also used : IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) StringLogger(org.neo4j.kernel.impl.util.StringLogger)

Example 8 with StringLogger

use of org.neo4j.kernel.impl.util.StringLogger in project graphdb by neo4j-attic.

the class AbstractStore method loadStorage.

//    public AbstractStore( String fileName )
//    {
//        super( fileName );
//    }
protected void loadStorage() {
    try {
        long fileSize = getFileChannel().size();
        String expectedVersion = getTypeAndVersionDescriptor();
        byte[] version = new byte[UTF8.encode(expectedVersion).length];
        ByteBuffer buffer = ByteBuffer.wrap(version);
        if (fileSize >= version.length) {
            getFileChannel().position(fileSize - version.length);
        } else if (!isReadOnly()) {
            setStoreNotOk();
        }
        getFileChannel().read(buffer);
        if (!expectedVersion.equals(UTF8.decode(version)) && !isReadOnly()) {
            if (!versionFound(UTF8.decode(version))) {
                setStoreNotOk();
            }
        }
        if (getRecordSize() != 0 && (fileSize - version.length) % getRecordSize() != 0 && !isReadOnly()) {
            setStoreNotOk();
        }
        if (getStoreOk() && !isReadOnly()) {
            getFileChannel().truncate(fileSize - version.length);
        }
    } catch (IOException e) {
        throw new UnderlyingStorageException("Unable to load store " + getStorageFileName(), e);
    }
    try {
        if (!isReadOnly() || isBackupSlave()) {
            openIdGenerator();
        } else {
            openReadOnlyIdGenerator(getRecordSize());
        }
    } catch (InvalidIdGeneratorException e) {
        setStoreNotOk();
    } finally {
        if (!getStoreOk()) {
            if (getConfig() != null) {
                String storeDir = (String) getConfig().get("store_dir");
                StringLogger msgLog = StringLogger.getLogger(storeDir);
                msgLog.logMessage(getStorageFileName() + " non clean shutdown detected", true);
            }
        }
    }
    setWindowPool(new PersistenceWindowPool(getStorageFileName(), getRecordSize(), getFileChannel(), getMappedMem(), getIfMemoryMapped(), isReadOnly() && !isBackupSlave()));
}
Also used : IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) StringLogger(org.neo4j.kernel.impl.util.StringLogger)

Example 9 with StringLogger

use of org.neo4j.kernel.impl.util.StringLogger in project graphdb by neo4j-attic.

the class AbstractStore method rebuildIdGenerator.

/**
     * Rebuilds the {@link IdGenerator} by looping through all records and
     * checking if record in use or not.
     * 
     * @throws IOException
     *             if unable to rebuild the id generator
     */
protected void rebuildIdGenerator() {
    if (isReadOnly() && !isBackupSlave()) {
        throw new ReadOnlyDbException();
    }
    logger.fine("Rebuilding id generator for[" + getStorageFileName() + "] ...");
    closeIdGenerator();
    File file = new File(getStorageFileName() + ".id");
    if (file.exists()) {
        boolean success = file.delete();
        assert success;
    }
    createIdGenerator(getStorageFileName() + ".id");
    openIdGenerator();
    FileChannel fileChannel = getFileChannel();
    long highId = 1;
    long defraggedCount = 0;
    try {
        long fileSize = fileChannel.size();
        int recordSize = getRecordSize();
        boolean fullRebuild = true;
        if (getConfig() != null) {
            String mode = (String) getConfig().get("rebuild_idgenerators_fast");
            if (mode != null && mode.toLowerCase().equals("true")) {
                fullRebuild = false;
                highId = findHighIdBackwards();
            }
        }
        ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[1]);
        // Duplicated code block
        LinkedList<Long> freeIdList = new LinkedList<Long>();
        if (fullRebuild) {
            for (long i = 0; i * recordSize < fileSize && recordSize > 0; i++) {
                fileChannel.position(i * recordSize);
                fileChannel.read(byteBuffer);
                byteBuffer.flip();
                byte inUse = byteBuffer.get();
                byteBuffer.flip();
                nextId();
                if ((inUse & 0x1) == Record.NOT_IN_USE.byteValue()) {
                    freeIdList.add(i);
                } else {
                    highId = i;
                    while (!freeIdList.isEmpty()) {
                        freeId(freeIdList.removeFirst());
                        defraggedCount++;
                    }
                }
            }
        }
    } catch (IOException e) {
        throw new UnderlyingStorageException("Unable to rebuild id generator " + getStorageFileName(), e);
    }
    setHighId(highId + 1);
    if (getConfig() != null) {
        String storeDir = (String) getConfig().get("store_dir");
        StringLogger msgLog = StringLogger.getLogger(storeDir);
        msgLog.logMessage(getStorageFileName() + " rebuild id generator, highId=" + getHighId() + " defragged count=" + defraggedCount, true);
    }
    logger.fine("[" + getStorageFileName() + "] high id=" + getHighId() + " (defragged=" + defraggedCount + ")");
    closeIdGenerator();
    openIdGenerator();
}
Also used : FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) LinkedList(java.util.LinkedList) ReadOnlyDbException(org.neo4j.kernel.impl.core.ReadOnlyDbException) File(java.io.File) StringLogger(org.neo4j.kernel.impl.util.StringLogger)

Aggregations

StringLogger (org.neo4j.kernel.impl.util.StringLogger)9 IOException (java.io.IOException)6 ByteBuffer (java.nio.ByteBuffer)6 File (java.io.File)4 FileChannel (java.nio.channels.FileChannel)4 LinkedList (java.util.LinkedList)4 ReadOnlyDbException (org.neo4j.kernel.impl.core.ReadOnlyDbException)2 NioNeoDbPersistenceSource (org.neo4j.kernel.impl.nioneo.xa.NioNeoDbPersistenceSource)2 ActivityManager (android.app.ActivityManager)1 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)1 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)1 RuntimeMXBean (java.lang.management.RuntimeMXBean)1