use of org.bimserver.emf.IdEObjectImpl in project BIMserver by opensourceBIM.
the class DatabaseSession method writeWrappedValue.
private void writeWrappedValue(int pid, int rid, Object value, ByteBuffer buffer, PackageMetaData packageMetaData) throws BimserverDatabaseException {
IdEObject wrappedValue = (IdEObject) value;
EStructuralFeature eStructuralFeature = wrappedValue.eClass().getEStructuralFeature("wrappedValue");
Short cid = database.getCidOfEClass(wrappedValue.eClass());
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putShort((short) -cid);
buffer.order(ByteOrder.BIG_ENDIAN);
writePrimitiveValue(eStructuralFeature, wrappedValue.eGet(eStructuralFeature), buffer);
if (eStructuralFeature.getEType() == EcorePackage.eINSTANCE.getEDouble() || eStructuralFeature.getEType() == EcorePackage.eINSTANCE.getEDoubleObject()) {
EStructuralFeature fe = wrappedValue.eClass().getEStructuralFeature("wrappedValueAsString");
writePrimitiveValue(fe, wrappedValue.eGet(fe), buffer);
}
if (wrappedValue.eClass().getName().equals("IfcGloballyUniqueId")) {
EClass eClass = packageMetaData.getEClass("IfcGloballyUniqueId");
if (wrappedValue.getOid() == -1) {
((IdEObjectImpl) wrappedValue).setOid(newOid(eClass));
}
ByteBuffer valueBuffer = convertObjectToByteArray(wrappedValue, ByteBuffer.allocate(getExactSize(wrappedValue, packageMetaData, true)), packageMetaData);
ByteBuffer keyBuffer = createKeyBuffer(pid, wrappedValue.getOid(), rid);
try {
database.getKeyValueStore().storeNoOverwrite(eClass.getEPackage().getName() + "_" + eClass.getName(), keyBuffer.array(), valueBuffer.array(), this);
database.incrementCommittedWrites(1);
} catch (BimserverLockConflictException e) {
LOGGER.error("", e);
}
}
}
use of org.bimserver.emf.IdEObjectImpl 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.emf.IdEObjectImpl in project BIMserver by opensourceBIM.
the class DatabaseSession method readEmbeddedValue.
private IdEObject readEmbeddedValue(EStructuralFeature feature, ByteBuffer buffer, EClass eClass, QueryInterface query) {
IdEObject eObject = createInternal(eClass, query);
for (EStructuralFeature eStructuralFeature : eClass.getEAllStructuralFeatures()) {
if (eStructuralFeature.isMany()) {
// Not implemented
} else {
Object primitiveValue = readPrimitiveValue(eStructuralFeature.getEType(), buffer, query);
((IdEObjectImpl) eObject).setLoaded();
eObject.eSet(eStructuralFeature, primitiveValue);
}
}
return eObject;
}
use of org.bimserver.emf.IdEObjectImpl 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.emf.IdEObjectImpl in project BIMserver by opensourceBIM.
the class ModelHelper method copy.
@SuppressWarnings({ "unchecked", "rawtypes" })
private IdEObject copy(EClass originalEClass, IdEObject original, boolean setOid, ObjectIDM objectIDM) throws IfcModelInterfaceException {
if (!((IdEObjectImpl) original).isLoadedOrLoading()) {
return null;
}
if (converted.containsKey(original)) {
return converted.get(original);
}
IdEObject newObject = (IdEObject) objectFactory.create(original.eClass());
((IdEObjectImpl) newObject).setPid(original.getPid());
((IdEObjectImpl) newObject).setLoadingState(State.LOADED);
long oid = -1;
if (keepOriginalOids) {
oid = original.getOid();
((IdEObjectImpl) newObject).setOid(oid);
} else {
if (newObject.getOid() == -1) {
if (oidProvider != null) {
oid = oidProvider.newOid(newObject.eClass());
} else {
oid = original.getOid();
}
}
}
if (setOid && newObject.getOid() == -1) {
((IdEObjectImpl) newObject).setOid(oid);
}
converted.put(original, newObject);
if (newObject.eClass().getEAnnotation("wrapped") == null) {
targetModel.add(newObject.getOid(), newObject);
}
if (inverseFixes.containsKey(original.getOid())) {
InverseFix inverseFix = inverseFixes.get(original.getOid());
inverseFix.apply(newObject);
}
for (EStructuralFeature eStructuralFeature : original.eClass().getEAllStructuralFeatures()) {
boolean canFollow = objectIDM == null || objectIDM.shouldFollowReference(originalEClass, original.eClass(), eStructuralFeature);
Object get = original.eGet(eStructuralFeature);
if (eStructuralFeature instanceof EAttribute) {
if (get instanceof List) {
List list = (List) get;
List targetList = (List) newObject.eGet(eStructuralFeature);
for (Object o : list) {
targetList.add(o);
}
} else {
newObject.eSet(eStructuralFeature, get);
}
} else if (eStructuralFeature instanceof EReference) {
if (!canFollow) {
continue;
}
if (get == null) {
} else {
if (eStructuralFeature.isMany()) {
EList<EObject> list = (EList<EObject>) get;
AbstractEList<EObject> toList = (AbstractEList<EObject>) newObject.eGet(eStructuralFeature);
for (Object o : list) {
if (converted.containsKey(o)) {
toList.addUnique(converted.get(o));
} else {
if (canFollow) {
IdEObject result = copy(originalEClass, (IdEObject) o, setOid, objectIDM);
if (result != null) {
toList.addUnique(result);
}
} else {
// Findbugs says canFollow is always true at this point and it's right, TODO
if (eStructuralFeature.getName().equals("RelatedElements")) {
inverseFixes.put(((IdEObject) o).getOid(), new InverseFix(Ifc2x3tc1Package.eINSTANCE.getIfcRelContainedInSpatialStructure_RelatedElements(), newObject));
}
}
}
}
} else {
if (converted.containsKey(get)) {
newObject.eSet(eStructuralFeature, converted.get(get));
} else {
if (canFollow) {
newObject.eSet(eStructuralFeature, copy(originalEClass, (IdEObject) get, setOid, objectIDM));
}
}
}
}
}
}
return newObject;
}
Aggregations