com.mongodb.MongoException Java Examples

The following examples show how to use com.mongodb.MongoException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MongoDbStore.java    From swellrt with Apache License 2.0 6 votes vote down vote up
@Override
public AttachmentMetadata getMetadata(AttachmentId attachmentId) throws IOException {
  GridFSFile metadataFile = getMetadataGridFSFile(attachmentId);

  if (metadataFile == null)
    return null;

  try (GridFSDownloadStream metadataStream = metadataGrid
      .openDownloadStream(metadataFile.getObjectId())) {

    if (metadataStream == null) {
      return null;
    }

    AttachmentProto.AttachmentMetadata protoMetadata = AttachmentProto.AttachmentMetadata
        .parseFrom(metadataStream);

    return new AttachmentMetadataProtoImpl(protoMetadata);

  } catch (MongoException e) {
    throw new IOException(e);
  }

}
 
Example #2
Source File: EntityRepositoryTest.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateRetryWithError() {
    Repository<Entity> mockRepo = Mockito.spy(repository);
    Map<String, Object> studentBody = buildTestStudentEntity();
    Map<String, Object> studentMetaData = new HashMap<String, Object>();
    int noOfRetries = 5;

    Mockito.doThrow(new MongoException("Test Exception"))
            .when(((MongoEntityRepository) mockRepo))
            .internalCreate("student", null, studentBody, studentMetaData, "student");
    Mockito.doCallRealMethod()
            .when(mockRepo)
            .createWithRetries("student", null, studentBody, studentMetaData, "student",
                    noOfRetries);

    try {
        mockRepo.createWithRetries("student", null, studentBody, studentMetaData, "student",
                noOfRetries);
    } catch (MongoException ex) {
        assertEquals(ex.getMessage(), "Test Exception");
    }

    Mockito.verify((MongoEntityRepository) mockRepo, Mockito.times(noOfRetries))
            .internalCreate("student", null, studentBody, studentMetaData, "student");
}
 
Example #3
Source File: MongoDatabaseRunCmd.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings( "rawtypes" )
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {
	MongoDatabase db	= getMongoDatabase( _session, argStruct );
	cfData	cmdData	= getNamedParam(argStruct, "cmd", null );
	if ( cmdData == null )
		throwException(_session, "please specify the cmd parameter");
	
	try{
		Document cmr;
		
		if ( cmdData.getDataType() == cfData.CFSTRUCTDATA )
			cmr	= db.runCommand( getDocument( (cfStructData) cmdData ) );
		else
			cmr	= db.runCommand( new Document( cmdData.getString(), true ) );
		
		return tagUtils.convertToCfData((Map)cmr);
	} catch (MongoException me){
		throwException(_session, me.getMessage());
		return null;
	}
}
 
Example #4
Source File: MongoWrapper.java    From MongoDb-Sink-Connector with Apache License 2.0 6 votes vote down vote up
private MongoClient createClient(AbstractConfig config, MongoClientOptions options) {
    String host = config.getString(MONGO_HOST);
    int port = config.getInt(MONGO_PORT);

    try {
        MongoClientOptions actualOptions;
        if (options != null) {
            actualOptions = options;
        } else {
            actualOptions = new MongoClientOptions.Builder().build();
        }
        ServerAddress server = new ServerAddress(host, port);
        if (credentials != null) {
            return new MongoClient(server, credentials, actualOptions);
        } else {
            return new MongoClient(server, actualOptions);
        }
    } catch (MongoException ex) {
        log.error("Failed to create MongoDB client to {}:{} with credentials {}", host, port,
                credentials, ex);
        throw new ConnectException("MongoDb client cannot be created.", ex);
    }
}
 
Example #5
Source File: EntityPersistHandler.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
boolean update(String collectionName, Entity entity, List<Entity> failed, AbstractMessageReport report,
        ReportStats reportStats, Source nrSource) {
    boolean res = false;

    try {
        res = entityRepository.updateWithRetries(collectionName, entity, totalRetries);
        if (!res) {
            failed.add(entity);
        }
    } catch (MongoException e) {
        NestedRuntimeException wrapper = new NestedRuntimeException("Mongo Exception", e) {
            private static final long serialVersionUID = 1L;
        };
        reportWarnings(wrapper.getMostSpecificCause().getMessage(), collectionName,
                ((SimpleEntity) entity).getSourceFile(), report, reportStats, nrSource);
    }

    return res;
}
 
Example #6
Source File: MongoEvents.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Override
protected void doProcess(JCas jCas) throws AnalysisEngineProcessException {

  String documentId = ConsumerUtils.getExternalId(getDocumentAnnotation(jCas), contentHashAsId);

  // Delete any existing content in the database
  deleteAllContent(documentId);

  // Save
  try {
    saveEvents(documentId, jCas, textClass);
  } catch (MongoException | BsonSerializationException e) {
    getMonitor()
        .error(
            "Unable to persist relations to database - document {} will contain no relations",
            getDocumentAnnotation(jCas).getSourceUri(),
            e);
  }
}
 
Example #7
Source File: ProfilingWriter.java    From mongodb-slow-operations-profiler with GNU Affero General Public License v3.0 6 votes vote down vote up
private void init(MongoDbAccessor mongo) {
    LOG.info(">>> init");

    try {
        final MongoCollection<Document> profileCollection = getProfileCollection(mongo);

        IndexOptions indexOptions = new IndexOptions();
        indexOptions.background(true);
        LOG.info("Create index {ts:-1, lbl:1} in the background if it does not yet exists");
        profileCollection.createIndex(new BasicDBObject("ts",-1).append("lbl", 1), indexOptions);
        LOG.info("Create index {adr:1, db:1, ts:-1} in the background if it does not yet exists");
        profileCollection.createIndex(new BasicDBObject("adr",1).append("db",1).append("ts", -1), indexOptions);
        ApplicationStatusDto.addWebLog("ProfilingWriter is successfully connected to its collector database.");
    } catch (MongoException e) {
        LOG.error("Exception while connecting to: {}", serverDto.getHosts(), e);
        ApplicationStatusDto.addWebLog("ProfilingWriter could not connect to its collector database.");
    }
    
    LOG.info("<<< init");
}
 
Example #8
Source File: SimpleMongoDBStorageStrategyTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeSerializeStatementToDocument() throws RyaDAOException, MongoException, IOException {
    RyaStatement statement = storageStrategy.deserializeDocument(TEST_DOC);
    /*
     * Since RyaStatement creates a timestamp using JVM time if the timestamp is null, we want to re-null it
     * for this test.  Timestamp is created at insert time by the Server, this test
     * can be found in the RyaDAO.
     */
    statement.setTimestamp(null);
    assertEquals(testStatement, statement);

    statement = storageStrategy.deserializeDocument(TEST_DOC_2);
    /*
     * Since RyaStatement creates a timestamp using JVM time if the timestamp is null, we want to re-null it
     * for this test.  Timestamp is created at insert time by the Server, this test
     * can be found in the RyaDAO.
     */
    statement.setTimestamp(null);
    assertEquals(testStatement2, statement);
}
 
Example #9
Source File: MongoDBRyaDAO2IT.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testAdd() throws RyaDAOException, MongoException, IOException {
    final MongoDBRyaDAO dao = new MongoDBRyaDAO();
    try {
        dao.setConf(conf);
        dao.init();

        final RyaStatementBuilder builder = new RyaStatementBuilder();
        builder.setPredicate(new RyaIRI("http://temp.com"));
        builder.setSubject(new RyaIRI("http://subject.com"));
        builder.setObject(new RyaIRI("http://object.com"));

        final MongoDatabase db = conf.getMongoClient().getDatabase(conf.get(MongoDBRdfConfiguration.MONGO_DB_NAME));
        final MongoCollection<Document> coll = db.getCollection(conf.getTriplesCollectionName());

        dao.add(builder.build());

        assertEquals(coll.countDocuments(), 1);
    }  finally {
        dao.destroy();
    }
}
 
Example #10
Source File: MongoDBRyaDAO2IT.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testDelete() throws RyaDAOException, MongoException, IOException {
    final MongoDBRyaDAO dao = new MongoDBRyaDAO();
    try {
        dao.setConf(conf);
        dao.init();

        final RyaStatementBuilder builder = new RyaStatementBuilder();
        builder.setPredicate(new RyaIRI("http://temp.com"));
        builder.setSubject(new RyaIRI("http://subject.com"));
        builder.setObject(new RyaIRI("http://object.com"));
        final RyaStatement statement = builder.build();

        final MongoDatabase db = conf.getMongoClient().getDatabase(conf.get(MongoDBRdfConfiguration.MONGO_DB_NAME));
        final MongoCollection<Document> coll = db.getCollection(conf.getTriplesCollectionName());

        dao.add(statement);
        assertEquals(coll.countDocuments(), 1);

        dao.delete(statement, conf);
        assertEquals(coll.countDocuments(), 0);
    } finally {
        dao.destroy();
    }
}
 
Example #11
Source File: MongoTypeStorage.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<Type> get(final RyaIRI typeId) throws TypeStorageException {
    requireNonNull(typeId);

    try {
        final Document document = mongo.getDatabase(ryaInstanceName)
            .getCollection(COLLECTION_NAME)
            .find( makeIdFilter(typeId) )
            .first();

        return document == null ?
                Optional.empty() :
                Optional.of( TYPE_CONVERTER.fromDocument(document) );

    } catch(final MongoException | DocumentConverterException e) {
        throw new TypeStorageException("Could not get the Type with ID '" + typeId.getData() + "'.", e);
    }
}
 
Example #12
Source File: MongoDAL.java    From uncode-dal-all with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int _countByCriteria(Table table) {
	int count = 0;
	Map<String, Object> coditon = new HashMap<String, Object>();
	try {
		QueryCriteria queryCriteria = table.getQueryCriteria();
		DB db = database.getDB();
		Jongo jongo = new Jongo(db);
		for(Criteria criteria:queryCriteria.getOredCriteria()){
			for(Criterion criterion:criteria.getAllCriteria()){
				coditon = buildCriteria(criterion, coditon);
			}
		}
		long size = jongo.getCollection(queryCriteria.getTable()).count(JsonUtils.objToJson(coditon));
		count = (int) size;
	} catch (MongoException e) {
		LOG.error("mongo find error", e);
	}
	return count;
}
 
Example #13
Source File: ProfilingWriter.java    From mongodb-slow-operations-profiler with GNU Affero General Public License v3.0 6 votes vote down vote up
public ProfilingWriter(BlockingQueue<ProfilingEntry> jobQueue) {
    this.jobQueue = jobQueue;
    serverDto = ConfigReader.getCollectorServer();
    runningSince = new Date();

    final MongoDbAccessor mongo = getMongoDbAccessor();
    try {
        final MongoCollection<Document> profileCollection = getProfileCollection(mongo);

        IndexOptions indexOptions = new IndexOptions();
        indexOptions.background(true);
        LOG.info("Create index {ts:-1, lbl:1} in the background if it does not yet exists");
        profileCollection.createIndex(new BasicDBObject("ts",-1).append("lbl", 1), indexOptions);
        LOG.info("Create index {adr:1, db:1, ts:-1} in the background if it does not yet exists");
        profileCollection.createIndex(new BasicDBObject("adr",1).append("db",1).append("ts", -1), indexOptions);

        LOG.info("ProfilingWriter is ready at {}", serverDto.getHosts());

    } catch (MongoException e) {
        LOG.error("Exception while connecting to: {}", serverDto.getHosts(), e);
    }
}
 
Example #14
Source File: Mongo3DAL.java    From uncode-dal-all with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int _countByCriteria(Table table) {
	int count = 0;
	Map<String, Object> coditon = new HashMap<String, Object>();
	try {
		QueryCriteria queryCriteria = table.getQueryCriteria();
		com.mongodb.client.MongoDatabase db = database.getMongoDB();
		for(Criteria criteria:queryCriteria.getOredCriteria()){
			for(Criterion criterion:criteria.getAllCriteria()){
				coditon = buildCriteria(criterion, coditon);
			}
		}
		long size = db.getCollection(queryCriteria.getTable()).count(Document.parse((JSON.serialize(coditon))));
		LOG.debug("countByCriteria->collection:"+queryCriteria.getTable()+",script:"+JSON.serialize(coditon));
		count = (int) size;
	} catch (MongoException e) {
		LOG.error("mongo find error", e);
	}
	LOG.debug("_countByCriteria->result:"+count);
	return count;
}
 
Example #15
Source File: MongoCollectionRename.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {
	MongoDatabase	db	= getMongoDatabase( _session, argStruct );
	
	String collection	= getNamedStringParam(argStruct, "collection", null);
	if ( collection == null )
		throwException(_session, "please specify a collection");
	
	String name	= getNamedStringParam(argStruct, "name", null);
	if ( name == null )
		throwException(_session, "please specify a name");
	
	try{
		
		db
			.getCollection( collection )
			.renameCollection( new MongoNamespace( db.getName(), name ), new RenameCollectionOptions().dropTarget( getNamedBooleanParam(argStruct, "droptarget", false ) ) );

		return cfBooleanData.TRUE;
		
	} catch (MongoException me){
		throwException(_session, me.getMessage());
		return null;
	}
}
 
Example #16
Source File: MongoDatabaseList.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
public cfData execute( cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {
	MongoClient client = getMongoClient( _session, argStruct );

	try {
		cfArrayData arr = cfArrayData.createArray( 1 );

		client.listDatabaseNames().forEach( new Block<String>() {

			@Override
			public void apply( final String st ) {
				try {
					arr.addElement( new cfStringData( st ) );
				} catch ( cfmRunTimeException e ) {}
			}
		} );

		return arr;
	} catch ( MongoException me ) {
		throwException( _session, me.getMessage() );
		return null;
	}
}
 
Example #17
Source File: MongoCommander.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("boxing")
public static String ensureIndexes(MongoIndex index, DB dbConn, int indexOrder) {
    DBObject options = new BasicDBObject();
    options.put("name", "idx_" + indexOrder);
    options.put("unique", index.isUnique());
    options.put("sparse", index.isSparse());
    options.put("ns", dbConn.getCollection(index.getCollection()).getFullName());

    try {
        dbConn.getCollection(index.getCollection()).createIndex(new BasicDBObject(index.getKeys()), options);
        return null;
    } catch (MongoException e) {
        LOG.error("Failed to ensure index:{}", e.getMessage());
        return "Failed to ensure index:" + e.getMessage();
    }
}
 
Example #18
Source File: Mongo3DAL.java    From uncode-dal-all with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int _updateByCriteria(Table table) {
	Map<String, Object> coditon = new HashMap<String, Object>();
	try {
        QueryCriteria queryCriteria = table.getQueryCriteria();
        com.mongodb.client.MongoDatabase db = database.getMongoDB();
		for(Criteria criteria:queryCriteria.getOredCriteria()){
			for(Criterion criterion:criteria.getAllCriteria()){
				coditon = buildCriteria(criterion, coditon);
			}
		}
		Map<String, Object> vaule = new HashMap<String, Object>();
		vaule.put("$set", table.getParams());
		db.getCollection(queryCriteria.getTable()).updateMany(Document.parse((JSON.serialize(coditon))), Document.parse(JSON.serialize(vaule)));
		LOG.debug("updateByCriteria->collection:"+table.getTableName()+",value:"+JSON.serialize(vaule)+",condition:"+JSON.serialize(coditon));
	} catch (MongoException e) {
		LOG.error("mongo update error", e);
	}
	return 1;
}
 
Example #19
Source File: MongoCollectionIndexDrop.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {
	MongoDatabase	db	= getMongoDatabase( _session, argStruct );
	
	String collection	= getNamedStringParam(argStruct, "collection", null);
	if ( collection == null )
		throwException(_session, "please specify a collection");
	
	String index	= getNamedStringParam(argStruct, "index", null );
	
	try{
		if ( index != null )
			db.getCollection(collection).dropIndex(index);
		else
			db.getCollection(collection).dropIndexes();

		return cfBooleanData.TRUE;
	} catch (MongoException me){
		throwException(_session, me.getMessage());
		return null;
	}
}
 
Example #20
Source File: MongoDBRyaDAOIT.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testDelete() throws RyaDAOException, MongoException, IOException {
    final MongoDBRyaDAO dao = new MongoDBRyaDAO();
    try {
        dao.setConf(conf);
        dao.init();

        final RyaStatementBuilder builder = new RyaStatementBuilder();
        builder.setPredicate(new RyaIRI("http://temp.com"));
        builder.setSubject(new RyaIRI("http://subject.com"));
        builder.setObject(new RyaIRI("http://object.com"));
        builder.setColumnVisibility(new DocumentVisibility("C").flatten());
        final RyaStatement statement = builder.build();
        final MongoDatabase db = conf.getMongoClient().getDatabase(conf.get(MongoDBRdfConfiguration.MONGO_DB_NAME));
        final MongoCollection<Document> coll = db.getCollection(conf.getTriplesCollectionName());

        dao.add(statement);
        assertEquals(1, coll.countDocuments());

        dao.delete(statement, conf);
        assertEquals(0, coll.countDocuments());
    } finally {
        dao.destroy();
    }
}
 
Example #21
Source File: MongoDbOutput.java    From pentaho-mongodb-plugin with Apache License 2.0 5 votes vote down vote up
protected WriteResult batchRetryUsingSave( boolean lastRetry )
  throws MongoException, KettleException, MongoDbException {
  WriteResult result = null;
  int count = 0;
  logBasic( BaseMessages.getString( PKG, "MongoDbOutput.Messages.CurrentBatchSize", m_batch.size() ) );
  for ( int i = 0, len = m_batch.size(); i < len; i++ ) {
    DBObject toTry = m_batch.get( i );
    Object[] correspondingRow = m_batchRows.get( i );
    try {
      result = m_data.getCollection().save( toTry );
      count++;
    } catch ( MongoException ex ) {
      if ( !lastRetry ) {
        logBasic( BaseMessages.getString( PKG, "MongoDbOutput.Messages.SuccessfullySavedXDocuments", count ) );
        m_batch = copyExceptFirst( count, m_batch );
        m_batchRows = copyExceptFirst( count, m_batchRows );
        throw ex;
      }

      // Send this one to the error stream if doing error handling
      if ( getStepMeta().isDoingErrorHandling() ) {
        putError( getInputRowMeta(), correspondingRow, 1, ex.getMessage(), "", "MongoDbOutput" );
      } else {
        m_batch = copyExceptFirst( i + 1, m_batch );
        m_batchRows = copyExceptFirst( i + 1, m_batchRows );
        throw ex;
      }
    }
  }

  m_batch.clear();
  m_batchRows.clear();

  logBasic( BaseMessages.getString( PKG, "MongoDbOutput.Messages.SuccessfullySavedXDocuments", count ) );

  return result;
}
 
Example #22
Source File: MongoTypeStorage.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void create(final Type type) throws TypeStorageException {
    requireNonNull(type);

    try {
        mongo.getDatabase(ryaInstanceName)
            .getCollection(COLLECTION_NAME)
            .insertOne(TYPE_CONVERTER.toDocument(type));

    } catch(final MongoException e) {
        throw new TypeStorageException("Failed to create Type with ID '" + type.getId().getData() + "'.", e);
    }
}
 
Example #23
Source File: MongoDbSinkTask.java    From kafka-connect-mongodb with Apache License 2.0 5 votes vote down vote up
private void processSinkRecords(MongoCollection<BsonDocument> collection, List<SinkRecord> batch) {
    String collectionName = collection.getNamespace().getCollectionName();
    List<? extends WriteModel<BsonDocument>> docsToWrite =
            sinkConfig.isUsingCdcHandler(collectionName)
                    ? buildWriteModelCDC(batch,collectionName)
                    : buildWriteModel(batch,collectionName);
    try {
        if (!docsToWrite.isEmpty()) {
            LOGGER.debug("bulk writing {} document(s) into collection [{}]",
                    docsToWrite.size(), collection.getNamespace().getFullName());
            BulkWriteResult result = collection.bulkWrite(
                    docsToWrite, BULK_WRITE_OPTIONS);
            LOGGER.debug("mongodb bulk write result: " + result.toString());
        }
    } catch (MongoException mexc) {
        if (mexc instanceof BulkWriteException) {
            BulkWriteException bwe = (BulkWriteException) mexc;
            LOGGER.error("mongodb bulk write (partially) failed", bwe);
            LOGGER.error(bwe.getWriteResult().toString());
            LOGGER.error(bwe.getWriteErrors().toString());
            LOGGER.error(bwe.getWriteConcernError().toString());
        } else {
            LOGGER.error("error on mongodb operation", mexc);
            LOGGER.error("writing {} document(s) into collection [{}] failed -> remaining retries ({})",
                    docsToWrite.size(), collection.getNamespace().getFullName() ,remainingRetries);
        }
        if (remainingRetries-- <= 0) {
            throw new ConnectException("failed to write mongodb documents"
                    + " despite retrying -> GIVING UP! :( :( :(", mexc);
        }
        LOGGER.debug("deferring retry operation for {}ms", deferRetryMs);
        context.timeout(deferRetryMs);
        throw new RetriableException(mexc.getMessage(), mexc);
    }
}
 
Example #24
Source File: MongoTypeStorage.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public boolean delete(final RyaIRI typeId) throws TypeStorageException {
    requireNonNull(typeId);

    try {
        final Document deleted = mongo.getDatabase(ryaInstanceName)
            .getCollection(COLLECTION_NAME)
            .findOneAndDelete( makeIdFilter(typeId) );

        return deleted != null;

    } catch(final MongoException e) {
        throw new TypeStorageException("Could not delete the Type with ID '" + typeId.getData() + "'.", e);
    }
}
 
Example #25
Source File: DamnYouServiceImpl.java    From biliob_backend with MIT License 5 votes vote down vote up
@Async
@Override
public void saveData(ZipInputStream zipInputStream, ZipFile zipFile) throws MongoException, IOException {
    ZipEntry zipEntry;
    while ((zipEntry = zipInputStream.getNextEntry()) != null) {
        if (zipEntry.toString().endsWith("txt")) {
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(zipFile.getInputStream(zipEntry)));
            saveHistoryDataFromTxt(br);
            br.close();

        }
    }
}
 
Example #26
Source File: MongoGetInstanceDetailsIT.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void getDetails_instanceDoesNotHaveDetails() throws MongoException, TableExistsException, RyaClientException {
    // Mimic a pre-details rya install.
    final String instanceName = "instance_name";

    getMongoClient().getDatabase(instanceName).createCollection("rya_triples");

    // Verify that the operation returns empty.
    final RyaClient ryaClient = MongoRyaClientFactory.build(getConnectionDetails(), getMongoClient());
    final GetInstanceDetails getInstanceDetails = ryaClient.getGetInstanceDetails();
    final Optional<RyaDetails> details = getInstanceDetails.getDetails(instanceName);
    assertFalse( details.isPresent() );
}
 
Example #27
Source File: MongoDAL.java    From uncode-dal-all with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int _insert(Table table) {
	try {
		DB db = database.getDB();
		Jongo jongo = new Jongo(db);
		ObjectId newOid = ObjectId.get();
		table.getParams().put("_id", newOid);
		jongo.getCollection(table.getTableName()).save(table.getParams());
		table.getParams().put("id", newOid.toString());
	} catch (MongoException e) {
		LOG.error("mongo insert error", e);
	}
	return 1;
}
 
Example #28
Source File: MongoDbDeltaStore.java    From swellrt with Apache License 2.0 5 votes vote down vote up
@Override
public ImmutableSet<WaveletId> lookup(WaveId waveId) throws PersistenceException {


  BasicDBObject query = new BasicDBObject();
  query.put(MongoDbDeltaStoreUtil.FIELD_WAVE_ID, waveId.serialise());

  BasicDBObject projection = new BasicDBObject();
  projection.put(MongoDbDeltaStoreUtil.FIELD_WAVELET_ID, 1);


  final ImmutableSet.Builder<WaveletId> builder = ImmutableSet.builder();
  try {
    deltasCollection.find(query).projection(projection).forEach(new Block<BasicDBObject>() {

      @Override
      public void apply(BasicDBObject t) {
        builder
            .add(WaveletId.deserialise((String) t.get(MongoDbDeltaStoreUtil.FIELD_WAVELET_ID)));
      }

    });
  } catch (MongoException e) {
    throw new PersistenceException(e);
  }

  return builder.build();
}
 
Example #29
Source File: MongoDAL.java    From uncode-dal-all with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int _deleteByCriteria(Table table) {
	Map<String, Object> coditon = new HashMap<String, Object>();
	try {
        QueryCriteria queryCriteria = table.getQueryCriteria();
        DB db = database.getDB();
        Jongo jongo = new Jongo(db);
		for(Criteria criteria:queryCriteria.getOredCriteria()){
			for(Criterion criterion:criteria.getAllCriteria()){
				coditon = buildCriteria(criterion, coditon);
			}
		}
		Find find = jongo.getCollection(queryCriteria.getTable()).find(JsonUtils.objToJson(coditon));
	    Iterator<Map> iterator = find.as(Map.class).iterator();  
		while (iterator.hasNext()) {
			Map<String, Object> map = iterator.next();
			if (null != map) {
				if(map.containsKey("_id")){
					jongo.getCollection(table.getTableName()).remove(new ObjectId(String.valueOf(map.get("_id"))));
				}
			}
		}
	} catch (MongoException e) {
		LOG.error("mongo delete error", e);
	}
	return 1;
}
 
Example #30
Source File: MongoDbSmartUri.java    From rya with Apache License 2.0 5 votes vote down vote up
private void checkInit() throws SmartUriException {
    if (!isInit) {
        try {
            setupClient(conf);
        } catch (final UnknownHostException | MongoException | EntityStorageException e) {
            throw new SmartUriException("Failed to setup MongoDB client", e);
        }
    }
}