Java Code Examples for org.bson.Document#entrySet()

The following examples show how to use org.bson.Document#entrySet() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DocumentMemoryOperation.java    From jphp with Apache License 2.0 6 votes vote down vote up
@Override
public Memory unconvert(Environment env, TraceInfo trace, Document arg) throws Throwable {
    if (arg == null) return Memory.NULL;

    ArrayMemory result = ArrayMemory.createHashed(arg.size());

    for (Entry<String, Object> entry : arg.entrySet()) {
        if (entry.getValue() instanceof Document) {
            result.put(entry.getKey(), unconvert(env, trace, (Document) entry.getValue()));
        } else {
            result.put(entry.getKey(), Memory.wrap(env, entry.getValue()));
        }
    }

    return result;
}
 
Example 2
Source File: GridFSITTestBase.java    From nifi with Apache License 2.0 6 votes vote down vote up
public boolean fileHasProperties(String name, String bucketName, Map<String, String> attrs) {
    GridFSBucket bucket = GridFSBuckets.create(client.getDatabase(DB), bucketName);
    MongoCursor it = bucket.find(Document.parse(String.format("{ \"filename\": \"%s\" }", name))).iterator();
    boolean retVal = false;

    if (it.hasNext()) {
        GridFSFile file = (GridFSFile)it.next();
        Document metadata = file.getMetadata();
        if (metadata != null && metadata.size() == attrs.size()) {
            retVal = true;
            for (Map.Entry<String, Object> entry : metadata.entrySet()) {
                Object val = attrs.get(entry.getKey());
                if (val == null || !entry.getValue().equals(val)) {
                    retVal = false;
                    break;
                }
            }
        }
    }

    it.close();

    return retVal;
}
 
Example 3
Source File: InsertOperation.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(final MongoDatabase connection, final Document data) {
    for (final Entry<String, Object> entry : data.entrySet()) {
        final String collectionName = entry.getKey();
        final Object content = entry.getValue();

        final List<Document> indexes = getIndexData(content);
        if (!indexes.isEmpty()) {
            insertIndexes(connection.getCollection(collectionName), indexes);
        }

        final List<Document> entries = getCollectionData(content);
        if (!entries.isEmpty()) {
            insertData(connection.getCollection(collectionName), entries);
        }
    }
}
 
Example 4
Source File: CleanupStrategyProvider.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
private Document excludeCollections(final Iterable<Document> collections, final String... collectionsToExclude) {
    final List<String> toRetain = Arrays.asList(collectionsToExclude);

    final Document toDelete = new Document();

    for (final Document doc : collections) {
        if (!isSeedDocument(doc)) {
            final String collectionName = (String) doc.get("name");
            if (!toRetain.contains(collectionName) && !isSystemCollection(collectionName)) {
                toDelete.put(collectionName, doc);
            }
        } else {
            for (final Entry<String, Object> childCollection : doc.entrySet()) {
                if (!toRetain.contains(childCollection.getKey())) {
                    toDelete.put(childCollection.getKey(), childCollection.getValue());
                }
            }
        }
    }

    return toDelete;
}
 
Example 5
Source File: MongoGazetteer.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, Object> getAdditionalData(String key) {
  ObjectId id = getId(key);
  if (id == null) {
    return Collections.emptyMap();
  }

  Document doc =
      coll.find(new Document("_id", id))
          .projection(new Document(valueField, 0).append("_id", 0))
          .first();
  if (doc == null) {
    return Collections.emptyMap();
  }

  Map<String, Object> ret = new HashMap<>();

  for (Entry<String, Object> mongoEntry : doc.entrySet()) {
    ret.put(mongoEntry.getKey(), mongoEntry.getValue());
  }

  return ret;
}
 
Example 6
Source File: MongoDocumentHistory.java    From baleen with Apache License 2.0 6 votes vote down vote up
private Map<String, String> convertToStringMap(Object object) {
  if (object == null || !(object instanceof Document)) {
    return ImmutableMap.of();
  }
  Document dbo = (Document) object;

  Builder<String, String> builder = ImmutableMap.builder();
  for (Entry<String, Object> e : dbo.entrySet()) {
    if (e.getValue() instanceof String) {
      builder.put(e.getKey(), (String) e.getValue());
    } else {
      builder.put(e.getKey(), e.getValue().toString());
    }
  }

  return builder.build();
}
 
Example 7
Source File: MongoReader.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Override
protected void doGetNext(JCas jCas) throws IOException, CollectionException {
  ObjectId id = queue.remove(0);

  Document docIdField = new Document(idField, id);
  Document document = coll.find(docIdField).first();

  if (document == null) {
    getMonitor().error("No document returned from Mongo");
    throw new CollectionException();
  }

  String content = (String) document.get(contentField);

  InputStream is = IOUtils.toInputStream(content, Charset.defaultCharset());

  extractContent(is, mongo.getMongoURI() + "." + collection + "#" + id, jCas);

  for (Entry<String, Object> entry : document.entrySet()) {
    String key = entry.getKey();
    if (contentField.equals(key) || idField.equals(key)) {
      continue;
    } else {
      processMongoMetadataField(jCas, key, entry.getValue());
    }
  }

  if (deleteSource) {
    coll.deleteOne(docIdField);
  }
}
 
Example 8
Source File: IndexOptionsBuilder.java    From morphia with Apache License 2.0 5 votes vote down vote up
IndexOptionsBuilder(final IndexOptions original, final String prefix) {
    super(original);
    if (!"".equals(original.partialFilter())) {
        final Document parse = Document.parse(original.partialFilter());
        final Document filter = new Document();
        for (final Entry<String, Object> entry : parse.entrySet()) {
            filter.put(prefix + "." + entry.getKey(), entry.getValue());
        }
        partialFilter(filter.toJson());
    }
}
 
Example 9
Source File: DocumentConverter.java    From javers with Apache License 2.0 5 votes vote down vote up
static JsonElement fromDocument(Document document) {
    JsonObject jsonObject = new JsonObject();
    for(Map.Entry<String,Object> e : document.entrySet()) {
        jsonObject.add(e.getKey(), createJsonElement(e.getValue()) );
    }
    return jsonObject;
}
 
Example 10
Source File: PutMongoRecord.java    From nifi with Apache License 2.0 5 votes vote down vote up
private Document convertArrays(Document doc) {
    Document retVal = new Document();
    for (Map.Entry<String, Object> entry : doc.entrySet()) {
        if (entry.getValue() != null && entry.getValue().getClass().isArray()) {
            retVal.put(entry.getKey(), convertArrays((Object[])entry.getValue()));
        } else if (entry.getValue() != null && (entry.getValue() instanceof Map || entry.getValue() instanceof Document)) {
            retVal.put(entry.getKey(), convertArrays(new Document((Map)entry.getValue())));
        } else {
            retVal.put(entry.getKey(), entry.getValue());
        }
    }

    return retVal;
}
 
Example 11
Source File: PipelineResultIteration.java    From rya with Apache License 2.0 5 votes vote down vote up
private QueryBindingSet docToBindingSet(Document result) {
    QueryBindingSet bindingSet = new QueryBindingSet(bindings);
    Document valueSet = result.get(AggregationPipelineQueryNode.VALUES, Document.class);
    Document typeSet = result.get(AggregationPipelineQueryNode.TYPES, Document.class);
    if (valueSet != null) {
        for (Map.Entry<String, Object> entry : valueSet.entrySet()) {
            String fieldName = entry.getKey();
            String valueString = entry.getValue().toString();
            String typeString = typeSet == null ? null : typeSet.getString(fieldName);
            String varName = varToOriginalName.getOrDefault(fieldName, fieldName);
            Value varValue;
            if (typeString == null || typeString.equals(XMLSchema.ANYURI.stringValue())) {
                varValue = VF.createIRI(valueString);
            }
            else {
                varValue = VF.createLiteral(valueString, VF.createIRI(typeString));
            }
            Binding existingBinding = bindingSet.getBinding(varName);
            // If this variable is not already bound, add it.
            if (existingBinding == null) {
                bindingSet.addBinding(varName, varValue);
            }
            // If it's bound to something else, the solutions are incompatible.
            else if (!existingBinding.getValue().equals(varValue)) {
                return null;
            }
        }
    }
    return bindingSet;
}
 
Example 12
Source File: MongoDBUtil.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public static Map<String, Field> createFieldFromDocument(Document doc) throws IOException {
  Set<Map.Entry<String, Object>> entrySet = doc.entrySet();
  Map<String, Field> fields = new HashMap<>(entrySet.size());
  for (Map.Entry<String, Object> entry : entrySet) {
    fields.put(entry.getKey(), jsonToField(entry.getValue()));
  }
  return fields;
}
 
Example 13
Source File: MongoDocumentHistory.java    From baleen with Apache License 2.0 5 votes vote down vote up
private Collection<HistoryEvent> convert(Document doc) {
  if (doc == null || doc.get(ENTITIES) == null || !(doc.get(ENTITIES) instanceof Document)) {
    LOGGER.warn("Invalid history document");
    return Collections.emptyList();
  }
  Document entities = (Document) doc.get(ENTITIES);

  List<HistoryEvent> history = Lists.newLinkedList();
  for (Entry<String, Object> entry : entities.entrySet()) {
    if (entry.getValue() instanceof List) {
      convertForEntity(history, entry.getKey(), (List<?>) entry.getValue());
    }
  }
  return history;
}
 
Example 14
Source File: MongoPullHanlder.java    From DBus with Apache License 2.0 5 votes vote down vote up
private DbusMessage buildMongoResultMessage(String outputVersion, String reqString,
                                            DBConfiguration dbConf, int batchNo, List<List<Object>> tuples,
                                            Document document, Boolean openFirst) throws Exception {
    String dsType = dbConf.getString(DBConfiguration.DataSourceInfo.DS_TYPE);
    DbusMessageBuilder builder = new DbusMessageBuilder(outputVersion);
    builder.build(DbusMessage.ProtocolType.DATA_INITIAL_DATA, getDbTypeAndNameSpace(reqString, dbConf), batchNo);
    if (openFirst) {
        Set<Map.Entry<String, Object>> entries = document.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            DataType dataType = DataType.convertDataType(dsType, coverToJavaDataType(entry.getValue()), null, null);
            builder.appendSchema(entry.getKey(), dataType, false);
        }
    } else {
        builder.appendSchema("jsonObj", DataType.JSONOBJECT, false);
    }
    for (List<Object> tuple : tuples) {
        builder.appendPayload(tuple.toArray());
    }

    DbusMessage message = builder.getMessage();
    // 脱敏
    UmsEncoder encoder = new PluggableMessageEncoder(PluginManagerProvider.getManager(), (e, column, m) -> {
        // TODO: 2018/5/25
    });
    encoder.encode(message, (List<EncodeColumn>) dbConf.get(DBConfiguration.TABEL_ENCODE_COLUMNS));
    return message;
}
 
Example 15
Source File: DataSetComparator.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private Document filterRequest(final Document expectedEntry, final List<String> columnsToExclude) {
    final Document filtered = new Document();
    for (final Entry<String, Object> entry : expectedEntry.entrySet()) {
        if (!columnsToExclude.contains(entry.getKey())) {
            filtered.put(entry.getKey(), entry.getValue());
        }
    }
    return filtered;
}
 
Example 16
Source File: CachedEncryptionEventListener.java    From spring-data-mongodb-encrypt with Apache License 2.0 5 votes vote down vote up
void cryptMap(Document document, Node node, Function<Object, Object> crypt) {
    Node mapChildren = node.children.get(0);
    for (Map.Entry<String, Object> entry : document.entrySet()) {
        try {
            cryptFields(entry.getValue(), mapChildren, crypt);
        } catch (FieldCryptException e) {
            throw e.chain(entry.getKey());
        }
    }
}
 
Example 17
Source File: CoreRemoteMongoCollectionUnitTests.java    From stitch-android-sdk with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testWatchCompactObjectIdIDs() throws IOException, InterruptedException {
  final CoreStitchServiceClient service = Mockito.mock(CoreStitchServiceClient.class);
  when(service.getCodecRegistry()).thenReturn(BsonUtils.DEFAULT_CODEC_REGISTRY);
  final CoreRemoteMongoClient client =
      CoreRemoteClientFactory.getClient(
          service,
          getClientInfo(),
          null);
  final CoreRemoteMongoCollection<Document> coll = getCollection(client);

  final Stream<CompactChangeEvent<Document>> mockStream = Mockito.mock(Stream.class);

  doReturn(mockStream).when(service).streamFunction(any(), any(), any(Decoder.class));

  final ObjectId[] expectedIDs = new ObjectId[] {new ObjectId(), new ObjectId(), new ObjectId()};
  coll.watchCompact(expectedIDs);

  final ArgumentCaptor<String> funcNameArg = ArgumentCaptor.forClass(String.class);
  final ArgumentCaptor<List> funcArgsArg = ArgumentCaptor.forClass(List.class);
  final ArgumentCaptor<Decoder<CompactChangeEvent>> decoderArgumentCaptor =
      ArgumentCaptor.forClass(Decoder.class);
  verify(service)
      .streamFunction(
          funcNameArg.capture(),
          funcArgsArg.capture(),
          decoderArgumentCaptor.capture());

  assertEquals("watch", funcNameArg.getValue());
  assertEquals(1, funcArgsArg.getValue().size());
  final Document expectedArgs = new Document();
  expectedArgs.put("database", "dbName1");
  expectedArgs.put("collection", "collName1");
  expectedArgs.put("ids", Arrays.stream(expectedIDs).map(BsonObjectId::new)
      .collect(Collectors.toSet()));
  expectedArgs.put("useCompactEvents", true);

  for (final Map.Entry<String, Object> entry : expectedArgs.entrySet()) {
    final Object capturedValue = ((Document)funcArgsArg.getValue().get(0)).get(entry.getKey());
    assertEquals(entry.getValue(), capturedValue);
  }
  assertEquals(ResultDecoders.compactChangeEventDecoder(new DocumentCodec()),
      decoderArgumentCaptor.getValue());
}
 
Example 18
Source File: CoreRemoteMongoCollectionUnitTests.java    From stitch-android-sdk with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testWatchObjectIdIDs() throws IOException, InterruptedException {
  final CoreStitchServiceClient service = Mockito.mock(CoreStitchServiceClient.class);
  when(service.getCodecRegistry()).thenReturn(BsonUtils.DEFAULT_CODEC_REGISTRY);
  final CoreRemoteMongoClient client =
      CoreRemoteClientFactory.getClient(
          service,
          getClientInfo(),
          null);
  final CoreRemoteMongoCollection<Document> coll = getCollection(client);

  final Stream<ChangeEvent<Document>> mockStream = Mockito.mock(Stream.class);

  doReturn(mockStream).when(service).streamFunction(any(), any(), any(Decoder.class));

  final ObjectId[] expectedIDs = new ObjectId[] {new ObjectId(), new ObjectId(), new ObjectId()};
  coll.watch(expectedIDs);

  final ArgumentCaptor<String> funcNameArg = ArgumentCaptor.forClass(String.class);
  final ArgumentCaptor<List> funcArgsArg = ArgumentCaptor.forClass(List.class);
  final ArgumentCaptor<Decoder<ChangeEvent>> decoderArgumentCaptor =
      ArgumentCaptor.forClass(Decoder.class);
  verify(service)
      .streamFunction(
          funcNameArg.capture(),
          funcArgsArg.capture(),
          decoderArgumentCaptor.capture());

  assertEquals("watch", funcNameArg.getValue());
  assertEquals(1, funcArgsArg.getValue().size());
  final Document expectedArgs = new Document();
  expectedArgs.put("database", "dbName1");
  expectedArgs.put("collection", "collName1");
  expectedArgs.put("ids", Arrays.stream(expectedIDs).map(BsonObjectId::new)
      .collect(Collectors.toSet()));
  expectedArgs.put("useCompactEvents", false);

  for (final Map.Entry<String, Object> entry : expectedArgs.entrySet()) {
    final Object capturedValue = ((Document)funcArgsArg.getValue().get(0)).get(entry.getKey());
    assertEquals(entry.getValue(), capturedValue);
  }
  assertEquals(ResultDecoders.changeEventDecoder(new DocumentCodec()),
      decoderArgumentCaptor.getValue());
}
 
Example 19
Source File: CoreRemoteMongoCollectionUnitTests.java    From stitch-android-sdk with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testWatchWithFilter() throws IOException, InterruptedException {
  final CoreStitchServiceClient service = Mockito.mock(CoreStitchServiceClient.class);
  when(service.getCodecRegistry()).thenReturn(BsonUtils.DEFAULT_CODEC_REGISTRY);
  final CoreRemoteMongoClient client =
      CoreRemoteClientFactory.getClient(
          service,
          getClientInfo(),
          null);
  final CoreRemoteMongoCollection<Document> coll = getCollection(client);

  final Stream<ChangeEvent<Document>> mockStream = Mockito.mock(Stream.class);

  doReturn(mockStream).when(service).streamFunction(any(), any(), any(Decoder.class));

  final BsonDocument expectedFilter = new BsonDocument(
      "fullDocument.field", new BsonString("someValue"));
  coll.watchWithFilter(expectedFilter);

  final ArgumentCaptor<String> funcNameArg = ArgumentCaptor.forClass(String.class);
  final ArgumentCaptor<List> funcArgsArg = ArgumentCaptor.forClass(List.class);
  final ArgumentCaptor<Decoder<ChangeEvent>> decoderArgumentCaptor =
      ArgumentCaptor.forClass(Decoder.class);
  verify(service)
      .streamFunction(
          funcNameArg.capture(),
          funcArgsArg.capture(),
          decoderArgumentCaptor.capture());

  assertEquals("watch", funcNameArg.getValue());
  assertEquals(1, funcArgsArg.getValue().size());
  final Document expectedArgs = new Document();
  expectedArgs.put("database", "dbName1");
  expectedArgs.put("collection", "collName1");
  expectedArgs.put("useCompactEvents", false);
  expectedArgs.put("filter", expectedFilter);

  for (final Map.Entry<String, Object> entry : expectedArgs.entrySet()) {
    final Object capturedValue = ((Document)funcArgsArg.getValue().get(0)).get(entry.getKey());
    assertEquals(entry.getValue(), capturedValue);
  }
  assertEquals(ResultDecoders.changeEventDecoder(new DocumentCodec()),
      decoderArgumentCaptor.getValue());
}
 
Example 20
Source File: CoreRemoteMongoCollectionUnitTests.java    From stitch-android-sdk with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testWatchFullCollection() throws IOException, InterruptedException {
  final CoreStitchServiceClient service = Mockito.mock(CoreStitchServiceClient.class);
  when(service.getCodecRegistry()).thenReturn(BsonUtils.DEFAULT_CODEC_REGISTRY);
  final CoreRemoteMongoClient client =
      CoreRemoteClientFactory.getClient(
          service,
          getClientInfo(),
          null);
  final CoreRemoteMongoCollection<Document> coll = getCollection(client);

  final Stream<ChangeEvent<Document>> mockStream = Mockito.mock(Stream.class);

  doReturn(mockStream).when(service).streamFunction(any(), any(), any(Decoder.class));

  coll.watch();

  final ArgumentCaptor<String> funcNameArg = ArgumentCaptor.forClass(String.class);
  final ArgumentCaptor<List> funcArgsArg = ArgumentCaptor.forClass(List.class);
  final ArgumentCaptor<Decoder<ChangeEvent>> decoderArgumentCaptor =
      ArgumentCaptor.forClass(Decoder.class);
  verify(service)
      .streamFunction(
          funcNameArg.capture(),
          funcArgsArg.capture(),
          decoderArgumentCaptor.capture());

  assertEquals("watch", funcNameArg.getValue());
  assertEquals(1, funcArgsArg.getValue().size());
  final Document expectedArgs = new Document();
  expectedArgs.put("database", "dbName1");
  expectedArgs.put("collection", "collName1");
  expectedArgs.put("useCompactEvents", false);

  for (final Map.Entry<String, Object> entry : expectedArgs.entrySet()) {
    final Object capturedValue = ((Document)funcArgsArg.getValue().get(0)).get(entry.getKey());
    assertEquals(entry.getValue(), capturedValue);
  }
  assertEquals(ResultDecoders.changeEventDecoder(new DocumentCodec()),
      decoderArgumentCaptor.getValue());
}