List of usage examples for io.netty.util AttributeMap attr
<T> Attribute<T> attr(AttributeKey<T> key);
From source file:com.torodb.mongowp.mongoserver.api.toro.ToroQueryCommandProcessor.java
License:Open Source License
@Override public CountReply count(CountRequest request) throws Exception { AttributeMap attributeMap = request.getAttributes(); ToroConnection connection = attributeMap.attr(ToroRequestProcessor.CONNECTION).get(); QueryCriteria queryCriteria;/*from www . j ava 2 s. c o m*/ if (request.getQuery() == null) { queryCriteria = TrueQueryCriteria.getInstance(); } else { queryCriteria = queryCriteriaTranslator.translate(request.getQuery()); } ToroTransaction transaction = connection.createTransaction(); try { Integer count = transaction.count(request.getCollection(), queryCriteria).get(); return new CountReply(count); } finally { transaction.close(); } }
From source file:com.torodb.mongowp.mongoserver.api.toro.ToroQueryCommandProcessor.java
License:Open Source License
@Override public com.eightkdata.mongowp.mongoserver.api.pojos.InsertResponse insert(BSONDocument document, AttributeMap attributeMap) throws Exception { ToroConnection connection = attributeMap.attr(ToroRequestProcessor.CONNECTION).get(); String collection = (String) document.getValue("insert"); WriteFailMode writeFailMode = getWriteFailMode(document); WriteConcern writeConcern = getWriteConcern(document); Iterable<?> documents = (Iterable<?>) document.getValue("documents"); List<ToroDocument> inserts = new ArrayList<ToroDocument>(); Iterator<?> documentsIterator = documents.iterator(); while (documentsIterator.hasNext()) { BSONObject object = (BSONObject) documentsIterator.next(); inserts.add(new BSONToroDocument(object)); }//from w w w. j a va 2 s . c om ToroTransaction transaction = connection.createTransaction(); ToroInsertResponse.Builder responseBuilder = new ToroInsertResponse.Builder(); try { Future<InsertResponse> futureInsertResponse = transaction.insertDocuments(collection, inserts, writeFailMode); Future<?> futureCommitResponse = transaction.commit(); if (writeConcern.getW() > 0) { InsertResponse insertResponse = futureInsertResponse.get(); futureCommitResponse.get(); responseBuilder.setN(insertResponse.getInsertedSize()); } LastError lastError = new ToroLastError(RequestOpCode.OP_QUERY, QueryAndWriteOperationsQueryCommand.delete, futureInsertResponse, futureCommitResponse, false, null); attributeMap.attr(ToroRequestProcessor.LAST_ERROR).set(lastError); responseBuilder.setOk(true); return responseBuilder.build(); } finally { transaction.close(); } }
From source file:com.torodb.mongowp.mongoserver.api.toro.ToroQueryCommandProcessor.java
License:Open Source License
@Override public void update(BSONDocument document, MessageReplier messageReplier) throws Exception { AttributeMap attributeMap = messageReplier.getAttributeMap(); ToroConnection connection = attributeMap.attr(ToroRequestProcessor.CONNECTION).get(); Map<String, Object> keyValues = new HashMap<String, Object>(); String collection = (String) document.getValue("update"); WriteFailMode writeFailMode = getWriteFailMode(document); WriteConcern writeConcern = getWriteConcern(document); Iterable<?> documents = (Iterable<?>) document.getValue("updates"); List<UpdateOperation> updates = new ArrayList<UpdateOperation>(); Iterator<?> documentsIterator = documents.iterator(); while (documentsIterator.hasNext()) { BSONObject object = (BSONObject) documentsIterator.next(); QueryCriteria queryCriteria = queryCriteriaTranslator.translate((BSONObject) object.get("q")); UpdateAction updateAction = UpdateActionTranslator.translate((BSONObject) object.get("u")); boolean upsert = getBoolean(object, "upsert", false); boolean onlyOne = !getBoolean(object, "multi", false); updates.add(new UpdateOperation(queryCriteria, updateAction, upsert, onlyOne)); }//w w w. ja va2s .c o m ToroTransaction transaction = connection.createTransaction(); try { Future<UpdateResponse> futureUpdateResponse = transaction.update(collection, updates, writeFailMode); Future<?> futureCommitResponse = transaction.commit(); if (writeConcern.getW() > 0) { UpdateResponse updateResponse = futureUpdateResponse.get(); futureCommitResponse.get(); keyValues.put("n", updateResponse.getModified()); } LastError lastError = new ToroLastError(RequestOpCode.OP_QUERY, QueryAndWriteOperationsQueryCommand.update, futureUpdateResponse, futureCommitResponse, false, null); attributeMap.attr(ToroRequestProcessor.LAST_ERROR).set(lastError); } finally { transaction.close(); } keyValues.put("ok", MongoWP.OK); BSONDocument reply = new MongoBSONDocument(keyValues); messageReplier.replyMessageNoCursor(reply); }
From source file:com.torodb.mongowp.mongoserver.api.toro.ToroQueryCommandProcessor.java
License:Open Source License
@Override public void delete(BSONDocument document, MessageReplier messageReplier) throws Exception { AttributeMap attributeMap = messageReplier.getAttributeMap(); ToroConnection connection = attributeMap.attr(ToroRequestProcessor.CONNECTION).get(); Map<String, Object> keyValues = new HashMap<String, Object>(); String collection = (String) document.getValue("delete"); WriteFailMode writeFailMode = getWriteFailMode(document); WriteConcern writeConcern = getWriteConcern(document); Iterable<?> documents = (Iterable<?>) document.getValue("deletes"); List<DeleteOperation> deletes = new ArrayList<DeleteOperation>(); Iterator<?> documentsIterator = documents.iterator(); while (documentsIterator.hasNext()) { BSONObject object = (BSONObject) documentsIterator.next(); QueryCriteria queryCriteria = queryCriteriaTranslator.translate((BSONObject) object.get("q")); boolean singleRemove = getBoolean(object, "limit", false); deletes.add(new DeleteOperation(queryCriteria, singleRemove)); }/*from w w w . j a va 2 s.co m*/ ToroTransaction transaction = connection.createTransaction(); try { Future<DeleteResponse> futureDeleteResponse = transaction.delete(collection, deletes, writeFailMode); Future<?> futureCommitResponse = transaction.commit(); if (writeConcern.getW() > 0) { DeleteResponse deleteResponse = futureDeleteResponse.get(); futureCommitResponse.get(); keyValues.put("n", deleteResponse.getDeleted()); } LastError lastError = new ToroLastError(RequestOpCode.OP_QUERY, QueryAndWriteOperationsQueryCommand.delete, futureDeleteResponse, futureCommitResponse, false, null); attributeMap.attr(ToroRequestProcessor.LAST_ERROR).set(lastError); } finally { transaction.close(); } keyValues.put("ok", MongoWP.OK); BSONDocument reply = new MongoBSONDocument(keyValues); messageReplier.replyMessageNoCursor(reply); }
From source file:com.torodb.mongowp.mongoserver.api.toro.ToroQueryCommandProcessor.java
License:Open Source License
@Override public void drop(BSONDocument document, MessageReplier messageReplier) throws Exception { AttributeMap attributeMap = messageReplier.getAttributeMap(); ToroConnection connection = attributeMap.attr(ToroRequestProcessor.CONNECTION).get(); Map<String, Object> keyValues = new HashMap<String, Object>(); String collection = (String) document.getValue("drop"); connection.dropCollection(collection); keyValues.put("ok", MongoWP.OK); BSONDocument reply = new MongoBSONDocument(keyValues); messageReplier.replyMessageNoCursor(reply); }
From source file:com.torodb.mongowp.mongoserver.api.toro.ToroQueryCommandProcessor.java
License:Open Source License
@Override public void createIndexes(@Nonnull BSONDocument document, @Nonnull MessageReplier messageReplier) throws Exception { BSONObject keyValues = new BasicBSONObject(); Object collectionValue = document.getValue("createIndexes"); if (!(collectionValue instanceof String)) { keyValues.put("ok", MongoWP.KO); keyValues.put("code", (double) 13111); keyValues.put("errmsg", "exception: wrong type for field (createIndexes)"); messageReplier.replyMessageNoCursor(new MongoBSONDocument(keyValues)); return;/*from w w w. j a v a 2 s . co m*/ } String collection = (String) collectionValue; Object indexesValue = document.getValue("indexes"); if (!(indexesValue instanceof List)) { keyValues.put("ok", MongoWP.KO); keyValues.put("errmsg", "indexes has to be an array"); messageReplier.replyMessageNoCursor(new MongoBSONDocument(keyValues)); return; } //TODO: Unsafe cast List<BSONObject> uncastedIndexes = (List<BSONObject>) indexesValue; AttributeMap attributeMap = messageReplier.getAttributeMap(); ToroConnection connection = attributeMap.attr(ToroRequestProcessor.CONNECTION).get(); int numIndexesBefore; ToroTransaction transaction = null; try { transaction = connection.createTransaction(); numIndexesBefore = transaction.getIndexes(collection).size(); try { for (BSONObject uncastedIndex : uncastedIndexes) { String name = (String) uncastedIndex.removeField("name"); BSONObject key = (BSONObject) uncastedIndex.removeField("key"); Boolean unique = (Boolean) uncastedIndex.removeField("unique"); unique = unique != null ? unique : false; Boolean sparse = (Boolean) uncastedIndex.removeField("sparse"); sparse = sparse != null ? sparse : false; if (!uncastedIndex.keySet().isEmpty()) { keyValues.put("ok", MongoWP.KO); String errmsg = "Options " + uncastedIndex.keySet().toString() + " are not supported"; keyValues.put("errmsg", errmsg); messageReplier.replyMessageNoCursor(new MongoBSONDocument(keyValues)); return; } IndexedAttributes.Builder indexedAttsBuilder = new IndexedAttributes.Builder(); for (String path : key.keySet()) { AttributeReference attRef = parseAttributeReference(path); //TODO: Check that key.get(path) is a number!! boolean ascending = ((Number) key.get(path)).intValue() > 0; indexedAttsBuilder.addAttribute(attRef, ascending); } transaction.createIndex(collection, name, indexedAttsBuilder.build(), unique, sparse).get(); } int numIndexesAfter = transaction.getIndexes(collection).size(); transaction.commit().get(); keyValues.put("ok", MongoWP.OK); keyValues.put("createdCollectionAutomatically", false); keyValues.put("numIndexesBefore", numIndexesBefore); keyValues.put("numIndexesAfter", numIndexesAfter); BSONDocument reply = new MongoBSONDocument(keyValues); messageReplier.replyMessageNoCursor(reply); } catch (ExecutionException ex) { if (ex.getCause() instanceof ExistentIndexException) { keyValues.put("ok", MongoWP.OK); keyValues.put("note", ex.getCause().getMessage()); keyValues.put("numIndexesBefore", numIndexesBefore); messageReplier.replyMessageNoCursor(new MongoBSONDocument(keyValues)); } else { throw ex; } } } finally { if (transaction != null) { transaction.close(); } } }
From source file:com.torodb.mongowp.mongoserver.api.toro.ToroQueryCommandProcessor.java
License:Open Source License
@Override public void deleteIndexes(BSONDocument query, MessageReplier messageReplier) throws Exception { Map<String, Object> keyValues = Maps.newHashMap(); Object dropIndexesValue = query.getValue("deleteIndexes"); Object indexValue = query.getValue("index"); if (!(dropIndexesValue instanceof String)) { keyValues.put("ok", MongoWP.KO); keyValues.put("errmsg", "The field 'dropIndexes' must be a string"); messageReplier.replyMessageNoCursor(new MongoBSONDocument(keyValues)); return;//ww w . j av a2 s . c o m } if (!(indexValue instanceof String)) { keyValues.put("ok", MongoWP.KO); keyValues.put("errmsg", "The field 'index' must be a string"); messageReplier.replyMessageNoCursor(new MongoBSONDocument(keyValues)); return; } String collection = (String) dropIndexesValue; String indexName = (String) indexValue; AttributeMap attributeMap = messageReplier.getAttributeMap(); ToroConnection connection = attributeMap.attr(ToroRequestProcessor.CONNECTION).get(); ToroTransaction transaction = null; try { transaction = connection.createTransaction(); if (indexName.equals("*")) { //TODO: Support * in deleteIndexes keyValues.put("ok", MongoWP.KO); keyValues.put("errmsg", "The wildcard '*' is not supported by ToroDB right now"); messageReplier.replyMessageNoCursor(new MongoBSONDocument(keyValues)); return; } Boolean removed = transaction.dropIndex(collection, indexName).get(); if (!removed) { keyValues.put("ok", MongoWP.KO); keyValues.put("errmsg", "index not found with name [" + indexName + "]"); messageReplier.replyMessageNoCursor(new MongoBSONDocument(keyValues)); return; } transaction.commit(); keyValues.put("ok", MongoWP.OK); messageReplier.replyMessageNoCursor(new MongoBSONDocument(keyValues)); } finally { if (transaction != null) { transaction.close(); } } }
From source file:com.torodb.mongowp.mongoserver.api.toro.ToroQueryCommandProcessor.java
License:Open Source License
@Override public void create(BSONDocument document, MessageReplier messageReplier) throws Exception { String collection = (String) document.getValue("create"); Boolean capped = (Boolean) document.getValue("capped"); if (capped != null && capped) { // Other flags silently ignored messageReplier.replyQueryCommandFailure(ErrorCode.UNIMPLEMENTED_FLAG, "capped"); return;//from w w w . ja va 2 s . c o m } AttributeMap attributeMap = messageReplier.getAttributeMap(); ToroConnection connection = attributeMap.attr(ToroRequestProcessor.CONNECTION).get(); Map<String, Object> keyValues = new HashMap<String, Object>(); if (connection.createCollection(collection, null)) { keyValues.put("ok", MongoWP.OK); } else { keyValues.put("ok", MongoWP.KO); keyValues.put("errmsg", "collection already exists"); } messageReplier.replyMessageNoCursor(new MongoBSONDocument(keyValues)); }
From source file:com.torodb.mongowp.mongoserver.api.toro.ToroQueryCommandProcessor.java
License:Open Source License
@Override public void getLastError(Object w, boolean j, boolean fsync, int wtimeout, MessageReplier messageReplier) throws Exception { AttributeMap attributeMap = messageReplier.getAttributeMap(); LastError lastError = attributeMap.attr(ToroRequestProcessor.LAST_ERROR).get(); lastError.getLastError(w, j, fsync, wtimeout, messageReplier); }
From source file:com.torodb.mongowp.mongoserver.api.toro.ToroQueryCommandProcessor.java
License:Open Source License
@Override public boolean handleError(QueryCommand userCommand, MessageReplier messageReplier, Throwable throwable) throws Exception { //TODO: Map real mongo error codes ErrorCode errorCode = ErrorCode.INTERNAL_ERROR; AttributeMap attributeMap = messageReplier.getAttributeMap(); LastError lastError = new ToroLastError(RequestOpCode.OP_QUERY, userCommand, null, null, true, errorCode); attributeMap.attr(ToroRequestProcessor.LAST_ERROR).set(lastError); messageReplier.replyQueryCommandFailure(errorCode, throwable.getMessage()); return true;//from www . j av a2 s .co m }