Search in sources :

Example 1 with Record

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

the class NewAttributeChange method change.

@Override
public void change(Database database, DatabaseSession databaseSession) throws NotImplementedException, BimserverDatabaseException {
    EClass eClass = eAttribute.getEContainingClass();
    KeyValueStore keyValueStore = database.getKeyValueStore();
    for (EClass subClass : schema.getSubClasses(eClass)) {
        try {
            // No data migration if the database is new
            if (subClass.getEAnnotation("nodatabase") == null) {
                RecordIterator recordIterator = keyValueStore.getRecordIterator(subClass.getEPackage().getName() + "_" + subClass.getName(), databaseSession);
                try {
                    Record record = recordIterator.next();
                    while (record != null) {
                        ByteBuffer buffer = ByteBuffer.wrap(record.getValue());
                        PackageMetaData packageMetaData = database.getMetaDataManager().getPackageMetaData(subClass.getEPackage().getName());
                        int newUnsettedLength = packageMetaData.getUnsettedLength(subClass);
                        int previousUnsettedLength = packageMetaData.getUnsettedLength(subClass, eAttribute);
                        byte[] unsetted = new byte[newUnsettedLength];
                        buffer.get(unsetted, 0, previousUnsettedLength);
                        int fieldCounter = 0;
                        for (EStructuralFeature feature : subClass.getEAllStructuralFeatures()) {
                            if (packageMetaData.useForDatabaseStorage(subClass, feature)) {
                                if (feature == eAttribute) {
                                    unsetted[fieldCounter / 8] |= (1 << (fieldCounter % 8));
                                }
                                fieldCounter++;
                            }
                        }
                        int extra = 0;
                        ByteBuffer newBuffer = ByteBuffer.allocate(record.getValue().length + (newUnsettedLength - previousUnsettedLength) + extra);
                        newBuffer.put(unsetted);
                        buffer.position(previousUnsettedLength);
                        newBuffer.put(buffer);
                        keyValueStore.store(subClass.getEPackage().getName() + "_" + subClass.getName(), record.getKey(), newBuffer.array(), 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) PackageMetaData(org.bimserver.emf.PackageMetaData) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) KeyValueStore(org.bimserver.database.KeyValueStore) Record(org.bimserver.database.Record) ByteBuffer(java.nio.ByteBuffer) BimserverLockConflictException(org.bimserver.database.BimserverLockConflictException)

Example 2 with Record

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

the class NewReferenceChange method change.

@Override
public void change(Database database, DatabaseSession databaseSession) throws NotImplementedException, BimserverDatabaseException {
    EClass eClass = eReference.getEContainingClass();
    KeyValueStore keyValueStore = database.getKeyValueStore();
    for (EClass subClass : schema.getSubClasses(eClass)) {
        try {
            if (subClass.getEAnnotation("nodatabase") == null) {
                RecordIterator recordIterator = keyValueStore.getRecordIterator(subClass.getEPackage().getName() + "_" + subClass.getName(), databaseSession);
                try {
                    Record record = recordIterator.next();
                    while (record != null) {
                        ByteBuffer buffer = ByteBuffer.wrap(record.getValue());
                        int nrStartBytesBefore = (int) Math.ceil(nrFeaturesBefore / 8.0);
                        int nrStartBytesAfter = (int) Math.ceil((nrFeaturesBefore + 1) / 8.0);
                        byte x = buffer.get();
                        if (x != nrStartBytesBefore) {
                            throw new BimserverDatabaseException("Size to not match");
                        }
                        byte[] unsetted = new byte[nrStartBytesAfter];
                        buffer.get(unsetted, 0, x);
                        if (eReference.isUnsettable()) {
                            unsetted[(nrFeaturesBefore + 1) / 8] |= (1 << ((nrFeaturesBefore + 1) % 8));
                        }
                        int extra = 0;
                        if (!eReference.isUnsettable()) {
                            if (eReference.isMany()) {
                                extra = 4;
                            } else {
                                extra = 2;
                            }
                        }
                        ByteBuffer newBuffer = ByteBuffer.allocate(record.getValue().length + (nrStartBytesAfter - nrStartBytesBefore) + extra);
                        newBuffer.put((byte) nrStartBytesAfter);
                        newBuffer.put(unsetted);
                        buffer.position(1 + nrStartBytesBefore);
                        newBuffer.put(buffer);
                        if (!eReference.isUnsettable()) {
                            if (eReference.isMany()) {
                                newBuffer.putInt(0);
                            } else {
                                buffer.order(ByteOrder.LITTLE_ENDIAN);
                                newBuffer.putShort((short) -1);
                                buffer.order(ByteOrder.BIG_ENDIAN);
                            }
                        }
                        keyValueStore.store(subClass.getEPackage().getName() + "_" + subClass.getName(), record.getKey(), newBuffer.array(), 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 3 with Record

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

the class StreamingCheckinDatabaseAction method rollback.

public void rollback() throws BimserverDatabaseException {
    // TODO do we need to remove indices too?
    LOGGER.info("Rolling back");
    int pid = newRevision.getProject().getId();
    int rid = newRevision.getRid();
    Map<EClass, Long> startOids = getDatabaseSession().getStartOids();
    if (startOids == null) {
        throw new BimserverDatabaseException("No objects changed");
    }
    int deleted = 0;
    for (EClass eClass : startOids.keySet()) {
        Long startOid = startOids.get(eClass);
        ByteBuffer mustStartWith = ByteBuffer.wrap(new byte[4]);
        mustStartWith.putInt(pid);
        ByteBuffer startSearchWith = ByteBuffer.wrap(new byte[12]);
        startSearchWith.putInt(pid);
        startSearchWith.putLong(startOid);
        String tableName = eClass.getEPackage().getName() + "_" + eClass.getName();
        try {
            if (!getDatabaseSession().getKeyValueStore().isTransactional(getDatabaseSession(), tableName)) {
                // System.out.println("Checking " + tableName);
                try (RecordIterator recordIterator = getDatabaseSession().getKeyValueStore().getRecordIterator(tableName, mustStartWith.array(), startSearchWith.array(), getDatabaseSession())) {
                    Record record = recordIterator.next();
                    while (record != null) {
                        // System.out.println("Deleting from " + tableName);
                        ByteBuffer keyBuffer = ByteBuffer.wrap(record.getKey());
                        // pid
                        keyBuffer.getInt();
                        // oid
                        keyBuffer.getLong();
                        int keyRid = -keyBuffer.getInt();
                        if (keyRid == rid) {
                            getDatabaseSession().getKeyValueStore().delete(tableName, record.getKey(), getDatabaseSession());
                            deleted++;
                        }
                        record = recordIterator.next();
                    }
                } catch (BimserverLockConflictException e) {
                    e.printStackTrace();
                } catch (BimserverDatabaseException e) {
                    e.printStackTrace();
                }
            }
        } catch (BimserverDatabaseException e1) {
            e1.printStackTrace();
        }
    }
    LOGGER.info("Deleted " + deleted + " objects in rollback");
// getDatabaseSession().getKeyValueStore().sync();
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) RecordIterator(org.bimserver.database.RecordIterator) EClass(org.eclipse.emf.ecore.EClass) AtomicLong(java.util.concurrent.atomic.AtomicLong) Record(org.bimserver.database.Record) ByteBuffer(java.nio.ByteBuffer) BimserverLockConflictException(org.bimserver.database.BimserverLockConflictException)

Example 4 with Record

use of org.bimserver.database.Record 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 5 with Record

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

Aggregations

ByteBuffer (java.nio.ByteBuffer)7 Record (org.bimserver.database.Record)7 EClass (org.eclipse.emf.ecore.EClass)7 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)5 RecordIterator (org.bimserver.database.RecordIterator)5 BimserverLockConflictException (org.bimserver.database.BimserverLockConflictException)4 KeyValueStore (org.bimserver.database.KeyValueStore)4 SearchingRecordIterator (org.bimserver.database.SearchingRecordIterator)2 PackageMetaData (org.bimserver.emf.PackageMetaData)2 List (java.util.List)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 BerkeleyKeyValueStore (org.bimserver.database.berkeley.BerkeleyKeyValueStore)1 HashMapVirtualObject (org.bimserver.shared.HashMapVirtualObject)1 EReference (org.eclipse.emf.ecore.EReference)1 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)1