Search in sources :

Example 1 with KeyValueStore

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

the class CommandLine method run.

@Override
public void run() {
    reader = new BufferedReader(new InputStreamReader(System.in));
    running = true;
    try {
        while (running) {
            try {
                String line = reader.readLine();
                if (line == null) {
                    Thread.sleep(50);
                    continue;
                }
                if (line.equalsIgnoreCase("exit")) {
                    bimServer.stop();
                    return;
                } else if (line.startsWith("dumpmodel")) {
                    try {
                        long roid = Long.parseLong(line.substring(9).trim());
                        DatabaseSession databaseSession = bimServer.getDatabase().createSession(OperationType.READ_ONLY);
                        try {
                            DownloadDatabaseAction downloadDatabaseAction = new DownloadDatabaseAction(bimServer, databaseSession, AccessMethod.INTERNAL, roid, -1, -1, new SystemAuthorization(1, TimeUnit.HOURS));
                            IfcModelInterface model = downloadDatabaseAction.execute();
                            LOGGER.info("Model size: " + model.size());
                            List<IfcWall> walls = model.getAll(IfcWall.class);
                            List<IfcProject> projects = model.getAll(IfcProject.class);
                            List<IfcSlab> slabs = model.getAll(IfcSlab.class);
                            List<IfcWindow> windows = model.getAll(IfcWindow.class);
                            LOGGER.info("Walls: " + walls.size());
                            LOGGER.info("Windows: " + windows.size());
                            LOGGER.info("Projects: " + projects.size());
                            LOGGER.info("Slabs: " + slabs.size());
                        } catch (UserException e) {
                            LOGGER.error("", e);
                        } catch (BimserverLockConflictException e) {
                            LOGGER.error("", e);
                        } catch (BimserverDatabaseException e) {
                            LOGGER.error("", e);
                        } finally {
                            databaseSession.close();
                        }
                    } catch (Exception e) {
                        LOGGER.error("", e);
                    }
                } else if (line.equalsIgnoreCase("dump")) {
                    LOGGER.info("Dumping all thread's track traces...");
                    LOGGER.info("");
                    Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
                    for (Thread t : allStackTraces.keySet()) {
                        LOGGER.info(t.getName());
                        StackTraceElement[] stackTraceElements = allStackTraces.get(t);
                        for (StackTraceElement stackTraceElement : stackTraceElements) {
                            LOGGER.info("\t" + stackTraceElement.getClassName() + ":" + stackTraceElement.getLineNumber() + "." + stackTraceElement.getMethodName());
                        }
                        LOGGER.info("");
                    }
                    LOGGER.info("Done printing stack traces");
                    LOGGER.info("");
                    Thread.sleep(10000);
                } else if (line.equals("migrate")) {
                    LOGGER.info("Starting migration...");
                    try {
                        bimServer.getDatabase().getMigrator().migrate();
                        bimServer.getServerInfoManager().update();
                    } catch (MigrationException e) {
                        LOGGER.error("", e);
                    } catch (InconsistentModelsException e) {
                        LOGGER.error("", e);
                    } catch (Exception e) {
                        LOGGER.error("", e);
                    }
                } else if (line.equals("clearendpoints")) {
                    bimServer.getEndPointManager().clear();
                } else if (line.startsWith("showall")) {
                    KeyValueStore keyValueStore = ((Database) bimServer.getDatabase()).getKeyValueStore();
                    Set<String> allTableNames = keyValueStore.getAllTableNames();
                    long total = 0;
                    for (String tableName : allTableNames) {
                        long size = keyValueStore.count(tableName);
                        total += size;
                        if (size != 0) {
                            LOGGER.info(tableName + " " + size);
                        }
                    }
                    LOGGER.info("total: " + total);
                } else {
                    LOGGER.info("Unknown command");
                }
            } catch (IOException e) {
                LOGGER.error("", e);
            }
        }
    } catch (InterruptedException e) {
    }
}
Also used : InconsistentModelsException(org.bimserver.database.migrations.InconsistentModelsException) Set(java.util.Set) DatabaseSession(org.bimserver.database.DatabaseSession) IfcModelInterface(org.bimserver.emf.IfcModelInterface) KeyValueStore(org.bimserver.database.KeyValueStore) SystemAuthorization(org.bimserver.webservices.authorization.SystemAuthorization) List(java.util.List) UserException(org.bimserver.shared.exceptions.UserException) DownloadDatabaseAction(org.bimserver.database.actions.DownloadDatabaseAction) IfcWall(org.bimserver.models.ifc2x3tc1.IfcWall) IfcProject(org.bimserver.models.ifc2x3tc1.IfcProject) IfcWindow(org.bimserver.models.ifc2x3tc1.IfcWindow) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) InconsistentModelsException(org.bimserver.database.migrations.InconsistentModelsException) IOException(java.io.IOException) BimserverLockConflictException(org.bimserver.database.BimserverLockConflictException) MigrationException(org.bimserver.database.migrations.MigrationException) UserException(org.bimserver.shared.exceptions.UserException) IfcSlab(org.bimserver.models.ifc2x3tc1.IfcSlab) MigrationException(org.bimserver.database.migrations.MigrationException) BufferedReader(java.io.BufferedReader) BimserverLockConflictException(org.bimserver.database.BimserverLockConflictException)

Example 2 with KeyValueStore

use of org.bimserver.database.KeyValueStore 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 3 with KeyValueStore

use of org.bimserver.database.KeyValueStore 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 newIndex = nrFeaturesBefore + 1;
                        int nrStartBytesAfter = (int) Math.ceil(newIndex / 8.0);
                        byte[] unsetted = new byte[nrStartBytesAfter];
                        buffer.get(unsetted, 0, nrStartBytesBefore);
                        if (eReference.isUnsettable()) {
                            unsetted[newIndex / 8] |= (1 << (newIndex % 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(unsetted);
                        buffer.position(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();
                    }
                } finally {
                    recordIterator.close();
                }
            }
        } catch (BimserverLockConflictException e) {
            LOGGER.error("", e);
        }
    }
}
Also used : RecordIterator(org.bimserver.database.RecordIterator) 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 4 with KeyValueStore

use of org.bimserver.database.KeyValueStore 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
                    int foundPid = keyBuffer.getInt();
                    if (foundPid != pid) {
                        throw new RuntimeException("PID does not match, this should never happen, please contact developer");
                    }
                    // 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)

Example 5 with KeyValueStore

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

Aggregations

KeyValueStore (org.bimserver.database.KeyValueStore)5 ByteBuffer (java.nio.ByteBuffer)4 BimserverLockConflictException (org.bimserver.database.BimserverLockConflictException)4 Record (org.bimserver.database.Record)4 RecordIterator (org.bimserver.database.RecordIterator)4 EClass (org.eclipse.emf.ecore.EClass)4 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)2 PackageMetaData (org.bimserver.emf.PackageMetaData)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 List (java.util.List)1 Set (java.util.Set)1 DatabaseSession (org.bimserver.database.DatabaseSession)1 DownloadDatabaseAction (org.bimserver.database.actions.DownloadDatabaseAction)1 BerkeleyKeyValueStore (org.bimserver.database.berkeley.BerkeleyKeyValueStore)1 InconsistentModelsException (org.bimserver.database.migrations.InconsistentModelsException)1 MigrationException (org.bimserver.database.migrations.MigrationException)1 IfcModelInterface (org.bimserver.emf.IfcModelInterface)1 IfcProject (org.bimserver.models.ifc2x3tc1.IfcProject)1