// FIXME convert to hbird archiver interface and move this to the archiver.
  private static DBObject buildMongoQuery(Map<String, String> aoData) {
    // Get datatables values
    long startTime = Long.parseLong(aoData.get("startTime"));
    long endTime = Long.parseLong(aoData.get("endTime"));
    String search = aoData.get("sSearch");

    // Build mongo query
    // @formatter:off
    DBObject mongoQuery = new BasicDBObject();
    mongoQuery.put(
        "receivedTime", BasicDBObjectBuilder.start("$gte", startTime).add("$lte", endTime).get());

    if (search != null && (!search.isEmpty())) {
      LOG.trace("Adding search query " + search);
      Pattern match = Pattern.compile(search, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
      // Note, normally you would pass the Pattern object to the Java Mongo driver but when using
      // distributed routing
      // over JMS you can only send objectified primitives. This means we have to create the search
      // string ourselves.
      DBObject matchString = new BasicDBObject("$regex", match.toString()).append("$options", "im");
      mongoQuery.put("name", matchString);
    }

    // @formatter:on

    return mongoQuery;
  }
 /* Returns List of Wikipedia page-ids of pages the string 'anchor' points to in Wikipedia */
 public List<Long> getPages(String anchor) {
   db.requestStart();
   List<Long> PageCollection = new ArrayList<Long>();
   BasicDBObject query = new BasicDBObject();
   query.put("anchor", anchor);
   BasicDBObject fields = new BasicDBObject("pages", true).append("_id", false);
   DBObject obj =
       table.findOne(query, fields); // System.out.println("num of results = "+curs.count());
   if (obj != null) {
     JSONParser jp = new JSONParser();
     JSONArray jarr = null;
     try {
       jarr = (JSONArray) jp.parse(obj.get("pages").toString());
     } catch (ParseException e) {
       jarr = new JSONArray();
     }
     // System.out.println("Link Freq = "+o.get("anchPageFreq").toString());
     for (int i = 0; i < jarr.size(); i++) {
       JSONObject objects = (JSONObject) jarr.get(i);
       PageCollection.add((long) (objects.get("page_id")));
     }
   }
   db.requestDone();
   return PageCollection;
 } // End getPages()
  @Test
  public void delete() {
    // setup data to delete
    String id = "deleteTest1";
    DBObject obj = new BasicDBObject();
    obj.put("_id", id);
    WriteResult wr = coll.insert(obj);

    try (DBCursor c = coll.find(null)) {
      Assert.assertEquals("count on collection", 1, c.count());
    }

    // execute delete
    BasicDocDeleter deleter = new BasicDocDeleter();
    CRUDOperationContext ctx = new TestCRUDOperationContext(CRUDOperation.DELETE);
    DBObject mongoQuery = new BasicDBObject();
    mongoQuery.put("_id", id);
    CRUDDeleteResponse response = new CRUDDeleteResponse();
    deleter.delete(ctx, coll, mongoQuery, response);

    Assert.assertTrue(response.getNumDeleted() == 1);

    // verify nothing left in collection
    Assert.assertEquals("count on collection", 0, coll.find(null).count());
  }
  @Test
  public void shouldCorrectlySerialiseMultiPointsInGeometryCollection() {
    // given
    MultiPoint multiPoint = GeoJson.multiPoint(point(1, 2), point(3, 5), point(19, 13));
    GeometryCollection geometryCollection = GeoJson.geometryCollection(multiPoint);

    // when
    DBObject dbObject = getMorphia().toDBObject(geometryCollection);

    assertThat(dbObject, is(notNullValue()));
    assertThat(
        dbObject.toString(),
        JSONMatcher.jsonEqual(
            "  {"
                + "  type: 'GeometryCollection', "
                + "  geometries: "
                + "  ["
                + "    {"
                + "     type: 'MultiPoint', "
                + "     coordinates: [ [ 2.0,  1.0],"
                + "                    [ 5.0,  3.0],"
                + "                    [13.0, 19.0] ]"
                + "    },"
                + "  ]"
                + " }"
                + "}"));
  }
  public void processHostTaskData() {
    try {
      PreparedStatement pstmt = sqlConnection.prepareStatement(Queries.getSqlInsertHostTaskInfo());
      AggregationOutput aggregationOutput =
          mongoTask.getAggregatedOutput("task", Queries.getHostTaskInfoQuery());
      for (DBObject obj : aggregationOutput.results()) {
        String timeStamp = (String) obj.get("timestamp");
        String action = (String) obj.get("action");
        String target = (String) obj.get("target");
        double timeTaken = Double.parseDouble(obj.get("timeTaken").toString());

        pstmt.setTimestamp(
            1,
            new java.sql.Timestamp(
                MongoAggregationHelper.jsonDateFormat.parse(timeStamp).getTime()));
        pstmt.setString(2, target);
        pstmt.setString(3, action);
        pstmt.setDouble(4, timeTaken);
        pstmt.setInt(5, 1);
        pstmt.addBatch();
      }
      pstmt.executeBatch();
    } catch (SQLException | ParseException s) {
      s.printStackTrace();
    }
  }
  @Test
  public void testStore() {
    HasExpiryField hasExpiryField = new HasExpiryField();
    hasExpiryField.setOfferIs("Good");
    Calendar c = Calendar.getInstance();
    hasExpiryField.setOfferExpiresAt(c.getTime());
    ds.getMapper().addMappedClass(HasExpiryField.class);
    ds.ensureIndexes();
    ds.save(hasExpiryField);

    DB db = ds.getDB();
    DBCollection dbCollection = db.getCollection("HasExpiryField");
    List<DBObject> indexes = dbCollection.getIndexInfo();

    Assert.assertNotNull(indexes);
    Assert.assertEquals(2, indexes.size());
    DBObject index = null;
    for (DBObject candidateIndex : indexes) {
      if (candidateIndex.containsField("expireAfterSeconds")) {
        index = candidateIndex;
      }
    }
    Assert.assertNotNull(index);
    Assert.assertTrue(index.containsField("expireAfterSeconds"));
    Assert.assertEquals(60, index.get("expireAfterSeconds"));
  }
  @Test
  public void shouldCorrectlySerialisePointsInGeometryCollection() {
    // given
    Point point = point(3.0, 7.0);
    GeometryCollection geometryCollection = GeoJson.geometryCollection(point);

    // when
    DBObject dbObject = getMorphia().toDBObject(geometryCollection);

    // then use the underlying driver to ensure it was persisted correctly to the database
    assertThat(dbObject, is(notNullValue()));
    assertThat(
        dbObject.toString(),
        JSONMatcher.jsonEqual(
            "  {"
                + "  type: 'GeometryCollection', "
                + "  geometries: "
                + "  ["
                + "    {"
                + "     type: 'Point', "
                + "     coordinates: [7.0, 3.0]"
                + "    }, "
                + "  ]"
                + "}"));
  }
  static String addRemovePrefix(String prefix, String object, boolean add) {
    if (prefix == null) {
      throw new IllegalArgumentException("prefix");
    }
    if (object == null) {
      throw new NullPointerException("object");
    }
    if (object.length() == 0) {
      return "";
    }
    DBObject bsonObject = (DBObject) JSON.parse(object);

    BasicBSONObject newObject = new BasicBSONObject();
    for (String key : bsonObject.keySet()) {
      if (add) {
        newObject.put(prefix + key, bsonObject.get(key));
      } else {
        if (key.startsWith(prefix)) {
          newObject.put(key.substring(prefix.length()), bsonObject.get(key));
        } else {
          newObject.put(key, bsonObject.get(key));
        }
      }
    }
    return newObject.toString();
  }
  /** Loads the list of commands from one game */
  @Override
  public List<Object> getAll(int gameID) {
    try {
      db.start();
      DBCollection collection = db.getDB().getCollection("moves");

      XStream xStream = new XStream();

      DBCursor cursor = collection.find();

      if (cursor == null) return null;

      List<Object> commands = new ArrayList<Object>();

      while (cursor.hasNext()) {
        DBObject obj = cursor.next();
        String xml = (String) obj.get(Integer.toString(gameID));
        if (xml != null) commands.add((Object) xStream.fromXML(xml));
      }

      db.stop(true);

      return commands;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
Beispiel #10
0
  public DBObject toDBObject() {
    DBObject dbObject = new BasicDBObject();
    dbObject.put("review", review.toDBObject());
    dbObject.put("phrases", phrases);

    return dbObject;
  }
  @Test
  public void testBasicQuery() {
    BasicDBObject query = new BasicDBObject();
    query.put(EmployeeProperties.EMPLOYEE_ID, "28241");

    BasicDBObject fields = new BasicDBObject();
    fields.put(EmployeeProperties.ID, 0);
    fields.put(EmployeeProperties.LAST_NAME, 1);
    fields.put(EmployeeProperties.EMPLOYEE_ID, 1);

    DBCollection collection = mongoOps.getCollection(EmployeeProperties.COLLECTION);
    log.info(
        "MongoDB Query Explain Plan:  "
            + collection
                .find(query, fields)
                .hint(new BasicDBObject(EmployeeProperties.EMPLOYEE_ID, 1))
                .explain());
    DBCursor cursor =
        collection.find(query, fields).hint(new BasicDBObject(EmployeeProperties.EMPLOYEE_ID, 1));

    log.info(cursor.count() + "");

    assertNotNull("cursor was null.", cursor);
    assertEquals("cursor count was not 1.", 1, cursor.count());

    log.info("Query (" + query.toString() + ")");

    while (cursor.hasNext()) {
      DBObject dbo = cursor.next();
      log.info(dbo.toString());
      assertEquals("Keyset size not equal to 2.", 2, dbo.keySet().size());
    }

    log.info(cursor.explain().toString());
  }
 public void ensureCaps() {
   for (MappedClass mc : mapr.getMappedClasses())
     if (mc.getEntityAnnotation() != null && mc.getEntityAnnotation().cap().value() > 0) {
       CappedAt cap = mc.getEntityAnnotation().cap();
       String collName = mapr.getCollectionName(mc.getClazz());
       BasicDBObjectBuilder dbCapOpts = BasicDBObjectBuilder.start("capped", true);
       if (cap.value() > 0) dbCapOpts.add("size", cap.value());
       if (cap.count() > 0) dbCapOpts.add("max", cap.count());
       DB db = getDB();
       if (db.getCollectionNames().contains(collName)) {
         DBObject dbResult = db.command(BasicDBObjectBuilder.start("collstats", collName).get());
         if (dbResult.containsField("capped")) {
           // TODO: check the cap options.
           log.warning("DBCollection already exists is cap'd already; doing nothing. " + dbResult);
         } else {
           log.warning(
               "DBCollection already exists with same name("
                   + collName
                   + ") and is not cap'd; not creating cap'd version!");
         }
       } else {
         getDB().createCollection(collName, dbCapOpts.get());
         log.debug("Created cap'd DBCollection (" + collName + ") with opts " + dbCapOpts);
       }
     }
 }
Beispiel #13
0
 /**
  * Hydrates this from a DBObject - used when reading chunks from the database
  *
  * @param o object to get data from
  */
 GridFSChunk(DBObject o) {
   _id = (ObjectId) o.get("_id");
   _files_id = (ObjectId) o.get("files_id");
   _n = (Integer) o.get("n");
   _data = (byte[]) o.get("data");
   _size = _data.length;
 }
 /* Returns map of Wikipedia page-ids to number of inlinks to those pages. Page ids are pages the string 'anchor' points to in Wikipedia */
 public Map<Long, Integer> getPagesMap(String anchor) {
   db.requestStart();
   Map<Long, Integer> PageCollection = new HashMap<Long, Integer>();
   BasicDBObject query = new BasicDBObject();
   query.put("anchor", anchor);
   BasicDBObject fields =
       new BasicDBObject("page_id", true)
           .append("pages", true)
           .append("page_freq", true)
           .append("anchor_freq", true)
           .append("_id", false);
   DBObject ans =
       table.findOne(query, fields); // System.out.println("num of results = "+curs.count());
   db.requestDone();
   if (ans != null) {
     JSONParser jp = new JSONParser();
     JSONArray jo = null;
     try { // System.out.println(ans.get("pages"));
       jo = (JSONArray) jp.parse(ans.get("pages").toString());
     } catch (ParseException e) {
       e.printStackTrace();
     } // System.out.println("Link Freq = "+o.get("anchPageFreq").toString());
     for (int i = 0; i < jo.size(); i++) {
       JSONObject object = (JSONObject) jo.get(i);
       Long pId = (long) (object.get("page_id"));
       Long pValue0 = (long) object.get("page_freq");
       int pValue = pValue0.intValue();
       if (PageCollection.containsKey(pId)) {
         pValue = PageCollection.get(pId) + pValue;
       }
       PageCollection.put(pId, pValue);
     }
   }
   return PageCollection;
 } // End getPagesMap()
  @Test
  public void convertsJodaTimeTypesCorrectly() {

    List<Converter<?, ?>> converters = new ArrayList<Converter<?, ?>>();
    converters.add(new LocalDateToDateConverter());
    converters.add(new DateToLocalDateConverter());

    List<Class<?>> customSimpleTypes = new ArrayList<Class<?>>();
    customSimpleTypes.add(LocalDate.class);
    mappingContext.setCustomSimpleTypes(customSimpleTypes);

    converter = new MappingMongoConverter(mappingContext);
    converter.setConverters(converters);
    converter.afterPropertiesSet();

    Person person = new Person();
    person.birthDate = new LocalDate();

    DBObject dbObject = new BasicDBObject();
    converter.write(person, dbObject);

    assertTrue(dbObject.get("birthDate") instanceof Date);

    Person result = converter.read(Person.class, dbObject);
    assertThat(result.birthDate, is(notNullValue()));
  }
 private void toMongoFormat(final DBObject indexObj) {
   Set<String> indexKeys = indexObj.keySet();
   for (String indexKey : indexKeys) {
     Double indexValue = (Double) indexObj.get(indexKey);
     indexObj.put(indexKey, indexValue.intValue());
   }
 }
 public Person_login createPerson(Person_login p) {
   DBObject doc = PersonConverter.toDBObject(p);
   this.col.insert(doc);
   ObjectId id = (ObjectId) doc.get("_id");
   p.setId(id.toString());
   return p;
 }
Beispiel #18
0
    @Override
    public int compare(Object o1, Object o2) {
      if (isDBObjectButNotDBList(o1) && isDBObjectButNotDBList(o2)) {
        DBObject dbo1 = (DBObject) o1;
        DBObject dbo2 = (DBObject) o2;
        for (String sortKey : orderByKeySet) {
          final List<String> path = Util.split(sortKey);
          int sortDirection = (Integer) orderBy.get(sortKey);

          List<Object> o1list = getEmbeddedValues(path, dbo1);
          List<Object> o2list = getEmbeddedValues(path, dbo2);

          int compareValue = compareLists(o1list, o2list) * sortDirection;
          if (compareValue != 0) {
            return compareValue;
          }
        }
        return 0;
      } else if (isDBObjectButNotDBList(o1) || isDBObjectButNotDBList(o2)) {
        DBObject dbo = (DBObject) (o1 instanceof DBObject ? o1 : o2);
        for (String sortKey : orderByKeySet) {
          final List<String> path = Util.split(sortKey);
          int sortDirection = (Integer) orderBy.get(sortKey);

          List<Object> foundValues = getEmbeddedValues(path, dbo);

          if (!foundValues.isEmpty()) {
            return o1 instanceof DBObject ? sortDirection : -sortDirection;
          }
        }
        return compareTo(o1, o2);
      } else {
        return compareTo(o1, o2);
      }
    }
  protected void showDocument(DBObject doc, String level) {
    PrintStream pout = System.out;
    for (String key : doc.keySet()) {
      pout.print(level);
      pout.print(key);
      pout.print(" = ");

      Object value = doc.get(key);
      if (value instanceof DBObject) {
        DBObject child = (DBObject) value;
        pout.println();
        showDocument(child, level + "    ");
      } else {
        if (value instanceof String) {
          pout.print("\"");
        }

        pout.print(value);
        if (value instanceof String) {
          pout.print("\"");
        }

        pout.println();
      }
    }
  }
Beispiel #20
0
 @Test
 public void testConvertJavaListToDbList() {
   DBCollection collection = newCollection();
   collection.insert(new BasicDBObject("_id", 1).append("n", Arrays.asList(1, 2)));
   DBObject result = collection.findOne();
   assertTrue("not a DBList", result.get("n") instanceof BasicDBList);
 }
  @Test
  public void shouldCorrectlySerialiseLineStringsInGeometryCollection() {
    // given
    LineString lineString = lineString(point(1, 2), point(3, 5), point(19, 13));
    GeometryCollection geometryCollection = GeoJson.geometryCollection(lineString);
    getMorphia().getMapper().addMappedClass(Point.class);

    // when
    DBObject dbObject = getMorphia().toDBObject(geometryCollection);

    assertThat(dbObject, is(notNullValue()));
    assertThat(
        dbObject.toString(),
        JSONMatcher.jsonEqual(
            "  {"
                + "  type: 'GeometryCollection', "
                + "  geometries: "
                + "  ["
                + "    {"
                + "     type: 'LineString', "
                + "     coordinates: [ [ 2.0,  1.0],"
                + "                    [ 5.0,  3.0],"
                + "                    [13.0, 19.0] ]"
                + "    },"
                + "  ]"
                + "}"));
  }
Beispiel #22
0
 @Test
 public void testConvertJavaMapToDBObject() {
   DBCollection collection = newCollection();
   collection.insert(new BasicDBObject("_id", 1).append("n", Collections.singletonMap("a", 1)));
   DBObject result = collection.findOne();
   assertTrue("not a DBObject", result.get("n") instanceof BasicDBObject);
 }
  public void processHostData() {
    try {

      PreparedStatement pstmt = sqlConnection.prepareStatement(Queries.getSqlInsertHostInfo());
      AggregationOutput aggregationOutput =
          mongoTask.getAggregatedOutput("host", Queries.getHostInfoQuery());
      for (DBObject obj : aggregationOutput.results()) {
        String hostname = (String) obj.get("hostname");
        Double cpuUsage = Double.parseDouble(obj.get("cpuusage").toString());
        Double cpuMax = Double.parseDouble(obj.get("cpumax").toString());
        Double cpuPercent = Double.parseDouble(obj.get("cpupercentage").toString());
        Double memUsage = Double.parseDouble(obj.get("memusage").toString());
        Double memMax = Double.parseDouble(obj.get("memmax").toString());
        Double memPercent = Double.parseDouble(obj.get("mempercentage").toString());
        Double upTime = Double.parseDouble(obj.get("uptime").toString());
        Double tx = Double.parseDouble(obj.get("tx").toString());
        Double rx = Double.parseDouble(obj.get("rx").toString());
        pstmt.setTimestamp(1, new java.sql.Timestamp(timeStamp.getTime()));
        pstmt.setString(2, hostname);
        pstmt.setDouble(3, cpuUsage);
        pstmt.setDouble(4, cpuMax);
        pstmt.setDouble(5, cpuPercent);
        pstmt.setDouble(6, memUsage);
        pstmt.setDouble(7, memMax);
        pstmt.setDouble(8, memPercent);
        pstmt.setDouble(9, upTime);
        pstmt.setDouble(10, tx);
        pstmt.setDouble(11, rx);
        pstmt.addBatch();
      }
      pstmt.executeBatch();
    } catch (SQLException s) {
      s.printStackTrace();
    }
  }
  /*------------------------------------------------------------ */
  @Override
  protected void invalidateSession(String idInCluster) {
    __log.debug("MongoSessionManager:invalidateSession:invalidating " + idInCluster);

    super.invalidateSession(idInCluster);

    /*
     * pull back the 'valid' value, we can check if its false, if is we don't need to
     * reset it to false
     */
    DBObject validKey = new BasicDBObject(__VALID, true);
    DBObject o = _sessions.findOne(new BasicDBObject(__ID, idInCluster), validKey);

    if (o != null && (Boolean) o.get(__VALID)) {
      BasicDBObject update = new BasicDBObject();
      BasicDBObject sets = new BasicDBObject();
      sets.put(__VALID, false);
      sets.put(__INVALIDATED, System.currentTimeMillis());
      update.put("$set", sets);

      BasicDBObject key = new BasicDBObject(__ID, idInCluster);

      _sessions.update(key, update);
    }
  }
 private void deleteCall(DBObject call) throws UnknownHostException {
   if (call.containsField("_id")) {
     Resources.getDatabaseActivity()
         .getCollection("MissedCalls")
         .remove(new BasicDBObject("_id", call.get("_id")));
   }
 }
Beispiel #26
0
 private void checkIndex(final DBObject dbObject) {
   assertTrue((Boolean) dbObject.get("background"));
   assertTrue((Boolean) dbObject.get("unique"));
   assertTrue((Boolean) dbObject.get("sparse"));
   assertEquals(42L, dbObject.get("expireAfterSeconds"));
   assertEquals(new BasicDBObject("name", 1).append("text", -1), dbObject.get("key"));
 }
  private void getTweetWithRTCountGreaterThan(int gt) {

    Mongo mongo = null;
    try {
      mongo = new Mongo(MONGO_DB_HOST, MONGO_DB_PORT);
      DB db = mongo.getDB(MONGO_DB_NAME);
      DBCollection collection = db.getCollection(MONGO_DB_COLLECTION);

      DBCursor cursor = collection.find(new BasicDBObject("rTCount", new BasicDBObject("$gt", gt)));

      DBObject dbObject;

      System.out.println("\n\n========================================");
      System.out.println("Displaying Tweet with RTCount > " + gt);

      while (cursor.hasNext()) {
        dbObject = cursor.next();
        System.out.println(
            "user "
                + dbObject.get("user")
                + "tweet "
                + dbObject.get("tweet")
                + " RTCount "
                + dbObject.get("rTCount"));
      }

      System.out.println("========================================\n\n");

    } catch (UnknownHostException e) {
      e.printStackTrace();
    } finally {
      if (mongo != null) mongo.close();
    }
  }
Beispiel #28
0
  /**
   * 获取表"collectionName"的主键id
   *
   * @param collectionName
   * @return
   */
  public Long getIndexIdFromMongo(String collectionName) {

    long indexNum = 0;
    try {
      // 主键存储表名:index
      DBCollection collection = getCollection("TableName.INDEX_TABLE_NAME");
      // 获取某个应用对象存储表的表名
      BasicDBObject oldDB = new BasicDBObject("name", collectionName);
      BasicDBObject update = new BasicDBObject("$inc", new BasicDBObject("id", 1));
      DBObject reFields = new BasicDBObject();
      reFields.put("id", "id");

      // 自增主键id
      DBObject result = collection.findAndModify(oldDB, reFields, null, false, update, true, true);
      if (result.get("id") == null) {
        throw new RuntimeException("获取主键id异常,请重试!!!");
      }
      indexNum = Long.parseLong(result.get("id").toString());
      if (indexNum <= 0) {
        throw new RuntimeException("获取主键id异常,请重试!!!");
      }
    } catch (Exception e) {
      logger.error("BaseMongoDAO.countRecord查询数据条数时发生异常,入参collectionName=" + collectionName, e);
    }
    return indexNum;
  }
Beispiel #29
0
  @Override
  public String Json(String nombreDB, int clienteId) throws UnknownHostException, JSONException {
    // TODO Auto-generated method stub
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    DB base = mongoClient.getDB(nombreDB);

    DBCollection collection = base.getCollection("Json");
    BasicDBObject query = new BasicDBObject();
    query.put("id", clienteId);
    DBCursor cursor = collection.find(query);

    if (cursor.size() == 0) {
      //        	System.out.println("********************\n");
      //        	System.out.println("No existe el cliente, no se puede ingresar json");
      //        	System.out.println("********************\n");

      return "No existe el cliente, no se puede ingresar json";
    }
    // Existe el cliente

    DBObject objeto = (DBObject) cursor.next();

    DBObject json = (DBObject) objeto.get("json");
    //  DBObject j = JSON.parse(json.toString());
    JSONObject aj = new JSONObject(json.toString());

    return aj.toString();
  }
  public void categorizarFotografia(ObjectId id, String emocionTutor, String tutor) {
    DBObject set = new BasicDBObject();
    set.put("emocionTutor", emocionTutor);
    set.put("tutor", tutor);

    bitacoraFotografias.update(new BasicDBObject("_id", id), new BasicDBObject("$set", set));
  }