Exemple #1
0
 public DBCollection getCollection(String name, boolean slave) {
   DBCollection collection = db.getCollection(name);
   if (slave) {
     collection.setReadPreference(ReadPreference.SECONDARY);
   } else {
     collection.setReadPreference(ReadPreference.PRIMARY);
   }
   return collection;
 }
Exemple #2
0
 public DBCollection getCollection(String name) {
   DBCollection collection = db.getCollection(name);
   collection.setReadPreference(db.getReadPreference());
   return collection;
 }
  /**
   * Execute the interaction and return output record. The spec is either GET, PUT or DELETE
   * interaction.
   */
  public Record execute(InteractionSpec spec, Record record) throws ResourceException {
    if (!(spec instanceof MongoInteractionSpec)) {
      throw EISException.invalidInteractionSpecType();
    }
    if (!(record instanceof MongoRecord)) {
      throw EISException.invalidRecordType();
    }
    MongoInteractionSpec mongoSpec = (MongoInteractionSpec) spec;
    MongoRecord input = (MongoRecord) record;
    MongoOperation operation = mongoSpec.getOperation();
    String collectionName = mongoSpec.getCollection();
    if (operation == null) {
      ResourceException resourceException = new ResourceException("Mongo operation must be set");
      throw resourceException;
    }
    if (operation == MongoOperation.EVAL) {
      Object result = this.connection.getDB().eval(mongoSpec.getCode());
      return buildRecordFromDBObject((DBObject) result);
    }
    if (collectionName == null) {
      ResourceException resourceException = new ResourceException("DB Collection name must be set");
      throw resourceException;
    }
    try {
      DBCollection collection = this.connection.getDB().getCollection(collectionName);
      if (mongoSpec.getOptions() > 0) {
        collection.setOptions(mongoSpec.getOptions());
      }
      if (mongoSpec.getReadPreference() != null) {
        collection.setReadPreference(mongoSpec.getReadPreference());
      }
      if (mongoSpec.getWriteConcern() != null) {
        collection.setWriteConcern(mongoSpec.getWriteConcern());
      }
      if (operation == MongoOperation.INSERT) {
        DBObject object = buildDBObject(input);
        collection.insert(object);
      } else if (operation == MongoOperation.REMOVE) {
        DBObject object = buildDBObject(input);
        collection.remove(object);
      } else if (operation == MongoOperation.FIND) {
        DBObject sort = null;
        if (input.containsKey(MongoRecord.SORT)) {
          sort = buildDBObject((MongoRecord) input.get(MongoRecord.SORT));
          input.remove(MongoRecord.SORT);
        }
        DBObject select = null;
        if (input.containsKey("$select")) {
          select = buildDBObject((MongoRecord) input.get("$select"));
          input.remove("$select");
        }
        DBObject object = buildDBObject(input);
        DBCursor cursor = collection.find(object, select);
        if (sort != null) {
          cursor.sort(sort);
        }
        try {
          if (mongoSpec.getSkip() > 0) {
            cursor.skip(mongoSpec.getSkip());
          }
          if (mongoSpec.getLimit() != 0) {
            cursor.limit(mongoSpec.getLimit());
          }
          if (mongoSpec.getBatchSize() != 0) {
            cursor.batchSize(mongoSpec.getBatchSize());
          }
          if (!cursor.hasNext()) {
            return null;
          }
          MongoListRecord results = new MongoListRecord();
          while (cursor.hasNext()) {
            DBObject result = cursor.next();
            results.add(buildRecordFromDBObject(result));
          }
          return results;
        } finally {
          cursor.close();
        }

      } else {
        throw new ResourceException("Invalid operation: " + operation);
      }
    } catch (Exception exception) {
      ResourceException resourceException = new ResourceException(exception.toString());
      resourceException.initCause(exception);
      throw resourceException;
    }
    return null;
  }