Search in sources :

Example 1 with OFileClassic

use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.

the class OWOWCache method fileNameById.

public String fileNameById(long fileId) {
    final int intId = extractFileId(fileId);
    fileId = composeFileId(id, intId);
    filesLock.acquireReadLock();
    try {
        final OFileClassic f = files.get(fileId);
        return f != null ? f.getName() : null;
    } finally {
        filesLock.releaseReadLock();
    }
}
Also used : OFileClassic(com.orientechnologies.orient.core.storage.fs.OFileClassic)

Example 2 with OFileClassic

use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.

the class OWOWCache method close.

public long[] close() throws IOException {
    flush();
    if (!commitExecutor.isShutdown()) {
        commitExecutor.shutdown();
        try {
            if (!commitExecutor.awaitTermination(5, TimeUnit.MINUTES))
                throw new OWriteCacheException("Background data flush task cannot be stopped.");
        } catch (InterruptedException e) {
            OLogManager.instance().error(this, "Data flush thread was interrupted");
            Thread.currentThread().interrupt();
            throw OException.wrapException(new OWriteCacheException("Data flush thread was interrupted"), e);
        }
    }
    final List<Long> result = new ArrayList<Long>();
    filesLock.acquireWriteLock();
    try {
        final Collection<Integer> intIds = nameIdMap.values();
        for (Integer intId : intIds) {
            if (intId < 0)
                continue;
            final long fileId = composeFileId(id, intId);
            // we remove files because when we reopen storage we will reload them
            final OFileClassic fileClassic = files.remove(fileId);
            fileClassic.close();
            result.add(fileId);
        }
        if (nameIdMapHolder != null) {
            nameIdMapHolder.setLength(0);
            for (Map.Entry<String, Integer> entry : nameIdMap.entrySet()) {
                writeNameIdEntry(new NameFileIdEntry(entry.getKey(), entry.getValue()), false);
            }
            nameIdMapHolder.getFD().sync();
            nameIdMapHolder.close();
        }
        nameIdMap.clear();
        final long[] ds = new long[result.size()];
        int counter = 0;
        for (long id : result) {
            ds[counter] = id;
            counter++;
        }
        return ds;
    } finally {
        filesLock.releaseWriteLock();
    }
}
Also used : OFileClassic(com.orientechnologies.orient.core.storage.fs.OFileClassic) OWriteCacheException(com.orientechnologies.orient.core.exception.OWriteCacheException) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 3 with OFileClassic

use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.

the class OWOWCache method addFile.

public long addFile(String fileName, long fileId) throws IOException {
    filesLock.acquireWriteLock();
    try {
        OFileClassic fileClassic;
        Integer existingFileId = nameIdMap.get(fileName);
        final int intId = extractFileId(fileId);
        if (existingFileId != null && existingFileId >= 0) {
            if (existingFileId == intId)
                throw new OStorageException("File with name '" + fileName + "'' already exists in storage '" + storageLocal.getName() + "'");
            else
                throw new OStorageException("File with given name already exists but has different id " + existingFileId + " vs. proposed " + fileId);
        }
        fileId = composeFileId(id, intId);
        fileClassic = files.get(fileId);
        if (fileClassic != null) {
            if (!fileClassic.getName().equals(fileName))
                throw new OStorageException("File with given id exists but has different name " + fileClassic.getName() + " vs. proposed " + fileName);
        } else {
            if (fileCounter < intId)
                fileCounter = intId;
            fileClassic = createFileInstance(fileName);
            createFile(fileClassic);
            files.add(fileId, fileClassic);
        }
        nameIdMap.put(fileName, intId);
        writeNameIdEntry(new NameFileIdEntry(fileName, intId), true);
        return fileId;
    } catch (InterruptedException e) {
        throw OException.wrapException(new OStorageException("Thread was interrupted"), e);
    } finally {
        filesLock.releaseWriteLock();
    }
}
Also used : OStorageException(com.orientechnologies.orient.core.exception.OStorageException) OFileClassic(com.orientechnologies.orient.core.storage.fs.OFileClassic)

Example 4 with OFileClassic

use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.

the class OWOWCache method exists.

public boolean exists(long fileId) {
    filesLock.acquireReadLock();
    try {
        final int intId = extractFileId(fileId);
        fileId = composeFileId(id, intId);
        final OFileClassic file = files.get(fileId);
        if (file == null)
            return false;
        return file.exists();
    } finally {
        filesLock.releaseReadLock();
    }
}
Also used : OFileClassic(com.orientechnologies.orient.core.storage.fs.OFileClassic)

Example 5 with OFileClassic

use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.

the class OWOWCache method renameFile.

public void renameFile(long fileId, String oldFileName, String newFileName) throws IOException {
    final int intId = extractFileId(fileId);
    fileId = composeFileId(id, intId);
    filesLock.acquireWriteLock();
    try {
        OClosableEntry<Long, OFileClassic> entry = files.acquire(fileId);
        if (entry == null)
            return;
        try {
            OFileClassic file = entry.get();
            final String osFileName = file.getName();
            if (osFileName.startsWith(oldFileName)) {
                final File newFile = new File(storageLocal.getStoragePath() + File.separator + newFileName + osFileName.substring(osFileName.lastIndexOf(oldFileName) + oldFileName.length()));
                boolean renamed = file.renameTo(newFile);
                while (!renamed) {
                    renamed = file.renameTo(newFile);
                }
            }
        } finally {
            files.release(entry);
        }
        nameIdMap.remove(oldFileName);
        nameIdMap.put(newFileName, intId);
        writeNameIdEntry(new NameFileIdEntry(oldFileName, -1), false);
        writeNameIdEntry(new NameFileIdEntry(newFileName, intId), true);
    } catch (InterruptedException e) {
        throw OException.wrapException(new OStorageException("Thread was interrupted"), e);
    } finally {
        filesLock.releaseWriteLock();
    }
}
Also used : OStorageException(com.orientechnologies.orient.core.exception.OStorageException) AtomicLong(java.util.concurrent.atomic.AtomicLong) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) OFileClassic(com.orientechnologies.orient.core.storage.fs.OFileClassic)

Aggregations

OFileClassic (com.orientechnologies.orient.core.storage.fs.OFileClassic)24 OCachePointer (com.orientechnologies.orient.core.storage.cache.OCachePointer)9 ByteBuffer (java.nio.ByteBuffer)9 OStorageException (com.orientechnologies.orient.core.exception.OStorageException)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)7 OModifiableBoolean (com.orientechnologies.common.types.OModifiableBoolean)6 OLogSequenceNumber (com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber)6 WriteAheadLogTest (com.orientechnologies.orient.core.storage.impl.local.paginated.wal.WriteAheadLogTest)6 Test (org.testng.annotations.Test)6 OSessionStoragePerformanceStatistic (com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic)2 CRC32 (java.util.zip.CRC32)2 Assert (org.testng.Assert)2 OWriteCacheException (com.orientechnologies.orient.core.exception.OWriteCacheException)1 OPageDataVerificationError (com.orientechnologies.orient.core.storage.cache.OPageDataVerificationError)1 File (java.io.File)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 Lock (java.util.concurrent.locks.Lock)1