Search in sources :

Example 6 with BimserverLockConflictException

use of org.bimserver.database.BimserverLockConflictException in project BIMserver by opensourceBIM.

the class BerkeleyKeyValueStore method getRecordIterator.

@Override
public SearchingRecordIterator getRecordIterator(String tableName, byte[] mustStartWith, byte[] startSearchingAt, DatabaseSession databaseSession, boolean keysOnly) throws BimserverLockConflictException, BimserverDatabaseException {
    Cursor cursor = null;
    try {
        TableWrapper tableWrapper = getTableWrapper(tableName);
        cursor = tableWrapper.getDatabase().openCursor(getTransaction(databaseSession, tableWrapper), getCursorConfig(tableWrapper));
        BerkeleySearchingRecordIterator berkeleySearchingRecordIterator = new BerkeleySearchingRecordIterator(cursor, this, cursorCounter.incrementAndGet(), mustStartWith, startSearchingAt, keysOnly);
        if (MONITOR_CURSOR_STACK_TRACES) {
            openCursors.put(berkeleySearchingRecordIterator.getCursorId(), new Exception().getStackTrace());
        }
        return berkeleySearchingRecordIterator;
    } catch (BimserverLockConflictException e) {
        if (cursor != null) {
            try {
                cursor.close();
                throw e;
            } catch (DatabaseException e1) {
                LOGGER.error("", e1);
            }
        }
    } catch (DatabaseException e1) {
        LOGGER.error("", e1);
    }
    return null;
}
Also used : Cursor(com.sleepycat.je.Cursor) BimserverLockConflictException(org.bimserver.database.BimserverLockConflictException) DatabaseException(com.sleepycat.je.DatabaseException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) DatabaseException(com.sleepycat.je.DatabaseException) IOException(java.io.IOException) BimserverLockConflictException(org.bimserver.database.BimserverLockConflictException) LockConflictException(com.sleepycat.je.LockConflictException) EnvironmentLockedException(com.sleepycat.je.EnvironmentLockedException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException)

Example 7 with BimserverLockConflictException

use of org.bimserver.database.BimserverLockConflictException in project BIMserver by opensourceBIM.

the class BerkeleyKeyValueStore method storeNoOverwrite.

@Override
public void storeNoOverwrite(String tableName, byte[] key, byte[] value, int index, int length, DatabaseSession databaseSession) throws BimserverDatabaseException, BimserverLockConflictException, BimserverConcurrentModificationDatabaseException {
    DatabaseEntry dbKey = new DatabaseEntry(key);
    DatabaseEntry dbValue = new DatabaseEntry(value, index, length);
    try {
        TableWrapper tableWrapper = getTableWrapper(tableName);
        OperationStatus putNoOverwrite = tableWrapper.getDatabase().putNoOverwrite(getTransaction(databaseSession, tableWrapper), dbKey, dbValue);
        if (putNoOverwrite == OperationStatus.KEYEXIST) {
            // TODO temporary test
            tableWrapper.getDatabase().put(getTransaction(databaseSession, tableWrapper), dbKey, dbValue);
            ByteBuffer keyBuffer = ByteBuffer.wrap(key);
            if (key.length == 16) {
                int pid = keyBuffer.getInt();
                long oid = keyBuffer.getLong();
                int rid = -keyBuffer.getInt();
                LOGGER.warn("Key exists: pid: " + pid + ", oid: " + oid + ", rid: " + rid + ", " + databaseSession.getEClassForOid(oid).getName());
                throw new BimserverConcurrentModificationDatabaseException("Key exists: pid: " + pid + ", oid: " + oid + ", rid: " + rid);
            } else {
                LOGGER.warn("Key exists");
            // throw new BimserverConcurrentModificationDatabaseException("Key exists: " );
            }
        }
    } catch (LockConflictException e) {
        throw new BimserverLockConflictException(e);
    } catch (DatabaseException e) {
        throw new BimserverDatabaseException("", e);
    }
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) OperationStatus(com.sleepycat.je.OperationStatus) BimserverLockConflictException(org.bimserver.database.BimserverLockConflictException) LockConflictException(com.sleepycat.je.LockConflictException) DatabaseEntry(com.sleepycat.je.DatabaseEntry) ByteBuffer(java.nio.ByteBuffer) BimserverLockConflictException(org.bimserver.database.BimserverLockConflictException) DatabaseException(com.sleepycat.je.DatabaseException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException)

Example 8 with BimserverLockConflictException

use of org.bimserver.database.BimserverLockConflictException in project BIMserver by opensourceBIM.

the class BerkeleySearchingRecordIterator method next.

@Override
public Record next() throws BimserverLockConflictException {
    if (nextStartSearchingAt != null) {
        return getFirstNext(nextStartSearchingAt);
    }
    DatabaseEntry key = new DatabaseEntry();
    DatabaseEntry value = new DatabaseEntry();
    if (onlyKeys) {
        value.setPartial(0, 0, true);
    }
    try {
        OperationStatus next = cursor.getNext(key, value, LockMode.DEFAULT);
        if (next == OperationStatus.SUCCESS) {
            byte[] firstBytes = new byte[mustStartWith.length];
            System.arraycopy(key.getData(), 0, firstBytes, 0, mustStartWith.length);
            if (Arrays.equals(firstBytes, mustStartWith)) {
                return new BerkeleyRecord(key, value);
            }
        }
    } catch (LockConflictException e) {
        throw new BimserverLockConflictException(e);
    } catch (DatabaseException e) {
        LOGGER.error("", e);
    }
    return null;
}
Also used : OperationStatus(com.sleepycat.je.OperationStatus) BimserverLockConflictException(org.bimserver.database.BimserverLockConflictException) LockConflictException(com.sleepycat.je.LockConflictException) DatabaseEntry(com.sleepycat.je.DatabaseEntry) BimserverLockConflictException(org.bimserver.database.BimserverLockConflictException) DatabaseException(com.sleepycat.je.DatabaseException)

Example 9 with BimserverLockConflictException

use of org.bimserver.database.BimserverLockConflictException in project BIMserver by opensourceBIM.

the class BerkeleyTransaction method commit.

@Override
public void commit() throws BimserverLockConflictException, BimserverDatabaseException {
    try {
        transaction.commit();
        transactionAlive = false;
    } catch (LockConflictException e) {
        throw new BimserverLockConflictException(e);
    } catch (DatabaseException e) {
        throw new BimserverDatabaseException(e);
    }
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) BimserverLockConflictException(org.bimserver.database.BimserverLockConflictException) LockConflictException(com.sleepycat.je.LockConflictException) BimserverLockConflictException(org.bimserver.database.BimserverLockConflictException) DatabaseException(com.sleepycat.je.DatabaseException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException)

Example 10 with BimserverLockConflictException

use of org.bimserver.database.BimserverLockConflictException in project BIMserver by opensourceBIM.

the class NewAttributeChange method change.

@Override
public void change(Database database, DatabaseSession databaseSession) throws NotImplementedException, BimserverDatabaseException {
    EClass eClass = eAttribute.getEContainingClass();
    KeyValueStore keyValueStore = database.getKeyValueStore();
    for (EClass subClass : schema.getSubClasses(eClass)) {
        try {
            // No data migration if the database is new
            if (subClass.getEAnnotation("nodatabase") == null) {
                RecordIterator recordIterator = keyValueStore.getRecordIterator(subClass.getEPackage().getName() + "_" + subClass.getName(), databaseSession);
                try {
                    Record record = recordIterator.next();
                    while (record != null) {
                        ByteBuffer buffer = ByteBuffer.wrap(record.getValue());
                        PackageMetaData packageMetaData = database.getMetaDataManager().getPackageMetaData(subClass.getEPackage().getName());
                        int newUnsettedLength = packageMetaData.getUnsettedLength(subClass);
                        int previousUnsettedLength = packageMetaData.getUnsettedLength(subClass, eAttribute);
                        byte[] unsetted = new byte[newUnsettedLength];
                        buffer.get(unsetted, 0, previousUnsettedLength);
                        int fieldCounter = 0;
                        for (EStructuralFeature feature : subClass.getEAllStructuralFeatures()) {
                            if (packageMetaData.useForDatabaseStorage(subClass, feature)) {
                                if (feature == eAttribute) {
                                    unsetted[fieldCounter / 8] |= (1 << (fieldCounter % 8));
                                }
                                fieldCounter++;
                            }
                        }
                        int extra = 0;
                        ByteBuffer newBuffer = ByteBuffer.allocate(record.getValue().length + (newUnsettedLength - previousUnsettedLength) + extra);
                        newBuffer.put(unsetted);
                        buffer.position(previousUnsettedLength);
                        newBuffer.put(buffer);
                        keyValueStore.store(subClass.getEPackage().getName() + "_" + subClass.getName(), record.getKey(), newBuffer.array(), databaseSession);
                        record = recordIterator.next();
                    }
                } catch (BimserverDatabaseException e) {
                    LOGGER.error("", e);
                } finally {
                    recordIterator.close();
                }
            }
        } catch (BimserverLockConflictException e) {
            LOGGER.error("", e);
        }
    }
}
Also used : RecordIterator(org.bimserver.database.RecordIterator) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) EClass(org.eclipse.emf.ecore.EClass) PackageMetaData(org.bimserver.emf.PackageMetaData) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) KeyValueStore(org.bimserver.database.KeyValueStore) Record(org.bimserver.database.Record) ByteBuffer(java.nio.ByteBuffer) BimserverLockConflictException(org.bimserver.database.BimserverLockConflictException)

Aggregations

BimserverLockConflictException (org.bimserver.database.BimserverLockConflictException)23 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)17 DatabaseException (com.sleepycat.je.DatabaseException)8 LockConflictException (com.sleepycat.je.LockConflictException)8 UserException (org.bimserver.shared.exceptions.UserException)7 DatabaseEntry (com.sleepycat.je.DatabaseEntry)6 ByteBuffer (java.nio.ByteBuffer)5 EClass (org.eclipse.emf.ecore.EClass)5 OperationStatus (com.sleepycat.je.OperationStatus)4 IOException (java.io.IOException)4 KeyValueStore (org.bimserver.database.KeyValueStore)4 Record (org.bimserver.database.Record)4 RecordIterator (org.bimserver.database.RecordIterator)4 User (org.bimserver.models.store.User)4 ServerException (org.bimserver.shared.exceptions.ServerException)4 Date (java.util.Date)3 HashMap (java.util.HashMap)3 DatabaseSession (org.bimserver.database.DatabaseSession)3 SPluginInformation (org.bimserver.interfaces.objects.SPluginInformation)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2