Search in sources :

Example 6 with BSONTimestamp

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));
}
Also used : BSONTimestamp(org.bson.types.BSONTimestamp) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 7 with BSONTimestamp

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));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BSONTimestamp(org.bson.types.BSONTimestamp) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 8 with BSONTimestamp

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;
}
Also used : ObjectId(org.bson.types.ObjectId) BSONObject(org.bson.BSONObject) GregorianCalendar(java.util.GregorianCalendar) DBRef(com.mongodb.DBRef) MaxKey(org.bson.types.MaxKey) BSONTimestamp(org.bson.types.BSONTimestamp) Code(org.bson.types.Code) Date(java.util.Date) CodeWScope(org.bson.types.CodeWScope) MinKey(org.bson.types.MinKey) SimpleTimeZone(java.util.SimpleTimeZone) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) DBObject(com.mongodb.DBObject) Binary(org.bson.types.Binary) SimpleDateFormat(java.text.SimpleDateFormat) BsonUndefined(org.bson.BsonUndefined) ParsePosition(java.text.ParsePosition)

Example 9 with BSONTimestamp

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());
    }
}
Also used : UuidCodec(org.bson.codecs.UuidCodec) Symbol(org.bson.types.Symbol) MaxKey(org.bson.types.MaxKey) BSONTimestamp(org.bson.types.BSONTimestamp) Code(org.bson.types.Code) Date(java.util.Date) CodeWScope(org.bson.types.CodeWScope) MinKey(org.bson.types.MinKey) Binary(org.bson.types.Binary)

Example 10 with BSONTimestamp

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()));
}
Also used : BSONWritable(com.mongodb.hadoop.io.BSONWritable) BasicBSONObject(org.bson.BasicBSONObject) ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) BSONTimestamp(org.bson.types.BSONTimestamp) TimestampWritable(org.apache.hadoop.hive.serde2.io.TimestampWritable) BasicBSONObject(org.bson.BasicBSONObject) Timestamp(java.sql.Timestamp) BSONTimestamp(org.bson.types.BSONTimestamp) Date(java.util.Date) Test(org.junit.Test)

Aggregations

BSONTimestamp (org.bson.types.BSONTimestamp)12 Date (java.util.Date)8 Test (org.junit.Test)8 Binary (org.bson.types.Binary)7 Code (org.bson.types.Code)6 CodeWScope (org.bson.types.CodeWScope)6 ObjectId (org.bson.types.ObjectId)6 BasicDBObject (com.mongodb.BasicDBObject)5 MaxKey (org.bson.types.MaxKey)5 MinKey (org.bson.types.MinKey)5 DBObject (com.mongodb.DBObject)3 DBRef (com.mongodb.DBRef)3 SimpleDateFormat (java.text.SimpleDateFormat)3 GregorianCalendar (java.util.GregorianCalendar)3 SimpleTimeZone (java.util.SimpleTimeZone)3 BasicBSONObject (org.bson.BasicBSONObject)3 Symbol (org.bson.types.Symbol)3 UUID (java.util.UUID)2 Pattern (java.util.regex.Pattern)2 BSONObject (org.bson.BSONObject)2