Examples of DBCollection


Examples of com.mongodb.DBCollection

    /**
     * Returns the collection with a given name. This is a Mockito mock object, so verifications can be performed on
     * it.
     */
    public DBCollection getCollection(String collectionName) {
      DBCollection collection = collections.get( collectionName );
      return collection != null ? collection : defaultCollection;
    }
View Full Code Here

Examples of com.mongodb.DBCollection

    final String collectionName = "Drink";

    MongoDBDatastoreProvider provider = (MongoDBDatastoreProvider) this.getService( DatastoreProvider.class );

    DB database = provider.getDatabase();
    DBCollection collection = database.getCollection( collectionName );
    BasicDBObject water = new BasicDBObject();
    water.put( "_id", "1234" );
    water.put( "name", "Water" );
    water.put( "volume", "1L" );
    collection.insert( water );

    List<String> selectedColumns = new ArrayList<String>();
    selectedColumns.add( "name" );
    Tuple tuple = this.getTuple( collectionName, "1234", selectedColumns );

    assertNotNull( tuple );
    Set<String> retrievedColumn = tuple.getColumnNames();

    /*
     * The dialect will return all columns (which include _id field) so we have to substract 1 to check if
     * the right number of columns has been loaded.
     */
    assertEquals( selectedColumns.size(), retrievedColumn.size() - 1 );
    assertTrue( retrievedColumn.containsAll( selectedColumns ) );

    collection.remove( water );
  }
View Full Code Here

Examples of com.mongodb.DBCollection

   * an extra column is manually added to the association document
   */
  protected void addExtraColumn() {
    MongoDBDatastoreProvider provider = (MongoDBDatastoreProvider) this.getService( DatastoreProvider.class );
    DB database = provider.getDatabase();
    DBCollection collection = database.getCollection( "associations_Project_Module" );
    BasicDBObject query = new BasicDBObject( 1 );
    query.put( "_id", new BasicDBObject( "Project_id", "projectID" ) );

    BasicDBObject updater = new BasicDBObject( 1 );
    updater.put( "$push", new BasicDBObject( "extraColumn", 1 ) );
    collection.update( query, updater );
  }
View Full Code Here

Examples of com.mongodb.DBCollection

    }
  }

  @Override
  public void insertOrUpdateAssociation(AssociationKey key, Association association, AssociationContext associationContext) {
    DBCollection collection;
    DBObject query;
    MongoDBAssociationSnapshot assocSnapshot = (MongoDBAssociationSnapshot) association.getSnapshot();
    String associationField;

    AssociationStorageStrategy storageStrategy = getAssociationStorageStrategy( key, associationContext );
    WriteConcern writeConcern = getWriteConcern( associationContext );

    List<?> rows = getAssociationRows( association, key );
    Object toStore = key.getMetadata().isOneToOne() ? rows.get( 0 ) : rows;

    // We need to execute the previous operations first or it won't be able to find the key that should have
    // been created
    executeBatch( associationContext.getOperationsQueue() );
    if ( storageStrategy == AssociationStorageStrategy.IN_ENTITY ) {
      collection = this.getCollection( key.getEntityKey() );
      query = this.prepareIdObject( key.getEntityKey() );
      associationField = key.getMetadata().getCollectionRole();

      ( (MongoDBTupleSnapshot) associationContext.getEntityTuple().getSnapshot() ).getDbObject().put( key.getMetadata().getCollectionRole(), toStore );
    }
    else {
      collection = getAssociationCollection( key, storageStrategy );
      query = assocSnapshot.getQueryObject();
      associationField = ROWS_FIELDNAME;
    }

    DBObject update = new BasicDBObject( "$set", new BasicDBObject( associationField, toStore ) );

    collection.update( query, update, true, false, writeConcern );
  }
View Full Code Here

Examples of com.mongodb.DBCollection

        ( (MongoDBTupleSnapshot) associationContext.getEntityTuple().getSnapshot() ).getDbObject().removeField( key.getMetadata().getCollectionRole() );
        getCollection( key.getEntityKey() ).update( entity, updater, true, false, writeConcern );
      }
    }
    else {
      DBCollection collection = getAssociationCollection( key, storageStrategy );
      DBObject query = associationKeyToObject( key, storageStrategy );

      int nAffected = collection.remove( query, writeConcern ).getN();
      log.removedAssociation( nAffected );
    }
  }
View Full Code Here

Examples of com.mongodb.DBCollection

    }
  }

  @Override
  public Number nextValue(NextValueRequest request) {
    DBCollection currentCollection = getCollection( request.getKey().getTable() );
    DBObject query = this.prepareIdObject( request.getKey() );
    //all columns should match to find the value

    String valueColumnName = request.getKey().getMetadata().getValueColumnName();

    BasicDBObject update = new BasicDBObject();
    //FIXME how to set the initialValue if the document is not present? It seems the inc value is used as initial new value
    Integer incrementObject = Integer.valueOf( request.getIncrement() );
    this.addSubQuery( "$inc", update, valueColumnName, incrementObject );
    DBObject result = currentCollection.findAndModify( query, null, null, false, update, false, true );
    Object idFromDB;
    idFromDB = result == null ? null : result.get( valueColumnName );
    if ( idFromDB == null ) {
      //not inserted yet so we need to add initial value to increment to have the right next value in the DB
      //FIXME that means there is a small hole as when there was not value in the DB, we do add initial value in a non atomic way
      BasicDBObject updateForInitial = new BasicDBObject();
      this.addSubQuery( "$inc", updateForInitial, valueColumnName, request.getInitialValue() );
      currentCollection.findAndModify( query, null, null, false, updateForInitial, false, true );
      idFromDB = request.getInitialValue(); //first time we ask this value
    }
    else {
      idFromDB = result.get( valueColumnName );
    }
View Full Code Here

Examples of com.mongodb.DBCollection

  @Override
  public void forEachTuple(ModelConsumer consumer, EntityKeyMetadata... entityKeyMetadatas) {
    DB db = provider.getDatabase();
    for ( EntityKeyMetadata entityKeyMetadata : entityKeyMetadatas ) {
      DBCollection collection = db.getCollection( entityKeyMetadata.getTable() );
      for ( DBObject dbObject : collection.find() ) {
        consumer.consume( new Tuple( new MongoDBTupleSnapshot( dbObject, entityKeyMetadata, UPDATE ) ) );
      }
    }
  }
View Full Code Here

Examples of com.mongodb.DBCollection

  public ClosableIterator<Tuple> executeBackendQuery(BackendQuery<MongoDBQueryDescriptor> backendQuery, QueryParameters queryParameters) {
    MongoDBQueryDescriptor queryDescriptor = backendQuery.getQuery();

    EntityKeyMetadata entityKeyMetadata = backendQuery.getSingleEntityKeyMetadataOrNull();
    String collectionName = getCollectionName( backendQuery, queryDescriptor, entityKeyMetadata );
    DBCollection collection = provider.getDatabase().getCollection( collectionName );

    switch( queryDescriptor.getOperation() ) {
      case FIND:
        return doFind( queryDescriptor, queryParameters, collection, entityKeyMetadata );
      case COUNT:
View Full Code Here

Examples of com.mongodb.DBCollection

    }
  }

  private void executeBatchRemove(Map<DBCollection, BatchInsertionTask> inserts, RemoveTupleOperation tupleOperation) {
    EntityKey entityKey = tupleOperation.getEntityKey();
    DBCollection collection = getCollection( entityKey );
    BatchInsertionTask batchedInserts = inserts.get( collection );

    if ( batchedInserts != null && batchedInserts.containsKey( entityKey ) ) {
      batchedInserts.remove( entityKey );
    }
View Full Code Here

Examples of com.mongodb.DBCollection

  public ParameterMetadataBuilder getParameterMetadataBuilder() {
    return NoOpParameterMetadataBuilder.INSTANCE;
  }

  private void prepareForInsert(Map<DBCollection, BatchInsertionTask> inserts, MongoDBTupleSnapshot snapshot, EntityKey entityKey, Tuple tuple, WriteConcern writeConcern) {
    DBCollection collection = getCollection( entityKey );
    BatchInsertionTask batchInsertion = getOrCreateBatchInsertionTask( inserts, collection, writeConcern );
    DBObject document = getCurrentDocument( snapshot, batchInsertion, entityKey );
    DBObject newDocument = objectForInsert( tuple, document );
    inserts.get( collection ).put( entityKey, newDocument );
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.