// 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;
  }
Example #2
1
 // ----This function gets as a parameter a list of terms----
 // ---------the function returns a list of tweet ids from collection search_results-----------
 // ----------------------------------------------------------------------------------------
 public LinkedList<String> get_tweets(LinkedList<String> search_terms) {
   log4j.info("starting function get_tweets");
   LinkedList<String> result = new LinkedList<String>();
   Iterator<String> terms = search_terms.iterator();
   long curr_time = System.currentTimeMillis();
   long min_time = curr_time - this.frame_time; // time below min_time will be ignored
   int count_all = 0; // tweets counter
   while (terms.hasNext()) {
     int count = 0;
     String term = terms.next();
     DBObject st = new BasicDBObject();
     try {
       st.put("searchword", term);
       DBObject obj = this.collsr.findOne(st); // look for the relevant document
       String[] tweets_plus_time =
           obj.get("tweets")
               .toString()
               .split(","); // make an array, even indexes are tweet_id's and odd indexes are their
       // time
       String new_string =
           ""; // the string to replace eventually the current field 'tweets' in the document
       for (int i = 0;
           i < tweets_plus_time.length - 1;
           i += 2) // go over the tweet ids from the document
       {
         if (Long.parseLong(tweets_plus_time[i + 1])
             >= min_time) // tweet time is within the time frame
         {
           result.add(tweets_plus_time[i]); // add tweet id to result
           count++;
           if (new_string == "") // add tweet information without leading comma
           {
             new_string += tweets_plus_time[i] + "," + tweets_plus_time[i + 1];
             // count++;
           } else // add tweet information with leading comma
           {
             new_string += "," + tweets_plus_time[i] + "," + tweets_plus_time[i + 1];
           }
         }
       }
       count_all += count;
       log4j.info(count + " tweets for term: " + term);
       obj.put("tweets", new_string); // replace 'tweets' field
       obj.put("last_update", System.currentTimeMillis()); // update time of update
       collsr.save(obj);
     } catch (NullPointerException e) {
       log4j.info("search_term: " + term + ", is not in collection search_results");
     }
   }
   log4j.info("over_all there are " + count_all + " tweets to compare!!!");
   log4j.info("ending function get_tweets");
   return result;
 }
  @Override
  public void insertModel(Server model) {
    BasicDBObject dbServer = new BasicDBObject();
    dbServer.put("_id", model.getId());
    dbServer.put("created_at", model.getCreated_at());
    dbServer.put("updated_at", model.getUpdated_at());
    dbServer.put("network_id", model.getNetwork().getId().toString());
    dbServer.put("node_id", null);
    dbServer.put("server_type_id", model.getServerType().getId().toString());
    dbServer.put("port", 0);
    dbServer.put("container", "NULL");
    dbServer.put("players", new BasicDBObject());

    BasicDBList metaDataList = new BasicDBList();
    for (Map.Entry<String, ServerMetaData> metaDataEntry : model.getMetaData().entrySet()) {
      DBObject dbMetaData = new BasicDBObject();
      dbMetaData.put("_id", metaDataEntry.getValue().getId());
      dbMetaData.put("created_at", metaDataEntry.getValue().getCreated_at());
      dbMetaData.put("updated_at", metaDataEntry.getValue().getUpdated_at());
      dbMetaData.put("key", metaDataEntry.getValue().getKey());
      dbMetaData.put("value", metaDataEntry.getValue().getValue());
      metaDataList.add(dbMetaData);
    }
    dbServer.put("metaData", metaDataList);

    dbServer.put("number", 0);

    getDatabase().insert("servers", dbServer);
  }
  @Override
  public void saveModel(Server model) {
    BasicDBObject dbServer = new BasicDBObject();
    dbServer.put("updated_at", model.getUpdated_at());
    if (model.getNode() != null) {
      dbServer.put("node_id", model.getNode().getId().toString());
    }
    dbServer.put("port", model.getPort());
    dbServer.put("container", model.getContainerId());
    dbServer.put("players", new BasicDBObject(model.getPlayers()));

    BasicDBList metaDataList = new BasicDBList();
    for (Map.Entry<String, ServerMetaData> metaDataEntry : model.getMetaData().entrySet()) {
      DBObject dbMetaData = new BasicDBObject();
      dbMetaData.put("_id", metaDataEntry.getValue().getId());
      dbMetaData.put("created_at", metaDataEntry.getValue().getCreated_at());
      dbMetaData.put("updated_at", metaDataEntry.getValue().getUpdated_at());
      dbMetaData.put("key", metaDataEntry.getValue().getKey());
      dbMetaData.put("value", metaDataEntry.getValue().getValue());
      metaDataList.add(dbMetaData);
    }

    dbServer.put("metaData", metaDataList);

    dbServer.put("number", model.getNumber());
    getDatabase()
        .updateDocument(
            "servers",
            new BasicDBObject("_id", model.getId()),
            new BasicDBObject("$set", dbServer));
  }
 private void importActivity(JSONObject activity, String portal_image_url) {
   // System.out.println(activity.toString());
   try {
     JSONObject member = activity.getJSONObject("member");
     if (member.containsKey("id")) {
       String title =
           activity.getString("body").replace(activity.getString("image_large_url"), "").trim();
       DBObject photo = new BasicDBObject();
       photo.put("portal_image_url", portal_image_url);
       if (mPhotoCollection.count(photo) > 0) {
         System.err.println(portal_image_url + " already exists");
         return;
       } else {
         System.out.println("Inserting " + portal_image_url);
       }
       String imageSourceUrl =
           Config.PORTAL_URL.startsWith("http:")
               ? portal_image_url.replace("https://", "http://")
               : portal_image_url;
       BufferedImage[] images = ImageUtil.convertImage(new URL(imageSourceUrl));
       if (images == null) {
         System.err.println(portal_image_url + " corrupted");
         return;
       }
       saveThumbnailImages(photo, images, "/" + getFileId(portal_image_url) + ".png");
       photo.put("uploader_user_id", member.getString("id"));
       photo.put("title", title);
       Object exifObj = getExifData(portal_image_url);
       Date date = null;
       if (exifObj instanceof JSONObject) {
         JSONObject exif = (JSONObject) exifObj;
         if (exif.containsKey("date")) {
           try {
             date = FORMAT_DATE_ISO.parse(exif.getString("date"));
           } catch (ParseException e) {
             e.printStackTrace();
           }
         }
         if (exif.containsKey("location")) {
           photo.put("exif_location", exif.getJSONArray("location"));
         }
       }
       if (date == null && activity.containsKey("created_at")) {
         try {
           date = FORMAT_DATE.parse(activity.getString("created_at"));
         } catch (ParseException e) {
           e.printStackTrace();
         }
       }
       if (date != null) {
         photo.put("exif_date", date);
       }
       mPhotoCollection.insert(photo);
       // System.out.println(photo);
       // System.out.println();
     }
   } catch (JSONException | MalformedURLException e) {
     e.printStackTrace();
   }
 }
Example #6
0
  public DBObject toDBObject() {
    DBObject dbObject = new BasicDBObject();
    dbObject.put("review", review.toDBObject());
    dbObject.put("phrases", phrases);

    return dbObject;
  }
  @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());
  }
  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));
  }
  /*
   * (non-Javadoc)
   *
   * @see org.ow2.play.service.registry.api.Registry#put(java.lang.String,
   * java.lang.String)
   */
  @Override
  @WebMethod
  public void put(String name, String url) throws RegistryException {
    if (logger.isLoggable(Level.FINE)) {
      logger.fine(String.format("Put url %s for name %s", url, name));
    }
    checkInitialized();

    if (name == null || url == null) {
      throw new RegistryException("Can not put null values name = %s, url = %s", name, url);
    }

    // update the entry if it already exists
    DBObject filter = new BasicDBObject();
    filter.put(NAME_KEY, name);

    DBObject filtered = collection.findOne(filter);
    if (filtered != null) {
      filtered.put(URL_KEY, url);
      collection.save(filtered);
    } else {
      DBObject o = new BasicDBObject();
      o.put(NAME_KEY, name);
      o.put(URL_KEY, url);
      collection.insert(o);
    }
  }
  /**
   * Writes the given {@link Map} to the given {@link DBObject} considering the given {@link
   * TypeInformation}.
   *
   * @param obj must not be {@literal null}.
   * @param dbo must not be {@literal null}.
   * @param propertyType must not be {@literal null}.
   * @return
   */
  protected DBObject writeMapInternal(
      Map<Object, Object> obj, DBObject dbo, TypeInformation<?> propertyType) {

    for (Map.Entry<Object, Object> entry : obj.entrySet()) {

      Object key = entry.getKey();
      Object val = entry.getValue();

      if (conversions.isSimpleType(key.getClass())) {

        String simpleKey = prepareMapKey(key);
        if (val == null || conversions.isSimpleType(val.getClass())) {
          writeSimpleInternal(val, dbo, simpleKey);
        } else if (val instanceof Collection || val.getClass().isArray()) {
          dbo.put(
              simpleKey,
              writeCollectionInternal(
                  asCollection(val), propertyType.getMapValueType(), new BasicDBList()));
        } else {
          DBObject newDbo = new BasicDBObject();
          TypeInformation<?> valueTypeInfo =
              propertyType.isMap() ? propertyType.getMapValueType() : ClassTypeInformation.OBJECT;
          writeInternal(val, newDbo, valueTypeInfo);
          dbo.put(simpleKey, newDbo);
        }
      } else {
        throw new MappingException("Cannot use a complex object as a key value.");
      }
    }

    return dbo;
  }
  public List<DBObject> getSourceBrowsers() {

    DBObject groupFields = new BasicDBObject("_id", "$device");
    groupFields.put("sum", new BasicDBObject("$sum", "$sum"));
    DBObject group = new BasicDBObject("$group", groupFields);
    DBObject sort = new BasicDBObject("$sort", new BasicDBObject("sum", 1));

    List<DBObject> pipeline = Arrays.asList(group, sort);

    // allowDiskUse
    AggregationOptions options =
        AggregationOptions.builder().allowDiskUse(true).batchSize(10000).build();
    Cursor cursor = device.aggregate(pipeline, options);

    List<DBObject> list = new ArrayList<DBObject>();
    while (cursor.hasNext()) {
      DBObject object = cursor.next();
      DBObject newObj = new BasicDBObject();
      newObj.put("device", object.get("_id"));
      newObj.put("sum", object.get("sum"));
      list.add(newObj);
    }
    cursor.close();
    return list;
  }
Example #12
0
 public DBObject create(DBCollection collection, Object variomlObject, String asNamed) {
   DBObject d = decoder.decode(toBSON4MONGO(variomlObject), collection);
   ObjectId id = generateId();
   DBObject _new = (DBObject) JSON.parse("{}");
   _new.put(asNamed, d);
   _new.put("_id", id);
   return _new;
 }
  public void categorizarFotografiaUsuario(ObjectId id, String usuario, String emocion) {
    DBObject object = new BasicDBObject();
    object.put("_id", id);
    object.put("usuario", usuario);
    object.put("emocion", emocion);

    bitacoraFotografiasUsuario.save(object);
  }
  /** @see DATADOC-128 */
  @Test
  public void ignoresDocumentsStoredTypeIfCompletelyDifferentTypeRequested() {

    DBObject dbObject = new BasicDBObject();
    dbObject.put("birthDate", new LocalDate());
    dbObject.put(MappingMongoConverter.CUSTOM_TYPE_KEY, Person.class.getName());

    assertThat(converter.read(BirthDateContainer.class, dbObject), is(BirthDateContainer.class));
  }
Example #15
0
  public DBObject to_DBObject() {
    DBObject data = new BasicDBObject();

    data.put("vm_id", String.valueOf(get_vm_id()));
    data.put("vm_port", String.valueOf(get_vm_port()));
    data.put("hwaddress", get_hwaddress());

    return data;
  };
  /** @see DATADOC-128 */
  @Test
  public void usesDocumentsStoredTypeIfSubtypeOfRequest() {

    DBObject dbObject = new BasicDBObject();
    dbObject.put("birthDate", new LocalDate());
    dbObject.put(MappingMongoConverter.CUSTOM_TYPE_KEY, Person.class.getName());

    assertThat(converter.read(Contact.class, dbObject), is(Person.class));
  }
 @Override
 public DBObject convert(Chart chart) {
   DBObject dbo = new BasicDBObject();
   if ((chart != null) && (chart.getId() != null))
     dbo.put("_id", new ObjectId(chart.getId().toString(16)));
   else dbo.put("_id", new ObjectId(new Date()));
   dbo.put("key", chart.getKey());
   return dbo;
 }
Example #18
0
 /**
  * SQL: SELECT fname FROM `employee` WHERE `salary` > 25000 order by fname ASC
  *
  * @param collection
  * @return
  */
 private static DBCursor sqlSort(DBCollection collection) {
   // TODO Auto-generated method stub
   DBObject obj = new BasicDBObject();
   DBObject field = new BasicDBObject();
   field.put("_id", 0);
   field.put("fname", 1);
   obj.put("salary", new BasicDBObject("$gt", 25000));
   return collection.find(obj, field).sort(new BasicDBObject("fname", 1));
 }
Example #19
0
 @Override
 public void write(String hash, String way) throws InterruptedException {
   DBObject dbObject = new BasicDBObject();
   dbObject.put("hash", hash);
   if (!isContain(dbObject)) {
     dbObject.put("way", way);
     hashColl.insert(dbObject);
   }
   Thread.sleep(300);
 }
  public void reiniciarSesion(Long idUsuario) {
    synchronized (bitacoraFotografias) {
      DBObject query = new BasicDBObject("usuario", idUsuario);
      query.put("sesionactual", true);
      DBObject update = new BasicDBObject();
      update.put("$set", new BasicDBObject("sesionactual", false));

      bitacoraFotografias.update(query, update, false, true);
    }
  }
Example #21
0
  private void combineUpdate(String field, String op, Object value) throws Exception {
    if (update == null) update = new BasicDBObject();

    DBObject o = (DBObject) update.get(op);
    if (o == null) update.put(op, o = new BasicDBObject());

    assert store.checkField(field, value);
    if (value instanceof Enum) value = value.toString();
    o.put(field, value);
  }
 /**
  * @param col SQL: Select fname, address FROM employee Where salary > 40000
  * @return
  */
 private static DBCursor sqlWhereTwoSelect(DBCollection col) {
   // TODO Auto-generated method stub
   DBObject obj = new BasicDBObject();
   DBObject fields = new BasicDBObject();
   fields.put("fname", 1);
   fields.put("address", 1);
   fields.put("_id", 0);
   obj.put("salary", new BasicDBObject("$gt", 40000));
   return col.find(obj, fields);
 }
  @Override
  public PageVO findAllOnePageByOwnerId(String ownerId) {

    DBObject queryCondition = new BasicDBObject();
    queryCondition.put("owner_id", ownerId);

    DBObject sort = new BasicDBObject();
    sort.put("type_value", 1);

    return batchSearchOnePage(queryCondition, sort, null);
  }
 protected void internalAddDBObject(
     DBObject object, String criteria, String column, Object value) {
   if (criteria.startsWith("$")) {
     Object subObject = object.get(criteria);
     if (subObject != null && subObject instanceof DBObject) {
       ((DBObject) subObject).put(column, value);
     } else {
       DBObject newCriteriaObject = new BasicDBObject(column, value);
       object.put(criteria, newCriteriaObject);
     }
   }
 }
Example #25
0
 /** 创建索引</br> 日期:2014-3-6 下午04:57:59 */
 @Test
 public void createIndex() {
   DBObject index = new BasicDBObject();
   // combine name&age as hybrid index
   //		index.put("secretStatus", 1);
   //		index.put("secretLevel", 1);
   //		index.put("timelimitType", 1);
   //
   index.put("clientId", 1);
   index.put("fileId", 1);
   bakFiles.createIndex(index);
 }
 /** Build the Mongo DBObject from the Map record. */
 public DBObject buildDBObject(MongoRecord record) {
   DBObject object = new BasicDBObject();
   for (Iterator iterator = record.entrySet().iterator(); iterator.hasNext(); ) {
     Map.Entry entry = (Map.Entry) iterator.next();
     if (entry.getValue() instanceof MongoRecord) {
       object.put((String) entry.getKey(), buildDBObject((MongoRecord) entry.getValue()));
     } else {
       object.put((String) entry.getKey(), entry.getValue());
     }
   }
   return object;
 }
Example #27
0
 public boolean addToken(Long id, String token) {
   try {
     DBObject object = new BasicDBObject();
     object.put("id", id);
     DBObject value = new BasicDBObject();
     value.put("token", token);
     return getDBCollection(TABLE_NAME).update(object, new BasicDBObject("$set", value)).getN()
         > -1;
   } catch (Exception e) {
     LOG.error(e.getMessage(), e);
   }
   return false;
 }
 protected DBObject createDBObjectWithKey(Object key) {
   DBObject dbo = new BasicDBObject();
   if (hasNumericalIdentifier || hasStringIdentifier) {
     dbo.put(MONGO_ID_FIELD, key);
   } else {
     if (key instanceof ObjectId) {
       dbo.put(MONGO_ID_FIELD, key);
     } else {
       dbo.put(MONGO_ID_FIELD, new ObjectId(key.toString()));
     }
   }
   return dbo;
 }
  @Override
  protected void setEmbeddedCollection(
      final DBObject nativeEntry,
      final String key,
      Collection<?> instances,
      List<DBObject> embeddedEntries) {
    if (instances == null || instances.isEmpty()) {
      nativeEntry.put(key, null);
      return;
    }

    nativeEntry.put(key, embeddedEntries);
  }
Example #30
0
 @Test
 public void tests() {
   DBObject query = new BasicDBObject(); // 查询条件
   query.put("secretStatus", 1);
   query.put("secretLevel", 2);
   query.put("timelimitType", 1);
   DBObject fields = new BasicDBObject(); // 显示字段
   fields.put("_id", 0);
   fields.put("fid", 1);
   DBObject sort = new BasicDBObject(); // 排序字段
   sort.put("fid", 1);
   System.out.println(bakFiles.find(query, fields).sort(sort).skip(100 * 0).limit(100).toArray());
 }