Search in sources :

Example 71 with BimserverDatabaseException

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

the class DatabaseSession method query.

@SuppressWarnings("unchecked")
public <T extends IdEObject> List<T> query(EAttribute attribute, Object value) throws BimserverLockConflictException, BimserverDatabaseException {
    List<T> result = new ArrayList<>();
    if (attribute.getEAnnotation("singleindex") != null) {
        String indexTableName = attribute.getEContainingClass().getEPackage().getName() + "_" + attribute.getEContainingClass().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);
        }
        List<byte[]> duplicates = database.getKeyValueStore().getDuplicates(indexTableName, queryBytes, this);
        for (byte[] indexValue : duplicates) {
            ByteBuffer buffer = ByteBuffer.wrap(indexValue);
            // pid
            buffer.getInt();
            long oid = buffer.getLong();
            result.add((T) get(oid, OldQuery.getDefault()));
        }
    }
    return result;
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer)

Example 72 with BimserverDatabaseException

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

the class DatabaseSession method getAvailableClassesInRevision.

public Set<String> getAvailableClassesInRevision(IfcModelInterface ifcModel, QueryInterface query) throws BimserverDatabaseException {
    checkOpen();
    try {
        getMap(ifcModel, query);
        Set<String> classes = new HashSet<String>();
        for (IdEObject idEObject : ifcModel.getValues()) {
            classes.add(idEObject.eClass().getName());
        }
        return classes;
    } catch (BimserverDatabaseException e) {
        LOGGER.error("", e);
    }
    return null;
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) IdEObject(org.bimserver.emf.IdEObject) HashSet(java.util.HashSet)

Example 73 with BimserverDatabaseException

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

the class DatabaseSession method convertObjectToByteArray.

private ByteBuffer convertObjectToByteArray(IdEObject object, ByteBuffer buffer, PackageMetaData packageMetaData) throws BimserverDatabaseException {
    int bufferSize = getExactSize(object, packageMetaData, true);
    if (bufferSize > buffer.capacity()) {
        LOGGER.debug("Buffer too small (" + bufferSize + ")");
        buffer = ByteBuffer.allocate(bufferSize);
    }
    int unsettedLength = packageMetaData.getUnsettedLength(object.eClass());
    byte[] unsetted = new byte[unsettedLength];
    int fieldCounter = 0;
    for (EStructuralFeature feature : object.eClass().getEAllStructuralFeatures()) {
        if (packageMetaData.useForDatabaseStorage(object.eClass(), feature)) {
            if (useUnsetBit(feature, object)) {
                unsetted[fieldCounter / 8] |= (1 << (fieldCounter % 8));
            }
            fieldCounter++;
        }
    }
    buffer.put(unsetted);
    EClass eClass = getEClassForOid(object.getOid());
    if (!eClass.isSuperTypeOf(object.eClass())) {
        throw new BimserverDatabaseException("Object with oid " + object.getOid() + " is a " + object.eClass().getName() + " but it's cid-part says it's a " + eClass.getName());
    }
    for (EStructuralFeature feature : object.eClass().getEAllStructuralFeatures()) {
        if (packageMetaData.useForDatabaseStorage(object.eClass(), feature)) {
            if (!useUnsetBit(feature, object)) {
                if (feature.isMany()) {
                    writeList(object, buffer, packageMetaData, feature);
                } else {
                    Object value = object.eGet(feature);
                    if (feature.getEType() instanceof EEnum) {
                        if (value == null) {
                            buffer.putInt(-1);
                        } else {
                            EEnum eEnum = (EEnum) feature.getEType();
                            EEnumLiteral eEnumLiteral = eEnum.getEEnumLiteralByLiteral(((Enum<?>) value).toString());
                            if (eEnumLiteral != null) {
                                buffer.putInt(eEnumLiteral.getValue());
                            } else {
                                LOGGER.error(((Enum<?>) value).toString() + " not found");
                                buffer.putInt(-1);
                            }
                        }
                    } else if (feature.getEType() instanceof EClass) {
                        if (value == null) {
                            buffer.order(ByteOrder.LITTLE_ENDIAN);
                            buffer.putShort((short) -1);
                            buffer.order(ByteOrder.BIG_ENDIAN);
                        } else {
                            IdEObject referencedObject = (IdEObject) value;
                            EClass referencedClass = referencedObject.eClass();
                            if (feature.getEAnnotation("dbembed") != null) {
                                writeEmbeddedValue(object.getPid(), object.getRid(), value, buffer, packageMetaData);
                            } else if (referencedClass.getEAnnotation("wrapped") != null) {
                                writeWrappedValue(object.getPid(), object.getRid(), value, buffer, packageMetaData);
                            } else {
                                writeReference(object, value, buffer, feature);
                            }
                        }
                    } else if (feature.getEType() instanceof EDataType) {
                        writePrimitiveValue(feature, value, buffer);
                    }
                }
            }
        }
    }
    if (buffer.position() != bufferSize) {
        throw new BimserverDatabaseException("Value buffer sizes do not match for " + object.eClass().getName() + " " + buffer.position() + "/" + bufferSize);
    }
    return buffer;
}
Also used : EEnum(org.eclipse.emf.ecore.EEnum) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) EClass(org.eclipse.emf.ecore.EClass) IdEObject(org.bimserver.emf.IdEObject) EDataType(org.eclipse.emf.ecore.EDataType) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) 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) EEnum(org.eclipse.emf.ecore.EEnum)

Example 74 with BimserverDatabaseException

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

the class DatabaseSession method readReference.

private IdEObject readReference(EClass originalQueryClass, ByteBuffer buffer, IfcModelInterface model, IdEObject object, EStructuralFeature feature, EClass eClass, QueryInterface query, TodoList todoList) throws BimserverDatabaseException {
    // TODO next bit seems to make no sense, why detect a deleted record when reading a reference??
    if (buffer.capacity() == 1 && buffer.get(0) == -1) {
        buffer.position(buffer.position() + 1);
        return null;
    }
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    long oid = buffer.getLong();
    buffer.order(ByteOrder.BIG_ENDIAN);
    IdEObject foundInCache = objectCache.get(oid);
    if (foundInCache != null) {
        return foundInCache;
    }
    if (model.contains(oid)) {
        return model.get(oid);
    }
    IdEObjectImpl newObject = createInternal(eClass, query);
    newObject.setOid(oid);
    if (perRecordVersioning(newObject)) {
        newObject.setPid(Database.STORE_PROJECT_ID);
    } else {
        newObject.setPid(query.getPid());
    }
    newObject.setRid(query.getRid());
    try {
        newObject.setModel(model);
    } catch (IfcModelInterfaceException e) {
        LOGGER.error("", e);
    }
    objectCache.put(oid, newObject);
    if (query.isDeep() && object.eClass().getEAnnotation("wrapped") == null) {
        if (feature.getEAnnotation("nolazyload") == null) {
            todoList.add(newObject);
        }
    } else {
        if (object.eClass().getEAnnotation("wrapped") == null) {
            try {
                model.addAllowMultiModel(oid, newObject);
            } catch (IfcModelInterfaceException e) {
                throw new BimserverDatabaseException(e);
            }
        }
    }
    return newObject;
}
Also used : IdEObjectImpl(org.bimserver.emf.IdEObjectImpl) IfcModelInterfaceException(org.bimserver.emf.IfcModelInterfaceException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) IdEObject(org.bimserver.emf.IdEObject)

Example 75 with BimserverDatabaseException

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

the class DatabaseSession method writeReference.

private void writeReference(IdEObject object, Object value, ByteBuffer buffer, EStructuralFeature feature) throws BimserverDatabaseException {
    IdEObject idEObject = (IdEObject) value;
    if (idEObject.getOid() < 0) {
        throw new BimserverDatabaseException("Writing a reference with oid " + idEObject.getOid() + ", this is not supposed to happen, referenced: " + idEObject.getOid() + " " + value + " from " + object.getOid() + " " + object);
    }
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.putLong(idEObject.getOid());
    buffer.order(ByteOrder.BIG_ENDIAN);
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) IdEObject(org.bimserver.emf.IdEObject)

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