Example usage for com.mongodb BasicDBObject getInt

List of usage examples for com.mongodb BasicDBObject getInt

Introduction

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

Prototype

public int getInt(final String key) 

Source Link

Document

Returns the value of a field as an int .

Usage

From source file:org.sipfoundry.sipxconfig.mongo.MongoServer.java

License:Open Source License

public MongoServer(BasicDBObject dbo) {
    m_id = dbo.getInt("_id");
    m_name = dbo.getString("name");
    m_type = SERVER;//from w  ww .  j  a v  a 2s .  c o  m
    if (StringUtils.contains(m_name, String.valueOf(MongoSettings.ARBITER_PORT))) {
        m_type = ARBITER;
    }
    m_configured = true;
    m_state = dbo.getString("stateStr");
    m_health = UP;
    if (dbo.containsField(HEALTH)) {
        if (dbo.getInt(HEALTH) == 0) {
            m_health = DOWN;
        }
    }
    if (dbo.containsField(ERR_MSG)) {
        m_errMsg = dbo.getString(ERR_MSG);
    }
    try {
        BSONTimestamp optime = (BSONTimestamp) dbo.get("optime");
        if (optime.getTime() != 0) {
            m_optimeDate = new Date((long) optime.getTime() * 1000).toString();
        }
    } catch (NumberFormatException ex) {
        m_optimeDate = NA;
    }
}

From source file:pl.nask.hsn2.os.ObjectStore.java

License:Open Source License

private Builder getBuilder(String key, BasicDBObject objectFound) {
    Object value = objectFound.get(key);
    Builder builder = Attribute.newBuilder().setName(key);

    if (value instanceof Integer || key.equalsIgnoreCase("depth")) {
        builder.setType(Type.INT).setDataInt(objectFound.getInt(key));
    } else if (value instanceof Long) {
        builder.setType(Type.OBJECT).setDataObject(objectFound.getLong(key));
    } else if (value instanceof BasicDBObject) {

        BasicDBObject mongoObj = (BasicDBObject) value;
        if (mongoObj.get("type").equals("TIME")) {
            builder.setType(Type.TIME).setDataTime(mongoObj.getLong("time"));
        } else if (mongoObj.get("type").equals("BYTES")) {
            builder.setType(Type.BYTES).setDataBytes(Reference.newBuilder().setKey(mongoObj.getLong("key"))
                    .setStore(mongoObj.getInt("store")).build());
        }/*from w  w  w  .j  a v  a  2 s  .com*/
    } else if (value instanceof Boolean) {
        builder.setType(Type.BOOL).setDataBool(objectFound.getBoolean(key));
    } else {
        builder.setType(Type.STRING).setDataString(objectFound.getString(key));
    }
    return builder;
}

From source file:rapture.sheet.mongodb.MongoCellStore.java

License:Open Source License

public List<RaptureSheetCell> findCellsByEpoch(final String sheetName, final int dimension,
        final long minEpochFilter) {

    MongoRetryWrapper<List<RaptureSheetCell>> wrapper = new MongoRetryWrapper<List<RaptureSheetCell>>() {

        public DBCursor makeCursor() {
            DBCollection collection = MongoDBFactory.getDB(instanceName).getCollection(tableName);
            BasicDBObject query = new BasicDBObject();
            query.put(KEY, sheetName);//from   w ww.  j  a  v a 2 s .  co  m
            query.put(DIM, dimension);
            return collection.find(query);
        }

        public List<RaptureSheetCell> action(DBCursor cursor) {
            List<RaptureSheetCell> ret = new ArrayList<RaptureSheetCell>();
            Long maxEpoch = 0L;

            while (cursor.hasNext()) {
                BasicDBObject val = (BasicDBObject) cursor.next();
                RaptureSheetCell cell = new RaptureSheetCell();
                cell.setColumn(val.getInt(COL));
                cell.setRow(val.getInt(ROW));
                cell.setData(val.getString(VALUE));
                boolean shouldAdd;
                if (val.containsField(EPOCH)) {
                    long currEpoch = val.getLong(EPOCH);
                    shouldAdd = (currEpoch > minEpochFilter);
                    cell.setEpoch(currEpoch);
                    if (maxEpoch < currEpoch) {
                        maxEpoch = currEpoch;
                    }
                } else {
                    shouldAdd = true;
                }

                if (shouldAdd)
                    ret.add(cell);
            }
            return ret;
        }
    };
    return wrapper.doAction();
}

From source file:rapture.sheet.mongodb.MongoCellStore.java

License:Open Source License

public void deleteColumn(final String sheetName, final int column) {
    final DBCollection collection = MongoDBFactory.getDB(instanceName).getCollection(tableName);
    MongoRetryWrapper<Object> wrapper = new MongoRetryWrapper<Object>() {

        public DBCursor makeCursor() {

            // Deleting a column is two things.
            // (a) find and remove all cells (in all dimensions) that have
            // this as a
            // column
            // (b) modify all cells that have a column > this column,
            // decreasing
            // their column by 1
            BasicDBObject query = new BasicDBObject();
            query.put(KEY, sheetName);/*from   w  ww.  j a v  a  2 s . c om*/
            query.put(COL, column);
            collection.findAndRemove(query);

            BasicDBObject changeQuery = new BasicDBObject();
            changeQuery.put(KEY, sheetName);
            BasicDBObject testQuery = new BasicDBObject();
            testQuery.put("$gt", column);
            changeQuery.put(COL, testQuery);

            return collection.find(changeQuery);
        }

        public Object action(DBCursor cursor) {
            while (cursor.hasNext()) {
                BasicDBObject object = (BasicDBObject) cursor.next();
                object.put(COL, object.getInt(COL) - 1);
                object.put(EPOCH, getLatestEpoch(sheetName, object.getInt(DIM)));
                collection.save(object);
            }

            return null;
        }
    };
    @SuppressWarnings("unused")
    Object o = wrapper.doAction();
}

From source file:rapture.sheet.mongodb.MongoCellStore.java

License:Open Source License

public void deleteRow(final String sheetName, final int row) {
    final DBCollection collection = MongoDBFactory.getDB(instanceName).getCollection(tableName);
    MongoRetryWrapper<Object> wrapper = new MongoRetryWrapper<Object>() {

        public DBCursor makeCursor() {
            // Deleting a column is two things.
            // (a) find and remove all cells (in all dimensions) that have
            // this as a
            // column
            // (b) modify all cells that have a column > this column,
            // decreasing
            // their column by 1
            DBCollection collection = MongoDBFactory.getDB(instanceName).getCollection(tableName);
            BasicDBObject query = new BasicDBObject();
            query.put(KEY, sheetName);/*  w w w  .  ja v a  2s  .c  o m*/
            query.put(ROW, row);
            collection.findAndRemove(query);

            BasicDBObject changeQuery = new BasicDBObject();
            changeQuery.put(KEY, sheetName);
            BasicDBObject testQuery = new BasicDBObject();
            testQuery.put("$gt", row);
            changeQuery.put(ROW, testQuery);
            return collection.find(changeQuery);
        }

        public Object action(DBCursor cursor) {

            while (cursor.hasNext()) {
                BasicDBObject object = (BasicDBObject) cursor.next();
                object.put(ROW, object.getInt(ROW) - 1);
                object.put(EPOCH, getLatestEpoch(sheetName, object.getInt(DIM)));
                collection.save(object);
            }

            return null;
        }
    };
    @SuppressWarnings("unused")
    Object o = wrapper.doAction();
}

From source file:recomendacao.Recomendacao.java

/**
 * Filmes para o usurio/*from   w w  w. j av  a  2s .  co m*/
 */
public void recomendar() {

    long startTime = System.currentTimeMillis();

    double soma = 0;

    //Recuperar todos os usurios
    DBCursor usersCursor = usersCollection.find();
    //Para cada usuro
    while (usersCursor.hasNext()) {
        long startTimeUser = System.currentTimeMillis();

        BasicDBObject user = (BasicDBObject) usersCursor.next();

        //Ver qual o gnero que ele mais assiste (baseado nos rates)
        //e recuperar todos os filmes assistidos pelo user
        DBCursor ratesCursor = ratesCollection.find(new BasicDBObject("userID", user.get("_id")));

        Map<String, Integer> categoriasCount = new HashMap<>();
        BasicDBList moviesAssistidos = new BasicDBList();

        while (ratesCursor.hasNext()) {
            BasicDBObject rate = (BasicDBObject) ratesCursor.next();

            //Recuperar o filme
            Integer movieID = rate.getInt("movieID");
            BasicDBObject movie = (BasicDBObject) moviesCollection.findOne(new BasicDBObject("_id", movieID));
            BasicDBList categories = (BasicDBList) movie.get("categories");

            moviesAssistidos.add(movieID);

            try {
                //Guardar a contagem
                for (Object categoryObject : categories) {
                    String category = (String) categoryObject;

                    int count = 0;
                    if (categoriasCount.containsKey(category)) {
                        count = categoriasCount.get(category);
                    }
                    count++;
                    categoriasCount.put(category, count);
                }
            } catch (NullPointerException ex) {
                System.out.println(movie.get("name") + " no tem categoria...");
            }

            //                    System.out.println("movie: " + movie);
            //                    break;
        }

        //Selecionar a melhor categoria
        String melhorCategoria = null;
        int maior = -1;

        for (String category : categoriasCount.keySet()) {
            int count = categoriasCount.get(category);

            if (count > maior) {
                maior = count;
                melhorCategoria = category;
            }
        }

        BasicDBObject categoryObject = (BasicDBObject) categoriesCollection
                .findOne(new BasicDBObject("name", melhorCategoria));
        BasicDBList todosMoviesDaCategoria = (BasicDBList) categoryObject.get("movies");

        //                System.out.println("moviesAssistidos: " + moviesAssistidos.size());
        //                System.out.println("todosMoviesDaCategoria: " + todosMoviesDaCategoria.size());
        //Selecionar os filmes da categoria que o user no assistiu
        todosMoviesDaCategoria.removeAll(moviesAssistidos);

        //                System.out.println("todosMoviesDaCategoria: " + todosMoviesDaCategoria.size());
        BasicDBObject selectObject = new BasicDBObject();
        selectObject.put("movieID", new BasicDBObject("$in", todosMoviesDaCategoria));

        BasicDBObject sortObject = new BasicDBObject();
        sortObject.put("rating", -1);
        sortObject.put("timestamp", -1);

        ratesCursor = ratesCollection.find(selectObject);
        ratesCursor.sort(sortObject);
        ratesCursor.limit(5);

        BasicDBList moviesRecomendados = new BasicDBList();

        while (ratesCursor.hasNext()) {
            BasicDBObject rate = (BasicDBObject) ratesCursor.next();

            Integer movieID = rate.getInt("movieID");
            DBObject movie = moviesCollection.findOne(new BasicDBObject("_id", movieID));
            moviesRecomendados.add(movie);
        }

        //            System.out.println("Recomendao para " + user.getInt("_id") + ":");
        //            for (Object movie : moviesRecomendados) {
        //                System.out.println("\t" + movie);
        //            }
        //            System.out.println();

        long finishTimeUser = System.currentTimeMillis();

        System.out.print("Tempo de execuo para recomendar o user " + user.getInt("_id") + ": ");
        System.out.println(((double) (finishTimeUser - startTimeUser)) / 1000.0 + " segundos.");

        soma += ((double) (finishTimeUser - startTimeUser)) / 1000.0;

        //            break;
    }

    System.out.println();

    System.out.println("Mdia de tempo de execuo para cada usurio: " + soma / 943.0 + " segundos.");

    System.out.println();

    long finishTime = System.currentTimeMillis();

    System.out.print("Tempo de execuo do algortmo: ");
    System.out.println(((double) (finishTime - startTime)) / 1000.0 + " segundos.");

}

From source file:stirling.fix.session.store.MongoSessionStore.java

License:Apache License

private Sequence outgoingSeq(BasicDBObject sessionDoc) {
    Sequence seq = new Sequence();
    seq.reset(sessionDoc.getInt("outgoingSeq"));
    return seq;/*from w  w  w.j a va 2  s . c om*/
}

From source file:stirling.fix.session.store.MongoSessionStore.java

License:Apache License

private Sequence incomingSeq(BasicDBObject sessionDoc) {
    Sequence seq = new Sequence();
    seq.reset(sessionDoc.getInt("incomingSeq"));
    return seq;/*from   w  w w  . j av a  2s  .  c  o m*/
}

From source file:tango.dataStructure.Cell.java

License:Open Source License

public Cell(BasicDBObject dbCell, Field f, Experiment xp) {
    this.field = f;
    this.xp = xp;
    this.mc = xp.getConnector();
    this.nbStructures = xp.getNBStructures(false);
    this.channels = new AbstractStructure[xp.getNBStructures(true)];
    this.inputImages = new InputCellImages(this);
    this.segImages = new SegmentedCellImages(this);
    this.idx = dbCell.getInt("idx");
    setName();/*from   ww w.ja v  a  2s. com*/
    this.tag = new Tag(dbCell.getInt("tag", 0));
    this.id = (ObjectId) dbCell.get("_id");
    this.thumbnails = new ImageIcon[xp.getNBFiles()];
}

From source file:tango.gui.DataManager.java

License:Open Source License

private void writeC2CMisc(File output, MultiKey dk, String key, String delimiter) {
    try {//  www.j av  a  2  s  .c  om
        DBCursor cur = mc.getXPNuclei(xp.getName());
        cur.sort(new BasicDBObject("field_id", 1).append("idx", 1));
        int nbNuc = cur.count();
        FileWriter fstream = new FileWriter(output);
        BufferedWriter out = new BufferedWriter(fstream);
        String headers = "nucId" + delimiter + "field" + delimiter + "nuc.idx" + delimiter + "idx" + delimiter
                + key;
        out.write(headers);
        out.newLine();
        while (cur.hasNext()) {
            BasicDBObject nuc = (BasicDBObject) cur.next();
            ObjectId nucId = (ObjectId) nuc.get("_id");
            int nucIdx = nuc.getInt("idx");
            String fieldName = mc.getField((ObjectId) nuc.get("field_id")).getString("name");
            String line = nucId + delimiter + fieldName + delimiter + nucIdx + delimiter;
            //C2C
            BasicDBObject mes = mc.getMeasurementStructure(nucId, dk.getKeys(), true);
            Object o = mes.get(key);
            if (o instanceof BasicDBList) {
                BasicDBList list = ((BasicDBList) o);
                for (int i = 0; i < list.size(); i++) {
                    out.write(line + i + delimiter + list.get(i));
                    out.newLine();
                }
            } else if (o instanceof Number || o instanceof String) {
                out.write(line + "1" + delimiter + o.toString());
                out.newLine();
            }
        }
        out.close();
        cur.close();
    } catch (Exception e) {
        exceptionPrinter.print(e, "extract key: " + key, Core.GUIMode);
    }
}