Example usage for com.mongodb WriteConcern ACKNOWLEDGED

List of usage examples for com.mongodb WriteConcern ACKNOWLEDGED

Introduction

In this page you can find the example usage for com.mongodb WriteConcern ACKNOWLEDGED.

Prototype

WriteConcern ACKNOWLEDGED

To view the source code for com.mongodb WriteConcern ACKNOWLEDGED.

Click Source Link

Document

Write operations that use this write concern will wait for acknowledgement, using the default write concern configured on the server.

Usage

From source file:com.ibm.sae.LoadDreamHomeDB.java

/***************************************************************/
 private static void loadNotification(DB db, DBCollection coll) {
     System.out.println("LoadDreamHomeDB:loadNotification begins");

     coll.drop();/* w w w  . jav  a2 s  . com*/

     DBObject notificationDoc = new BasicDBObject("notificationId", 4999).append("agentId", 1000)
             .append("clientId", 4000);
     WriteResult wr = coll.insert(notificationDoc, WriteConcern.ACKNOWLEDGED);

     System.out.println("LoadDreamHomeDB:loadNotification ends");
 }

From source file:com.ibm.sae.LoadDreamHomeDB.java

/***************************************************************/
 private static void loadOffice(DB db, DBCollection coll) {
     System.out.println("LoadDreamHomeDB:loadOffice begins");

     coll.drop();/*from ww  w . ja v a2  s .co  m*/

     DBObject officeDoc = new BasicDBObject("officeId", 2999).append("officeName", "Valley North")
             .append("officeManager", "Erlich Bachman")
             .append("officeAddr", new BasicDBObject("address", "223 Mountain Drive")
                     .append("city", "Buena Vista").append("state", "California").append("zip", "65746"))
             .append("numProperties", 0);
     WriteResult wr = coll.insert(officeDoc, WriteConcern.ACKNOWLEDGED);

     System.out.println("LoadDreamHomeDB:loadOffice ends");
 }

From source file:com.ibm.sae.LoadDreamHomeDB.java

/***************************************************************/
 private static void loadProperty(DB db, DBCollection coll) {
     System.out.println("LoadDreamHomeDB:loadOffice begins");

     coll.drop();/*from  ww  w .  j av  a  2s  . c  om*/

     DBObject propertyDoc = new BasicDBObject("propertyId", 2000)
             .append("location",
                     new BasicDBObject("address", "1024 College").append("city", "Wheaton")
                             .append("state", "California").append("zip", "76857")
                             .append("longitude", "35.601623").append("latitude", "-78.245908"))
             .append("sqFeet", 2895).append("numBeds", 4).append("numBaths", 3)
             .append("description", "Two blocks from university").append("askingPrice", 789900.00);
     WriteResult wr = coll.insert(propertyDoc, WriteConcern.ACKNOWLEDGED);

     DBObject propertyDoc2 = new BasicDBObject("propertyId", 2001)
             .append("location",
                     new BasicDBObject("address", "435 Weston").append("city", "Springfield")
                             .append("state", "California").append("zip", "76857")
                             .append("longitude", "36.507623").append("latitude", "-79.145509"))
             .append("sqFeet", 3200).append("numBeds", 5).append("numBaths", 3)
             .append("description", "Nice cottage by lake").append("askingPrice", 569900.00);
     WriteResult wr2 = coll.insert(propertyDoc2, WriteConcern.ACKNOWLEDGED);

     DBObject propertyDoc3 = new BasicDBObject("propertyId", 2002)
             .append("location",
                     new BasicDBObject("address", "2240 Berlin").append("city", "Florence")
                             .append("state", "California").append("zip", "76857")
                             .append("longitude", "31.086579").append("latitude", "-72.357987"))
             .append("sqFeet", 3950).append("numBeds", 5).append("numBaths", 5)
             .append("description", "Mansion in the city").append("askingPrice", 645800.00);
     WriteResult wr3 = coll.insert(propertyDoc3, WriteConcern.ACKNOWLEDGED);

     System.out.println("LoadDreamHomeDB:loadProperty ends");
 }

From source file:com.ikanow.infinit.e.data_model.store.MongoDbManager.java

License:Apache License

@SuppressWarnings("deprecation")
public static void main(String[] args) throws UnknownHostException {
    MongoClient mc = new MongoClient(args[0]);
    long tnow = 0;
    DB db = mc.getDB("test");
    DBCollection test = db.getCollection("test123");
    BasicDBObject outObj = new BasicDBObject();
    int ITS = 1000;
    test.drop();/*w w w.  ja va  2 s.c  o  m*/

    boolean checkPerformance = false;
    boolean checkFunctionality = false;
    boolean checkErrors = false;

    // 1] Performance

    if (checkPerformance) {

        // ack'd
        db.setWriteConcern(WriteConcern.ACKNOWLEDGED);
        test.drop();
        tnow = new Date().getTime();
        for (int i = 0; i < ITS; ++i) {
            outObj.remove("_id");
            outObj.put("val", i);
            test.save(outObj);
        }
        tnow = new Date().getTime() - tnow;
        System.out.println("1: Ack'd: " + tnow);

        // un ack'd
        db.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
        test.drop();
        tnow = new Date().getTime();
        outObj = new BasicDBObject();
        for (int i = 0; i < ITS; ++i) {
            outObj.remove("_id");
            outObj.put("val", i);
            test.save(outObj);
        }
        tnow = new Date().getTime() - tnow;
        System.out.println("2: unAck'd: " + tnow);

        // un ack'd but call getLastError
        db.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
        test.drop();
        tnow = new Date().getTime();
        outObj = new BasicDBObject();
        for (int i = 0; i < ITS; ++i) {
            outObj.remove("_id");
            outObj.put("val", i);
            test.save(outObj);
            db.getLastError();
        }
        tnow = new Date().getTime() - tnow;
        test.drop();
        System.out.println("3: unAck'd but GLEd: " + tnow);

        // ack'd override
        db.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
        test.drop();
        tnow = new Date().getTime();
        outObj = new BasicDBObject();
        for (int i = 0; i < ITS; ++i) {
            outObj.remove("_id");
            outObj.put("val", i);
            test.save(outObj, WriteConcern.ACKNOWLEDGED);
            db.getLastError();
        }
        tnow = new Date().getTime() - tnow;
        System.out.println("4: unAck'd but ACKd: " + tnow);

        // Performance Results:
        // 2.6) (unack'd 100ms ... ack'd 27000)
        // 2.4) (same)
    }

    // 2] Functionality

    if (checkFunctionality) {

        // Unack:
        db.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
        WriteResult wr = test.update(new BasicDBObject(),
                new BasicDBObject(DbManager.set_, new BasicDBObject("val2", "x")), false, true);
        CommandResult cr = db.getLastError();
        System.out.println("UNACK: wr: " + wr);
        System.out.println("UNACK: cr: " + cr);

        // bonus, check that we get N==0 when insert dup object
        WriteResult wr2 = test.insert(outObj);
        System.out.println("ACK wr2 = " + wr2.getN() + " all = " + wr2);
        CommandResult cr2 = db.getLastError();
        System.out.println("ACK cr2 = " + cr2);

        // Ack1:
        db.setWriteConcern(WriteConcern.ACKNOWLEDGED);
        wr = test.update(new BasicDBObject(), new BasicDBObject(DbManager.set_, new BasicDBObject("val3", "x")),
                false, true);
        cr = db.getLastError();
        System.out.println("ACK1: wr: " + wr);
        System.out.println("ACK1: cr: " + cr);

        // Ack2:
        db.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
        wr = test.update(new BasicDBObject(), new BasicDBObject(DbManager.set_, new BasicDBObject("val4", "x")),
                false, true, WriteConcern.ACKNOWLEDGED);
        cr = db.getLastError();
        System.out.println("ACK2: wr: " + wr);
        System.out.println("ACK2: cr: " + cr);

        // bonus, check that we get N==0 when insert dup object
        wr2 = test.insert(outObj);
        System.out.println("ACK wr2 = " + wr2.getN() + " all = " + wr2);

        // Functionality results:
        // 2.6: unack wr == N/A, otherwise both have "n", "ok"
        // 2.4: unack wr == N/A all other wrs + crs identical 
    }

    if (checkErrors) {

        //set up sharding
        DbManager.getDB("admin").command(new BasicDBObject("enablesharding", "test"));
        // Ack:
        try {
            test.drop();
            test.createIndex(new BasicDBObject("key", 1));
            BasicDBObject command1 = new BasicDBObject("shardcollection", "test.test123");
            command1.append("key", new BasicDBObject("key", 1));
            DbManager.getDB("admin").command(command1);

            db.setWriteConcern(WriteConcern.ACKNOWLEDGED);
            outObj = new BasicDBObject("key", "test");
            test.save(outObj);
            WriteResult wr = test.update(new BasicDBObject(),
                    new BasicDBObject(DbManager.set_, new BasicDBObject("key", "test2")));
            System.out.println("ACK wr = " + wr);
        } catch (Exception e) {
            System.out.println("ACK err = " + e.toString());
        }

        // UnAck:
        try {
            test.drop();
            test.createIndex(new BasicDBObject("key", 1));
            BasicDBObject command1 = new BasicDBObject("shardcollection", "test.test123");
            command1.append("key", new BasicDBObject("key", 1));
            DbManager.getDB("admin").command(command1);

            db.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
            outObj = new BasicDBObject("key", "test");
            test.save(outObj);
            WriteResult wr = test.update(new BasicDBObject(),
                    new BasicDBObject(DbManager.set_, new BasicDBObject("key", "test2")), false, false,
                    WriteConcern.ACKNOWLEDGED);
            System.out.println("ACK override wr = " + wr);
        } catch (Exception e) {
            System.out.println("ACK override  err = " + e.toString());
        }

        // UnAck:
        try {
            test.drop();
            test.createIndex(new BasicDBObject("key", 1));
            BasicDBObject command1 = new BasicDBObject("shardcollection", "test.test123");
            command1.append("key", new BasicDBObject("key", 1));
            DbManager.getDB("admin").command(command1);

            db.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
            outObj = new BasicDBObject("key", "test");
            test.save(outObj);
            WriteResult wr = test.update(new BasicDBObject(),
                    new BasicDBObject(DbManager.set_, new BasicDBObject("key", "test2")));
            System.out.println("UNACK wr = " + wr);
        } catch (Exception e) {
            System.out.println("UNACK err = " + e.toString());
        }

        // UnAck + GLE:
        try {
            test.drop();
            test.createIndex(new BasicDBObject("key", 1));
            BasicDBObject command1 = new BasicDBObject("shardcollection", "test.test123");
            command1.append("key", new BasicDBObject("key", 1));
            DbManager.getDB("admin").command(command1);

            db.setWriteConcern(WriteConcern.UNACKNOWLEDGED);
            outObj = new BasicDBObject("key", "test");
            test.save(outObj);
            WriteResult wr = test.update(new BasicDBObject(),
                    new BasicDBObject(DbManager.set_, new BasicDBObject("key", "test2")));
            CommandResult cr = db.getLastError();
            System.out.println("UNACK GLE wr = " + wr);
            System.out.println("UNACK GLE cr = " + cr);
        } catch (Exception e) {
            System.out.println("UNACK GLE err = " + e.toString());
        }

        // Error handling:

        // 2.6:
        // Ack - exception
        // Ack override - exception
        // UnAck - no error given
        // UnAck + GLE  - gle error

        // 2.4:
        // Ack - exception
        // Ack override - exception
        // UnAck - no error given
        // UnAck + GLE  - gle error

    }
}

From source file:com.ikanow.infinit.e.processing.custom.scheduler.CustomScheduleManager.java

License:Open Source License

public static void createOrUpdatedSavedQueryCache() {
    if (null == _appLock) {
        _appLock = MongoApplicationLock.getLock(DbManager.getCustom().getSavedQueryCache().getDB().getName());
    }//  w  ww  . j a v a 2 s.c  om
    // the built-in applock acquisition code requires more persistent threads so we use the alternate mechanism that
    // just wipes out anything that hasn't been used in the last 2 minutes and then uses the existing contention handling to
    // allow one thread to grab it
    _appLock.clearStaleLocksOnTime(120);
    if (_appLock.acquire(100)) {
        BasicDBObject query = new BasicDBObject(SharePojo.type_, DocumentQueueControlPojo.SavedQueryQueue);
        List<SharePojo> savedQueries = SharePojo.listFromDb(DbManager.getSocial().getShare().find(query),
                SharePojo.listType());
        if (null != savedQueries) {
            for (SharePojo savedQueryShare : savedQueries) {
                if (null != savedQueryShare.getShare()) {
                    DocumentQueueControlPojo savedQuery = DocumentQueueControlPojo
                            .fromApi(savedQueryShare.getShare(), DocumentQueueControlPojo.class);

                    // Is this query well formed?
                    if ((null != savedQuery.getQueryInfo()) && ((null != savedQuery.getQueryInfo().getQuery())
                            || (null != savedQuery.getQueryInfo().getQueryId()))) {
                        Date now = new Date();
                        long freqOffset;
                        // Check if it's time to run the query
                        if (savedQuery.getQueryInfo()
                                .getFrequency() == DocumentQueueControlPojo.SavedQueryInfo.DocQueueFrequency.Hourly) {
                            freqOffset = 3600L * 1000L;
                            //(nothing to do here, just run whenever)
                        } //TESTED (test3)
                        else if (savedQuery.getQueryInfo()
                                .getFrequency() == DocumentQueueControlPojo.SavedQueryInfo.DocQueueFrequency.Daily) {
                            if (null != savedQuery.getQueryInfo().getFrequencyOffset()) { // hour of day
                                freqOffset = 12L * 3600L * 1000L; // (already check vs hour of day so be more relaxed) 
                                Calendar calendar = GregorianCalendar.getInstance();
                                calendar.setTime(now);

                                //DEBUG
                                //System.out.println("DAILY: " + calendar.get(Calendar.HOUR_OF_DAY) + " VS " + savedQuery.getQueryInfo().getFrequencyOffset());   

                                if (calendar.get(Calendar.HOUR_OF_DAY) != savedQuery.getQueryInfo()
                                        .getFrequencyOffset()) {
                                    continue;
                                } //TESTED (test4)
                            } else {
                                freqOffset = 24L * 3600L * 1000L; // (just run every 24 hours)                            
                            }
                        } //TESTED (test4)
                        else if (savedQuery.getQueryInfo()
                                .getFrequency() == DocumentQueueControlPojo.SavedQueryInfo.DocQueueFrequency.Weekly) {
                            if (null != savedQuery.getQueryInfo().getFrequencyOffset()) { // day of week
                                freqOffset = 3L * 24L * 3600L * 1000L; // (already check vs day of week so be more relaxed) 
                                Calendar calendar = GregorianCalendar.getInstance();
                                calendar.setTime(now);

                                //DEBUG
                                //System.out.println("WEEKLY: " + calendar.get(Calendar.DAY_OF_WEEK) + " VS " + savedQuery.getQueryInfo().getFrequencyOffset());

                                if (calendar.get(Calendar.DAY_OF_WEEK) != savedQuery.getQueryInfo()
                                        .getFrequencyOffset()) {
                                    continue;
                                }
                            } else {
                                freqOffset = 7L * 24L * 3600L * 1000L; //(just run every 7 days)
                            }
                        } //TESTED (test5)
                        else
                            continue; // (no -supported- frequency, don't run)

                        long nowTime = now.getTime();

                        //DEBUG
                        //System.out.println("Comparing: " + savedQuery.getQueryInfo().getLastRun() + " VS " + now + " @ " + freqOffset/1000L);

                        if ((null == savedQuery.getQueryInfo().getLastRun()) || ((nowTime
                                - savedQuery.getQueryInfo().getLastRun().getTime()) > freqOffset)) {
                            try {
                                //(does nothing if the share already exists)
                                WriteResult wr = DbManager.getCustom().getSavedQueryCache()
                                        .insert(savedQueryShare.toDb(), WriteConcern.ACKNOWLEDGED);

                                if (wr.getN() > 0) { // if we've actually done something, update the main share table also
                                    savedQuery.getQueryInfo().setLastRun(now);
                                    savedQueryShare.setShare(savedQuery.toApi());
                                    // (this will overwrite the existing version)
                                    DbManager.getSocial().getShare().save(savedQueryShare.toDb());
                                } //TESTED (by hand with prints)
                            } catch (MongoException e) {
                            } // just carry on - this is equivalent to getLastERror returning true

                        } //TESTED (test3-5)

                    } // (end saved query actually has a query)
                }
            } //(end loop over saved queries)
        }
    } //(end acquired app lock)
}

From source file:com.jive.myco.seyren.mongo.MongoStore.java

License:Apache License

@Inject
public MongoStore(SeyrenConfig seyrenConfig) {
    try {/*from  w ww  .jav a2 s  . c  om*/
        String uri = seyrenConfig.getMongoUrl();
        MongoClientURI mongoClientUri = new MongoClientURI(uri);
        MongoClient mongoClient = new MongoClient(mongoClientUri);
        DB mongo = mongoClient.getDB(mongoClientUri.getDatabase());
        mongo.setWriteConcern(WriteConcern.ACKNOWLEDGED);
        this.mongo = mongo;
        bootstrapMongo();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.picdrop.guice.RepositoryModule.java

@Override
protected MorphiaRepository<Collection.CollectionItem> provideCollectionItemRepo() {
    return MorphiaRepository.Builder.forType(Collection.CollectionItem.class)
            .withWriteConcern(WriteConcern.ACKNOWLEDGED).withDatastore(null).build();
}

From source file:com.picdrop.guice.RepositoryModule.java

@Override
protected PrincipalAwareMorphiaRepository<Collection> provideCollectionRepo() {
    return PrincipalAwareMorphiaRepository.Builder.forType(Collection.class)
            .withWriteConcern(WriteConcern.ACKNOWLEDGED).withDatastore(null).build();
}

From source file:com.picdrop.guice.RepositoryModule.java

@Override
protected PrincipalAwareMorphiaRepository<FileResource> provideResourceRepo() {
    return PrincipalAwareMorphiaRepository.Builder.forType(FileResource.class)
            .withWriteConcern(WriteConcern.ACKNOWLEDGED).withDatastore(null).build();
}

From source file:com.picdrop.guice.RepositoryModule.java

@Override
protected PrincipalAwareMorphiaRepository<Share> provideShareRepo() {
    return PrincipalAwareMorphiaRepository.Builder.forType(Share.class)
            .withWriteConcern(WriteConcern.ACKNOWLEDGED).withDatastore(null).build();
}