Java Code Examples for org.bson.BsonDocument#containsKey()

The following examples show how to use org.bson.BsonDocument#containsKey() . 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: GridFSTest.java    From mongo-java-driver-reactivestreams with Apache License 2.0 6 votes vote down vote up
private List<BsonDocument> processFiles(final BsonArray bsonArray, final List<BsonDocument> documents) {
    for (BsonValue rawDocument : bsonArray.getValues()) {
        if (rawDocument.isDocument()) {
            BsonDocument document = rawDocument.asDocument();
            if (document.get("length").isInt32()) {
                document.put("length", new BsonInt64(document.getInt32("length").getValue()));
            }
            if (document.containsKey("metadata") && document.getDocument("metadata").isEmpty()) {
                document.remove("metadata");
            }
            if (document.containsKey("aliases") && document.getArray("aliases").getValues().size() == 0) {
                document.remove("aliases");
            }
            if (document.containsKey("contentType") && document.getString("contentType").getValue().length() == 0) {
                document.remove("contentType");
            }
            documents.add(document);
        }
    }
    return documents;
}
 
Example 2
Source File: SubscriptionType.java    From epcis with Apache License 2.0 6 votes vote down vote up
public SubscriptionType(BsonDocument doc) {

		if (doc.containsKey("subscriptionID")) {
			this.subscriptionID = doc.getString("subscriptionID").getValue();
		}
		if (doc.containsKey("dest")) {
			this.dest = doc.getString("dest").getValue();
		}
		if (doc.containsKey("schedule")) {
			this.schedule = doc.getString("schedule").getValue();
		}
		if (doc.containsKey("trigger")) {
			this.trigger = doc.getString("trigger").getValue();
		}
		if (doc.containsKey("initialRecordTime")) {
			this.initialRecordTime = doc.getString("initialRecordTime").getValue();
		}
		if (doc.containsKey("reportIfEmpty")) {
			this.reportIfEmpty = doc.getBoolean("reportIfEmpty").getValue();
		}
		if (doc.containsKey("pollParameters")) {
			this.pollParameters = new PollParameters(doc.getDocument("pollParameters"));
		}
	}
 
Example 3
Source File: DebeziumCdcHandler.java    From kafka-connect-mongodb with Apache License 2.0 6 votes vote down vote up
public CdcOperation getCdcOperation(BsonDocument doc) {
    try {
        if(!doc.containsKey(OPERATION_TYPE_FIELD_PATH)
                || !doc.get(OPERATION_TYPE_FIELD_PATH).isString()) {
            throw new DataException("error: value doc is missing CDC operation type of type string");
        }
        CdcOperation op = operations.get(OperationType.fromText(
                doc.get(OPERATION_TYPE_FIELD_PATH).asString().getValue())
        );
        if(op == null) {
            throw new DataException("error: no CDC operation found in mapping for op="
                    + doc.get(OPERATION_TYPE_FIELD_PATH).asString().getValue());
        }
        return op;
    } catch (IllegalArgumentException exc){
        throw new DataException("error: parsing CDC operation failed",exc);
    }
}
 
Example 4
Source File: GridFSTest.java    From mongo-java-driver-rx with Apache License 2.0 6 votes vote down vote up
private void doDownload(final BsonDocument arguments, final BsonDocument assertion) {
    Throwable error = null;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    try {
        gridFSBucket.downloadToStream(arguments.getObjectId("id").getValue(), toAsyncOutputStream(outputStream))
                .timeout(30, SECONDS).toList().toBlocking().first();
        outputStream.close();
    } catch (Throwable e) {
        error = e;
    }

    if (assertion.containsKey("result")) {
        assertNull("Should not have thrown an exception", error);
        assertEquals(DatatypeConverter.printHexBinary(outputStream.toByteArray()).toLowerCase(),
                assertion.getDocument("result").getString("$hex").getValue());
    } else if (assertion.containsKey("error")) {
        assertNotNull("Should have thrown an exception", error);
    }
}
 
Example 5
Source File: GridFSTest.java    From mongo-java-driver-rx with Apache License 2.0 6 votes vote down vote up
private void doDownloadByName(final BsonDocument arguments, final BsonDocument assertion) {
    Throwable error = null;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    try {
        GridFSDownloadOptions options = new GridFSDownloadOptions();
        if (arguments.containsKey("options")) {
            int revision = arguments.getDocument("options").getInt32("revision").getValue();
            options.revision(revision);
        }

        gridFSBucket.downloadToStream(arguments.getString("filename").getValue(), toAsyncOutputStream(outputStream),
                options).timeout(30, SECONDS).toList().toBlocking().first();
        outputStream.close();
    } catch (Throwable e) {
        error = e;
    }
    if (assertion.containsKey("result")) {
        assertNull("Should not have thrown an exception", error);
        assertEquals(DatatypeConverter.printHexBinary(outputStream.toByteArray()).toLowerCase(),
                assertion.getDocument("result").getString("$hex").getValue());
    } else if (assertion.containsKey("error")) {
        assertNotNull("Should have thrown an exception", error);
    }
}
 
Example 6
Source File: GridFSTest.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
private BsonDocument parseHexDocument(final BsonDocument document, final String hexDocument) {
    if (document.containsKey(hexDocument) && document.get(hexDocument).isDocument()) {
        byte[] bytes = DatatypeConverter.parseHexBinary(document.getDocument(hexDocument).getString("$hex").getValue());
        document.put(hexDocument, new BsonBinary(bytes));
    }
    return document;
}
 
Example 7
Source File: JavaTimeTypeTest.java    From immutables with Apache License 2.0 5 votes vote down vote up
@Test
void localDateTime() {
  LocalDateTimeHolderRepository repository = new LocalDateTimeHolderRepository(backend);
  LocalDateTime value = LocalDateTime.now();
  ImmutableLocalDateTimeHolder holder = TypeHolder.LocalDateTimeHolder.generator().get().withValue(value).withOptional(value).withNullable(null);
  repository.insert(holder);
  BsonDocument doc = fetch();

  BsonDateTime expected = new BsonDateTime(value.toInstant(ZoneOffset.UTC).toEpochMilli());
  check(doc.get("value")).is(expected);
  check(doc.get("optional")).is(expected);
  if (doc.containsKey("nullable")) {
    check(doc.get("nullable")).is(BsonNull.VALUE);
  }
}
 
Example 8
Source File: OAuthUtil.java    From epcis with Apache License 2.0 5 votes vote down vote up
public static boolean isAccessible(String userID, List<String> friendList, BsonDocument doc) {

		if (!doc.containsKey("accessModifier") || !doc.containsKey("userID"))
			return true;
		String am = doc.get("accessModifier").asString().getValue();
		String providerID = doc.get("userID").asString().getValue();

		// Non-public document && No authorization
		if (userID == null) {
			return false;
		}

		// If Owner, accessible
		if (providerID.equals(userID)) {
			return true;
		}

		// If Not Owner
		if (am.equals("Friend")) {
			if (friendList.contains(providerID)) {
				return true;
			} else {
				return false;
			}
		}

		return false;
	}
 
Example 9
Source File: ChangeEvents.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
private static DocumentVersionInfo.Version versionForDocument(final BsonDocument doc) {
  if (!doc.containsKey(DataSynchronizer.DOCUMENT_VERSION_FIELD)) {
    return null;
  }

  return DocumentVersionInfo.Version.fromBsonDocument(
      doc.getDocument(DataSynchronizer.DOCUMENT_VERSION_FIELD)
  );
}
 
Example 10
Source File: DataSynchronizer.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Given a BSON document, remove any forbidden fields and return the document. If no changes are
 * made, the original document reference is returned. If changes are made, a cloned copy of the
 * document with the changes will be returned.
 *
 * @param document the document from which to remove forbidden fields
 *
 * @return a BsonDocument without any forbidden fields.
 */
static BsonDocument sanitizeDocument(
    final @Nullable BsonDocument document
) {
  if (document == null) {
    return null;
  }
  if (document.containsKey(DOCUMENT_VERSION_FIELD)) {
    final BsonDocument clonedDoc = document.clone();
    clonedDoc.remove(DOCUMENT_VERSION_FIELD);

    return clonedDoc;
  }
  return document;
}
 
Example 11
Source File: MongoReaderUtil.java    From epcis with Apache License 2.0 5 votes vote down vote up
static EPCISEventExtensionType putEPCISEventExtensionType(BsonDocument dbObject, int zone) {
	EPCISEventExtensionType eeet = new EPCISEventExtensionType();
	if (dbObject.get("eventID") != null) {
		eeet.setEventID(dbObject.getString("eventID").getValue());
	} else {
		if (dbObject.containsKey("_id")) {
			eeet.setEventID(dbObject.getObjectId("_id").getValue().toHexString());
		}
	}
	if (dbObject.get("errorDeclaration") != null) {
		ErrorDeclarationType edt = new ErrorDeclarationType();
		BsonDocument error = dbObject.getDocument("errorDeclaration");
		if (error.containsKey("declarationTime")) {
			edt.setDeclarationTime(getXMLGregorianCalendar(error.getDateTime("declarationTime")));
		}
		if (error.containsKey("reason")) {
			edt.setReason(error.getString("reason").getValue());
		}
		if (error.containsKey("correctiveEventIDs")) {
			BsonArray correctiveEventIDs = error.getArray("correctiveEventIDs");
			List<String> correctiveIDs = new ArrayList<String>();
			Iterator<BsonValue> cIDIterator = correctiveEventIDs.iterator();
			while (cIDIterator.hasNext()) {
				String cID = cIDIterator.next().asString().getValue();
				correctiveIDs.add(cID);
			}
			if (correctiveIDs.size() != 0) {
				CorrectiveEventIDsType ceit = new CorrectiveEventIDsType();
				ceit.setCorrectiveEventID(correctiveIDs);
				edt.setCorrectiveEventIDs(ceit);
			}
		}
		if (error.containsKey("any")) {
			edt.setAny(putAny(error.getDocument("any"), null));
		}
		eeet.setErrorDeclaration(edt);
	}
	return eeet;
}
 
Example 12
Source File: JavaTimeTypeTest.java    From immutables with Apache License 2.0 5 votes vote down vote up
@Test
void instant() {
  InstantHolderRepository repository = new InstantHolderRepository(backend);
  Instant value = Instant.now();
  ImmutableInstantHolder holder = TypeHolder.InstantHolder.generator().get().withValue(value).withOptional(value).withNullable(null);
  repository.insert(holder);
  BsonDocument doc = fetch();

  BsonDateTime expected = new BsonDateTime(value.toEpochMilli());
  check(doc.get("value")).is(expected);
  check(doc.get("optional")).is(expected);
  if (doc.containsKey("nullable")) {
    check(doc.get("nullable")).is(BsonNull.VALUE);
  }
}
 
Example 13
Source File: OAuthUtil.java    From epcis with Apache License 2.0 5 votes vote down vote up
public static boolean isAccessible(String userID, List<String> friendList, BsonDocument doc) {

		if (!doc.containsKey("accessModifier") || !doc.containsKey("userID"))
			return true;
		String am = doc.get("accessModifier").asString().getValue();
		String providerID = doc.get("userID").asString().getValue();

		// Non-public document && No authorization
		if (userID == null) {
			return false;
		}

		// If Owner, accessible
		if (providerID.equals(userID)) {
			return true;
		}

		// If Not Owner
		if (am.equals("Friend")) {
			if (friendList.contains(providerID)) {
				return true;
			} else {
				return false;
			}
		}

		return false;
	}
 
Example 14
Source File: JavaTimeTypeTest.java    From immutables with Apache License 2.0 5 votes vote down vote up
@Test
void localDate() {
  LocalDateHolderRepository repository = new LocalDateHolderRepository(backend);
  LocalDate value = LocalDate.now();
  ImmutableLocalDateHolder holder = TypeHolder.LocalDateHolder.generator().get().withValue(value).withOptional(value).withNullable(null);
  repository.insert(holder);
  BsonDocument doc = fetch();

  BsonDateTime expected = new BsonDateTime(value.atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli());
  check(doc.get("value")).is(expected);
  check(doc.get("optional")).is(expected);
  if (doc.containsKey("nullable")) {
    check(doc.get("nullable")).is(BsonNull.VALUE);
  }
}
 
Example 15
Source File: GridFSTest.java    From mongo-java-driver-rx with Apache License 2.0 5 votes vote down vote up
private BsonDocument parseHexDocument(final BsonDocument document, final String hexDocument) {
    if (document.containsKey(hexDocument) && document.get(hexDocument).isDocument()) {
        byte[] bytes = DatatypeConverter.parseHexBinary(document.getDocument(hexDocument).getString("$hex").getValue());
        document.put(hexDocument, new BsonBinary(bytes));
    }
    return document;
}
 
Example 16
Source File: MongoReaderUtil.java    From epcis with Apache License 2.0 5 votes vote down vote up
static EPCISEventExtensionType putEPCISEventExtensionType(BsonDocument dbObject, int zone) {
	EPCISEventExtensionType eeet = new EPCISEventExtensionType();
	if (dbObject.get("eventID") != null) {
		eeet.setEventID(dbObject.getString("eventID").getValue());
	} else {
		if (dbObject.containsKey("_id")) {
			eeet.setEventID(dbObject.getString("_id").getValue());
		}
	}
	if (dbObject.get("errorDeclaration") != null) {
		ErrorDeclarationType edt = new ErrorDeclarationType();
		BsonDocument error = dbObject.getDocument("errorDeclaration");
		if (error.containsKey("declarationTime")) {
			edt.setDeclarationTime(getXMLGregorianCalendar(error.getDateTime("declarationTime")));
		}
		if (error.containsKey("reason")) {
			edt.setReason(error.getString("reason").getValue());
		}
		if (error.containsKey("correctiveEventIDs")) {
			BsonArray correctiveEventIDs = error.getArray("correctiveEventIDs");
			List<String> correctiveIDs = new ArrayList<String>();
			Iterator<BsonValue> cIDIterator = correctiveEventIDs.iterator();
			while (cIDIterator.hasNext()) {
				String cID = cIDIterator.next().asString().getValue();
				correctiveIDs.add(cID);
			}
			if (correctiveIDs.size() != 0) {
				CorrectiveEventIDsType ceit = new CorrectiveEventIDsType();
				ceit.setCorrectiveEventID(correctiveIDs);
				edt.setCorrectiveEventIDs(ceit);
			}
		}
		if (error.containsKey("any")) {
			edt.setAny(putAny(error.getDocument("any"), null));
		}
		eeet.setErrorDeclaration(edt);
	}
	return eeet;
}
 
Example 17
Source File: MongoSourceTask.java    From mongo-kafka with Apache License 2.0 4 votes vote down vote up
@Override
public List<SourceRecord> poll() {
  final long startPoll = time.milliseconds();
  LOGGER.debug("Polling Start: {}", startPoll);
  List<SourceRecord> sourceRecords = new ArrayList<>();
  boolean publishFullDocumentOnly = sourceConfig.getBoolean(PUBLISH_FULL_DOCUMENT_ONLY_CONFIG);
  int maxBatchSize = sourceConfig.getInt(POLL_MAX_BATCH_SIZE_CONFIG);
  long nextUpdate = startPoll + sourceConfig.getLong(POLL_AWAIT_TIME_MS_CONFIG);
  String prefix = sourceConfig.getString(TOPIC_PREFIX_CONFIG);
  Map<String, Object> partition = createPartitionMap(sourceConfig);

  while (isRunning.get()) {
    Optional<BsonDocument> next = getNextDocument();
    long untilNext = nextUpdate - time.milliseconds();

    if (!next.isPresent()) {
      if (untilNext > 0) {
        LOGGER.debug("Waiting {} ms to poll", untilNext);
        time.sleep(untilNext);
        continue; // Re-check stop flag before continuing
      }
      return sourceRecords.isEmpty() ? null : sourceRecords;
    } else {
      BsonDocument changeStreamDocument = next.get();

      Map<String, String> sourceOffset = new HashMap<>();
      sourceOffset.put("_id", changeStreamDocument.getDocument("_id").toJson());
      if (isCopying.get()) {
        sourceOffset.put("copy", "true");
      }

      String topicName =
          getTopicNameFromNamespace(
              prefix, changeStreamDocument.getDocument("ns", new BsonDocument()));

      Optional<String> jsonDocument = Optional.empty();
      if (publishFullDocumentOnly) {
        if (changeStreamDocument.containsKey("fullDocument")) {
          jsonDocument = Optional.of(changeStreamDocument.getDocument("fullDocument").toJson());
        }
      } else {
        jsonDocument = Optional.of(changeStreamDocument.toJson());
      }

      jsonDocument.ifPresent(
          (json) -> {
            LOGGER.trace("Adding {} to {}: {}", json, topicName, sourceOffset);
            String keyJson = new BsonDocument("_id", changeStreamDocument.get("_id")).toJson();
            sourceRecords.add(
                new SourceRecord(
                    partition,
                    sourceOffset,
                    topicName,
                    Schema.STRING_SCHEMA,
                    keyJson,
                    Schema.STRING_SCHEMA,
                    json));
          });

      if (sourceRecords.size() == maxBatchSize) {
        LOGGER.debug(
            "Reached '{}': {}, returning records", POLL_MAX_BATCH_SIZE_CONFIG, maxBatchSize);
        return sourceRecords;
      }
    }
  }
  return null;
}
 
Example 18
Source File: CoreDocumentSynchronizationConfig.java    From stitch-android-sdk with Apache License 2.0 4 votes vote down vote up
static CoreDocumentSynchronizationConfig fromBsonDocument(final BsonDocument document) {
  keyPresent(ConfigCodec.Fields.DOCUMENT_ID_FIELD, document);
  keyPresent(ConfigCodec.Fields.NAMESPACE_FIELD, document);
  keyPresent(ConfigCodec.Fields.SCHEMA_VERSION_FIELD, document);
  keyPresent(ConfigCodec.Fields.LAST_RESOLUTION_FIELD, document);
  keyPresent(ConfigCodec.Fields.IS_STALE, document);
  keyPresent(ConfigCodec.Fields.IS_PAUSED, document);

  final int schemaVersion =
      document.getNumber(ConfigCodec.Fields.SCHEMA_VERSION_FIELD).intValue();
  if (schemaVersion != 1) {
    throw new IllegalStateException(
        String.format(
            "unexpected schema version '%d' for %s",
            schemaVersion,
            CoreDocumentSynchronizationConfig.class.getSimpleName()));
  }

  final MongoNamespace namespace =
      new MongoNamespace(document.getString(ConfigCodec.Fields.NAMESPACE_FIELD).getValue());

  final BsonDocument lastVersion;
  if (document.containsKey(ConfigCodec.Fields.LAST_KNOWN_REMOTE_VERSION_FIELD)) {
    lastVersion = document.getDocument(ConfigCodec.Fields.LAST_KNOWN_REMOTE_VERSION_FIELD);
  } else {
    lastVersion = null;
  }

  final ChangeEvent<BsonDocument> lastUncommittedChangeEvent;
  if (document.containsKey(ConfigCodec.Fields.LAST_UNCOMMITTED_CHANGE_EVENT)) {
    final BsonBinary eventBin =
        document.getBinary(ConfigCodec.Fields.LAST_UNCOMMITTED_CHANGE_EVENT);
    final BsonReader innerReader = new BsonBinaryReader(ByteBuffer.wrap(eventBin.getData()));
    lastUncommittedChangeEvent = ResultDecoders.changeEventDecoder(BSON_DOCUMENT_CODEC)
        .decode(innerReader, DecoderContext.builder().build());
  } else {
    lastUncommittedChangeEvent = null;
  }

  return new CoreDocumentSynchronizationConfig(
      null,
      namespace,
      document.get(ConfigCodec.Fields.DOCUMENT_ID_FIELD),
      lastUncommittedChangeEvent,
      document.getNumber(ConfigCodec.Fields.LAST_RESOLUTION_FIELD).longValue(),
      lastVersion,
      new ReentrantReadWriteLock(),
      document.getBoolean(ConfigCodec.Fields.IS_STALE).getValue(),
      document.getBoolean(ConfigCodec.Fields.IS_PAUSED, new BsonBoolean(false)).getValue(),
      document.getInt64(ConfigCodec.Fields.LAST_KNOWN_HASH_FIELD, new BsonInt64(0))
        .getValue());
}
 
Example 19
Source File: MongoWriterUtil.java    From epcis with Apache License 2.0 4 votes vote down vote up
static public void addBasicTimestampProperties(ChronoGraph pg, Long eventTime, BsonValue classElem,
		String readPoint, String bizLocation, BsonArray sourceList, BsonArray destinationList) {

	BsonDocument classDoc = classElem.asDocument();
	String epcClass = classDoc.getString("epcClass").getValue();

	BsonDocument classProperty = new BsonDocument();
	if (!classDoc.containsKey("epcClass"))
		return;
	if (classDoc.containsKey("quantity"))
		classProperty.put("quantity", classDoc.getDouble("quantity"));
	if (classDoc.containsKey("uom"))
		classProperty.put("uom", classDoc.getString("uom"));
	pg.getChronoVertex(epcClass).setTimestampProperties(eventTime, classProperty);

	// Read Point
	if (readPoint != null) {
		pg.addTimestampEdgeProperties(epcClass, readPoint, "isLocatedIn", eventTime,
				new BsonDocument("isReadPoint", new BsonBoolean(true)));
	}
	// BizLocation
	if (bizLocation != null) {
		pg.addTimestampEdgeProperties(epcClass, bizLocation, "isLocatedIn", eventTime,
				new BsonDocument("isReadPoint", new BsonBoolean(false)));
	}

	if (sourceList != null) {
		sourceList.parallelStream().forEach(elem -> {
			BsonDocument sourceDoc = elem.asDocument();
			if (sourceDoc.containsKey("urn:epcglobal:cbv:sdt:possessing_party")) {
				pg.addTimestampEdgeProperties(epcClass,
						sourceDoc.getString("urn:epcglobal:cbv:sdt:possessing_party").getValue(), "isPossessed",
						eventTime, new BsonDocument("action", new BsonString("DELETE")));
			}

			if (sourceDoc.containsKey("urn:epcglobal:cbv:sdt:owning_party")) {
				pg.addTimestampEdgeProperties(epcClass,
						sourceDoc.getString("urn:epcglobal:cbv:sdt:owning_party").getValue(), "isOwned", eventTime,
						new BsonDocument("action", new BsonString("DELETE")));
			}
		});
	}

	if (destinationList != null) {
		destinationList.parallelStream().forEach(elem -> {
			BsonDocument destDoc = elem.asDocument();
			if (destDoc.containsKey("urn:epcglobal:cbv:sdt:possessing_party")) {
				pg.addTimestampEdgeProperties(epcClass,
						destDoc.getString("urn:epcglobal:cbv:sdt:possessing_party").getValue(), "isPossessed",
						eventTime, new BsonDocument("action", new BsonString("ADD")));
			}

			if (destDoc.containsKey("urn:epcglobal:cbv:sdt:owning_party")) {
				pg.addTimestampEdgeProperties(epcClass,
						destDoc.getString("urn:epcglobal:cbv:sdt:owning_party").getValue(), "isOwned", eventTime,
						new BsonDocument("action", new BsonString("ADD")));
			}
		});
	}
}
 
Example 20
Source File: DeleteOneDefaultStrategy.java    From mongo-kafka with Apache License 2.0 4 votes vote down vote up
@Override
public BsonValue generateId(final SinkDocument doc, final SinkRecord orig) {
  BsonDocument kd = doc.getKeyDoc().get();
  return kd.containsKey(ID_FIELD) ? kd : new BsonDocument(ID_FIELD, kd);
}