Search in sources :

Example 21 with HashMapVirtualObject

use of org.bimserver.shared.HashMapVirtualObject in project BIMserver by opensourceBIM.

the class QueryObjectProvider method next.

@Override
public HashMapVirtualObject next() throws BimserverDatabaseException {
    if (start == -1) {
        start = System.nanoTime();
    }
    try {
        while (!stack.isEmpty()) {
            if (stack.size() > MAX_STACK_SIZE) {
                dumpEndQuery();
                throw new BimserverDatabaseException("Query stack size > " + MAX_STACK_SIZE + ", probably a bug, please report");
            }
            stackFrame = stack.peek();
            if (stackFrame.isDone()) {
                stack.pop();
                continue;
            }
            stackFramesProcessed++;
            if (stackFramesProcessed > MAX_STACK_FRAMES_PROCESSED) {
                dumpEndQuery();
                throw new BimserverDatabaseException("Too many stack frames processed ( > " + MAX_STACK_FRAMES_PROCESSED + "), probably a bug, or possibly a very large model, please report");
            }
            boolean done = stackFrame.process();
            stackFrame.setDone(done);
            if (stackFrame instanceof ObjectProvidingStackFrame) {
                HashMapVirtualObject currentObject = ((ObjectProvidingStackFrame) stackFrame).getCurrentObject();
                if (currentObject != null) {
                    if (!oidsRead.contains(currentObject.getOid())) {
                        oidsRead.add(currentObject.getOid());
                        return currentObject;
                    }
                }
            }
        }
    } catch (Exception e) {
        if (e instanceof BimserverDatabaseException) {
            throw (BimserverDatabaseException) e;
        }
        throw new BimserverDatabaseException(e);
    }
    return null;
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) QueryException(org.bimserver.database.queries.om.QueryException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException)

Example 22 with HashMapVirtualObject

use of org.bimserver.shared.HashMapVirtualObject in project BIMserver by opensourceBIM.

the class DatabaseReadingStackFrame method processPossibleInclude.

protected void processPossibleInclude(HashMapVirtualObject object, CanInclude previousInclude, Include include) throws QueryException, BimserverDatabaseException {
    if (include.hasTypes()) {
        for (TypeDef filterClass : include.getTypes()) {
            // TODO also exclude the possibly excluded types here
            if (!filterClass.geteClass().isSuperTypeOf(object.eClass())) {
                // TODO too many times queries are spending time here
                return;
            }
        }
    }
    if (include.hasDirectFields()) {
        for (EReference eReference : include.getFieldsDirect()) {
            Object ref = object.get(eReference.getName());
            if (ref != null) {
                if (ref instanceof List) {
                    for (Long r : (List<Long>) ref) {
                        HashMapVirtualObject byOid = getByOid(r, true);
                        int index = object.addDirectListReference(eReference, byOid);
                        object.addUseForSerialization(eReference, index);
                        processPossibleIncludes(byOid, byOid.eClass(), include);
                    }
                } else {
                    if (ref instanceof Long) {
                        HashMapVirtualObject byOid = getByOid((Long) ref, true);
                        if (byOid == null) {
                            throw new BimserverDatabaseException("Object with oid " + ref + " not found (" + queryObjectProvider.getDatabaseSession().getEClassForOid((Long) ref).getName() + ")");
                        }
                        object.setDirectReference(eReference, byOid);
                        object.addUseForSerialization(eReference);
                        processPossibleIncludes(byOid, byOid.eClass(), include);
                    } else {
                        object.setDirectReference(eReference, (HashMapWrappedVirtualObject) ref);
                    }
                }
            }
        }
    }
    if (include.hasFields() && !include.isExclude()) {
        for (EStructuralFeature eStructuralFeature : include.getFields()) {
            object.addUseForSerialization(eStructuralFeature);
        }
    }
    getQueryObjectProvider().push(new QueryIncludeStackFrame(getQueryObjectProvider(), getReusable(), previousInclude, include, object, queryPart));
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) TypeDef(org.bimserver.database.queries.om.Include.TypeDef) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) HashMapWrappedVirtualObject(org.bimserver.shared.HashMapWrappedVirtualObject) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) WrappedVirtualObject(org.bimserver.shared.WrappedVirtualObject) List(java.util.List) EReference(org.eclipse.emf.ecore.EReference)

Example 23 with HashMapVirtualObject

use of org.bimserver.shared.HashMapVirtualObject in project BIMserver by opensourceBIM.

the class DatabaseReadingStackFrame method readList.

private Object readList(HashMapVirtualObject idEObject, ByteBuffer buffer, EStructuralFeature feature) throws BimserverDatabaseException {
    if (feature.getEType() instanceof EEnum) {
    } else if (feature.getEType() instanceof EClass) {
        if (buffer.capacity() == 1 && buffer.get(0) == -1) {
            buffer.position(buffer.position() + 1);
        } else {
            int listSize = buffer.getInt();
            for (int i = 0; i < listSize; i++) {
                if (feature.getEAnnotation("twodimensionalarray") != null) {
                    HashMapVirtualObject newObject = new HashMapVirtualObject(reusable, (EClass) feature.getEType());
                    EClass eClass = (EClass) feature.getEType();
                    EStructuralFeature eStructuralFeature = eClass.getEStructuralFeature("List");
                    buffer.order(ByteOrder.LITTLE_ENDIAN);
                    short cid = buffer.getShort();
                    buffer.order(ByteOrder.BIG_ENDIAN);
                    EClass referenceClass = queryObjectProvider.getDatabaseSession().getEClass((short) (-cid));
                    if (referenceClass == null) {
                        throw new BimserverDatabaseException("No class found for cid " + (-cid));
                    }
                    Object result = readList(newObject, buffer, eStructuralFeature);
                    if (eStructuralFeature.getEType() == EcorePackage.eINSTANCE.getEDouble() || eStructuralFeature.getEType() == EcorePackage.eINSTANCE.getEDoubleObject()) {
                        result = readList(newObject, buffer, eClass.getEStructuralFeature("ListAsString"));
                    }
                    idEObject.setListItem(feature, i, newObject);
                } else {
                    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 (referenceClass == null) {
                            throw new BimserverDatabaseException("No class found for cid " + (-cid));
                        }
                        EStructuralFeature wv = referenceClass.getEStructuralFeature("wrappedValue");
                        if (wv != null && wv.isMany()) {
                            HashMapVirtualObject eObject = new HashMapVirtualObject(reusable, referenceClass);
                            readList(eObject, buffer, wv);
                            idEObject.setListItem(feature, i, eObject);
                        } else {
                            idEObject.setListItem(feature, i, readWrappedValue(feature, buffer, referenceClass));
                        }
                        idEObject.addUseForSerialization(feature, i);
                    } else if (cid > 0) {
                        // positive cid means value is a
                        // reference
                        // to another record
                        EClass referenceClass = queryObjectProvider.getDatabaseSession().getEClass(cid);
                        if (referenceClass == null) {
                            throw new BimserverDatabaseException("Cannot find class with cid " + cid);
                        }
                        buffer.position(buffer.position() - 2);
                        long rf = readReference(buffer, feature, referenceClass);
                        idEObject.setListItemReference(feature, i, referenceClass, rf, -1);
                        if (rf != -1) {
                            if (queryObjectProvider.hasReadOrIsGoingToRead((rf)) || queryObjectProvider.hasReadOrIsGoingToRead(referenceClass)) {
                                idEObject.addUseForSerialization(feature, i);
                            }
                        }
                    // } else if (cid == 0) {
                    // throw new BimserverDatabaseException("Cid cannot be 0");
                    }
                }
            }
        }
    } else if (feature.getEType() instanceof EDataType) {
        int listSize = buffer.getInt();
        if (listSize < 0) {
            throw new BimserverDatabaseException("Negative array size for " + feature.getEContainingClass().getName() + "." + feature.getName());
        }
        for (int i = 0; i < listSize; i++) {
            Object reference = readPrimitiveValue(feature.getEType(), buffer);
            if (reference != null) {
                idEObject.setListItem(feature, i, reference);
            }
        }
    }
    return null;
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) EClass(org.eclipse.emf.ecore.EClass) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) EDataType(org.eclipse.emf.ecore.EDataType) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) HashMapWrappedVirtualObject(org.bimserver.shared.HashMapWrappedVirtualObject) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) WrappedVirtualObject(org.bimserver.shared.WrappedVirtualObject) EEnum(org.eclipse.emf.ecore.EEnum)

Example 24 with HashMapVirtualObject

use of org.bimserver.shared.HashMapVirtualObject in project BIMserver by opensourceBIM.

the class StreamingGeometryGenerator method placementToMatrix.

private double[] placementToMatrix(HashMapVirtualObject placement) {
    HashMapVirtualObject placementRelTo = placement.getDirectFeature(packageMetaData.getEReference("IfcLocalPlacement", "PlacementRelTo"));
    double[] matrix = Matrix.identity();
    if (placement.eClass().getName().equals("IfcLocalPlacement")) {
        HashMapVirtualObject relativePlacement = placement.getDirectFeature(packageMetaData.getEReference("IfcLocalPlacement", "RelativePlacement"));
        if (relativePlacement != null && relativePlacement.eClass().getName().equals("IfcAxis2Placement3D")) {
            matrix = placement3DToMatrix(relativePlacement);
        }
    }
    if (placementRelTo != null) {
        double[] baseMatrix = placementToMatrix(placementRelTo);
        double[] rhs = matrix;
        matrix = Matrix.identity();
        Matrix.multiplyMM(matrix, 0, baseMatrix, 0, rhs, 0);
    }
    return matrix;
}
Also used : HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject)

Example 25 with HashMapVirtualObject

use of org.bimserver.shared.HashMapVirtualObject in project BIMserver by opensourceBIM.

the class DatabaseReadingStackFrame method convertByteArrayToObject.

protected HashMapVirtualObject convertByteArrayToObject(EClass eClass, long oid, ByteBuffer buffer, int rid) throws BimserverDatabaseException {
    try {
        int unsettedLength = getPackageMetaData().getUnsettedLength(eClass);
        byte[] unsetted = new byte[unsettedLength];
        buffer.get(unsetted);
        int fieldCounter = 0;
        HashMapVirtualObject idEObject = new HashMapVirtualObject(reusable, eClass, oid, new UUID(buffer.getLong(), buffer.getLong()));
        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((EAttribute) 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) {
                                if (feature instanceof EAttribute) {
                                    idEObject.setAttribute((EAttribute) feature, newValue);
                                } else {
                                    if (newValue instanceof Long) {
                                        idEObject.setReference((EReference) feature, (Long) newValue);
                                    } else {
                                        idEObject.setReference((EReference) feature, (WrappedVirtualObject) 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) EAttribute(org.eclipse.emf.ecore.EAttribute) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) HashMapWrappedVirtualObject(org.bimserver.shared.HashMapWrappedVirtualObject) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) WrappedVirtualObject(org.bimserver.shared.WrappedVirtualObject) EEnumLiteral(org.eclipse.emf.ecore.EEnumLiteral) BufferOverflowException(java.nio.BufferOverflowException) UUID(java.util.UUID) EEnumImpl(org.eclipse.emf.ecore.impl.EEnumImpl) BufferUnderflowException(java.nio.BufferUnderflowException)

Aggregations

HashMapVirtualObject (org.bimserver.shared.HashMapVirtualObject)44 QueryObjectProvider (org.bimserver.database.queries.QueryObjectProvider)23 Query (org.bimserver.database.queries.om.Query)23 QueryPart (org.bimserver.database.queries.om.QueryPart)22 List (java.util.List)20 EClass (org.eclipse.emf.ecore.EClass)20 PackageMetaData (org.bimserver.emf.PackageMetaData)18 UserException (org.bimserver.shared.exceptions.UserException)17 EReference (org.eclipse.emf.ecore.EReference)15 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)13 HashMapWrappedVirtualObject (org.bimserver.shared.HashMapWrappedVirtualObject)12 OldQuery (org.bimserver.database.OldQuery)11 IOException (java.io.IOException)9 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 Include (org.bimserver.database.queries.om.Include)8 QueryException (org.bimserver.database.queries.om.QueryException)7 ByteBuffer (java.nio.ByteBuffer)6 Revision (org.bimserver.models.store.Revision)6 EAttribute (org.eclipse.emf.ecore.EAttribute)6