Search in sources :

Example 1 with RecordIterator

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

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

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

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

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

the class BimServer method checkPidRid.

private int checkPidRid(DatabaseSession session, Project project, int pid, int rid) throws BimserverDatabaseException, BimserverLockConflictException {
    ByteBuffer buffer = ByteBuffer.allocate(4);
    buffer.putInt(pid);
    int removed = 0;
    KeyValueStore keyValueStore = session.getKeyValueStore();
    PackageMetaData packageMetaData = bimDatabase.getMetaDataManager().getPackageMetaData(project.getSchema());
    for (EClass eClass : packageMetaData.getAllClasses()) {
        String tableName = bimDatabase.getTableName(eClass);
        if (!keyValueStore.isTransactional(session, tableName)) {
            try (RecordIterator iterator = keyValueStore.getRecordIterator(tableName, buffer.array(), buffer.array(), session, true)) {
                Record record = iterator.next();
                while (record != null) {
                    ByteBuffer keyBuffer = ByteBuffer.wrap(record.getKey());
                    if (keyBuffer.capacity() != 16) {
                        throw new BimserverDatabaseException("Unexpected key size: " + keyBuffer.capacity());
                    }
                    // pid
                    keyBuffer.getInt();
                    // oid
                    keyBuffer.getLong();
                    // rid
                    int recordRid = -keyBuffer.getInt();
                    if (recordRid > rid) {
                        keyValueStore.delete(tableName, record.getKey(), session);
                        removed++;
                    }
                    record = iterator.next();
                }
            }
        }
    }
    return removed;
}
Also used : RecordIterator(org.bimserver.database.RecordIterator) EClass(org.eclipse.emf.ecore.EClass) PackageMetaData(org.bimserver.emf.PackageMetaData) KeyValueStore(org.bimserver.database.KeyValueStore) BerkeleyKeyValueStore(org.bimserver.database.berkeley.BerkeleyKeyValueStore) Record(org.bimserver.database.Record) ByteBuffer(java.nio.ByteBuffer)

Aggregations

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