コード例 #1
0
 public void addCardInfoToNeo4j() {
   DBCursor cursor = cardCollection.find();
   DBObject sortObject = new BasicDBObject();
   sortObject.put("name", 1);
   // cursor = cursor.sort(sortObject);
   cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
   int count = 0;
   while (cursor.hasNext()) {
     DBObject cardObject = cursor.next();
     String cardName = cardObject.get("name").toString().toUpperCase();
     if (cardNodes.get(cardName) == null) {
       Node newCardNode = graphDb.createNode();
       newCardNode.setProperty("name", cardName);
       for (String property : cardObject.keySet()) {
         if (!property.equalsIgnoreCase("name")) {
           newCardNode.setProperty(property, cardObject.get(property).toString());
         }
       }
       cardNodes.put(cardName, newCardNode);
     } else {
       Node node = cardNodes.get(cardName);
       for (String property : cardObject.keySet()) {
         if (!property.equalsIgnoreCase("name")) {
           node.setProperty(property, cardObject.get(property).toString());
         }
       }
     }
     count++;
     System.out.println("Processed " + count + " card");
   }
 }
コード例 #2
0
  @Override
  public List<Favorite> getFavorites(String email) throws UserNotFound {
    if (email == null) {
      throw new IllegalArgumentException("Email cannot be null");
    }
    DBCollection col = db.getCollection(MongoDBConstants.COLLECTION_USERS);
    // Search user by email
    DBObject userDb = col.findOne(new BasicDBObject(MongoDBConstants.USER_PROP_EMAIL, email));
    if (userDb == null) {
      throw new UserNotFound("Unable to find user with email '" + email + "'");
    }

    DBObject favoritesDb = (DBObject) userDb.get(MongoDBConstants.USER_PROP_FAVORITES);
    if (favoritesDb == null) {
      favoritesDb = new BasicDBObject();

      userDb.put(MongoDBConstants.USER_PROP_FAVORITES, favoritesDb);
      col.save(userDb);
    }

    List<Favorite> returnValue = new ArrayList<Favorite>(favoritesDb.keySet().size());
    for (String stationId : favoritesDb.keySet()) {
      Favorite favorite = new Favorite();
      favorite.setStationId(stationId);
      DBObject favoriteItemsDb = (DBObject) favoritesDb.get(stationId);
      favorite.setLastMessageId(
          (Long) favoriteItemsDb.get(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID));
      returnValue.add(favorite);
    }
    return returnValue;
  }
コード例 #3
0
 private DBObject modifyNullsToUnsets(DBObject nativeEntry) {
   DBObject unsets = new BasicDBObject();
   DBObject sets = new BasicDBObject();
   for (String key : nativeEntry.keySet()) {
     Object o = nativeEntry.get(key);
     if (o == null) {
       unsets.put(key, 1);
     } else if ("_id".equals(key)) {
     } else if (o instanceof Object[]) {
       sets.put(key, o);
       for (Object o2 : (Object[]) o) {
         if (o2 instanceof DBObject) {
           removeNullEntries((DBObject) o2);
         }
       }
     } else if (o instanceof List) {
       sets.put(key, o);
       for (Object o2 : (List) o) {
         if (o2 instanceof DBObject) {
           removeNullEntries((DBObject) o2);
         }
       }
     } else if (o instanceof DBObject) {
       sets.put(key, removeNullEntries((DBObject) o));
     } else {
       sets.put(key, o);
     }
   }
   DBObject newEntry = new BasicDBObject();
   newEntry.put("$set", sets);
   if (!unsets.keySet().isEmpty()) {
     newEntry.put("$unset", unsets);
   }
   return newEntry;
 }
コード例 #4
0
  @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());
  }
コード例 #5
0
 public MongoResultSet(MongoData mongo, String schema) throws SQLException {
   this._cursor = mongo.getCursor();
   this._schema = schema;
   this._table = mongo.getTable();
   this.isSum = mongo.getCount() > 0;
   this._sum = mongo.getCount();
   this.isGroupBy = mongo.getType();
   if (this.isGroupBy) {
     dblist = mongo.getGrouyBys();
     this.isSum = true;
   }
   if (this._cursor != null) {
     select = (String[]) _cursor.getKeysWanted().keySet().toArray(new String[0]);
     if (this._cursor.hasNext()) {
       _cur = _cursor.next();
       if (_cur != null) {
         if (select.length == 0) {
           SetFields(_cur.keySet());
         }
         _row = 1;
       }
     }
     if (select.length == 0) {
       select = new String[] {"_id"};
       SetFieldType(true);
     } else {
       SetFieldType(false);
     }
   } else {
     SetFields(mongo.getFields().keySet()); // new String[]{"COUNT(*)"};
     SetFieldType(mongo.getFields());
   }
 }
コード例 #6
0
  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();
      }
    }
  }
コード例 #7
0
 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());
   }
 }
  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();
  }
コード例 #9
0
    public SortSpecificationComparator(DBObject orderBy) {
      this.orderBy = orderBy;
      this.orderByKeySet = orderBy.keySet();

      if (this.orderByKeySet.isEmpty()) {
        throw new FongoException("The $sort pattern is empty when it should be a set of fields.");
      }
    }
コード例 #10
0
 /** Does a deep copy of an object to allow for subsequent modification */
 public static DBObject copyDBObject(DBObject dbObject) {
   DBObject orig = dbObject;
   BasicDBObjectBuilder dbObjectBuilder = BasicDBObjectBuilder.start();
   for (String field : orig.keySet()) {
     Object value = orig.get(field);
     dbObjectBuilder.add(field, value);
   }
   return dbObjectBuilder.get();
 }
コード例 #11
0
ファイル: MongoFindCommand.java プロジェクト: araqne/logdb
  private Map<String, Object> convert(DBObject doc) {
    Map<String, Object> m = new HashMap<String, Object>();

    for (String key : doc.keySet()) {
      m.put(key, convert(doc.get(key)));
    }

    return m;
  }
コード例 #12
0
 /** @return @throws BasicException */
 @Override
 public boolean next() throws BasicException {
   if (m_dbCursor != null) {
     if (m_dbCursor.hasNext()) {
       m_currentObject = m_dbCursor.next();
       return true;
     } else return false;
   } else return !m_dbObject.keySet().isEmpty();
 }
コード例 #13
0
 /**
  * Only build the filter for this keys.
  *
  * @param ref query for filter.
  * @param keys must match to build the filter.
  */
 public Filter buildFilter(DBObject ref, Collection<String> keys) {
   AndFilter andFilter = new AndFilter();
   for (String key : ref.keySet()) {
     if (keys.contains(key)) {
       Object expression = ref.get(key);
       andFilter.addFilter(buildExpressionFilter(key, expression));
     }
   }
   return andFilter;
 }
コード例 #14
0
ファイル: BsonDBDecoder.java プロジェクト: cexbrayat/jongo
    @Override
    public Object createObject(byte[] data, int offset) {
      DBObject dbo = new LazyBsonByte(data, offset, this);

      Iterator it = dbo.keySet().iterator();
      if (it.hasNext() && it.next().equals("$ref") && dbo.containsField("$id")) {
        return new DBRef(db, dbo);
      }
      return dbo;
    }
コード例 #15
0
  @Override
  public OpaqueValue getValue(DBObject dbObject) {
    DBObject histogram = (DBObject) dbObject.get(HISTOGRAM_NAME);
    Object[] keys = histogram.keySet().toArray();
    Integer l = 0;

    if (keys.length != 0) l = Integer.parseInt(histogram.get((String) keys[0]).toString());

    return (OpaqueValue) new OpaqueValue<Number>(0l, l);
  }
コード例 #16
0
 public Filter buildFilter(DBObject ref) {
   AndFilter andFilter = new AndFilter();
   if (ref != null) {
     for (String key : ref.keySet()) {
       Object expression = ref.get(key);
       andFilter.addFilter(buildExpressionFilter(key, expression));
     }
   }
   return andFilter;
 }
コード例 #17
0
 private boolean filterMatch(DBObject filter, DBObject object) {
   for (String key : filter.keySet()) {
     if (!object.containsField(key)) {
       return false;
     }
     if (!filter.get(key).equals(object.get(key))) {
       return false;
     }
   }
   return true;
 }
コード例 #18
0
  private int getNumberOfEmbeddedAssociations(DBObject entity) {
    int numberOfReferences = 0;

    for (String fieldName : entity.keySet()) {
      Object field = entity.get(fieldName);
      if (isAssociation(field)) {
        numberOfReferences++;
      }
    }

    return numberOfReferences;
  }
コード例 #19
0
  public Map<String, Object> message() {
    Map<String, Object> map = new HashMap<>();

    Object property = dbObject.get(MESSAGE);
    if (property == null || property instanceof DBObject) {
      DBObject messageObject = (DBObject) property;
      if (messageObject != null) {
        for (String key : messageObject.keySet()) {
          map.put(key, messageObject.get(key));
        }
      }
    }

    return map;
  }
コード例 #20
0
  private int compareDBObjects(DBObject db0, DBObject db1) {
    Iterator<String> i0 = db0.keySet().iterator();
    Iterator<String> i1 = db1.keySet().iterator();

    while (i0.hasNext() || i1.hasNext()) {
      String key0 = i0.hasNext() ? i0.next() : null;
      String key1 = i1.hasNext() ? i1.next() : null;

      int keyComparison = Util.compareToNullable(key0, key1);
      if (keyComparison != 0) {
        return keyComparison;
      }

      Object value0 = key0 == null ? null : db0.get(key0);
      Object value1 = key1 == null ? null : db1.get(key1);

      int valueComparison = compareObjects(value0, value1);
      if (valueComparison != 0) {
        return valueComparison;
      }
    }

    return 0;
  }
コード例 #21
0
  @Test
  public void serialNumberNotStored() throws Exception {
    // writer.writeId("D01");
    writer.writeOid(RootOidDefault.deString(SPEC_NAME + ":" + "D01", new OidMarshaller()));
    writer.flush();

    final DBCollection instances = testDb.getCollection(SPEC_NAME);
    assertEquals(1, instances.getCount());
    final DBObject object = instances.findOne();

    assertEquals(SPEC_NAME + ":" + "D01", object.get("_oid"));
    assertEquals("D01", object.get("_id"));

    assertEquals(2, object.keySet().size());
  }
コード例 #22
0
  /*------------------------------------------------------------ */
  @Override
  protected synchronized NoSqlSession loadSession(String clusterId) {
    DBObject o = _sessions.findOne(new BasicDBObject(__ID, clusterId));

    __log.debug("MongoSessionManager:loaded " + o);

    if (o == null) {
      return null;
    }

    Boolean valid = (Boolean) o.get(__VALID);
    if (valid == null || !valid) {
      return null;
    }

    try {
      Object version = o.get(getContextKey(__VERSION));
      Long created = (Long) o.get(__CREATED);
      Long accessed = (Long) o.get(__ACCESSED);

      NoSqlSession session = new NoSqlSession(this, created, accessed, clusterId, version);

      // get the attributes for the context
      DBObject attrs = (DBObject) getNestedValue(o, getContextKey());

      __log.debug("MongoSessionManager:attrs: " + attrs);
      if (attrs != null) {
        for (String name : attrs.keySet()) {
          if (__METADATA.equals(name)) {
            continue;
          }

          String attr = decodeName(name);
          Object value = decodeValue(attrs.get(name));

          session.doPutOrRemove(attr, value);
          session.bindValue(attr, value);
        }
      }
      session.didActivate();

      return session;
    } catch (Exception e) {
      LOG.warn(e);
    }
    return null;
  }
コード例 #23
0
  /**
   * Reads the given {@link DBObject} into a {@link Map}. will recursively resolve nested {@link
   * Map}s as well.
   *
   * @param type the {@link Map} {@link TypeInformation} to be used to unmarshall this {@link
   *     DBObject}.
   * @param dbObject must not be {@literal null}
   * @param path must not be {@literal null}
   * @return
   */
  @SuppressWarnings("unchecked")
  protected Map<Object, Object> readMap(
      TypeInformation<?> type, DBObject dbObject, ObjectPath path) {

    Assert.notNull(dbObject, "DBObject must not be null!");
    Assert.notNull(path, "Object path must not be null!");

    Class<?> mapType = typeMapper.readType(dbObject, type).getType();

    TypeInformation<?> keyType = type.getComponentType();
    Class<?> rawKeyType = keyType == null ? null : keyType.getType();

    TypeInformation<?> valueType = type.getMapValueType();
    Class<?> rawValueType = valueType == null ? null : valueType.getType();

    Map<Object, Object> map =
        CollectionFactory.createMap(mapType, rawKeyType, dbObject.keySet().size());
    Map<String, Object> sourceMap = dbObject.toMap();

    for (Entry<String, Object> entry : sourceMap.entrySet()) {
      if (typeMapper.isTypeKey(entry.getKey())) {
        continue;
      }

      Object key = potentiallyUnescapeMapKey(entry.getKey());

      if (rawKeyType != null) {
        key = conversionService.convert(key, rawKeyType);
      }

      Object value = entry.getValue();

      if (value instanceof DBObject) {
        map.put(key, read(valueType, (DBObject) value, path));
      } else if (value instanceof DBRef) {
        map.put(
            key,
            DBRef.class.equals(rawValueType) ? value : read(valueType, readRef((DBRef) value)));
      } else {
        Class<?> valueClass = valueType == null ? null : valueType.getType();
        map.put(key, getPotentiallyConvertedSimpleRead(value, valueClass));
      }
    }

    return map;
  }
コード例 #24
0
  private ResourceState createACE(
      String createdResourceURI, SecurityContext securityContext, AutoRuleConfig autoRuleConfig) {
    DBObject dbObject = new BasicDBObject();
    dbObject.put(ACE_REALM, securityContext.getRealm());
    dbObject.put(ACE_USER_ID, securityContext.getSubject());
    dbObject.put(ACE_RESOURCE_PATH, createdResourceURI);
    dbObject.put(ACE_ACTIONS, autoRuleConfig.getAutoAddedOwnerPermissions().toArray());
    dbObject.put(ACE_PERMITTED, true);
    this.aclCollection.insert(dbObject);

    log.debug("Created ACE: " + dbObject);

    ResourceState createdState = new DefaultResourceState();
    for (String key : dbObject.keySet()) {
      createdState.putProperty(key, dbObject.get(key));
    }
    return createdState;
  }
コード例 #25
0
ファイル: QueryImpl.java プロジェクト: rlodge/morphia
  @Override
  @Deprecated
  public DBObject getFieldsObject() {
    DBObject projection = getOptions().getProjection();
    if (projection == null || projection.keySet().size() == 0) {
      return null;
    }

    final MappedClass mc = ds.getMapper().getMappedClass(clazz);

    Entity entityAnnotation = mc.getEntityAnnotation();
    final BasicDBObject fieldsFilter = copy(projection);

    if (includeFields && entityAnnotation != null && !entityAnnotation.noClassnameStored()) {
      fieldsFilter.put(Mapper.CLASS_NAME_FIELDNAME, 1);
    }

    return fieldsFilter;
  }
コード例 #26
0
  private static DBObject normalize(DBObject obj) {
    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();

    Iterator<String> iterator = obj.keySet().iterator();
    builder.push("$set");
    while (iterator.hasNext()) {

      String key = iterator.next();

      if (Constants.ID_PROPERTY.equals(key)) continue;

      Object value = obj.get(key);

      if (value == null) continue;

      builder.add(key, value);
    }

    return builder.get();
  }
コード例 #27
0
  /**
   * Removes the type information from the entire conversion result.
   *
   * @param object
   * @param recursively whether to apply the removal recursively
   * @return
   */
  private Object removeTypeInfo(Object object, boolean recursively) {

    if (!(object instanceof DBObject)) {
      return object;
    }

    DBObject dbObject = (DBObject) object;
    String keyToRemove = null;

    for (String key : dbObject.keySet()) {

      if (recursively) {

        Object value = dbObject.get(key);

        if (value instanceof BasicDBList) {
          for (Object element : (BasicDBList) value) {
            removeTypeInfo(element, recursively);
          }
        } else {
          removeTypeInfo(value, recursively);
        }
      }

      if (typeMapper.isTypeKey(key)) {

        keyToRemove = key;

        if (!recursively) {
          break;
        }
      }
    }

    if (keyToRemove != null) {
      dbObject.removeField(keyToRemove);
    }

    return dbObject;
  }
コード例 #28
0
  // --------------------------------------------------------------------------------
  private void drawTreeRoot(Tree _tree, DBObject data) {
    // reset tree
    _tree.removeAll();

    if (data != null) {
      TreeItem root = new TreeItem(_tree, SWT.NONE);
      root.setText(data.get("_id") + "");
      root.setImage(documentImage);
      root.setData("fieldName", "");
      root.setData("value", data.get("_id"));
      fieldNameTreeItemMap.put("", root);

      boolean expand = (data.keySet().size() < 35);
      drawItem("", root, data.toMap(), expand);
    }

    nameText.setText("");
    typeCombo.select(0);
    typeCombo.setEnabled(false);
    valueText.setText("");
    updateButton.setEnabled(false);
  }
コード例 #29
0
 private DBObject removeNullEntries(DBObject nativeEntry) {
   for (String key : new HashSet<String>(nativeEntry.keySet())) {
     Object o = nativeEntry.get(key);
     if (o == null) {
       nativeEntry.removeField(key);
     } else if (o instanceof Object[]) {
       for (Object o2 : (Object[]) o) {
         if (o2 instanceof DBObject) {
           removeNullEntries((DBObject) o2);
         }
       }
     } else if (o instanceof List) {
       for (Object o2 : (List) o) {
         if (o2 instanceof DBObject) {
           removeNullEntries((DBObject) o2);
         }
       }
     } else if (o instanceof DBObject) {
       removeNullEntries((DBObject) o);
     }
   }
   return nativeEntry;
 }
コード例 #30
0
ファイル: MapReduce.java プロジェクト: Kodextor/mongojack
 com.mongodb.MapReduceCommand build(JacksonDBCollection<?, ?> collection) {
   DBObject query = null;
   if (this.query != null) {
     query = collection.serializeQuery(this.query);
   }
   com.mongodb.MapReduceCommand command =
       new com.mongodb.MapReduceCommand(
           collection.getDbCollection(),
           map,
           reduce,
           this.collection,
           outputType.getDriverType(),
           query);
   if (finalize != null) {
     command.setFinalize(finalize);
   }
   if (readPreference != null) {
     command.setReadPreference(readPreference);
   }
   if (outputDB != null) {
     command.setOutputDB(outputDB);
   }
   if (sort != null) {
     command.setSort(sort);
   }
   command.setLimit(limit);
   if (scope != null) {
     command.setScope(scope);
   }
   command.setVerbose(verbose);
   if (extra != null) {
     for (String key : extra.keySet()) {
       command.addExtraOption(key, extra.get(key));
     }
   }
   return command;
 }