Example usage for com.mongodb BasicDBObject append

List of usage examples for com.mongodb BasicDBObject append

Introduction

In this page you can find the example usage for com.mongodb BasicDBObject append.

Prototype

@Override
public BasicDBObject append(final String key, final Object val) 

Source Link

Document

Add a key/value pair to this object

Usage

From source file:com.edgytech.umongo.CollectionPanel.java

License:Apache License

public void findAndModify(final ButtonBase button) {
    final DBCollection col = getCollectionNode().getCollection();
    final DBObject query = ((DocBuilderField) getBoundUnit(Item.famQuery)).getDBObject();
    final DBObject fields = ((DocBuilderField) getBoundUnit(Item.famFields)).getDBObject();
    final DBObject sort = ((DocBuilderField) getBoundUnit(Item.famSort)).getDBObject();
    final BasicDBObject update = (BasicDBObject) ((DocBuilderField) getBoundUnit(Item.famUpdate)).getDBObject();
    final boolean remove = getBooleanFieldValue(Item.famRemove);
    final boolean returnNew = getBooleanFieldValue(Item.famReturnNew);
    final boolean upsert = getBooleanFieldValue(Item.famUpsert);

    BasicDBObject cmd = new BasicDBObject("findandmodify", col.getName());
    if (query != null && !query.keySet().isEmpty()) {
        cmd.append("query", query);
    }/*from   ww  w  . j av a2 s  . com*/
    if (fields != null && !fields.keySet().isEmpty()) {
        cmd.append("fields", fields);
    }
    if (sort != null && !sort.keySet().isEmpty()) {
        cmd.append("sort", sort);
    }

    if (remove) {
        cmd.append("remove", remove);
    } else {
        if (update != null && !update.keySet().isEmpty()) {
            // if 1st key doesn't start with $, then object will be inserted as is, need to check it
            String key = update.keySet().iterator().next();
            if (key.charAt(0) != '$') {
                MongoUtils.checkObject(update, false, false);
            }
            cmd.append("update", (DBObject) update.copy());
        }
        if (returnNew) {
            cmd.append("new", returnNew);
        }
        if (upsert) {
            cmd.append("upsert", upsert);
        }
    }

    new DbJobCmd(col.getDB(), cmd, null, button).addJob();
}

From source file:com.edgytech.umongo.CollectionPanel.java

License:Apache License

public void moveChunk(ButtonBase button) {
    FormDialog dialog = (FormDialog) ((MenuItem) getBoundUnit(Item.moveChunk)).getDialog();
    ComboBox combo = (ComboBox) getBoundUnit(Item.mvckToShard);
    combo.value = 0;/*from   ww  w. j  av  a 2s. co  m*/
    combo.items = getCollectionNode().getDbNode().getMongoNode().getShardNames();
    combo.structureComponent();

    if (!dialog.show()) {
        return;
    }

    BasicDBObject cmd = new BasicDBObject("moveChunk", getCollectionNode().getCollection().getFullName());
    DBObject query = ((DocBuilderField) getBoundUnit(Item.mvckQuery)).getDBObject();
    cmd.append("find", query);
    cmd.append("to", getStringFieldValue(Item.mvckToShard));
    new DbJobCmd(getCollectionNode().getDbNode().getDb().getSisterDB("admin"), cmd).addJob();

}

From source file:com.edgytech.umongo.CollectionPanel.java

License:Apache License

public void splitChunk(ButtonBase button) {
    BasicDBObject cmd = new BasicDBObject("split", getCollectionNode().getCollection().getFullName());
    DBObject query = ((DocBuilderField) getBoundUnit(Item.spckQuery)).getDBObject();
    if (getBooleanFieldValue(Item.spckOnValue)) {
        cmd.append("middle", query);
    } else {//from   w w  w.j  av  a2 s.c o  m
        cmd.append("find", query);
    }
    new DbJobCmd(getCollectionNode().getDbNode().getDb().getSisterDB("admin"), cmd).addJob();
}

From source file:com.edgytech.umongo.ReplSetPanel.java

License:Apache License

public void compareReplicas(ButtonBase button) {
    final String stat = getStringFieldValue(Item.crStat);
    new DbJob() {

        @Override//from w w w .  j av  a 2  s  .c  o  m
        public Object doRun() {
            ReplSetNode node = getReplSetNode();
            if (!node.hasChildren())
                return null;

            ArrayList<MongoClient> svrs = new ArrayList<MongoClient>();
            for (XmlUnit unit : node.getChildren()) {
                ServerNode svr = (ServerNode) unit;
                MongoClient svrm = svr.getServerMongoClient();
                try {
                    svrm.getDatabaseNames();
                } catch (Exception e) {
                    continue;
                }
                svrs.add(svrm);
            }

            BasicDBObject res = new BasicDBObject();
            MongoClient m = getReplSetNode().getMongoClient();
            for (String dbname : m.getDatabaseNames()) {
                DB db = m.getDB(dbname);
                BasicDBObject dbres = new BasicDBObject();
                for (String colname : db.getCollectionNames()) {
                    DBCollection col = db.getCollection(colname);
                    BasicDBObject colres = new BasicDBObject();
                    BasicDBObject values = new BasicDBObject();
                    boolean same = true;
                    long ref = -1;
                    for (MongoClient svrm : svrs) {
                        DBCollection svrcol = svrm.getDB(dbname).getCollection(colname);
                        long value = 0;
                        if (stat.startsWith("Count")) {
                            value = svrcol.count();
                        } else if (stat.startsWith("Data Size")) {
                            CommandResult stats = svrcol.getStats();
                            value = stats.getLong("size");
                        }
                        values.append(svrm.getConnectPoint(), value);
                        if (ref < 0)
                            ref = value;
                        else if (ref != value)
                            same = false;
                    }
                    if (!same) {
                        colres.append("values", values);
                        dbres.append(colname, colres);
                    }
                }
                if (!dbres.isEmpty()) {
                    res.append(dbname, dbres);
                }
            }

            return res;
        }

        @Override
        public String getNS() {
            return "*";
        }

        @Override
        public String getShortName() {
            return "Compare Replicas";
        }
    }.addJob();
}

From source file:com.effektif.mongo.MongoJobStore.java

License:Apache License

public BasicDBObject createDbQuery(JobQuery query) {
    if (query == null) {
        query = new JobQuery();
    }//from  w ww .ja  v  a2 s .c  o m
    BasicDBObject dbQuery = new BasicDBObject();
    // TODO use MongoQuery filterOrganization(dbQuery, JobFields.organizationId);
    if (query.getJobId() != null) {
        dbQuery.append(_ID, new ObjectId(query.getJobId()));
    }
    return dbQuery;
}

From source file:com.effektif.mongo.MongoWorkflowInstanceStore.java

License:Apache License

@Override
public void flush(WorkflowInstanceImpl workflowInstance) {
    if (log.isDebugEnabled())
        log.debug("Flushing workflow instance...");

    WorkflowInstanceUpdates updates = workflowInstance.getUpdates();

    DBObject query = BasicDBObjectBuilder.start().add(_ID, new ObjectId(workflowInstance.id.getInternal()))
            // I don't recall what this line was for... if you re-add it, please add a comment to explain
            // .add(LOCK,  writeLock(workflowInstance.lock))
            .get();//from w  w  w. j av a  2s .  co m

    BasicDBObject sets = new BasicDBObject();
    BasicDBObject unsets = new BasicDBObject();
    BasicDBObject update = new BasicDBObject();

    if (updates.isEndChanged) {
        // if (log.isDebugEnabled()) log.debug("  Workflow instance ended");
        if (workflowInstance.end != null) {
            sets.append(END, workflowInstance.end.toDate());
            sets.append(DURATION, workflowInstance.duration);
        } else {
            unsets.append(END, 1);
            unsets.append(DURATION, 1);
        }
    }
    if (updates.isEndStateChanged) {
        sets.append(END_STATE, workflowInstance.getEndState());
    }

    // MongoDB can't combine updates of array elements together with 
    // adding elements to that array.  That's why we overwrite the whole
    // activity instance array when an update happened in there.
    // We do archive the ended (and joined) activity instances into a separate collection 
    // that doesn't have to be loaded.
    if (updates.isActivityInstancesChanged) {
        BasicDBList dbActivityInstances = writeActiveActivityInstances(workflowInstance.activityInstances);
        sets.append(ACTIVITY_INSTANCES, dbActivityInstances);
    }

    if (updates.isVariableInstancesChanged) {
        writeVariableInstances(sets, workflowInstance);
    }

    if (updates.isWorkChanged) {
        List<String> work = writeWork(workflowInstance.work);
        if (work != null) {
            sets.put(WORK, work);
        } else {
            unsets.put(WORK, 1);
        }
    }

    if (updates.isAsyncWorkChanged) {
        List<String> workAsync = writeWork(workflowInstance.workAsync);
        if (workAsync != null) {
            sets.put(WORK_ASYNC, workAsync);
        } else {
            unsets.put(WORK_ASYNC, 1);
        }
    }

    if (updates.isNextActivityInstanceIdChanged) {
        sets.put(NEXT_ACTIVITY_INSTANCE_ID, workflowInstance.nextActivityInstanceId);
    }

    if (updates.isNextVariableInstanceIdChanged) {
        sets.put(NEXT_VARIABLE_INSTANCE_ID, workflowInstance.nextVariableInstanceId);
    }

    if (updates.isLockChanged) {
        // a lock is only removed 
        unsets.put(LOCK, 1);
    }

    if (updates.isJobsChanged) {
        List<BasicDBObject> dbJobs = writeJobs(workflowInstance.jobs);
        if (dbJobs != null) {
            sets.put(JOBS, dbJobs);
        } else {
            unsets.put(JOBS, 1);
        }
    }

    if (updates.isPropertiesChanged) {
        if (workflowInstance.properties != null && workflowInstance.properties.size() > 0)
            sets.append(PROPERTIES, new BasicDBObject(workflowInstance.getProperties()));
        else
            unsets.append(PROPERTIES, 1);
    }

    if (!sets.isEmpty()) {
        update.append("$set", sets);
    }
    if (!unsets.isEmpty()) {
        update.append("$unset", unsets);
    }
    if (!update.isEmpty()) {
        workflowInstancesCollection.update("flush-workflow-instance", query, update, false, false);
    }

    // reset the update tracking as all changes have been saved
    workflowInstance.trackUpdates(false);
}

From source file:com.effektif.mongo.MongoWorkflowInstanceStore.java

License:Apache License

protected BasicDBObject createDbQuery(WorkflowInstanceQuery query) {
    if (query == null) {
        query = new WorkflowInstanceQuery();
    }/*from   w  w w.  j  a  va  2s  .c  om*/
    BasicDBObject dbQuery = new BasicDBObject();
    if (query.getWorkflowInstanceId() != null) {
        dbQuery.append(_ID, new ObjectId(query.getWorkflowInstanceId().getInternal()));
    }

    if (query.getActivityId() != null) {
        dbQuery.append(ACTIVITY_INSTANCES,
                new BasicDBObject("$elemMatch", new BasicDBObject(ACTIVITY_ID, query.getActivityId())
                        .append(WORK_STATE, new BasicDBObject("$exists", true))));
    }

    if (query.getLockedBefore() != null) {
        dbQuery.append(LOCK + "." + Lock.TIME, new BasicDBObject("$lt", query.getLockedBefore().toDate()));
    }

    return dbQuery;
}

From source file:com.effektif.mongo.MongoWorkflowStore.java

License:Apache License

protected BasicDBObject createDbQuery(WorkflowQuery query) {
    BasicDBObject dbQuery = new BasicDBObject();
    if (query.getWorkflowId() != null) {
        dbQuery.append(_ID, new ObjectId(query.getWorkflowId().getInternal()));
    }/*from w w  w . j  ava 2  s  .  c  o m*/
    if (query.getWorkflowSource() != null) {
        dbQuery.append(SOURCE_WORKFLOW_ID, query.getWorkflowSource());
    }
    return dbQuery;
}

From source file:com.effektif.mongo.MongoWorkflowStore.java

License:Apache License

public DBObject writeOrderBy(List<OrderBy> orderBy) {
    BasicDBObject dbOrderBy = new BasicDBObject();
    for (OrderBy element : orderBy) {
        String dbField = getDbField(element.getField());
        int dbDirection = (element.getDirection() == OrderDirection.asc ? 1 : -1);
        dbOrderBy.append(dbField, dbDirection);
    }/*from w  w  w.  j  a  v a 2 s .c  om*/
    return dbOrderBy;
}

From source file:com.emuneee.camerasyncmanager.util.DatabaseUtil.java

License:Apache License

/**
 * Updates a list cameras//from   w  w  w. jav  a 2 s . c o m
 * @param cameras
 * @return
 */
public boolean updateCameras(List<Camera> cameras) {
    sLogger.info("Updating " + cameras.size() + " cameras");
    boolean result = false;
    DB db = getDatabase();

    try {
        DBCollection collection = db.getCollection("camera");

        for (Camera camera : cameras) {
            BasicDBObject query = new BasicDBObject("_id", camera.getId());
            BasicDBObject dbObj = cameraToDBObject(camera);
            dbObj.append("updated", new Date());
            sLogger.debug("Updating: " + dbObj.toString());
            collection.update(query, dbObj);
            result = true;
        }
    } catch (Exception e) {
        sLogger.error("Exception inserting cameras");
        sLogger.error(e);
    }

    return result;
}