Package com.mongodb

Examples of com.mongodb.DBObject


  }

  //not for embedded
  private DBObject findAssociation(AssociationKey key, AssociationContext associationContext, AssociationStorageStrategy storageStrategy) {
    ReadPreference readPreference = getReadPreference( associationContext );
    final DBObject associationKeyObject = associationKeyToObject( key, storageStrategy );

    return getAssociationCollection( key, storageStrategy ).findOne( associationKeyObject, getProjection( key, false ), readPreference );
  }
View Full Code Here


    // 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 ) {
      DBObject entity = getEmbeddingEntity( key, associationContext );

      if ( entity != null && hasField( entity, key.getMetadata().getCollectionRole() ) ) {
        return new Association( new MongoDBAssociationSnapshot( entity, key, storageStrategy ) );
      }
      else {
        return null;
      }
    }
    final DBObject result = findAssociation( key, associationContext, storageStrategy );
    if ( result == null ) {
      return null;
    }
    else {
      return new Association( new MongoDBAssociationSnapshot( result, key, storageStrategy ) );
View Full Code Here

  @Override
  public Association createAssociation(AssociationKey key, AssociationContext associationContext) {
    AssociationStorageStrategy storageStrategy = getAssociationStorageStrategy( key, associationContext );

    DBObject document = storageStrategy == AssociationStorageStrategy.IN_ENTITY
        ? getEmbeddingEntity( key, associationContext )
        : associationKeyToObject( key, storageStrategy );

    return new Association( new MongoDBAssociationSnapshot( document, key, storageStrategy ) );
  }
View Full Code Here

    if ( rowKeyColumnsToPersist.length == 1 ) {
      return row.get( rowKeyColumnsToPersist[0] );
    }
    // otherwise a DBObject with the row contents
    else {
      DBObject rowObject = new BasicDBObject( rowKeyColumnsToPersist.length );
      for ( String column : rowKeyColumnsToPersist ) {
        rowObject.put( column, row.get( column ) );
      }

      return rowObject;
    }
  }
View Full Code Here

  }

  @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

  public void removeAssociation(AssociationKey key, AssociationContext associationContext) {
    AssociationStorageStrategy storageStrategy = getAssociationStorageStrategy( key, associationContext );
    WriteConcern writeConcern = getWriteConcern( associationContext );

    if ( storageStrategy == AssociationStorageStrategy.IN_ENTITY ) {
      DBObject entity = this.prepareIdObject( key.getEntityKey() );
      if ( entity != null ) {
        BasicDBObject updater = new BasicDBObject();
        addSubQuery( "$unset", updater, key.getMetadata().getCollectionRole(), Integer.valueOf( 1 ) );
        ( (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

  }

  @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 );
    }
    if ( idFromDB.getClass().equals( Integer.class ) || idFromDB.getClass().equals( Long.class ) ) {
      Number id = (Number) idFromDB;
      //idFromDB is the one used and the BD contains the next available value to use
      return id;
View Full Code Here

    if ( storageStrategy == AssociationStorageStrategy.IN_ENTITY ) {
      throw new AssertionFailure( MongoHelpers.class.getName()
          + ".associationKeyToObject should not be called for associations embedded in entity documents");
    }
    Object[] columnValues = key.getColumnValues();
    DBObject columns = new BasicDBObject( columnValues.length );

    int i = 0;
    for ( String name : key.getColumnNames() ) {
      columns.put( name, columnValues[i++] );
    }

    BasicDBObject idObject = new BasicDBObject( 1 );

    if ( storageStrategy == AssociationStorageStrategy.GLOBAL_COLLECTION ) {
      columns.put( MongoDBDialect.TABLE_FIELDNAME, key.getTable() );
    }
    idObject.put( MongoDBDialect.ID_FIELDNAME, columns );
    return idObject;
  }
View Full Code Here

  }

  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

    DBObject newDocument = objectForInsert( tuple, document );
    inserts.get( collection ).put( entityKey, newDocument );
  }

  private DBObject getCurrentDocument(MongoDBTupleSnapshot snapshot, BatchInsertionTask batchInsert, EntityKey entityKey) {
    DBObject fromBatchInsertion = batchInsert.get( entityKey );
    return fromBatchInsertion != null ? fromBatchInsertion : snapshot.getDbObject();
  }
View Full Code Here

TOP

Related Classes of com.mongodb.DBObject

Copyright © 2018 www.massapicom. 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.