Search in sources :

Example 51 with BimserverDatabaseException

use of org.bimserver.BimserverDatabaseException 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 52 with BimserverDatabaseException

use of org.bimserver.BimserverDatabaseException 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());
                    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));
                    }
                    EStructuralFeature eStructuralFeature = ((EClass) feature.getEType()).getEStructuralFeature("List");
                    Object result = readList(newObject, buffer, eStructuralFeature);
                    if (result != null) {
                        newObject.setAttribute(newObject.eClass().getEStructuralFeature("List"), result);
                    }
                    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));
                        }
                        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 (feature.getEType() instanceof EDataType) {
        int listSize = buffer.getInt();
        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) EEnum(org.eclipse.emf.ecore.EEnum)

Example 53 with BimserverDatabaseException

use of org.bimserver.BimserverDatabaseException in project BIMserver by opensourceBIM.

the class WarServerInitializer method contextInitialized.

public void contextInitialized(ServletContextEvent servletContextEvent) {
    ServletContext servletContext = servletContextEvent.getServletContext();
    Path homeDir = null;
    if (servletContext.getAttribute("homedir") != null) {
        homeDir = Paths.get((String) servletContext.getAttribute("homedir"));
    }
    if (homeDir == null && servletContext.getInitParameter("homedir") != null) {
        homeDir = Paths.get(servletContext.getInitParameter("homedir"));
    }
    boolean autoMigrate = false;
    if (servletContext.getAttribute("autoMigrate") != null) {
        autoMigrate = (Boolean) servletContext.getAttribute("autoMigrate");
    }
    if (autoMigrate == false && servletContext.getInitParameter("autoMigrate") != null) {
        autoMigrate = Boolean.valueOf(servletContext.getInitParameter("autoMigrate"));
    }
    String realPath = servletContext.getRealPath("/");
    if (!realPath.endsWith("/")) {
        realPath = realPath + "/";
    }
    Path baseDir = Paths.get(realPath + "WEB-INF");
    if (homeDir == null) {
        homeDir = baseDir;
    }
    ResourceFetcher resourceFetcher = new WarResourceFetcher(servletContext, homeDir);
    BimServerConfig config = new BimServerConfig();
    config.setAutoMigrate(autoMigrate);
    config.setEnvironment(Environment.WAR);
    config.setHomeDir(homeDir);
    config.setResourceFetcher(resourceFetcher);
    if (homeDir != null) {
        // Basically doing this twice (also in BimServer.init), but this makes sure the logback.xml file is copied to the homedir
        try {
            BimServer.initHomeDir(config);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    setupLogging(homeDir);
    try {
        fixLogging(config);
        config.setClassPath(makeClassPath(resourceFetcher.getFile("lib")));
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    config.setStartEmbeddedWebServer(false);
    bimServer = new BimServer(config);
    Jsr356Impl.setDefaultServletContext(servletContextEvent.getServletContext());
    Logger LOGGER = LoggerFactory.getLogger(WarServerInitializer.class);
    LOGGER.info("Servlet Context Name: " + servletContext.getServletContextName());
    try {
        bimServer.start();
    } catch (ServerException e) {
        LOGGER.error("", e);
    } catch (DatabaseInitException e) {
        LOGGER.error("", e);
    } catch (BimserverDatabaseException e) {
        LOGGER.error("", e);
    } catch (PluginException e) {
        LOGGER.error("", e);
    } catch (DatabaseRestartRequiredException e) {
        LOGGER.error("", e);
    }
    servletContext.setAttribute("bimserver", bimServer);
}
Also used : Path(java.nio.file.Path) ServerException(org.bimserver.shared.exceptions.ServerException) WarResourceFetcher(org.bimserver.resources.WarResourceFetcher) BimServer(org.bimserver.BimServer) PluginException(org.bimserver.shared.exceptions.PluginException) WarResourceFetcher(org.bimserver.resources.WarResourceFetcher) ResourceFetcher(org.bimserver.plugins.ResourceFetcher) IOException(java.io.IOException) BimServerConfig(org.bimserver.BimServerConfig) Logger(org.slf4j.Logger) DatabaseInitException(org.bimserver.database.berkeley.DatabaseInitException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) ServletContext(javax.servlet.ServletContext) DatabaseRestartRequiredException(org.bimserver.database.DatabaseRestartRequiredException)

Example 54 with BimserverDatabaseException

use of org.bimserver.BimserverDatabaseException in project BIMserver by opensourceBIM.

the class DatabaseSession method commit.

public void commit(ProgressHandler progressHandler) throws BimserverDatabaseException, ServiceException {
    checkOpen();
    try {
        if (progressHandler != null) {
            progressHandler.progress(0, objectsToCommit == null ? 0 : objectsToCommit.size());
        }
        int current = 0;
        long writes = 0;
        ByteBuffer keyBuffer = ByteBuffer.wrap(new byte[16]);
        if (objectsToDelete != null) {
            for (RecordIdentifierPlusType recordIdentifier : objectsToDelete) {
                fillKeyBuffer(keyBuffer, recordIdentifier);
                database.getKeyValueStore().storeNoOverwrite(recordIdentifier.getPackageName() + "_" + recordIdentifier.getClassName(), keyBuffer.array(), new byte[] { -1 }, this);
                writes++;
            }
        }
        // This buffer is reused for the values, it's position must be reset at the end of the loop, and the convertObjectToByteArray function is responsible for setting the buffer's position to the end of the (used part of the) buffer
        ByteBuffer reusableBuffer = ByteBuffer.allocate(32768);
        if (objectsToCommit != null) {
            for (IdEObject object : objectsToCommit) {
                if (object.getOid() == -1) {
                    throw new BimserverDatabaseException("Cannot store object with oid -1");
                }
                fillKeyBuffer(keyBuffer, object);
                if (DEVELOPER_DEBUG) {
                    LOGGER.info("Write: " + object.eClass().getName() + " " + "pid=" + object.getPid() + " oid=" + object.getOid() + " rid=" + object.getRid());
                }
                ByteBuffer valueBuffer = convertObjectToByteArray(object, reusableBuffer, getMetaDataManager().getPackageMetaData(object.eClass().getEPackage().getName()));
                int valueBufferPosition = valueBuffer.position();
                processPossibleIndices(keyBuffer, object.getPid(), object.getRid(), object.getOid(), object.eClass(), valueBuffer);
                if (object.eClass().getEAnnotation("nolazyload") == null && !overwriteEnabled) {
                    database.getKeyValueStore().storeNoOverwrite(object.eClass().getEPackage().getName() + "_" + object.eClass().getName(), keyBuffer.array(), valueBuffer.array(), 0, valueBufferPosition, this);
                } else {
                    database.getKeyValueStore().store(object.eClass().getEPackage().getName() + "_" + object.eClass().getName(), keyBuffer.array(), valueBuffer.array(), 0, valueBuffer.position(), this);
                }
                if (progressHandler != null) {
                    progressHandler.progress(++current, objectsToCommit.size());
                }
                writes++;
                // bimServerClient may have increased the size of the buffer by creating a new one, we keep using it for other objects
                reusableBuffer = valueBuffer;
                reusableBuffer.position(0);
            }
        }
        if (bimTransaction != null) {
            bimTransaction.commit();
            database.getKeyValueStore().sync();
        }
        database.incrementCommittedWrites(writes);
        close();
        if (postCommitActions != null) {
            for (PostCommitAction postCommitAction : postCommitActions) {
                postCommitAction.execute();
            }
        }
    } catch (BimserverDatabaseException e) {
        throw e;
    } catch (ServiceException e) {
        throw e;
    }
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) ServiceException(org.bimserver.shared.exceptions.ServiceException) IdEObject(org.bimserver.emf.IdEObject) ByteBuffer(java.nio.ByteBuffer)

Example 55 with BimserverDatabaseException

use of org.bimserver.BimserverDatabaseException in project BIMserver by opensourceBIM.

the class NotificationRegistryServiceImpl method updateProgressTopic.

@Override
public void updateProgressTopic(Long topicId, SLongActionState state) throws UserException, ServerException {
    DatabaseSession session = getBimServer().getDatabase().createSession();
    try {
        ProgressTopic topic = getBimServer().getNotificationsManager().getProgressTopic(topicId);
        ProgressNotification progressNotification = new ProgressNotification(getBimServer(), topic, getBimServer().getSConverter().convertFromSObject(state));
        getBimServer().getNotificationsManager().addToQueue(progressNotification);
    } catch (BimserverDatabaseException e) {
        handleException(e);
    } finally {
        session.close();
    }
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) ProgressTopic(org.bimserver.notifications.ProgressTopic) DatabaseSession(org.bimserver.database.DatabaseSession) ProgressNotification(org.bimserver.notifications.ProgressNotification)

Aggregations

BimserverDatabaseException (org.bimserver.BimserverDatabaseException)123 DatabaseSession (org.bimserver.database.DatabaseSession)56 UserException (org.bimserver.shared.exceptions.UserException)30 EClass (org.eclipse.emf.ecore.EClass)24 User (org.bimserver.models.store.User)20 ByteBuffer (java.nio.ByteBuffer)19 BimserverLockConflictException (org.bimserver.database.BimserverLockConflictException)18 ServerSettings (org.bimserver.models.store.ServerSettings)18 IOException (java.io.IOException)16 ServerSettingsSetter (org.bimserver.database.actions.ServerSettingsSetter)16 SetServerSettingDatabaseAction (org.bimserver.database.actions.SetServerSettingDatabaseAction)16 SServerSettings (org.bimserver.interfaces.objects.SServerSettings)16 ServerException (org.bimserver.shared.exceptions.ServerException)15 UserSettings (org.bimserver.models.store.UserSettings)12 ServiceException (org.bimserver.shared.exceptions.ServiceException)11 HashMapVirtualObject (org.bimserver.shared.HashMapVirtualObject)10 ArrayList (java.util.ArrayList)9 IdEObject (org.bimserver.emf.IdEObject)9 Project (org.bimserver.models.store.Project)9 SerializerPluginConfiguration (org.bimserver.models.store.SerializerPluginConfiguration)9