use of org.bson.types.BSONTimestamp in project camel by apache.
the class MongoDBTailTrackingStrategyTest method testExtractLastValForTimestamp.
@Test
public void testExtractLastValForTimestamp() throws Exception {
DBObject o = mock(DBObject.class);
final int lastVal = 1483701465;
when(o.get(INCREASING_FIELD_NAME)).thenReturn(new BSONTimestamp(lastVal, 1));
Object res = MongoDBTailTrackingEnum.TIMESTAMP.extractLastVal(o, INCREASING_FIELD_NAME);
assertThat(res, is(lastVal));
}
use of org.bson.types.BSONTimestamp in project camel by apache.
the class MongoDBTailTrackingStrategyTest method testExtracCreateQueryForTimestamp.
@Test
public void testExtracCreateQueryForTimestamp() throws Exception {
final int lastVal = 1483701465;
BasicDBObject basicDBObject = MongoDBTailTrackingEnum.TIMESTAMP.createQuery(lastVal, INCREASING_FIELD_NAME);
final Object actual = basicDBObject.get(INCREASING_FIELD_NAME);
assertThat(actual, is(notNullValue()));
assertThat(actual instanceof BasicDBObject, is(true));
assertThat(((BasicDBObject) actual).get("$gt") instanceof BSONTimestamp, is(true));
BSONTimestamp bsonTimestamp = (BSONTimestamp) ((BasicDBObject) actual).get("$gt");
assertThat(bsonTimestamp.getTime(), is(lastVal));
}
use of org.bson.types.BSONTimestamp in project mongo-java-driver by mongodb.
the class JSONCallback method objectDone.
@Override
public Object objectDone() {
String name = curName();
Object o = super.objectDone();
if (_lastArray) {
return o;
}
BSONObject b = (BSONObject) o;
// override the object if it's a special type
if (b.containsField("$oid")) {
o = new ObjectId((String) b.get("$oid"));
} else if (b.containsField("$date")) {
if (b.get("$date") instanceof Number) {
o = new Date(((Number) b.get("$date")).longValue());
} else {
SimpleDateFormat format = new SimpleDateFormat(_msDateFormat);
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
o = format.parse(b.get("$date").toString(), new ParsePosition(0));
if (o == null) {
// try older format with no ms
format = new SimpleDateFormat(_secDateFormat);
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
o = format.parse(b.get("$date").toString(), new ParsePosition(0));
}
}
} else if (b.containsField("$regex")) {
o = Pattern.compile((String) b.get("$regex"), BSON.regexFlags((String) b.get("$options")));
} else if (b.containsField("$ts")) {
//Legacy timestamp format
Integer ts = ((Number) b.get("$ts")).intValue();
Integer inc = ((Number) b.get("$inc")).intValue();
o = new BSONTimestamp(ts, inc);
} else if (b.containsField("$timestamp")) {
BSONObject tsObject = (BSONObject) b.get("$timestamp");
Integer ts = ((Number) tsObject.get("t")).intValue();
Integer inc = ((Number) tsObject.get("i")).intValue();
o = new BSONTimestamp(ts, inc);
} else if (b.containsField("$code")) {
if (b.containsField("$scope")) {
o = new CodeWScope((String) b.get("$code"), (DBObject) b.get("$scope"));
} else {
o = new Code((String) b.get("$code"));
}
} else if (b.containsField("$ref")) {
o = new DBRef((String) b.get("$ref"), b.get("$id"));
} else if (b.containsField("$minKey")) {
o = new MinKey();
} else if (b.containsField("$maxKey")) {
o = new MaxKey();
} else if (b.containsField("$uuid")) {
o = UUID.fromString((String) b.get("$uuid"));
} else if (b.containsField("$binary")) {
int type = (b.get("$type") instanceof String) ? Integer.valueOf((String) b.get("$type"), 16) : (Integer) b.get("$type");
byte[] bytes = DatatypeConverter.parseBase64Binary((String) b.get("$binary"));
o = new Binary((byte) type, bytes);
} else if (b.containsField("$undefined") && b.get("$undefined").equals(true)) {
o = new BsonUndefined();
} else if (b.containsField("$numberLong")) {
o = Long.valueOf((String) b.get("$numberLong"));
} else if (b.containsField("$numberDecimal")) {
o = Decimal128.parse((String) b.get("$numberDecimal"));
}
if (!isStackEmpty()) {
_put(name, o);
} else {
o = !BSON.hasDecodeHooks() ? o : BSON.applyDecodingHooks(o);
setRoot(o);
}
return o;
}
use of org.bson.types.BSONTimestamp in project mongo-java-driver by mongodb.
the class LazyBSONObject method readValue.
Object readValue(final BsonBinaryReader reader) {
switch(reader.getCurrentBsonType()) {
case DOCUMENT:
return readDocument(reader);
case ARRAY:
return readArray(reader);
case DOUBLE:
return reader.readDouble();
case STRING:
return reader.readString();
case BINARY:
byte binarySubType = reader.peekBinarySubType();
if (BsonBinarySubType.isUuid(binarySubType) && reader.peekBinarySize() == 16) {
return new UuidCodec().decode(reader, DecoderContext.builder().build());
}
BsonBinary binary = reader.readBinaryData();
if (binarySubType == BINARY.getValue() || binarySubType == OLD_BINARY.getValue()) {
return binary.getData();
} else {
return new Binary(binary.getType(), binary.getData());
}
case NULL:
reader.readNull();
return null;
case UNDEFINED:
reader.readUndefined();
return null;
case OBJECT_ID:
return reader.readObjectId();
case BOOLEAN:
return reader.readBoolean();
case DATE_TIME:
return new Date(reader.readDateTime());
case REGULAR_EXPRESSION:
BsonRegularExpression regularExpression = reader.readRegularExpression();
return Pattern.compile(regularExpression.getPattern(), BSON.regexFlags(regularExpression.getOptions()));
case DB_POINTER:
BsonDbPointer dbPointer = reader.readDBPointer();
return callback.createDBRef(dbPointer.getNamespace(), dbPointer.getId());
case JAVASCRIPT:
return new Code(reader.readJavaScript());
case SYMBOL:
return new Symbol(reader.readSymbol());
case JAVASCRIPT_WITH_SCOPE:
return new CodeWScope(reader.readJavaScriptWithScope(), (BSONObject) readJavaScriptWithScopeDocument(reader));
case INT32:
return reader.readInt32();
case TIMESTAMP:
BsonTimestamp timestamp = reader.readTimestamp();
return new BSONTimestamp(timestamp.getTime(), timestamp.getInc());
case INT64:
return reader.readInt64();
case DECIMAL128:
return reader.readDecimal128();
case MIN_KEY:
reader.readMinKey();
return new MinKey();
case MAX_KEY:
reader.readMaxKey();
return new MaxKey();
default:
throw new IllegalArgumentException("unhandled BSON type: " + reader.getCurrentBsonType());
}
}
use of org.bson.types.BSONTimestamp in project mongo-hadoop by mongodb.
the class BSONSerDeTest method testDates.
@Test
public void testDates() throws SerDeException {
String columnNames = "d";
String columnTypes = "timestamp";
Date d = new Date();
Timestamp value = new Timestamp(d.getTime());
BSONSerDe serde = new BSONSerDe();
Object result = helpDeserialize(serde, columnNames, columnTypes, value);
assertThat(value, equalTo(result));
result = serde.deserializeField(d, serde.columnTypes.get(0), "");
assertThat(value, equalTo(result));
BSONTimestamp bts = new BSONTimestamp(((Long) (d.getTime() / 1000L)).intValue(), 1);
result = serde.deserializeField(bts, serde.columnTypes.get(0), "");
// BSONTimestamp only takes an int, so the long returned in the Timestamp won't be the same
assertThat((long) bts.getTime(), equalTo(((Timestamp) result).getTime() / 1000L));
// Utilizes a timestampWritable because there's no native timestamp type in java for
// object inspector class to relate to
ObjectInspector innerInspector = PrimitiveObjectInspectorFactory.getPrimitiveObjectInspectorFromClass(TimestampWritable.class);
BasicBSONObject bObject = new BasicBSONObject();
BSONWritable serialized = (BSONWritable) helpSerialize(columnNames, innerInspector, bObject, new TimestampWritable(value), serde);
// The time going in to serialize is Timestamp but it comes out as BSONTimestamp
BasicBSONObject bsonWithTimestamp = new BasicBSONObject();
bsonWithTimestamp.put(columnNames, bts);
assertThat(value.getTime(), equalTo(((Date) serialized.getDoc().get(columnNames)).getTime()));
}
Aggregations