org.bson.BsonTimestamp Java Examples

The following examples show how to use org.bson.BsonTimestamp. 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: TestUpdateOps.java    From morphia with Apache License 2.0 6 votes vote down vote up
@Test
public void testCurrentDate() {
    getDs().save(new DumbColl("currentDate"));

    getDs().find(DumbColl.class)
           .update(currentDate("localDateTime"))
           .execute();

    Document document = getDatabase().getCollection(getMapper().getCollection(DumbColl.class).getNamespace().getCollectionName())
                                     .find()
                                     .first();
    Assert.assertNotNull(document.getDate("localDateTime"));

    getDs().find(DumbColl.class)
           .update(currentDate("localDateTime")
                       .type(TypeSpecification.TIMESTAMP))
           .execute();

    document = getDatabase().getCollection(getMapper().getCollection(DumbColl.class).getNamespace().getCollectionName())
                            .find()
                            .first();
    Assert.assertTrue(document.get("localDateTime") instanceof BsonTimestamp);
}
 
Example #2
Source File: BsonTypeMap.java    From morphia with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the map
 */
public BsonTypeMap() {
    map.put(List.class, BsonType.ARRAY);
    map.put(Binary.class, BsonType.BINARY);
    map.put(Boolean.class, BsonType.BOOLEAN);
    map.put(Date.class, BsonType.DATE_TIME);
    map.put(BsonDbPointer.class, BsonType.DB_POINTER);
    map.put(Document.class, BsonType.DOCUMENT);
    map.put(Double.class, BsonType.DOUBLE);
    map.put(Integer.class, BsonType.INT32);
    map.put(Long.class, BsonType.INT64);
    map.put(Decimal128.class, BsonType.DECIMAL128);
    map.put(MaxKey.class, BsonType.MAX_KEY);
    map.put(MinKey.class, BsonType.MIN_KEY);
    map.put(Code.class, BsonType.JAVASCRIPT);
    map.put(CodeWithScope.class, BsonType.JAVASCRIPT_WITH_SCOPE);
    map.put(ObjectId.class, BsonType.OBJECT_ID);
    map.put(BsonRegularExpression.class, BsonType.REGULAR_EXPRESSION);
    map.put(String.class, BsonType.STRING);
    map.put(Symbol.class, BsonType.SYMBOL);
    map.put(BsonTimestamp.class, BsonType.TIMESTAMP);
    map.put(BsonUndefined.class, BsonType.UNDEFINED);
}
 
Example #3
Source File: MongoDB.java    From jlogstash-input-plugin with Apache License 2.0 6 votes vote down vote up
private DateTime getLastDateTime(Object lastDateValue) {
    if (lastDateValue == null) {
        return null;
    }

    // ObjectId类型
    if ("id".equals(since_type)) {
        ObjectId objectId = (ObjectId) lastDateValue;
        return new DateTime(objectId.getDate());
    } else {
        Class<?> clazz = lastDateValue.getClass();
        if (String.class.isAssignableFrom(clazz)) {
            // TODO format
        } else if (BsonTimestamp.class.isAssignableFrom(clazz)) {
            // TODO convert
        }
    }
    return null;
}
 
Example #4
Source File: MongoDBSourceBuilder.java    From hazelcast-jet-contrib with Apache License 2.0 6 votes vote down vote up
private static <T, U> SupplierEx<StreamContext<T, U>> contextFn(
        SupplierEx<? extends MongoClient> connectionSupplier,
        FunctionEx<? super MongoClient, ? extends MongoDatabase> databaseFn,
        FunctionEx<? super MongoDatabase, ? extends MongoCollection<? extends T>> collectionFn,
        ConsumerEx<? super MongoClient> destroyFn,
        FunctionEx<? super MongoCollection<? extends T>, ? extends ChangeStreamIterable<? extends T>> searchFn,
        FunctionEx<? super ChangeStreamDocument<? extends T>, U> mapFn,
        FunctionEx<? super MongoClient, ? extends BsonTimestamp> startAtOperationTimeFn

) {
    return () -> {
        MongoClient client = connectionSupplier.get();
        MongoDatabase database = databaseFn.apply(client);
        MongoCollection<? extends T> collection = collectionFn.apply(database);
        ChangeStreamIterable<? extends T> changeStreamIterable = searchFn.apply(collection);
        return new StreamContext<>(client, changeStreamIterable, mapFn, destroyFn, startAtOperationTimeFn);
    };
}
 
Example #5
Source File: JsonStructConverter.java    From kafka-connect-mongodb with Apache License 2.0 6 votes vote down vote up
@Override
public Struct toStruct(Document document, Schema schema) {
	final Struct messageStruct = new Struct(schema);
	final BsonTimestamp bsonTimestamp = (BsonTimestamp) document.get("ts");
	final Integer seconds = bsonTimestamp.getTime();
	final Integer order = bsonTimestamp.getInc();
	messageStruct.put("timestamp", seconds);
	messageStruct.put("order", order);
	messageStruct.put("operation", document.get("op"));
	messageStruct.put("database", document.get("ns"));
	
	final Document modifiedDocument = (Document) document.get("o");
	messageStruct.put("object", modifiedDocument.toJson());

	return messageStruct;
}
 
Example #6
Source File: DocTimestampTest.java    From syncer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void compareTo() {
  DocTimestamp b_e = DocTimestamp.earliest;
  DocTimestamp b_l = DocTimestamp.latest;
  DocTimestamp b__l = DocTimestamp.latest;
  DocTimestamp b0 = new DocTimestamp( new BsonTimestamp(0));
  DocTimestamp b1 = new DocTimestamp( new BsonTimestamp(1));
  DocTimestamp b2 = new DocTimestamp( new BsonTimestamp(2));
  DocTimestamp b_b = new DocTimestamp( new BsonTimestamp((int) (System.currentTimeMillis() / 1000) - 1, 0));
  DocTimestamp bc = new DocTimestamp( new BsonTimestamp((int) (System.currentTimeMillis() / 1000) + 1, 0));

  assertEquals(b_e.compareTo(b0), -1);
  assertEquals(b_l.compareTo(b_b), 1);
  assertEquals(b_l.compareTo(bc), -1);
  assertEquals(b_e.compareTo(b_l), -1);
  assertEquals(b_l.compareTo(b__l), 0);
  assertEquals(b0.compareTo(b2), -1);
  assertEquals(b1.compareTo(b2), -1);
  assertEquals(b2.compareTo(b0), 1);
  assertEquals(b2.compareTo(bc), -1);
}
 
Example #7
Source File: MongoTypeUtil.java    From syncer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static Object convertBsonTypes(Object o) {
	if (o instanceof Map) {
		if (((Map) o).size() == 1 && ((Map) o).containsKey("$numberDecimal")) {
			return new BigDecimal((String) ((Map) o).get("$numberDecimal"));
		}
		for (Map.Entry<String, Object> e : ((Map<String, Object>) o).entrySet()) {
			e.setValue(convertBsonTypes(e.getValue()));
		}
	} else if (o instanceof List) {
		List list = (List) o;
		for (int i = 0; i < list.size(); i++) {
			list.set(i, convertBsonTypes(list.get(i)));
		}
	} else if (o instanceof Binary) {
		return ((Binary) o).getData();
	} else if (o instanceof Decimal128) {
		return ((Decimal128) o).bigDecimalValue();
	} else if (o instanceof BsonTimestamp) {
		return convertBson((BsonValue) o);
	} else if (o instanceof ObjectId) {
		return o.toString();
	}
	return o;
}
 
Example #8
Source File: StringStructConverter.java    From kafka-connect-mongodb with Apache License 2.0 5 votes vote down vote up
@Override
public Struct toStruct(Document document, Schema schema) {
       Struct messageStruct = new Struct(schema);
       BsonTimestamp bsonTimestamp = (BsonTimestamp) document.get("ts");
       Integer seconds = bsonTimestamp.getTime();
       Integer order = bsonTimestamp.getInc();
       messageStruct.put("timestamp", seconds);
       messageStruct.put("order", order);
       messageStruct.put("operation", document.get("op"));
       messageStruct.put("database", document.get("ns"));
       messageStruct.put("object", document.get("o").toString());
       
       return messageStruct;
}
 
Example #9
Source File: TypeConversionTest.java    From immutables with Apache License 2.0 5 votes vote down vote up
@Test
void parseExceptions() {
  Assertions.assertThrows(JsonParseException.class, () -> {
    Parsers.parserAt(new BsonBoolean(true)).getNumberValue();
  });

  Assertions.assertThrows(JsonParseException.class, () -> {
    Parsers.parserAt(new BsonBoolean(true)).getLongValue();
  });

  Assertions.assertThrows(JsonParseException.class, () -> {
    Parsers.parserAt(BsonNull.VALUE).getNumberValue();
  });

  Assertions.assertThrows(JsonParseException.class, () -> {
    Parsers.parserAt(BsonNull.VALUE).getNumberType();
  });

  Assertions.assertThrows(JsonParseException.class, () -> {
    Parsers.parserAt(new BsonInt32(42)).getBooleanValue();
  });

  Assertions.assertThrows(JsonParseException.class, () -> {
    Parsers.parserAt(new BsonBoolean(true)).getNumberType();
  });

  Assertions.assertThrows(JsonParseException.class, () -> {
    Parsers.parserAt(new BsonDateTime(42)).getBooleanValue();
  });

  Assertions.assertThrows(JsonParseException.class, () -> {
    Parsers.parserAt(new BsonTimestamp(42)).getBooleanValue();
  });
}
 
Example #10
Source File: TypeConversionTest.java    From immutables with Apache License 2.0 5 votes vote down vote up
@Test
void timestamp() throws IOException {
  final long epoch = System.currentTimeMillis();
  check(Parsers.parserAt(new BsonTimestamp(epoch)).getIntValue()).is((int) epoch);
  check(Parsers.parserAt(new BsonTimestamp(epoch)).getLongValue()).is(epoch);
  check(Parsers.parserAt(new BsonTimestamp(epoch)).getDoubleValue()).is((double) epoch);
  check(Parsers.parserAt(new BsonTimestamp(epoch)).getDecimalValue()).is(BigDecimal.valueOf(epoch));
  check(Parsers.parserAt(new BsonTimestamp(epoch)).getBigIntegerValue()).is(BigInteger.valueOf(epoch));
  check(Parsers.parserAt(new BsonTimestamp(epoch)).getText()).is(Long.toString(epoch));
}
 
Example #11
Source File: TypeConversionTest.java    From immutables with Apache License 2.0 5 votes vote down vote up
@Test
public void timestamp() throws IOException {
  final long epoch = System.currentTimeMillis();
  BsonTimestamp value = new BsonTimestamp(epoch);
  check(Jsons.readerAt(value).peek()).is(JsonToken.NUMBER);
  check(Jsons.readerAt(value).nextInt()).is((int) epoch);
  check(Jsons.readerAt(value).nextLong()).is(epoch);
  check(Jsons.readerAt(value).nextDouble()).is((double) epoch);
}
 
Example #12
Source File: MongoDBOplogSource.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private Record getOplogRecord() throws IOException {
  Document doc = cursor.tryNext();
  if (doc != null) {
    validateOpLogDocument(doc);
    BsonTimestamp timestamp = (BsonTimestamp) doc.get(TIMESTAMP_FIELD);
    lastOffsetTsSeconds = timestamp.getTime();
    lastOffsetTsOrdinal = timestamp.getInc();

    //This does not seem to be always increasing, but is unique,
    // we are not using it for offset but using it for source record id
    Long opId = doc.getLong(OP_LONG_HASH_FIELD);

    Record record = getContext().createRecord(
            MongoDBUtil.getSourceRecordId(
            configBean.mongoConfig.connectionString,
            configBean.mongoConfig.database,
            configBean.mongoConfig.collection,
            String.valueOf(opId) + "::" + createOffset()
        )
    );
    String ns = doc.getString(NS_FIELD);
    String opType = doc.getString(OP_TYPE_FIELD);
    record.getHeader().setAttribute(NS_FIELD, ns);
    record.getHeader().setAttribute(OP_TYPE_FIELD, opType);
    //Populate Generic operation type
    populateGenericOperationTypeInHeader(record, opType);
    record.set(Field.create(MongoDBUtil.createFieldFromDocument(doc)));
    return record;
  } else {
    LOG.trace("Document from Cursor is null, No More Records");
  }
  return null;
}
 
Example #13
Source File: MongoDBOplogSource.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private void prepareCursor(int timestampSeconds, int ordinal, List<OplogOpType> filterOplogTypes, int batchSize) {
  LOG.debug("Getting new cursor with offset - TimeStampInSeconds:'{}', Ordinal : '{}' and Batch Size : '{}'",timestampSeconds, ordinal, batchSize);
  FindIterable<Document> mongoCursorIterable = mongoCollection
      .find()
      //As the collection is a capped collection we use Tailable cursor which will return results in natural order in this case
      //based on ts timestamp field.
      //Tailable Await does not return and blocks, so we are using tailable.
      .cursorType(CursorType.Tailable)
      .batchSize(batchSize);

  List<Bson> andFilters = new ArrayList<>();
  //Only filter if we already have saved/initial offset specified or else both time_t and ordinal will not be -1.
  if (timestampSeconds > 0 && ordinal >= 0) {
    andFilters.add(Filters.gt(TIMESTAMP_FIELD, new BsonTimestamp(timestampSeconds, ordinal)));
  }

  if (!filterOplogTypes.isEmpty()) {
    List<Bson> oplogOptypeFilters = new ArrayList<>();
    Set<OplogOpType> oplogOpTypesSet = new HashSet<>();
    for (OplogOpType filterOplogopType : filterOplogTypes) {
      if (oplogOpTypesSet.add(filterOplogopType)) {
        oplogOptypeFilters.add(Filters.eq(OP_TYPE_FIELD, filterOplogopType.getOp()));
      }
    }
    //Add an or filter for filtered Or Types
    andFilters.add(Filters.or(oplogOptypeFilters));
  }
  //Finally and timestamp with oplog filters
  if (!andFilters.isEmpty()) {
    mongoCursorIterable = mongoCursorIterable.filter(Filters.and(andFilters));
  }
  cursor = mongoCursorIterable.iterator();
}
 
Example #14
Source File: CustomMongoHealthIndicator.java    From hesperides with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void doHealthCheck(Health.Builder builder) {
    // Les "hacks" d'appels à des méthodes privées sont nécessaires pour obtenir le serverSessionPoolInUseCount bien utile
    MongoClient mongoClient = getMongoClient((SimpleMongoDbFactory) this.mongoTemplate.getMongoDbFactory());
    builder.up()
            .withDetail("healthcheck", performHealthCheck(mongoTemplate))
            .withDetail("config", getMongoClientOptionsAsMap(mongoClient.getMongoClientOptions()))
            .withDetail("serverSessionPoolInUseCount", getServerSessionPool(mongoClient).getInUseCount())
            .withDetail("clusterTime", Optional.ofNullable(getCluster(mongoClient).getClusterTime()).map(BsonTimestamp::getValue).orElse(0L));
}
 
Example #15
Source File: MongodbSourceTask.java    From kafka-connect-mongodb with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates the timestamp of the message.
 *
 * @param message from which retrieve the timestamp
 * @return BsonTimestamp formatted as a String (seconds+inc)
 */
private String getTimestamp(Document message) {
    BsonTimestamp timestamp = (BsonTimestamp) message.get("ts");
    return new StringBuilder()
            .append(timestamp.getTime())
            .append("_")
            .append(timestamp.getInc())
            .toString();
}
 
Example #16
Source File: DataId.java    From syncer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static DocTimestamp fromMongoDataId(String dataId) {
  String[] split = dataId.split(SEP);
  if (split.length == 2
      || split.length == 3) { // for backward compatibility
    return new DocTimestamp(
        new BsonTimestamp(Integer.parseInt(split[0]), Integer.parseInt(split[1])));
  }
  throw new IllegalArgumentException(dataId);
}
 
Example #17
Source File: MongoDBSourceBuilder.java    From hazelcast-jet-contrib with Apache License 2.0 5 votes vote down vote up
private static <T, U> SupplierEx<StreamContext<T, U>> contextFn(
        SupplierEx<? extends MongoClient> connectionSupplier,
        FunctionEx<? super MongoClient, ? extends MongoDatabase> databaseFn,
        ConsumerEx<? super MongoClient> destroyFn,
        FunctionEx<? super MongoDatabase, ? extends ChangeStreamIterable<? extends T>> searchFn,
        FunctionEx<? super ChangeStreamDocument<? extends T>, U> mapFn,
        FunctionEx<? super MongoClient, ? extends BsonTimestamp> startAtOperationTimeFn
) {
    return () -> {
        MongoClient client = connectionSupplier.get();
        MongoDatabase database = databaseFn.apply(client);
        ChangeStreamIterable<? extends T> changeStreamIterable = searchFn.apply(database);
        return new StreamContext<>(client, changeStreamIterable, mapFn, destroyFn, startAtOperationTimeFn);
    };
}
 
Example #18
Source File: OpLogClientFactory.java    From kkbinlog with Apache License 2.0 5 votes vote down vote up
/**
 * 配置当前binlog位置
 *
 * @param oplogClient
 * @param binaryLogConfig
 */
private void configOpLogStatus(OplogClient oplogClient, BinaryLogConfig binaryLogConfig) {
    JSONObject binLogStatus = etcdService.getBinaryLogStatus(binaryLogConfig);
    if (binLogStatus != null) {
        int seconds = binLogStatus.getIntValue("binlogFilename");
        int inc = binLogStatus.getIntValue("binlogPosition");
        oplogClient.setTs(new BsonTimestamp(seconds, inc));
    }
}
 
Example #19
Source File: MongoDBSourceBuilder.java    From hazelcast-jet-contrib with Apache License 2.0 5 votes vote down vote up
private static <T, U> SupplierEx<StreamContext<T, U>> contextFn(
        SupplierEx<? extends MongoClient> connectionSupplier,
        ConsumerEx<? super MongoClient> destroyFn,
        FunctionEx<? super MongoClient, ? extends ChangeStreamIterable<? extends T>> searchFn,
        FunctionEx<? super ChangeStreamDocument<? extends T>, U> mapFn,
        FunctionEx<? super MongoClient, ? extends BsonTimestamp> startAtOperationTimeFn
) {
    return () -> {
        MongoClient client = connectionSupplier.get();
        ChangeStreamIterable<? extends T> changeStreamIterable = searchFn.apply(client);
        return new StreamContext<>(client, changeStreamIterable, mapFn, destroyFn, startAtOperationTimeFn);
    };
}
 
Example #20
Source File: MongoDBSourceBuilder.java    From hazelcast-jet-contrib with Apache License 2.0 5 votes vote down vote up
StreamContext(
        MongoClient client,
        ChangeStreamIterable<? extends T> changeStreamIterable,
        FunctionEx<? super ChangeStreamDocument<? extends T>, U> mapFn,
        ConsumerEx<? super MongoClient> destroyFn,
        FunctionEx<? super MongoClient, ? extends BsonTimestamp> startAtOperationTimeFn
) {
    this.client = client;
    this.changeStreamIterable = changeStreamIterable;
    this.mapFn = mapFn;
    this.destroyFn = destroyFn;

    this.timestamp = startAtOperationTimeFn == null ? null : startAtOperationTimeFn.apply(client);
}
 
Example #21
Source File: ClientSessionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 4 votes vote down vote up
@Override
public void advanceOperationTime(final BsonTimestamp operationTime) {
    wrapped.advanceOperationTime(operationTime);
}
 
Example #22
Source File: BaseMorphiaSession.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
public void advanceOperationTime(final BsonTimestamp operationTime) {
    session.advanceOperationTime(operationTime);
}
 
Example #23
Source File: BaseMorphiaSession.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
public BsonTimestamp getOperationTime() {
    return session.getOperationTime();
}
 
Example #24
Source File: DocumentWriter.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
public void writeTimestamp(final String name, final BsonTimestamp value) {
    writeName(name);
    state.value(value);
}
 
Example #25
Source File: DocumentWriter.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
public void writeTimestamp(final BsonTimestamp value) {
    state.value(value);
}
 
Example #26
Source File: DocumentReader.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
public BsonTimestamp readTimestamp(final String name) {
    verifyName(name);
    return readTimestamp();
}
 
Example #27
Source File: DocumentReader.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
public BsonTimestamp readTimestamp() {
    return (BsonTimestamp) stage().value();
}
 
Example #28
Source File: WritecontextTest.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
@Test
public void parseBsonArrayWithValues() throws IOException {

    BsonValue a = new BsonString("stest");
    BsonValue b = new BsonDouble(111);
    BsonValue c = new BsonBoolean(true);

    BsonDocument document = new BsonDocument()
            .append("int32", new BsonInt32(12))
            .append("int64", new BsonInt64(77L))
            .append("bo\"olean", new BsonBoolean(true))
            .append("date", new BsonDateTime(new Date().getTime()))
            .append("double", new BsonDouble(12.3))
            .append("string", new BsonString("pinpoint"))
            .append("objectId", new BsonObjectId(new ObjectId()))
            .append("code", new BsonJavaScript("int i = 10;"))
            .append("codeWithScope", new BsonJavaScriptWithScope("int x = y", new BsonDocument("y", new BsonInt32(1))))
            .append("regex", new BsonRegularExpression("^test.*regex.*xyz$", "big"))
            .append("symbol", new BsonSymbol("wow"))
            .append("timestamp", new BsonTimestamp(0x12345678, 5))
            .append("undefined", new BsonUndefined())
            .append("binary1", new BsonBinary(new byte[]{(byte) 0xe0, 0x4f, (byte) 0xd0, 0x20}))
            .append("oldBinary", new BsonBinary(BsonBinarySubType.OLD_BINARY, new byte[]{1, 1, 1, 1, 1}))
            .append("arrayInt", new BsonArray(Arrays.asList(a, b, c, new BsonInt32(7))))
            .append("document", new BsonDocument("a", new BsonInt32(77)))
            .append("dbPointer", new BsonDbPointer("db.coll", new ObjectId()))
            .append("null", new BsonNull())
            .append("decimal128", new BsonDecimal128(new Decimal128(55)));

    BasicDBObject query = new BasicDBObject();
    query.put("ComplexBson", document);

    logger.debug("document:{}", document);

    NormalizedBson stringStringValue = MongoUtil.parseBson(new Object[]{query}, true);
    logger.debug("val:{}", stringStringValue);

    List list = objectMapper.readValue("[" + stringStringValue.getNormalizedBson() + "]", List.class);
    Assert.assertEquals(list.size(), 1);
    Map<String, ?> query1Map = (Map<String, ?>) list.get(0);

    checkValue(query1Map);
}
 
Example #29
Source File: DataId.java    From syncer with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static MongoDataId fromDocument(BsonTimestamp timestamp) {
  return new MongoDataId(timestamp.getTime(), timestamp.getInc());
}
 
Example #30
Source File: ClientSessionImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 4 votes vote down vote up
@Override
public BsonTimestamp getOperationTime() {
    return wrapped.getOperationTime();
}