Search in sources :

Example 1 with SearchingRecordIterator

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

the class DatabaseReadingStackFrame method getByOid.

public HashMapVirtualObject getByOid(long oid, boolean useCache) throws BimserverDatabaseException {
    HashMapVirtualObject byOid = getQueryObjectProvider().getFromCache((long) oid);
    if (byOid != null) {
        return byOid;
    }
    EClass eClass = getQueryObjectProvider().getDatabaseSession().getEClassForOid(oid);
    ByteBuffer mustStartWith = ByteBuffer.wrap(new byte[12]);
    mustStartWith.putInt(reusable.getPid());
    mustStartWith.putLong(oid);
    ByteBuffer startSearchWith = ByteBuffer.wrap(new byte[16]);
    startSearchWith.putInt(reusable.getPid());
    startSearchWith.putLong(oid);
    startSearchWith.putInt(-reusable.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 null;
        }
        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 <= reusable.getRid()) {
            if (valueBuffer.capacity() == 1 && valueBuffer.get(0) == -1) {
                valueBuffer.position(valueBuffer.position() + 1);
                return null;
            // deleted entity
            } else {
                byOid = convertByteArrayToObject(eClass, keyOid, valueBuffer, keyRid);
                if (byOid != null && useCache) {
                    getQueryObjectProvider().cache(byOid);
                }
                return byOid;
            }
        } else {
            return null;
        }
    } finally {
        recordIterator.close();
    }
}
Also used : EClass(org.eclipse.emf.ecore.EClass) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) Record(org.bimserver.database.Record) SearchingRecordIterator(org.bimserver.database.SearchingRecordIterator) ByteBuffer(java.nio.ByteBuffer)

Example 2 with SearchingRecordIterator

use of org.bimserver.database.SearchingRecordIterator 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)

Aggregations

ByteBuffer (java.nio.ByteBuffer)2 Record (org.bimserver.database.Record)2 SearchingRecordIterator (org.bimserver.database.SearchingRecordIterator)2 EClass (org.eclipse.emf.ecore.EClass)2 List (java.util.List)1 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)1 HashMapVirtualObject (org.bimserver.shared.HashMapVirtualObject)1 EReference (org.eclipse.emf.ecore.EReference)1