use of org.bimserver.BimserverDatabaseException in project BIMserver by opensourceBIM.
the class DatabaseSession method writePrimitiveValue.
private void writePrimitiveValue(EStructuralFeature feature, Object value, ByteBuffer buffer) throws BimserverDatabaseException {
EClassifier type = feature.getEType();
if (type == EcorePackage.eINSTANCE.getEString()) {
if (value == null) {
buffer.putInt(-1);
} else {
String stringValue = (String) value;
byte[] bytes = stringValue.getBytes(Charsets.UTF_8);
if (bytes.length > Integer.MAX_VALUE) {
throw new BimserverDatabaseException("String value too long (max length is " + Integer.MAX_VALUE + ")");
}
buffer.putInt(bytes.length);
buffer.put(bytes);
}
} else if (type == EcorePackage.eINSTANCE.getEInt() || type == EcorePackage.eINSTANCE.getEIntegerObject()) {
if (value == null) {
buffer.putInt(0);
} else {
buffer.putInt((Integer) value);
}
} else if (type == EcorePackage.eINSTANCE.getEDouble() || type == EcorePackage.eINSTANCE.getEDoubleObject()) {
if (value == null) {
buffer.putDouble(0D);
} else {
buffer.putDouble((Double) value);
}
} else if (type == EcorePackage.eINSTANCE.getEFloat() || type == EcorePackage.eINSTANCE.getEFloatObject()) {
if (value == null) {
buffer.putFloat(0F);
} else {
buffer.putFloat((Float) value);
}
} else if (type == EcorePackage.eINSTANCE.getELong() || type == EcorePackage.eINSTANCE.getELongObject()) {
if (value == null) {
buffer.putLong(0L);
} else {
buffer.putLong((Long) value);
}
} else if (type == EcorePackage.eINSTANCE.getEBoolean() || type == EcorePackage.eINSTANCE.getEBooleanObject()) {
if (value == null) {
buffer.put((byte) 0);
} else {
buffer.put(((Boolean) value) ? (byte) 1 : (byte) 0);
}
} else if (type == EcorePackage.eINSTANCE.getEDate()) {
if (value == null) {
buffer.putLong(-1L);
} else {
buffer.putLong(((Date) value).getTime());
}
} else if (type.getName().equals("Tristate")) {
Enumerator eEnumLiteral = (Enumerator) value;
buffer.putInt(eEnumLiteral.getValue());
} else if (value instanceof Enumerator) {
Enumerator eEnumLiteral = (Enumerator) value;
buffer.putInt(eEnumLiteral.getValue());
} else if (type == EcorePackage.eINSTANCE.getEByteArray()) {
if (value == null) {
buffer.putInt(0);
} else {
byte[] bytes = (byte[]) value;
buffer.putInt(bytes.length);
buffer.put(bytes);
}
} else {
throw new RuntimeException("Unsupported type " + type.getName());
}
}
use of org.bimserver.BimserverDatabaseException in project BIMserver by opensourceBIM.
the class DatabaseSession method get.
@SuppressWarnings("unchecked")
public <T extends IdEObject> T get(IdEObject idEObject, long oid, IfcModelInterface model, QueryInterface query, TodoList todoList) throws BimserverDatabaseException {
checkOpen();
if (oid == -1) {
throw new BimserverDatabaseException("Cannot get object for oid " + oid);
}
if (objectsToCommit != null && objectsToCommit.containsOid(oid)) {
return (T) objectsToCommit.getByOid(oid);
}
EClass eClass = getEClassForOid(oid);
if (idEObject != null) {
if (!eClass.isSuperTypeOf(idEObject.eClass())) {
throw new BimserverDatabaseException("Object with oid " + oid + " is a " + idEObject.eClass().getName() + " but it's cid-part says it's a " + eClass.getName());
}
}
IdEObjectImpl cachedObject = (IdEObjectImpl) objectCache.get(oid);
if (cachedObject != null) {
idEObject = cachedObject;
if (cachedObject.getLoadingState() == State.LOADED && cachedObject.getRid() != Integer.MAX_VALUE) {
cachedObject.load();
return (T) cachedObject;
}
}
ByteBuffer mustStartWith = ByteBuffer.wrap(new byte[12]);
mustStartWith.putInt(query.getPid());
mustStartWith.putLong(oid);
ByteBuffer startSearchWith = ByteBuffer.wrap(new byte[16]);
startSearchWith.putInt(query.getPid());
startSearchWith.putLong(oid);
startSearchWith.putInt(-query.getRid());
SearchingRecordIterator recordIterator = database.getKeyValueStore().getRecordIterator(eClass.getEPackage().getName() + "_" + eClass.getName(), mustStartWith.array(), startSearchWith.array(), this);
try {
Record record = recordIterator.next();
if (record == null) {
return null;
}
reads++;
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 <= query.getRid()) {
if (idEObject != null && idEObject.getRid() == Integer.MAX_VALUE) {
((IdEObjectImpl) idEObject).setRid(keyRid);
}
if (model.contains(keyOid) && ((IdEObjectImpl) model.get(keyOid)).getLoadingState() == State.LOADED) {
return (T) model.get(keyOid);
} else {
if (valueBuffer.capacity() == 1 && valueBuffer.get(0) == -1) {
valueBuffer.position(valueBuffer.position() + 1);
return null;
// deleted entity
} else {
T convertByteArrayToObject = (T) convertByteArrayToObject(idEObject, eClass, eClass, keyOid, valueBuffer, model, keyRid, query, todoList);
if (convertByteArrayToObject.getRid() == Integer.MAX_VALUE) {
((IdEObjectImpl) convertByteArrayToObject).setRid(keyRid);
}
objectCache.put(oid, convertByteArrayToObject);
return convertByteArrayToObject;
}
}
} else {
return null;
}
} finally {
recordIterator.close();
}
}
use of org.bimserver.BimserverDatabaseException in project BIMserver by opensourceBIM.
the class DatabaseSession method processTodoList.
private void processTodoList(IfcModelInterface model, TodoList todoList, QueryInterface query) throws BimserverDatabaseException {
IdEObject idEObject = todoList.poll();
while (idEObject != null) {
IdEObject result = get(idEObject, idEObject.getOid(), model, query, todoList);
if (result == null) {
throw new BimserverDatabaseException("Object not found: " + query.getPid() + " " + query.getRid() + " " + idEObject.getOid() + " " + idEObject.eClass().getName());
}
if (!model.contains(result.getOid())) {
try {
model.addAllowMultiModel(result.getOid(), result);
} catch (IfcModelInterfaceException e) {
throw new BimserverDatabaseException(e);
}
}
idEObject = todoList.poll();
}
}
use of org.bimserver.BimserverDatabaseException in project BIMserver by opensourceBIM.
the class DatabaseSession method readList.
@SuppressWarnings("unchecked")
private Object readList(IdEObject idEObject, EClass originalQueryClass, ByteBuffer buffer, IfcModelInterface model, QueryInterface query, TodoList todoList, 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 {
/*
* TODO There still is a problem with this,
* when readReference (and all calls beyond
* that call) alter (by opposites) this
* list, this list can potentially grow too
* large
*
* Only can happen with non-unique
* references
*/
int listSize = buffer.getInt();
AbstractEList<Object> list = (AbstractEList<Object>) idEObject.eGet(feature);
for (int i = 0; i < listSize; i++) {
if (feature.getEAnnotation("twodimensionalarray") != null) {
IdEObjectImpl newObject = createInternal((EClass) feature.getEType(), query);
Object result = readList(newObject, originalQueryClass, buffer, model, query, todoList, newObject.eClass().getEStructuralFeature("List"));
if (result != null) {
newObject.eSet(newObject.eClass().getEStructuralFeature("List"), result);
}
list.addUnique(newObject);
} else {
IdEObject referencedObject = null;
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 = database.getEClassForCid((short) (-cid));
if (referenceClass == null) {
throw new BimserverDatabaseException("No class found for cid " + (-cid));
}
referencedObject = readWrappedValue(feature, buffer, referenceClass, query);
} else if (cid > 0) {
// positive cid means value is a
// reference
// to another record
EClass referenceClass = database.getEClassForCid(cid);
if (referenceClass == null) {
throw new BimserverDatabaseException("Cannot find class with cid " + cid);
}
buffer.position(buffer.position() - 2);
referencedObject = readReference(originalQueryClass, buffer, model, idEObject, feature, referenceClass, query, todoList);
}
if (referencedObject != null) {
if (!feature.getEType().isInstance(referencedObject)) {
throw new BimserverDatabaseException(referencedObject.getClass().getSimpleName() + " cannot be stored in list of type " + feature.getEType().getName() + " for feature " + feature.getName());
}
if (feature.isUnique()) {
list.add(referencedObject);
} else {
list.addUnique(referencedObject);
}
}
}
}
}
} else if (feature.getEType() instanceof EDataType) {
int listSize = buffer.getInt();
BasicEList<Object> list = new BasicEList<Object>(listSize);
for (int i = 0; i < listSize; i++) {
Object reference = readPrimitiveValue(feature.getEType(), buffer, query);
if (reference != null) {
list.addUnique(reference);
}
}
return list;
}
return null;
}
use of org.bimserver.BimserverDatabaseException in project BIMserver by opensourceBIM.
the class ByteBufferVirtualObject method write.
public ByteBuffer write() throws BimserverDatabaseException {
EClass eClass = getDatabaseInterface().getEClassForOid(getOid());
if (!eClass.isSuperTypeOf(eClass())) {
throw new BimserverDatabaseException("Object with oid " + getOid() + " is a " + eClass().getName() + " but it's cid-part says it's a " + eClass.getName());
}
int nrFeatures = getPackageMetaData().getNrDatabaseFeatures(eClass);
if (featureCounter > nrFeatures) {
throw new BimserverDatabaseException("Too many features seem to have been set on " + this.eClass.getName() + " " + featureCounter + " / " + nrFeatures);
} else if (featureCounter < nrFeatures) {
throw new BimserverDatabaseException("Not all features seem to have been set on " + this.eClass.getName() + " " + featureCounter + " / " + nrFeatures);
}
return buffer;
//
// if (buffer.position() != bufferSize) {
// throw new BimserverDatabaseException("Value buffer sizes do not match for " + eClass().getName() + " " + buffer.position() + "/" + bufferSize);
// }
// return buffer;
}
Aggregations