Search in sources :

Example 76 with BimserverDatabaseException

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

the class DatabaseSession method convertByteArrayToObject.

@SuppressWarnings({ "unused" })
private IdEObject convertByteArrayToObject(IdEObject idEObject, EClass originalQueryClass, EClass eClass, long oid, ByteBuffer buffer, IfcModelInterface model, int rid, QueryInterface query, TodoList todoList) throws BimserverDatabaseException {
    try {
        if (idEObject == null) {
            idEObject = createInternal(eClass, query);
            ((IdEObjectImpl) idEObject).setOid(oid);
            ((IdEObjectImpl) idEObject).setPid(query.getPid());
            if (rid == Integer.MAX_VALUE) {
                throw new BimserverDatabaseException("Database corrupt, rid cannot be " + Integer.MAX_VALUE);
            }
        }
        if (idEObject.eClass().getEAnnotation("wrapped") == null) {
            try {
                model.addAllowMultiModel(oid, idEObject);
            } catch (IfcModelInterfaceException e) {
                throw new BimserverDatabaseException(e);
            }
        }
        ((IdEObjectImpl) idEObject).setRid(rid);
        ((IdEObjectImpl) idEObject).useInverses(false);
        if (DEVELOPER_DEBUG && StorePackage.eINSTANCE == idEObject.eClass().getEPackage()) {
            LOGGER.info("Read: " + idEObject.eClass().getName() + " pid=" + query.getPid() + " oid=" + oid + " rid=" + rid);
        }
        ((IdEObjectImpl) idEObject).setLoadingState(State.LOADING);
        objectCache.put(oid, idEObject);
        int unsettedLength = model.getPackageMetaData().getUnsettedLength(eClass);
        byte[] unsetted = new byte[unsettedLength];
        buffer.get(unsetted);
        int fieldCounter = 0;
        for (EStructuralFeature feature : eClass.getEAllStructuralFeatures()) {
            try {
                if (model.getPackageMetaData().useForDatabaseStorage(eClass, feature)) {
                    boolean isUnsetted = (unsetted[fieldCounter / 8] & (1 << (fieldCounter % 8))) != 0;
                    if (isUnsetted) {
                        if (feature.isUnsettable()) {
                            idEObject.eUnset(feature);
                        } else if (feature.isMany()) {
                        // do nothing
                        } else if (feature.getDefaultValue() != null) {
                            idEObject.eSet(feature, feature.getDefaultValue());
                        }
                    } else {
                        if (!query.shouldFollowReference(originalQueryClass, eClass, feature)) {
                            // we have to do some reading to maintain a correct
                            // index
                            fakeRead(buffer, feature);
                        } else {
                            Object newValue = null;
                            if (feature.isMany()) {
                                newValue = readList(idEObject, originalQueryClass, buffer, model, query, todoList, feature);
                            } else {
                                if (feature.getEType() instanceof EEnum) {
                                    int enumOrdinal = buffer.getInt();
                                    if (enumOrdinal == -1) {
                                        newValue = null;
                                    } else {
                                        EClassifier eType = feature.getEType();
                                        EEnumLiteral enumLiteral = ((EEnumImpl) eType).getEEnumLiteral(enumOrdinal);
                                        if (enumLiteral != null) {
                                            newValue = enumLiteral.getInstance();
                                        }
                                    }
                                } else if (feature.getEType() instanceof EClass) {
                                    // EReference eReference = (EReference) feature;
                                    buffer.order(ByteOrder.LITTLE_ENDIAN);
                                    short cid = buffer.getShort();
                                    buffer.order(ByteOrder.BIG_ENDIAN);
                                    if (cid == -1) {
                                    // null, do nothing
                                    } else if (cid < 0) {
                                        // non minus one and negative cid means value is embedded in record
                                        EClass referenceClass = database.getEClassForCid((short) (-cid));
                                        if (feature.getEAnnotation("dbembed") != null) {
                                            newValue = readEmbeddedValue(feature, buffer, referenceClass, query);
                                        } else {
                                            newValue = readWrappedValue(feature, buffer, referenceClass, query);
                                        }
                                    } else if (cid > 0) {
                                        // positive cid means value is reference to other record
                                        EClass referenceClass = database.getEClassForCid(cid);
                                        if (referenceClass == null) {
                                            throw new BimserverDatabaseException("No eClass found for cid " + cid);
                                        }
                                        // readReference is going to read a long, which includes the 2 bytes for the cid
                                        buffer.position(buffer.position() - 2);
                                        newValue = readReference(originalQueryClass, buffer, model, idEObject, feature, referenceClass, query, todoList);
                                    // if (eReference.getEOpposite() != null &&
                                    // ((IdEObjectImpl)
                                    // newValue).isLoadedOrLoading()) {
                                    // newValue = null;
                                    // }
                                    }
                                } else if (feature.getEType() instanceof EDataType) {
                                    newValue = readPrimitiveValue(feature.getEType(), buffer, query);
                                }
                            }
                            if (newValue != null) {
                                idEObject.eSet(feature, newValue);
                            }
                        }
                    }
                    fieldCounter++;
                }
            } catch (StringIndexOutOfBoundsException e) {
                throw new BimserverDatabaseException("Reading " + eClass.getName() + "(" + oid + ")." + feature.getName(), e);
            } catch (BufferUnderflowException e) {
                throw new BimserverDatabaseException("Reading " + eClass.getName() + "(" + oid + ")." + feature.getName(), e);
            } catch (BufferOverflowException e) {
                throw new BimserverDatabaseException("Reading " + eClass.getName() + "(" + oid + ")." + feature.getName(), e);
            }
        }
        ((IdEObjectImpl) idEObject).setLoaded();
        ((IdEObjectImpl) idEObject).useInverses(true);
        if (DEVELOPER_DEBUG && idEObject.getRid() > 100000 || idEObject.getRid() < -100000) {
            LOGGER.debug("Improbable rid " + idEObject.getRid() + " - " + idEObject);
        }
        return idEObject;
    } catch (BufferUnderflowException e) {
        throw new BimserverDatabaseException("Reading " + eClass.getName(), e);
    } catch (BufferOverflowException e) {
        throw new BimserverDatabaseException("Reading " + eClass.getName(), e);
    }
}
Also used : IdEObjectImpl(org.bimserver.emf.IdEObjectImpl) EDataType(org.eclipse.emf.ecore.EDataType) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) EClassifier(org.eclipse.emf.ecore.EClassifier) EEnum(org.eclipse.emf.ecore.EEnum) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) IfcModelInterfaceException(org.bimserver.emf.IfcModelInterfaceException) EClass(org.eclipse.emf.ecore.EClass) VirtualObject(org.bimserver.shared.VirtualObject) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) EObject(org.eclipse.emf.ecore.EObject) IdEObject(org.bimserver.emf.IdEObject) EEnumLiteral(org.eclipse.emf.ecore.EEnumLiteral) BufferOverflowException(java.nio.BufferOverflowException) EEnumImpl(org.eclipse.emf.ecore.impl.EEnumImpl) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 77 with BimserverDatabaseException

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

the class DatabaseSession method getMap.

public void getMap(IfcModelInterface ifcModel, QueryInterface query) throws BimserverDatabaseException {
    checkOpen();
    TodoList todoList = new TodoList();
    if (query.getOidCounters() != null) {
        for (EClass eClass : query.getOidCounters().keySet()) {
            if (Thread.currentThread().isInterrupted()) {
                throw new BimserverDatabaseException("Thread interrupted");
            }
            if (eClass.getEAnnotation("nolazyload") == null && eClass.getEAnnotation("nodatabase") == null) {
                if (query.shouldIncludeClass(eClass)) {
                    getMap(eClass, ifcModel, query, todoList);
                }
            }
        }
    } else {
        LOGGER.info("Inefficient getMap");
        for (EClass eClass : database.getClasses()) {
            if (Thread.currentThread().isInterrupted()) {
                throw new BimserverDatabaseException("Thread interrupted");
            }
            if (eClass.getEAnnotation("nolazyload") == null && eClass.getEAnnotation("nodatabase") == null) {
                if (query.shouldIncludeClass(eClass)) {
                    getMap(eClass, ifcModel, query, todoList);
                }
            }
        }
    }
    processTodoList(ifcModel, todoList, query);
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) EClass(org.eclipse.emf.ecore.EClass)

Example 78 with BimserverDatabaseException

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

the class Migrator method migrate.

public Schema migrate() throws MigrationException, InconsistentModelsException {
    DatabaseSession session = database.createSession();
    try {
        Schema schema = migrate(session);
        session.commit();
        return schema;
    } catch (BimserverDatabaseException e) {
        throw new MigrationException(e);
    } catch (ServiceException e) {
        throw new MigrationException(e);
    } finally {
        session.close();
    }
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) ServiceException(org.bimserver.shared.exceptions.ServiceException) DatabaseSession(org.bimserver.database.DatabaseSession)

Example 79 with BimserverDatabaseException

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

the class AddIndexChange method change.

@Override
public void change(Database database, DatabaseSession databaseSession) throws NotImplementedException, BimserverDatabaseException {
    EClass eClass = eStructuralFeature.getEContainingClass();
    KeyValueStore keyValueStore = database.getKeyValueStore();
    for (EClass subClass : schema.getSubClasses(eClass)) {
        try {
            if (subClass.getEAnnotation("nodatabase") == null) {
                String indexTableName = subClass.getEPackage().getName() + "_" + subClass.getName() + "_" + eStructuralFeature.getName();
                boolean transactional = !(subClass.getEPackage() == Ifc4Package.eINSTANCE || subClass.getEPackage() == Ifc2x3tc1Package.eINSTANCE);
                keyValueStore.createIndexTable(indexTableName, databaseSession, transactional);
                RecordIterator recordIterator = keyValueStore.getRecordIterator(subClass.getEPackage().getName() + "_" + subClass.getName(), databaseSession);
                try {
                    Record record = recordIterator.next();
                    while (record != null) {
                        ByteBuffer buffer = ByteBuffer.wrap(record.getValue());
                        byte[] featureBytes = databaseSession.extractFeatureBytes(databaseSession, buffer, subClass, eStructuralFeature);
                        if (featureBytes != null) {
                            keyValueStore.store(indexTableName, featureBytes, record.getKey(), 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) KeyValueStore(org.bimserver.database.KeyValueStore) Record(org.bimserver.database.Record) ByteBuffer(java.nio.ByteBuffer) BimserverLockConflictException(org.bimserver.database.BimserverLockConflictException)

Example 80 with BimserverDatabaseException

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

the class DatabaseReadingStackFrame method convertByteArrayToObject.

protected HashMapVirtualObject convertByteArrayToObject(EClass eClass, long oid, ByteBuffer buffer, int rid) throws BimserverDatabaseException {
    try {
        HashMapVirtualObject idEObject = new HashMapVirtualObject(reusable, eClass);
        idEObject.setOid(oid);
        int unsettedLength = getPackageMetaData().getUnsettedLength(eClass);
        byte[] unsetted = new byte[unsettedLength];
        buffer.get(unsetted);
        int fieldCounter = 0;
        for (EStructuralFeature feature : eClass.getEAllStructuralFeatures()) {
            try {
                if (getPackageMetaData().useForDatabaseStorage(eClass, feature)) {
                    boolean isUnsetted = (unsetted[fieldCounter / 8] & (1 << (fieldCounter % 8))) != 0;
                    if (isUnsetted) {
                        if (feature.isUnsettable()) {
                            idEObject.eUnset(feature);
                        } else if (feature.isMany()) {
                        // do nothing
                        } else if (feature.getDefaultValue() != null) {
                            idEObject.setAttribute(feature, feature.getDefaultValue());
                        }
                    } else {
                        Object newValue = null;
                        if (feature.isMany()) {
                            newValue = readList(idEObject, buffer, feature);
                        } else {
                            if (feature.getEType() instanceof EEnum) {
                                int enumOrdinal = buffer.getInt();
                                if (enumOrdinal == -1) {
                                    newValue = null;
                                } else {
                                    EClassifier eType = feature.getEType();
                                    EEnumLiteral enumLiteral = ((EEnumImpl) eType).getEEnumLiteral(enumOrdinal);
                                    if (enumLiteral != null) {
                                        newValue = enumLiteral.getInstance();
                                    }
                                }
                            } else if (feature.getEType() instanceof EClass) {
                                // EReference eReference = (EReference) feature;
                                buffer.order(ByteOrder.LITTLE_ENDIAN);
                                short cid = buffer.getShort();
                                buffer.order(ByteOrder.BIG_ENDIAN);
                                if (cid == -1) {
                                // null, do nothing
                                } else if (cid < 0) {
                                    // negative cid means value is embedded in
                                    // record
                                    EClass referenceClass = queryObjectProvider.getDatabaseSession().getEClass((short) (-cid));
                                    if (feature.getEAnnotation("dbembed") != null) {
                                        newValue = readEmbeddedValue(feature, buffer, referenceClass);
                                    } else {
                                        newValue = readWrappedValue(feature, buffer, referenceClass);
                                    }
                                } else if (cid > 0) {
                                    // positive cid means value is reference to
                                    // other record
                                    EClass referenceClass = queryObjectProvider.getDatabaseSession().getEClass(cid);
                                    if (referenceClass == null) {
                                        throw new BimserverDatabaseException("No eClass found for cid " + cid);
                                    }
                                    buffer.position(buffer.position() - 2);
                                    newValue = readReference(buffer, feature, referenceClass);
                                    if ((Long) newValue != -1) {
                                        if (queryObjectProvider.hasReadOrIsGoingToRead(((Long) newValue)) || queryObjectProvider.hasReadOrIsGoingToRead(referenceClass)) {
                                            idEObject.addUseForSerialization(feature);
                                        }
                                    }
                                // if (eReference.getEOpposite() != null &&
                                // ((IdEObjectImpl)
                                // newValue).isLoadedOrLoading()) {
                                // newValue = null;
                                // }
                                }
                            } else if (feature.getEType() instanceof EDataType) {
                                newValue = readPrimitiveValue(feature.getEType(), buffer);
                            }
                            if (newValue != null) {
                                idEObject.setAttribute(feature, newValue);
                            }
                        }
                    }
                    fieldCounter++;
                }
            } catch (StringIndexOutOfBoundsException e) {
                throw new BimserverDatabaseException("Reading " + eClass.getName() + "." + feature.getName(), e);
            } catch (BufferUnderflowException e) {
                throw new BimserverDatabaseException("Reading " + eClass.getName() + "." + feature.getName() + " " + buffer.capacity(), e);
            } catch (BufferOverflowException e) {
                throw new BimserverDatabaseException("Reading " + eClass.getName() + "." + feature.getName(), e);
            }
        }
        return idEObject;
    } catch (BufferUnderflowException e) {
        throw new BimserverDatabaseException("Reading " + eClass.getName(), e);
    } catch (BufferOverflowException e) {
        throw new BimserverDatabaseException("Reading " + eClass.getName(), e);
    }
}
Also used : EDataType(org.eclipse.emf.ecore.EDataType) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) EClassifier(org.eclipse.emf.ecore.EClassifier) EEnum(org.eclipse.emf.ecore.EEnum) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) EClass(org.eclipse.emf.ecore.EClass) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) HashMapWrappedVirtualObject(org.bimserver.shared.HashMapWrappedVirtualObject) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) EEnumLiteral(org.eclipse.emf.ecore.EEnumLiteral) BufferOverflowException(java.nio.BufferOverflowException) EEnumImpl(org.eclipse.emf.ecore.impl.EEnumImpl) BufferUnderflowException(java.nio.BufferUnderflowException)

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