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) {
}
}
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);
}
}
}
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);
}
}
}
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;
}
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);
}
}
}
Aggregations