List of usage examples for com.mongodb BasicDBObject getObjectId
public ObjectId getObjectId(final String field)
From source file:ch.agent.crnickl.mongodb.MongoDatabaseMethods.java
License:Apache License
/** * @param bdo a {@link BasicDBObject}//from w ww .j a v a2s. co m * @return the argument's {@link ObjectId} */ public ObjectId getObjectId(BasicDBObject bdo) { return bdo.getObjectId(MongoDatabase.FLD_ID); }
From source file:ch.agent.crnickl.mongodb.ReadMethodsForChroniclesAndSeries.java
License:Apache License
/** * The method completes the attribute with the value found for one of the * entities in the list and returns true. If no value was found, the method * return false. If more than value was found, the one selected corresponds * to the first entity encountered in the list. * /* www .ja va 2s.c o m*/ * @param chronicles a list of chronicles * @param attribute an attribute * @return true if any value found * @throws T2DBException */ public boolean getAttributeValue(List<Chronicle> chronicles, Attribute<?> attribute) throws T2DBException { boolean found = false; Surrogate s = attribute.getProperty().getSurrogate(); Database db = s.getDatabase(); try { ObjectId[] chrOids = new ObjectId[chronicles.size()]; int offset = 0; for (Chronicle chronicle : chronicles) { chrOids[offset++] = getId(chronicle); } DBCursor cursor = getMongoDB(db).getAttributes().find(mongoObject(MongoDatabase.FLD_ATTR_PROP, getId(s), MongoDatabase.FLD_ATTR_CHRON, mongoObject(Operator.IN.op(), chrOids))); offset = Integer.MAX_VALUE; BasicDBObject objAtOffset = null; while (cursor.hasNext()) { BasicDBObject obj = (BasicDBObject) cursor.next(); ObjectId chrOid = obj.getObjectId(MongoDatabase.FLD_ATTR_CHRON); int offset1 = findOffset(chrOid, chronicles); if (offset1 < offset) { offset = offset1; objAtOffset = obj; } if (offset == 0) break; } if (objAtOffset != null) { ObjectId chrOid = objAtOffset.getObjectId(MongoDatabase.FLD_ATTR_CHRON); Surrogate chronicle = makeSurrogate(db, DBObjectType.CHRONICLE, chrOid); check(Permission.READ, chronicle); attribute.scan(objAtOffset.getString(MongoDatabase.FLD_ATTR_VALUE)); String description = objAtOffset.getString(MongoDatabase.FLD_ATTR_DESC); if (description.length() > 0) attribute.setDescription(description); found = true; } } catch (Exception e) { throw T2DBMsg.exception(e, E.E40120, attribute.getProperty().getName()); } return found; }
From source file:ch.agent.crnickl.mongodb.ReadMethodsForChroniclesAndSeries.java
License:Apache License
/** * Return a list of chronicles with a given value for a given property. * /* w w w .j a v a2 s. c om*/ * @param property a property * @param value a value * @param maxSize the maximum size of the result or 0 for no limit * @return a list of chronicles, possibly empty, never null * @throws T2DBException */ public <T> List<Chronicle> getChroniclesByAttributeValue(Property<T> property, T value, int maxSize) throws T2DBException { List<Chronicle> result = new ArrayList<Chronicle>(); Surrogate s = property.getSurrogate(); Database db = s.getDatabase(); try { DBCursor cursor = getMongoDB(db).getAttributes().find(mongoObject(MongoDatabase.FLD_ATTR_PROP, getId(s), MongoDatabase.FLD_ATTR_VALUE, property.getValueType().toString(value))); while (cursor.hasNext()) { BasicDBObject obj = (BasicDBObject) cursor.next(); ObjectId chrOid = obj.getObjectId(MongoDatabase.FLD_ATTR_CHRON); Surrogate chronicle = makeSurrogate(db, DBObjectType.CHRONICLE, chrOid); try { check(Permission.READ, chronicle); result.add(new ChronicleImpl(chronicle)); } catch (T2DBException e) { // ignore "unreadable" chronicles } } } catch (Exception e) { throw T2DBMsg.exception(e, E.E40119, property.getName(), value); } return result; }
From source file:ch.agent.crnickl.mongodb.ReadMethodsForChroniclesAndSeries.java
License:Apache License
/** * Find a series corresponding to a surrogate. * //from w ww .jav a2s . c o m * @param s a surrogate * @return a series or null * @throws T2DBException */ public <T> Series<T> getSeries(Surrogate s) throws T2DBException { BasicDBObject obj = (BasicDBObject) getObject(s, false); Throwable cause = null; if (obj != null) try { ObjectId chrOid = obj.getObjectId(MongoDatabase.FLD_SER_CHRON); int serNum = obj.getInt(MongoDatabase.FLD_SER_NUM); Chronicle chronicle = new ChronicleImpl( makeSurrogate(s.getDatabase(), DBObjectType.CHRONICLE, chrOid)); check(Permission.READ, chronicle); Series<T> series = new SeriesImpl<T>(chronicle, null, serNum, s); return series; } catch (Exception e) { cause = e; } throw T2DBMsg.exception(cause, E.E40104, s.toString()); }
From source file:ch.agent.crnickl.mongodb.ReadMethodsForChroniclesAndSeries.java
License:Apache License
/** * Return array of series in the positions corresponding to the * requested numbers. These numbers are series numbers within the schema, * not the series keys in the database. When a requested number in the array is * non-positive, the corresponding series will be null in the result. A * series can also be null when the requested number is positive but the * series has no data and has not been set up in the database (and therefore * has no series database key)./*from w w w.j a v a 2s.c o m*/ * <p> * Note. There is no exception if the chronicle does not exist. This behavior makes * it easier to implement the wishful transaction protocol (i.e. testing again if it * was okay to delete a chronicle after it was deleted). * * @param chronicle a chronicle * @param names an array of simple names to plug into the series * @param numbers an array of numbers * @return an array of series * @throws T2DBException */ public <T> Series<T>[] getSeries(Chronicle chronicle, String[] names, int[] numbers) throws T2DBException { if (names.length != numbers.length) throw new IllegalArgumentException("lengths of names[] and numbers[] differ"); @SuppressWarnings("unchecked") Series<T>[] result = new SeriesImpl[numbers.length]; if (result.length > 0) { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < numbers.length; i++) { if (numbers[i] > 0) { if (map.put(numbers[i], i) != null) throw new IllegalArgumentException("duplicate number: " + numbers[i]); } } Surrogate s = chronicle.getSurrogate(); Database db = s.getDatabase(); try { check(Permission.READ, chronicle); } catch (Exception e) { throw T2DBMsg.exception(e, E.E40104, s.toString()); } try { DBCursor cursor = getMongoDB(db) .getSeries().find( mongoObject(MongoDatabase.FLD_SER_CHRON, getId(chronicle), MongoDatabase.FLD_SER_NUM, mongoObject(Operator.IN.op(), numbers)), mongoObject(MongoDatabase.FLD_SER_NUM, 1)); while (cursor.hasNext()) { BasicDBObject obj = (BasicDBObject) cursor.next(); ObjectId serOid = obj.getObjectId(MongoDatabase.FLD_ID); int serNumber = obj.getInt(MongoDatabase.FLD_SER_NUM); int i = map.get(serNumber); result[i] = new SeriesImpl<T>(chronicle, names[i], numbers[i], makeSurrogate(db, DBObjectType.SERIES, serOid)); } } catch (Exception e) { throw T2DBMsg.exception(e, E.E40121, chronicle.getName(true)); } } return result; }
From source file:ch.agent.crnickl.mongodb.ReadMethodsForChroniclesAndSeries.java
License:Apache License
private Chronicle unpack(Database db, BasicDBObject obj) throws T2DBException { try {//from ww w . j av a2 s. c o m ObjectId id = obj.getObjectId(MongoDatabase.FLD_ID); Surrogate s = makeSurrogate(db, DBObjectType.CHRONICLE, id); ChronicleImpl.RawData data = new ChronicleImpl.RawData(); data.setSurrogate(s); data.setName(obj.getString(MongoDatabase.FLD_CHRON_NAME)); data.setDescription(obj.getString(MongoDatabase.FLD_CHRON_DESC)); ObjectId parentId = obj.getObjectId(MongoDatabase.FLD_CHRON_PARENT); data.setCollection(parentId == null ? db.getTopChronicle() : new ChronicleImpl(makeSurrogate(db, DBObjectType.CHRONICLE, parentId))); ObjectId schemaId = obj.getObjectId(MongoDatabase.FLD_CHRON_SCHEMA); data.setSchema(schemaId == null ? null : makeSurrogate(db, DBObjectType.SCHEMA, schemaId)); Chronicle chronicle = new ChronicleImpl(data); return chronicle; } catch (ClassCastException e) { throw T2DBMMsg.exception(e, J.J81010, obj.toString()); } }
From source file:ch.agent.crnickl.mongodb.ReadMethodsForProperty.java
License:Apache License
private <T> Property<T> unpack(Database db, BasicDBObject obj) throws T2DBException { try {// w ww. j ava 2 s . c o m ObjectId id = obj.getObjectId(MongoDatabase.FLD_ID); String name = obj.getString(MongoDatabase.FLD_PROP_NAME); ObjectId vtId = obj.getObjectId(MongoDatabase.FLD_PROP_VT); Boolean indexed = obj.getBoolean(MongoDatabase.FLD_PROP_INDEXED); Surrogate vts = makeSurrogate(db, DBObjectType.VALUE_TYPE, new MongoDBObjectId(vtId)); ValueType<T> vt = db.getValueType(vts); Surrogate s = makeSurrogate(db, DBObjectType.PROPERTY, new MongoDBObjectId(id)); checkIntegrity(vt, vts, s); return new PropertyImpl<T>(name, vt, indexed, s); } catch (ClassCastException e) { throw T2DBMMsg.exception(e, J.J81010, obj.toString()); } }
From source file:ch.agent.crnickl.mongodb.ReadMethodsForSchema.java
License:Apache License
private UpdatableSchema unpack(Database db, BasicDBObject obj, Set<ObjectId> cycleDetector) throws T2DBException { if (cycleDetector == null) cycleDetector = new HashSet<ObjectId>(); try {//w ww . jav a 2 s. c o m ObjectId id = obj.getObjectId(MongoDatabase.FLD_ID); Surrogate s = makeSurrogate(db, DBObjectType.SCHEMA, new MongoDBObjectId(id)); boolean cycleDetected = !cycleDetector.add(id); String name = obj.getString(MongoDatabase.FLD_SCHEMA_NAME); UpdatableSchema base = null; ObjectId baseId = obj.getObjectId(MongoDatabase.FLD_SCHEMA_BASE); if (baseId != null && !cycleDetected) { Surrogate baseSurr = makeSurrogate(db, DBObjectType.SCHEMA, new MongoDBObjectId(baseId)); base = getSchema(baseSurr, cycleDetector); } Collection<AttributeDefinition<?>> attribs = attributeDefinitions(s, 0, db, (BasicDBList) obj.get(MongoDatabase.FLD_SCHEMA_ATTRIBS)); Collection<SeriesDefinition> series = seriesDefinitions(s, db, (BasicDBList) obj.get(MongoDatabase.FLD_SCHEMA_SERIES)); return new UpdatableSchemaImpl(name, base, attribs, series, s); } catch (ClassCastException e) { throw T2DBMMsg.exception(e, J.J81010, obj.toString()); } }
From source file:ch.agent.crnickl.mongodb.ReadMethodsForSchema.java
License:Apache License
@SuppressWarnings({ "rawtypes", "unchecked" }) private AttributeDefinition<?> attributeDefinition(Surrogate schemaSurrogate, int seriesNr, Database db, BasicDBObject bo) throws T2DBException { int number = bo.getInt(MongoDatabase.FLD_ATTRIBDEF_NUM); boolean era = bo.getBoolean(MongoDatabase.FLD_ATTRIBDEF_ERASING); AttributeDefinitionImpl<?> def = null; if (era) {/* w w w. j a v a2s . co m*/ def = new AttributeDefinitionImpl(seriesNr, number, null, null); def.edit(); def.setErasing(true); } else { ObjectId propId = bo.getObjectId(MongoDatabase.FLD_ATTRIBDEF_PROP); Surrogate propSurr = makeSurrogate(db, DBObjectType.PROPERTY, new MongoDBObjectId(propId)); Property<?> prop = ((MongoDatabase) db).getReadMethodsForProperty().getProperty(propSurr); String val = bo.getString(MongoDatabase.FLD_ATTRIBDEF_VAL); checkIntegrity(prop, propSurr, schemaSurrogate); def = new AttributeDefinitionImpl(seriesNr, number, prop, prop.getValueType().scan(val)); } return def; }
From source file:ch.agent.crnickl.mongodb.ReadMethodsForValueType.java
License:Apache License
@SuppressWarnings("unchecked") private <T> ValueType<T> unpack(Database db, BasicDBObject obj) throws T2DBException { try {/*w ww .ja va2s .co m*/ ObjectId id = obj.getObjectId(MongoDatabase.FLD_ID); String name = obj.getString(MongoDatabase.FLD_VT_NAME); String type = obj.getString(MongoDatabase.FLD_VT_TYPE); Object values = obj.get(MongoDatabase.FLD_VT_VALUES); boolean restricted = values != null; Map<String, String> valueMap = null; if (restricted) valueMap = (Map<String, String>) ((BasicDBObject) values).toMap(); Surrogate s = makeSurrogate(db, DBObjectType.VALUE_TYPE, new MongoDBObjectId(id)); return new ValueTypeImpl<T>(name, restricted, type, valueMap, s); } catch (ClassCastException e) { throw T2DBMMsg.exception(e, J.J81010, obj.toString()); } }