Search in sources :

Example 81 with BimserverDatabaseException

use of org.bimserver.BimserverDatabaseException in project BIMserver by opensourceBIM.

the class FollowReferenceStackFrame method process.

@Override
public boolean process() throws BimserverDatabaseException, QueryException {
    if (getQueryObjectProvider().hasRead(oid)) {
        processPossibleIncludes(currentObject, null, include);
        return true;
    }
    if (hasRun) {
        return true;
    }
    hasRun = true;
    if (oid == -1) {
        throw new BimserverDatabaseException("Cannot get object for oid " + oid);
    }
    EClass eClass = getQueryObjectProvider().getDatabaseSession().getEClassForOid(oid);
    ByteBuffer mustStartWith = ByteBuffer.wrap(new byte[12]);
    mustStartWith.putInt(getReusable().getPid());
    mustStartWith.putLong(oid);
    ByteBuffer startSearchWith = ByteBuffer.wrap(new byte[16]);
    startSearchWith.putInt(getReusable().getPid());
    startSearchWith.putLong(oid);
    startSearchWith.putInt(-getReusable().getRid());
    SearchingRecordIterator recordIterator = getQueryObjectProvider().getDatabaseSession().getKeyValueStore().getRecordIterator(eClass.getEPackage().getName() + "_" + eClass.getName(), mustStartWith.array(), startSearchWith.array(), getQueryObjectProvider().getDatabaseSession());
    try {
        Record record = recordIterator.next();
        if (record == null) {
            return true;
        }
        getQueryObjectProvider().incReads();
        ByteBuffer keyBuffer = ByteBuffer.wrap(record.getKey());
        ByteBuffer valueBuffer = ByteBuffer.wrap(record.getValue());
        // pid
        keyBuffer.getInt();
        long keyOid = keyBuffer.getLong();
        int keyRid = -keyBuffer.getInt();
        if (keyRid <= getReusable().getRid()) {
            if (valueBuffer.capacity() == 1 && valueBuffer.get(0) == -1) {
                valueBuffer.position(valueBuffer.position() + 1);
                return true;
            // deleted entity
            } else {
                currentObject = convertByteArrayToObject(eClass, keyOid, valueBuffer, keyRid);
                if (currentObject != null) {
                    EReference opposite = getPackageMetaData().getInverseOrOpposite(currentObject.eClass(), fromReference);
                    if (opposite != null) {
                        Object x = currentObject.get(opposite.getName());
                        if (x instanceof List) {
                            List<Long> list = (List<Long>) x;
                            int index = list.indexOf(fromOid);
                            currentObject.addUseForSerialization(opposite, index);
                        } else {
                            currentObject.addUseForSerialization(opposite);
                        }
                    }
                }
                processPossibleIncludes(currentObject, null, include);
            }
        } else {
            return true;
        }
    } finally {
        recordIterator.close();
    }
    return true;
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) EClass(org.eclipse.emf.ecore.EClass) Record(org.bimserver.database.Record) List(java.util.List) SearchingRecordIterator(org.bimserver.database.SearchingRecordIterator) ByteBuffer(java.nio.ByteBuffer) EReference(org.eclipse.emf.ecore.EReference)

Example 82 with BimserverDatabaseException

use of org.bimserver.BimserverDatabaseException in project BIMserver by opensourceBIM.

the class QueryClassificationsAndTypesStackFrame method getOids.

public List<ObjectIdentifier> getOids(EClass eClass, EStructuralFeature eStructuralFeature, Object value, DatabaseInterface databaseInterface, int pid, int rid) throws BimserverDatabaseException {
    if (eStructuralFeature.getEAnnotation("singleindex") != null) {
        List<ObjectIdentifier> result = new ArrayList<>();
        String indexTableName = eStructuralFeature.getEContainingClass().getEPackage().getName() + "_" + eClass.getName() + "_" + eStructuralFeature.getName();
        byte[] queryBytes = null;
        if (value instanceof String) {
            queryBytes = ((String) value).getBytes(Charsets.UTF_8);
        } else if (value instanceof Integer) {
            queryBytes = BinUtils.intToByteArray((Integer) value);
        } else if (value instanceof Long) {
            queryBytes = BinUtils.longToByteArray((Long) value);
        } else {
            throw new BimserverDatabaseException("Unsupported type " + value);
        }
        ByteBuffer valueBuffer = ByteBuffer.allocate(queryBytes.length + 8);
        valueBuffer.putInt(pid);
        valueBuffer.putInt(-rid);
        valueBuffer.put(queryBytes);
        List<byte[]> duplicates = databaseInterface.getDuplicates(indexTableName, valueBuffer.array());
        for (byte[] duplicate : duplicates) {
            ByteBuffer buffer = ByteBuffer.wrap(duplicate);
            // pid
            buffer.getInt();
            long oid = buffer.getLong();
            result.add(new ObjectIdentifier(oid, (short) oid));
        }
        return result;
    } else {
        throw new UnsupportedOperationException();
    }
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) ObjectIdentifier(org.bimserver.database.ObjectIdentifier)

Example 83 with BimserverDatabaseException

use of org.bimserver.BimserverDatabaseException in project BIMserver by opensourceBIM.

the class QueryClassificationsAndTypesStackFrame method getOid.

public ObjectIdentifier getOid(EClass eClass, EAttribute attribute, Object value, DatabaseInterface databaseInterface, int pid, int rid) throws BimserverDatabaseException {
    if (attribute.getEAnnotation("singleindex") != null) {
        String indexTableName = attribute.getEContainingClass().getEPackage().getName() + "_" + eClass.getName() + "_" + attribute.getName();
        byte[] queryBytes = null;
        if (value instanceof String) {
            queryBytes = ((String) value).getBytes(Charsets.UTF_8);
        } else if (value instanceof Integer) {
            queryBytes = BinUtils.intToByteArray((Integer) value);
        } else {
            throw new BimserverDatabaseException("Unsupported type " + value);
        }
        ByteBuffer valueBuffer = ByteBuffer.allocate(queryBytes.length + 8);
        valueBuffer.putInt(pid);
        valueBuffer.putInt(-rid);
        valueBuffer.put(queryBytes);
        byte[] firstDuplicate = databaseInterface.get(indexTableName, valueBuffer.array());
        if (firstDuplicate != null) {
            ByteBuffer buffer = ByteBuffer.wrap(firstDuplicate);
            // pid
            buffer.getInt();
            long oid = buffer.getLong();
            return new ObjectIdentifier(oid, (short) oid);
        }
    } else {
        throw new UnsupportedOperationException(eClass.getName() + "." + attribute.getName() + " does not have a singleindex");
    }
    return null;
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) ByteBuffer(java.nio.ByteBuffer) ObjectIdentifier(org.bimserver.database.ObjectIdentifier)

Example 84 with BimserverDatabaseException

use of org.bimserver.BimserverDatabaseException in project BIMserver by opensourceBIM.

the class BerkeleyKeyValueStore method store.

@Override
public void store(String tableName, byte[] key, byte[] value, int offset, int length, DatabaseSession databaseSession) throws BimserverDatabaseException, BimserverLockConflictException {
    DatabaseEntry dbKey = new DatabaseEntry(key);
    DatabaseEntry dbValue = new DatabaseEntry(value, offset, length);
    try {
        TableWrapper tableWrapper = getTableWrapper(tableName);
        tableWrapper.getDatabase().put(getTransaction(databaseSession, tableWrapper), dbKey, dbValue);
    } 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) DatabaseEntry(com.sleepycat.je.DatabaseEntry) BimserverLockConflictException(org.bimserver.database.BimserverLockConflictException) DatabaseException(com.sleepycat.je.DatabaseException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException)

Example 85 with BimserverDatabaseException

use of org.bimserver.BimserverDatabaseException in project BIMserver by opensourceBIM.

the class BerkeleyKeyValueStore method createIndexTable.

public boolean createIndexTable(String tableName, DatabaseSession databaseSession, boolean transactional) throws BimserverDatabaseException {
    if (tables.containsKey(tableName)) {
        throw new BimserverDatabaseException("Table " + tableName + " already created");
    }
    DatabaseConfig databaseConfig = new DatabaseConfig();
    databaseConfig.setAllowCreate(true);
    boolean finalTransactional = transactional && useTransactions;
    // if (!transactional) {
    // databaseConfig.setCacheMode(CacheMode.EVICT_BIN);
    // }
    databaseConfig.setDeferredWrite(!finalTransactional);
    databaseConfig.setTransactional(finalTransactional);
    databaseConfig.setSortedDuplicates(true);
    Database database = environment.openDatabase(null, tableName, databaseConfig);
    if (database == null) {
        return false;
    }
    tables.put(tableName, new TableWrapper(database, finalTransactional));
    return true;
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) Database(com.sleepycat.je.Database) DatabaseConfig(com.sleepycat.je.DatabaseConfig)

Aggregations

BimserverDatabaseException (org.bimserver.BimserverDatabaseException)123 DatabaseSession (org.bimserver.database.DatabaseSession)56 UserException (org.bimserver.shared.exceptions.UserException)30 EClass (org.eclipse.emf.ecore.EClass)24 User (org.bimserver.models.store.User)20 ByteBuffer (java.nio.ByteBuffer)19 BimserverLockConflictException (org.bimserver.database.BimserverLockConflictException)18 ServerSettings (org.bimserver.models.store.ServerSettings)18 IOException (java.io.IOException)16 ServerSettingsSetter (org.bimserver.database.actions.ServerSettingsSetter)16 SetServerSettingDatabaseAction (org.bimserver.database.actions.SetServerSettingDatabaseAction)16 SServerSettings (org.bimserver.interfaces.objects.SServerSettings)16 ServerException (org.bimserver.shared.exceptions.ServerException)15 UserSettings (org.bimserver.models.store.UserSettings)12 ServiceException (org.bimserver.shared.exceptions.ServiceException)11 HashMapVirtualObject (org.bimserver.shared.HashMapVirtualObject)10 ArrayList (java.util.ArrayList)9 IdEObject (org.bimserver.emf.IdEObject)9 Project (org.bimserver.models.store.Project)9 SerializerPluginConfiguration (org.bimserver.models.store.SerializerPluginConfiguration)9