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:UnitTest3.java

License:Open Source License

public static int testgridfs() {

    long time1;/* w w  w. j a v a  2 s.c  o  m*/
    long time2;
    long time3;
    long time4;

    try {

        MongoClient mongo = new MongoClient("localhost", 27017);
        DB db = mongo.getDB("JFileDB");

        // TODO JFileMetaDataTable should be in MetaDB database
        DBCollection collection = db.getCollection("JFileMetaDataTable");

        String newFileName = "com.dilmus.scabi.testdata.in.App.class";

        File jFile = new File("/home/anees/workspace/testdata/in/App.class");

        // create a JFileTable namespace
        GridFS gfsj = new GridFS(db, "JFileTable");

        // get file from local drive
        GridFSInputFile gfsFile = gfsj.createFile(jFile);

        // set a new filename for identify purpose
        gfsFile.setFilename(newFileName);
        gfsFile.setContentType("class"); // jar, zip, war
        // save the image file into mongoDB
        gfsFile.save();

        // Let's create a new JSON document with some "metadata" information
        BasicDBObject info = new BasicDBObject();
        info.put("DBHost", "localhost");
        info.put("DBPort", "27017");
        info.put("JFileName", newFileName);
        info.put("JFileID", gfsFile.getId());
        info.put("JFileMD5", gfsFile.getMD5());
        collection.insert(info, WriteConcern.ACKNOWLEDGED);

        // print the result
        DBCursor cursor = gfsj.getFileList();
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }

        DBCursor cursor2 = collection.find();

        while (cursor2.hasNext()) {
            System.out.println(cursor2.next());
        }

        // get file by it's filename
        GridFSDBFile jForOutput = gfsj.findOne(newFileName);

        // save it into a new image file
        jForOutput.writeTo("/home/anees/workspace/testdata/out/AppOut.class");

        // remove the file from mongoDB
        // gfsj.remove(gfsj.findOne(newFileName));

        System.out.println("Done");
        mongo.close();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return 0;
}

From source file:applango.common.services.DB.mongo.mongoDB.java

public static DBObject removeRecordsFromDB(Applango applango, DBCollection coll) {
    logger.info("Remove all records with customerId : " + applango.getUsername());
    String jsonCustomer = "{'customerId' : '" + applango.getCustomerForoAuth() + "'}";
    DBObject dbObjectRecordQuery = (DBObject) JSON.parse(jsonCustomer);
    if (!(coll.count(dbObjectRecordQuery) == 0)) {
        coll.remove(dbObjectRecordQuery, WriteConcern.ACKNOWLEDGED);

    }//from ww w .j  ava2  s  . co  m
    return dbObjectRecordQuery;
}

From source file:br.bireme.scl.BrokenLinks.java

License:Open Source License

/**
 * Creates the CC Fields Collection and insert a lilacs metadoc document.
 * @param coll CC Fields Collection/*  www  .  java2  s  . c  o  m*/
 * @return true if ok, false if error
 */
private static boolean createCcFieldsCollection(final DBCollection coll) {
    assert coll != null;

    final BasicDBObject doc = new BasicDBObject();
    final BasicDBList lst = new BasicDBList();

    lst.add(1);
    lst.add(920);
    lst.add(930);
    doc.put(MST_FIELD, "LILACS");
    doc.put(ID_TAG_FIELD, 2);
    doc.put(URL_TAG_FIELD, 8);
    doc.put(CC_TAGS_FIELD, lst);

    final WriteResult ret = coll.save(doc, WriteConcern.ACKNOWLEDGED);

    return ret.getCachedLastError().ok();
}

From source file:br.bireme.scl.BrokenLinks.java

License:Open Source License

private static boolean saveRecord(final String mstName, final int id, final String url, final String err,
        final int urlTag, final List<Integer> ccsFlds, final Master mst, final DBCollection coll,
        final DBCollection hcoll, final Map<Integer, Map<String, Integer>> occMap)
        throws BrumaException, IOException {
    assert mstName != null;
    assert id > 0;
    assert url != null;
    assert urlTag > 0;
    assert err != null;
    assert ccsFlds != null;
    assert mst != null;
    assert coll != null;
    assert hcoll != null;
    assert occMap != null;

    final Record rec = mst.getRecord(id);
    if (!rec.isActive()) {
        //throw new BrumaException("not active record mfn=" + id);
        System.err.println("WARNING: record[" + id + "] is not active. " + "Ignoring it!");
        return false;
    }/*from w  ww . j a  va2s  . c  o m*/

    final List<Field> urls = rec.getFieldList(urlTag);
    final Date now = new Date();
    Date date;

    Map<String, Integer> fldMap = occMap.get(id);
    if (fldMap == null) {
        fldMap = new HashMap<String, Integer>();
        occMap.put(id, fldMap);
    }

    final int occ = nextOcc(url, urls, fldMap);
    if (occ == -1) {
        System.err.println("url[" + url + "] not found. mfn=" + id);
        //throw new IOException("url[" + url + "] not found. mfn=" + id);                
        return false;
    }

    final BasicDBObject query = new BasicDBObject(ID_FIELD, id + "_" + occ);
    final boolean ret;
    if (fixedRecently(hcoll, query, now, 60)) {
        ret = false;
    } else {
        final BasicDBObject obj = (BasicDBObject) coll.findOne(query);
        if (obj == null) {
            date = now;
        } else {
            date = obj.getDate(LAST_UPDATE_FIELD);
            if (date == null) {
                date = obj.getDate(DATE_FIELD);
            } else {
                final WriteResult wr = coll.remove(obj, WriteConcern.ACKNOWLEDGED);
                if (!wr.getCachedLastError().ok()) {
                    //TODO
                }
                date = obj.getDate(DATE_FIELD);
            }
        }
        final String url_d = EncDecUrl.decodeUrl(url);
        final String url_d_l = (url_d.length() >= 900) ? url_d.substring(0, 900) + "..." : url_d;
        final String url_l = (url.length() > 900) ? url.substring(0, 900) + "..." : url;
        final BasicDBObject doc = new BasicDBObject();
        doc.put(DATE_FIELD, date);
        doc.put(LAST_UPDATE_FIELD, now);
        doc.put(MST_FIELD, mstName);
        doc.put(ID_FIELD, id + "_" + occ);
        doc.put(BROKEN_URL_FIELD, url_l);
        doc.put(PRETTY_BROKEN_URL_FIELD, url_d_l);
        doc.put(MSG_FIELD, err);
        doc.put(CENTER_FIELD, getCCS(rec, ccsFlds));

        final WriteResult wres = coll.save(doc, WriteConcern.ACKNOWLEDGED);
        ret = wres.getCachedLastError().ok();
    }
    return ret;
}

From source file:br.bireme.scl.BrokenLinks.java

License:Open Source License

private static boolean removeOldDocs(final DBCollection coll) {
    assert coll != null;

    final Date now = new Date();
    final DBCursor cursor = coll.find();
    boolean ret = true;

    while (cursor.hasNext()) {
        final BasicDBObject obj = (BasicDBObject) cursor.next();
        final Date auxDate = obj.getDate(LAST_UPDATE_FIELD);
        if ((auxDate == null) || (now.getTime() - auxDate.getTime()) > 60 * 60 * 1000) {
            final WriteResult wr = coll.remove(obj, WriteConcern.ACKNOWLEDGED);
            ret = ret && wr.getCachedLastError().ok();
        }//w  w  w. j  a  va  2 s  . co m
    }
    return ret;
}

From source file:br.bireme.scl.CopyMongoDb.java

License:Open Source License

private static void copyDB(final String from_host, final String to_host, final String from_db,
        final String to_db, final String from_port, final String to_port, final boolean appendCollections,
        final boolean displayAllIds) throws UnknownHostException, IOException {
    assert from_host != null;
    assert to_host != null;
    assert from_db != null;
    assert to_db != null;
    assert from_port != null;
    assert to_port != null;

    final int MAX_LOOP_SIZE = 15000; // MongoException$CursorNotFound
    final MongoClient fromClient = new MongoClient(from_host, Integer.parseInt(from_port));
    final MongoClient toClient = new MongoClient(to_host, Integer.parseInt(to_port));
    final DB fromDb = fromClient.getDB(from_db);
    final DB toDb = toClient.getDB(to_db);

    if (!appendCollections) {
        toDb.dropDatabase();/*from  ww  w . j a v a 2s.  co  m*/
    }

    final Set<String> colls = fromDb.getCollectionNames();

    for (String cname : colls) {
        if (cname.equals("system.indexes")) {
            continue;
        }

        final DBCollection fromColl = fromDb.getCollection(cname);
        final DBCollection toColl = toDb.getCollection(cname);

        DBCursor cursor = fromColl.find();
        int curr = 0;

        System.out.println("Copying collection: " + cname);

        while (cursor.hasNext()) {
            if (curr % MAX_LOOP_SIZE == 0) {
                if (curr > 0) {
                    cursor.close();
                    cursor = fromColl.find().skip(curr);
                    if (!cursor.hasNext()) {
                        throw new IOException("hasNext() failed");
                    }
                }
            }
            final DBObject doc = cursor.next();
            final WriteResult ret = toColl.save(doc, WriteConcern.ACKNOWLEDGED);

            if (!ret.getCachedLastError().ok()) {
                System.err.println("write error doc id=" + doc.get("_id"));
            }
            if (++curr % 1000 == 0) {
                System.out.println("+++" + curr);
            }
            if (displayAllIds) {
                System.out.println(" id=" + doc.get("_id"));
            }
        }
        cursor.close();

        System.out.println();
    }
}

From source file:br.bireme.scl.Gizmo.java

License:Open Source License

Collection<Element> getNotExportedElements(final DBCollection coll, final DBCursor cursor) throws IOException {
    assert coll != null;
    assert cursor != null;

    final Collection<Element> col = new ArrayList<Element>();

    while (cursor.hasNext()) {
        final BasicDBObject obj = (BasicDBObject) cursor.next();
        final String id = obj.getString(ID_FIELD);
        final BasicDBList lst = (BasicDBList) obj.get(ELEM_LST_FIELD);
        if (lst == null) {
            throw new NullPointerException("Elem list espected");
        }/*from   www .  j  a v a 2 s.c om*/
        final BasicDBObject lelem = (BasicDBObject) lst.get(0);
        if (lelem == null) {
            throw new NullPointerException("Elem element espected");
        }
        if (!lelem.getBoolean(EXPORTED_FIELD)) {
            final Element elem = new Element(id, lelem.getString(BROKEN_URL_FIELD),
                    lelem.getString(PRETTY_BROKEN_URL_FIELD), lelem.getString(FIXED_URL_FIELD),
                    obj.getString(MST_FIELD), lelem.getDate(LAST_UPDATE_FIELD).toString(),
                    lelem.getString(USER_FIELD), null, false);
            col.add(elem);

            lelem.put(EXPORTED_FIELD, true);
            final WriteResult res = coll.save(obj, WriteConcern.ACKNOWLEDGED);

            if (!res.getCachedLastError().ok()) {
                throw new IOException("write doc[" + obj.getString(ID_FIELD) + "] failed");
            }
        }
    }

    return col;
}

From source file:br.bireme.scl.MongoOperations.java

License:Open Source License

public static boolean updateDocument(final DBCollection coll, final DBCollection hcoll, final String docId,
        final String fixedUrl, final String user, final boolean automatic) throws IOException {
    if (coll == null) {
        throw new NullPointerException("coll");
    }/*from ww  w  .j  a v  a2 s.co  m*/
    if (hcoll == null) {
        throw new NullPointerException("hcoll");
    }
    if (docId == null) {
        throw new NullPointerException("docId");
    }
    if (fixedUrl == null) {
        throw new NullPointerException("fixedUrl");
    }
    if (user == null) {
        throw new NullPointerException("user");
    }
    if (fixedUrl.length() >= 900) {
        throw new IOException("fixedUrl is too long >= 900. [" + fixedUrl + "]");
    }

    final BasicDBObject query = new BasicDBObject(ID_FIELD, docId);
    final BasicDBObject doc = (BasicDBObject) coll.findOne(query);

    if (doc == null) {
        throw new IOException("document not found id[" + docId + "]");
    }

    final BasicDBList lsthdoc;
    BasicDBObject hdoc = (BasicDBObject) hcoll.findOne(query);
    if (hdoc == null) {
        hdoc = new BasicDBObject();
        hdoc.append(ID_FIELD, docId);
        hdoc.append(MST_FIELD, (String) doc.get(MST_FIELD));
        hdoc.append(DATE_FIELD, (Date) doc.get(DATE_FIELD));
        lsthdoc = new BasicDBList();
        hdoc.append(ELEM_LST_FIELD, lsthdoc);
    } else {
        lsthdoc = (BasicDBList) hdoc.get(ELEM_LST_FIELD);
    }

    final String brokenUrl = doc.getString(BROKEN_URL_FIELD);
    final String brokenUrl_D = EncDecUrl.decodeUrl(brokenUrl);
    final String fixedUrl_E = EncDecUrl.encodeUrl(fixedUrl, CODEC, false);
    //final String fixedUrl_D = EncDecUrl.decodeUrl(fixedUrl);
    final BasicDBObject hcurdoc = new BasicDBObject();
    hcurdoc.append(BROKEN_URL_FIELD, brokenUrl).append(PRETTY_BROKEN_URL_FIELD, brokenUrl_D)
            .append(FIXED_URL_FIELD, fixedUrl_E).append(MSG_FIELD, (String) doc.get(MSG_FIELD))
            .append(CENTER_FIELD, (BasicDBList) doc.get(CENTER_FIELD)).append(AUTO_FIX_FIELD, automatic)
            .append(EXPORTED_FIELD, false).append(LAST_UPDATE_FIELD, new Date()).append(USER_FIELD, user);

    lsthdoc.add(0, hcurdoc);

    final boolean ret1 = coll.remove(doc, WriteConcern.ACKNOWLEDGED).getLastError().ok();
    final boolean ret2 = hcoll.save(hdoc).getLastError().ok();

    return ret1 && ret2;
}

From source file:br.bireme.scl.MongoOperations.java

License:Open Source License

public static boolean undoUpdateDocument(final DBCollection coll, final DBCollection hcoll, final String docId,
        final boolean updateBrokenColl) throws IOException {
    if (coll == null) {
        throw new NullPointerException("coll");
    }/*w ww.j  av a 2  s .  c  om*/
    if (hcoll == null) {
        throw new NullPointerException("hcoll");
    }
    if (docId == null) {
        throw new NullPointerException("docId");
    }

    final BasicDBObject query = new BasicDBObject(ID_FIELD, docId);
    final BasicDBObject hdoc = (BasicDBObject) hcoll.findOne(query);

    if (hdoc == null) {
        throw new IOException("document not found id[" + docId + "]");
    }
    final BasicDBList lst = (BasicDBList) hdoc.get(ELEM_LST_FIELD);
    final BasicDBObject hcurdoc = (BasicDBObject) lst.remove(0);
    if (hcurdoc == null) {
        throw new IOException("document last element found. Id[" + docId + "]");
    }
    final BasicDBObject doc = new BasicDBObject();
    doc.put(DATE_FIELD, hdoc.get(DATE_FIELD));
    doc.put(LAST_UPDATE_FIELD, hcurdoc.get(LAST_UPDATE_FIELD));
    doc.put(MST_FIELD, hdoc.get(MST_FIELD));
    doc.put(ID_FIELD, docId);
    doc.put(BROKEN_URL_FIELD, hcurdoc.get(BROKEN_URL_FIELD));
    doc.put(PRETTY_BROKEN_URL_FIELD, hcurdoc.get(PRETTY_BROKEN_URL_FIELD));
    doc.put(MSG_FIELD, hcurdoc.get(MSG_FIELD));
    doc.put(CENTER_FIELD, hcurdoc.get(CENTER_FIELD));

    final boolean ret1 = updateBrokenColl ? coll.save(doc).getLastError().ok() : true;
    final boolean ret2;

    if (lst.isEmpty()) {
        ret2 = hcoll.remove(query, WriteConcern.ACKNOWLEDGED).getLastError().ok();
    } else {
        ret2 = hcoll.save(hdoc, WriteConcern.ACKNOWLEDGED).getLastError().ok();
    }

    return ret1 && ret2;
}

From source file:br.bireme.scl.MongoOperations.java

License:Open Source License

public static boolean undoUpdateDocument2(final DBCollection coll, final DBCollection hcoll,
        final String fromDate) throws IOException, ParseException {
    if (coll == null) {
        throw new NullPointerException("coll");
    }//from  ww  w.j a  va2 s  . c  o  m
    if (hcoll == null) {
        throw new NullPointerException("hcoll");
    }
    final SimpleDateFormat simple = new SimpleDateFormat("yyyyMMdd");
    final Date date = (fromDate == null) ? new Date(0) : simple.parse(fromDate);
    final String updated = ELEM_LST_FIELD + ".0." + LAST_UPDATE_FIELD;
    final BasicDBObject qdate = new BasicDBObject("$gte", date);
    final BasicDBObject query = new BasicDBObject(updated, qdate);
    final BasicDBObject sort = new BasicDBObject(updated, -1);
    final DBCursor cursor = coll.find(query).sort(sort);

    boolean ret = true;

    while (cursor.hasNext()) {
        final BasicDBObject hdoc = (BasicDBObject) cursor.next();
        final BasicDBList lst = (BasicDBList) hdoc.get(ELEM_LST_FIELD);
        final BasicDBObject hcurdoc = (BasicDBObject) lst.remove(0);
        if (hcurdoc == null) {
            throw new IOException("document last element found.");
        }
        final BasicDBObject doc = new BasicDBObject();
        doc.put(DATE_FIELD, hdoc.get(DATE_FIELD));
        doc.put(LAST_UPDATE_FIELD, hcurdoc.get(LAST_UPDATE_FIELD));
        doc.put(MST_FIELD, hdoc.get(MST_FIELD));
        doc.put(ID_FIELD, hdoc.get(ID_FIELD));
        doc.put(BROKEN_URL_FIELD, hcurdoc.get(BROKEN_URL_FIELD));
        doc.put(PRETTY_BROKEN_URL_FIELD, hcurdoc.get(PRETTY_BROKEN_URL_FIELD));
        doc.put(MSG_FIELD, hcurdoc.get(MSG_FIELD));
        doc.put(CENTER_FIELD, hcurdoc.get(CENTER_FIELD));

        final boolean ret1 = coll.save(doc).getLastError().ok();
        final boolean ret2;

        if (lst.isEmpty()) {
            ret2 = hcoll.remove(query, WriteConcern.ACKNOWLEDGED).getLastError().ok();
        } else {
            ret2 = hcoll.save(hdoc, WriteConcern.ACKNOWLEDGED).getLastError().ok();
        }
        final boolean auxret = (ret1 && ret2);
        if (!auxret) {
            System.err.println("doc[" + hdoc.get(ID_FIELD) + "] write error");
        }
        ret &= auxret;
    }

    return ret;
}