List of usage examples for com.mongodb BasicDBObject get
public Object get(final String key)
From source file:org.apache.metamodel.mongodb.MongoDbDataContext.java
License:Apache License
private void convertToCursorObject(BasicDBObject query, FilterItem item) { if (item.isCompoundFilter()) { BasicDBList orList = new BasicDBList(); final FilterItem[] childItems = item.getChildItems(); for (FilterItem childItem : childItems) { BasicDBObject childObject = new BasicDBObject(); convertToCursorObject(childObject, childItem); orList.add(childObject);//from ww w . j av a 2 s . com } query.put("$or", orList); } else { final Column column = item.getSelectItem().getColumn(); final String columnName = column.getName(); final Object operand = item.getOperand(); final String operatorName = getOperatorName(item); final BasicDBObject existingFilterObject = (BasicDBObject) query.get(columnName); if (existingFilterObject == null) { if (operatorName == null) { if (OperatorType.LIKE.equals(item.getOperator())) { query.put(columnName, turnOperandIntoRegExp(operand)); } else { query.put(columnName, operand); } } else { query.put(columnName, new BasicDBObject(operatorName, operand)); } } else { if (operatorName == null) { throw new IllegalStateException( "Cannot retrieve records for a column with two EQUALS_TO operators"); } else { existingFilterObject.append(operatorName, operand); } } } }
From source file:org.apache.rya.mongodb.dao.SimpleMongoDBStorageStrategy.java
License:Apache License
public BasicDBObject serializeInternal(final RyaStatement statement) { String context = ""; if (statement.getContext() != null) { context = statement.getContext().getData(); }// w ww. java2 s .co m final String id = statement.getSubject().getData() + " " + statement.getPredicate().getData() + " " + statement.getObject().getData() + " " + context; byte[] bytes = id.getBytes(StandardCharsets.UTF_8); try { final MessageDigest digest = MessageDigest.getInstance("SHA-1"); bytes = digest.digest(bytes); } catch (final NoSuchAlgorithmException e) { LOG.error("Unable to perform SHA-1 on the ID, defaulting to raw bytes.", e); } if (statement.getMetadata() == null) { statement.setStatementMetadata(StatementMetadata.EMPTY_METADATA); } final BasicDBObject dvObject = DocumentVisibilityAdapter.toDBObject(statement.getColumnVisibility()); final BasicDBObject doc = new BasicDBObject(ID, new String(Hex.encodeHex(bytes))) .append(SUBJECT, statement.getSubject().getData()) .append(SUBJECT_HASH, DigestUtils.sha256Hex(statement.getSubject().getData())) .append(PREDICATE, statement.getPredicate().getData()) .append(PREDICATE_HASH, DigestUtils.sha256Hex(statement.getPredicate().getData())) .append(OBJECT, statement.getObject().getData()) .append(OBJECT_HASH, DigestUtils.sha256Hex(statement.getObject().getData())) .append(OBJECT_TYPE, statement.getObject().getDataType().toString()).append(CONTEXT, context) .append(STATEMENT_METADATA, statement.getMetadata().toString()) .append(DOCUMENT_VISIBILITY, dvObject.get(DOCUMENT_VISIBILITY)) .append(TIMESTAMP, statement.getTimestamp()); return doc; }
From source file:org.apache.rya.mongodb.document.visibility.DocumentVisibilityAdapter.java
License:Apache License
/** * Deserializes a MongoDB {@link DBObject} to a {@link DocumentVisibility}. * @param mongoObj the {@link DBObject} to be deserialized. * @return the {@link DocumentVisibility} object. * @throws MalformedDocumentVisibilityException *//*from www . j a v a2 s . c o m*/ public static DocumentVisibility toDocumentVisibility(final DBObject mongoObj) throws MalformedDocumentVisibilityException { try { final BasicDBObject basicObj = (BasicDBObject) mongoObj; final Object documentVisibilityObject = basicObj.get(DOCUMENT_VISIBILITY_KEY); Object[] documentVisibilityArray = null; if (documentVisibilityObject instanceof Object[]) { documentVisibilityArray = (Object[]) documentVisibilityObject; } else if (documentVisibilityObject instanceof BasicDBList) { documentVisibilityArray = DocumentVisibilityUtil .convertBasicDBListToObjectArray((BasicDBList) documentVisibilityObject); } final String documentVisibilityString = DocumentVisibilityUtil .multidimensionalArrayToBooleanString(documentVisibilityArray); final DocumentVisibility dv = documentVisibilityString == null ? MongoDbRdfConstants.EMPTY_DV : new DocumentVisibility(documentVisibilityString); return dv; } catch (final Exception e) { throw new MalformedDocumentVisibilityException( "Failed to make Document Visibility from Mongo Object, it is malformed.", e); } }
From source file:org.apache.rya.mongodb.instance.MongoDetailsAdapter.java
License:Apache License
private static PCJIndexDetails.Builder getPCJIndexDetails(final BasicDBObject basicObj) { final BasicDBObject pcjIndexDBO = (BasicDBObject) basicObj.get(PCJ_DETAILS_KEY); final PCJIndexDetails.Builder pcjBuilder = PCJIndexDetails.builder() .setEnabled(pcjIndexDBO.getBoolean(PCJ_ENABLED_KEY)) .setFluoDetails(new FluoDetails(pcjIndexDBO.getString(PCJ_FLUO_KEY))); final BasicDBList pcjs = (BasicDBList) pcjIndexDBO.get(PCJ_PCJS_KEY); if (pcjs != null) { for (int ii = 0; ii < pcjs.size(); ii++) { final BasicDBObject pcj = (BasicDBObject) pcjs.get(ii); pcjBuilder.addPCJDetails(toPCJDetails(pcj)); }// w w w .j av a 2 s . c om } return pcjBuilder; }
From source file:org.aw20.mongoworkbench.command.AggregateMongoCommand.java
License:Open Source License
@Override public void execute() throws Exception { MongoClient mdb = MongoFactory.getInst().getMongo(sName); if (mdb == null) throw new Exception("no server selected"); if (sDb == null) throw new Exception("no database selected"); MongoFactory.getInst().setActiveDB(sDb); DB db = mdb.getDB(sDb);//w w w .j av a 2 s .c o m BasicDBObject cmdMap = parseMongoCommandString(db, cmd); if (!cmdMap.containsField("aggregateArg")) throw new Exception("no aggregate document"); // Execute the command Object result = db.eval(cmd, (Object[]) null); if (result == null) throw new Exception("null returned"); if (!(result instanceof BasicDBObject)) throw new Exception("not correct type returned: " + result.getClass().getName()); BasicDBObject dbo = (BasicDBObject) result; if (dbo.containsField("result")) { dbListResult = (BasicDBList) dbo.get("result"); setMessage("# rows=" + dbListResult.size()); } else { setMessage("# rows=0"); } }
From source file:org.aw20.mongoworkbench.command.MapReduceMongoCommand.java
License:Open Source License
@SuppressWarnings("deprecation") @Override/*from w w w . java 2 s .c o m*/ public void execute() throws Exception { MongoClient mdb = MongoFactory.getInst().getMongo(sName); if (mdb == null) throw new Exception("no server selected"); if (sDb == null) throw new Exception("no database selected"); MongoFactory.getInst().setActiveDB(sDb); DB db = mdb.getDB(sDb); BasicDBObject cmdMap = parseMongoCommandString(db, cmd); if (!cmdMap.containsField("mapreduceArgs")) throw new Exception("no mapReduce document"); DBCollection collection = db.getCollection(sColl); // Build the Map BasicDBObject options = (BasicDBObject) ((BasicDBList) cmdMap.get("mapreduceArgs")).get(2); String outputCollection = null; String outputDB = null; MapReduceCommand.OutputType outputType = MapReduceCommand.OutputType.INLINE; if (options.get("out") instanceof String) { outputCollection = (String) options.get("out"); outputType = MapReduceCommand.OutputType.REPLACE; } else if (options.get("out") instanceof BasicDBObject) { BasicDBObject out = (BasicDBObject) options.get("out"); if (out.containsField("inline")) { outputCollection = null; } else if (out.containsField("replace")) { outputCollection = (String) out.get("replace"); outputType = MapReduceCommand.OutputType.REPLACE; } else if (out.containsField("merge")) { outputCollection = (String) out.get("merge"); outputType = MapReduceCommand.OutputType.MERGE; } else if (out.containsField("reduce")) { outputCollection = (String) out.get("reduce"); outputType = MapReduceCommand.OutputType.REDUCE; } if (out.containsField("db")) outputDB = (String) out.get("db"); } MapReduceCommand mrc = new MapReduceCommand(collection, ((Code) ((BasicDBList) cmdMap.get("mapreduceArgs")).get(0)).getCode(), ((Code) ((BasicDBList) cmdMap.get("mapreduceArgs")).get(1)).getCode(), outputCollection, outputType, (BasicDBObject) options.get("query")); if (outputDB != null) mrc.setOutputDB(outputDB); if (options.containsField("sort") && options.get("sort") instanceof DBObject) mrc.setSort((DBObject) options.get("sort")); if (options.containsField("scope") && options.get("scope") instanceof DBObject) mrc.setScope(((DBObject) options.get("scope")).toMap()); if (options.containsField("finalize") && options.get("scope") instanceof Code) mrc.setFinalize(((Code) options.get("scope")).getCode()); if (options.containsField("limit")) mrc.setLimit(StringUtil.toInteger(options.get("limit"), -1)); mrc.addExtraOption("jsMode", StringUtil.toBoolean(options.get("jsMode"), false)); mrc.setVerbose(StringUtil.toBoolean(options.get("verbose"), false)); // Run the actual mapreduce function MapReduceOutput mro = collection.mapReduce(mrc); // Pull the inline results if (mro.getOutputCollection() == null) { dbListResult = new BasicDBList(); Iterable<DBObject> it = mro.results(); for (DBObject dbo : it) { dbListResult.add(dbo); } } BasicDBObject dbo = mro.getRaw(); StringBuilder sb = new StringBuilder(); if (dbo.containsField("timeMillis")) sb.append("Time=").append(dbo.get("timeMillis")).append("ms; "); if (dbo.containsField("counts")) { BasicDBObject counts = (BasicDBObject) dbo.get("counts"); sb.append("Counts: input=" + counts.get("input")); sb.append("; emit=" + counts.get("emit")); sb.append("; reduce=" + counts.get("reduce")); sb.append("; output=" + counts.get("output")); } setMessage(sb.toString()); }
From source file:org.aw20.mongoworkbench.command.MongoCommand.java
License:Open Source License
protected BasicDBObject fixNumbers(BasicDBObject dbo) { Iterator<String> it = dbo.keySet().iterator(); while (it.hasNext()) { String field = it.next(); Object o = dbo.get(field); if (o instanceof Double) { dbo.put(field, NumberUtil.fixDouble((Double) o)); } else if (o instanceof BasicDBObject) { dbo.put(field, fixNumbers((BasicDBObject) o)); } else if (o instanceof BasicDBList) { dbo.put(field, fixNumbers((BasicDBList) o)); }// w ww . j a v a 2 s. c o m } return dbo; }
From source file:org.aw20.mongoworkbench.command.RemoveMongoCommand.java
License:Open Source License
@Override public void execute() throws Exception { MongoClient mdb = MongoFactory.getInst().getMongo(sName); if (mdb == null) throw new Exception("no server selected"); if (sDb == null) throw new Exception("no database selected"); MongoFactory.getInst().setActiveDB(sDb); DB db = mdb.getDB(sDb);// w w w. j a va 2 s . co m BasicDBObject cmdMap = parseMongoCommandString(db, cmd); if (!cmdMap.containsField("removeArgs")) throw new Exception("no remove document"); DBCollection collection = db.getCollection(sColl); BasicDBList args = (BasicDBList) cmdMap.get("removeArgs"); // Run the command db.requestStart(); WriteResult writeresult; try { writeresult = collection.remove((DBObject) args.get(0), WriteConcern.JOURNAL_SAFE); } finally { db.requestDone(); } // Get the result Map mwriteresult = (Map) JSON.parse(writeresult.toString()); mwriteresult.put("exeDate", new Date()); EventWorkBenchManager.getInst().onEvent(Event.WRITERESULT, mwriteresult); setMessage("Removed"); }
From source file:org.aw20.mongoworkbench.command.SaveMongoCommand.java
License:Open Source License
@Override public void execute() throws Exception { MongoClient mdb = MongoFactory.getInst().getMongo(sName); if (mdb == null) throw new Exception("no server selected"); if (sDb == null) throw new Exception("no database selected"); MongoFactory.getInst().setActiveDB(sDb); DB db = mdb.getDB(sDb);/*from w w w . ja v a 2 s . c om*/ BasicDBObject cmdMap = parseMongoCommandString(db, cmd); if (!cmdMap.containsField("saveArg")) throw new Exception("no save document"); DBObject document = fixNumbers((BasicDBObject) cmdMap.get("saveArg")); DBCollection collection = db.getCollection(sColl); // Run the command db.requestStart(); WriteResult writeresult; try { writeresult = collection.save(document, WriteConcern.JOURNAL_SAFE); id = document.get("_id"); } finally { db.requestDone(); } // Get the result Map mwriteresult = (Map) JSON.parse(writeresult.toString()); mwriteresult.put("exeDate", new Date()); EventWorkBenchManager.getInst().onEvent(Event.WRITERESULT, mwriteresult); setMessage("Saved: updatedExisting=" + mwriteresult.get("updatedExisting") + "; documentsUpdated=" + mwriteresult.get("n")); }
From source file:org.aw20.mongoworkbench.command.UpdateMongoCommand.java
License:Open Source License
@Override public void execute() throws Exception { MongoClient mdb = MongoFactory.getInst().getMongo(sName); if (mdb == null) throw new Exception("no server selected"); if (sDb == null) throw new Exception("no database selected"); MongoFactory.getInst().setActiveDB(sDb); DB db = mdb.getDB(sDb);/*from ww w . j av a 2s. com*/ BasicDBObject cmdMap = parseMongoCommandString(db, cmd); if (!cmdMap.containsField("updateArg")) throw new Exception("no update document"); List argList = (List) cmdMap.get("updateArg"); if (argList.size() == 1) throw new Exception("not enough parameters; db.collection.update(query, update, <upsert>, <multi>)"); DBCollection collection = db.getCollection(sColl); db.requestStart(); WriteResult writeresult = null; try { if (argList.size() == 2) { writeresult = collection.update((DBObject) argList.get(0), fixNumbers((BasicDBObject) argList.get(1))); } else if (argList.size() == 3) { boolean upsert = StringUtil.toBoolean(argList.get(2), false); writeresult = collection.update((DBObject) argList.get(0), fixNumbers((BasicDBObject) argList.get(1)), upsert, false); } else if (argList.size() == 4) { boolean upsert = StringUtil.toBoolean(argList.get(2), false); boolean multi = StringUtil.toBoolean(argList.get(3), false); writeresult = collection.update((DBObject) argList.get(0), fixNumbers((BasicDBObject) argList.get(1)), upsert, multi); } else throw new Exception("too many parameters; db.collection.update(query, update, <upsert>, <multi>)"); } finally { db.requestDone(); } // Get the result Map mwriteresult = (Map) JSON.parse(writeresult.toString()); mwriteresult.put("exeDate", new Date()); EventWorkBenchManager.getInst().onEvent(Event.WRITERESULT, mwriteresult); setMessage("Updated: updatedExisting=" + mwriteresult.get("updatedExisting") + "; documentsUpdated=" + mwriteresult.get("n")); }