Search in sources :

Example 1 with ReadOnlyDbException

use of org.neo4j.kernel.impl.core.ReadOnlyDbException 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)

Example 2 with ReadOnlyDbException

use of org.neo4j.kernel.impl.core.ReadOnlyDbException in project neo4j-mobile-android by neo4j-contrib.

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
     */
@Override
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)

Example 3 with ReadOnlyDbException

use of org.neo4j.kernel.impl.core.ReadOnlyDbException in project neo4j-mobile-android by neo4j-contrib.

the class NioNeoDbPersistenceSource method createTransaction.

public NeoStoreTransaction createTransaction(XaConnection connection) {
    if (xaDs.isReadOnly()) {
        throw new ReadOnlyDbException();
    }
    NeoStoreTransaction result = ((NeoStoreXaConnection) connection).getWriteTransaction();
    // This is not a very good solution. The XaConnection is only used when
    // delisting/releasing the nioneo xa resource. Maybe it should be stored
    // outside the ResourceConnection interface?
    result.setXaConnection(connection);
    return result;
}
Also used : ReadOnlyDbException(org.neo4j.kernel.impl.core.ReadOnlyDbException) NeoStoreTransaction(org.neo4j.kernel.impl.persistence.NeoStoreTransaction)

Aggregations

ReadOnlyDbException (org.neo4j.kernel.impl.core.ReadOnlyDbException)3 File (java.io.File)2 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 FileChannel (java.nio.channels.FileChannel)2 LinkedList (java.util.LinkedList)2 StringLogger (org.neo4j.kernel.impl.util.StringLogger)2 NeoStoreTransaction (org.neo4j.kernel.impl.persistence.NeoStoreTransaction)1