Search in sources :

Example 1 with BSONTimestamp

use of org.bson.types.BSONTimestamp in project mongo-hadoop by mongodb.

the class BSONComparator method compareValues.

/**
     * @param one, two - two objects with the same type Cast the two objects and compare the values
     */
private int compareValues(final Object one, final Object two) {
    int diff = 0;
    if (one instanceof Number) {
        // Need to be comparing all numeric values to one another, 
        // so cast all of them to Double
        diff = Double.valueOf(one.toString()).compareTo(Double.valueOf(two.toString()));
    } else if (one instanceof String) {
        diff = ((String) one).compareTo((String) two);
    } else if (one instanceof BSONObject) {
        // BasicBSONObject and BasicBSONList both covered in this cast
        diff = compare((BSONObject) one, (BSONObject) two);
    } else if (one instanceof Binary) {
        ByteBuffer buff1 = ByteBuffer.wrap(((Binary) one).getData());
        ByteBuffer buff2 = ByteBuffer.wrap(((Binary) two).getData());
        diff = buff1.compareTo(buff2);
    } else if (one instanceof byte[]) {
        ByteBuffer buff1 = ByteBuffer.wrap((byte[]) one);
        ByteBuffer buff2 = ByteBuffer.wrap((byte[]) two);
        diff = buff1.compareTo(buff2);
    } else if (one instanceof ObjectId) {
        diff = ((ObjectId) one).compareTo((ObjectId) two);
    } else if (one instanceof Boolean) {
        diff = ((Boolean) one).compareTo((Boolean) two);
    } else if (one instanceof Date) {
        diff = ((Date) one).compareTo((Date) two);
    } else if (one instanceof BSONTimestamp) {
        diff = ((BSONTimestamp) one).compareTo((BSONTimestamp) two);
    }
    return diff;
}
Also used : ObjectId(org.bson.types.ObjectId) LazyBSONObject(org.bson.LazyBSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) BSONTimestamp(org.bson.types.BSONTimestamp) Binary(org.bson.types.Binary) ByteBuffer(java.nio.ByteBuffer) Date(java.util.Date)

Example 2 with BSONTimestamp

use of org.bson.types.BSONTimestamp in project mongo-java-driver by mongodb.

the class BasicBSONEncoder method _putObjectField.

/**
     * Encodes any Object type
     *
     * @param name         the field name
     * @param initialValue the value to write
     */
protected void _putObjectField(final String name, final Object initialValue) {
    if ("_transientFields".equals(name)) {
        return;
    }
    if (name.contains("\0")) {
        throw new IllegalArgumentException("Document field names can't have a NULL character. (Bad Key: '" + name + "')");
    }
    if ("$where".equals(name) && initialValue instanceof String) {
        putCode(name, new Code((String) initialValue));
    }
    Object value = BSON.applyEncodingHooks(initialValue);
    if (value == null) {
        putNull(name);
    } else if (value instanceof Date) {
        putDate(name, (Date) value);
    } else if (value instanceof Number) {
        putNumber(name, (Number) value);
    } else if (value instanceof Decimal128) {
        putDecimal128(name, (Decimal128) value);
    } else if (value instanceof Character) {
        putString(name, value.toString());
    } else if (value instanceof String) {
        putString(name, value.toString());
    } else if (value instanceof ObjectId) {
        putObjectId(name, (ObjectId) value);
    } else if (value instanceof Boolean) {
        putBoolean(name, (Boolean) value);
    } else if (value instanceof Pattern) {
        putPattern(name, (Pattern) value);
    } else if (value instanceof Iterable) {
        putIterable(name, (Iterable) value);
    } else if (value instanceof BSONObject) {
        putObject(name, (BSONObject) value);
    } else if (value instanceof Map) {
        putMap(name, (Map) value);
    } else if (value instanceof byte[]) {
        putBinary(name, (byte[]) value);
    } else if (value instanceof Binary) {
        putBinary(name, (Binary) value);
    } else if (value instanceof UUID) {
        putUUID(name, (UUID) value);
    } else if (value.getClass().isArray()) {
        putArray(name, value);
    } else if (value instanceof Symbol) {
        putSymbol(name, (Symbol) value);
    } else if (value instanceof BSONTimestamp) {
        putTimestamp(name, (BSONTimestamp) value);
    } else if (value instanceof CodeWScope) {
        putCodeWScope(name, (CodeWScope) value);
    } else if (value instanceof Code) {
        putCode(name, (Code) value);
    } else if (value instanceof DBRef) {
        BSONObject temp = new BasicBSONObject();
        DBRef dbRef = (DBRef) value;
        temp.put("$ref", dbRef.getCollectionName());
        temp.put("$id", dbRef.getId());
        if (dbRef.getDatabaseName() != null) {
            temp.put("$db", dbRef.getDatabaseName());
        }
        putObject(name, temp);
    } else if (value instanceof MinKey) {
        putMinKey(name);
    } else if (value instanceof MaxKey) {
        putMaxKey(name);
    } else if (putSpecial(name, value)) {
    // no-op
    } else {
        throw new IllegalArgumentException("Can't serialize " + value.getClass());
    }
}
Also used : Pattern(java.util.regex.Pattern) ObjectId(org.bson.types.ObjectId) Symbol(org.bson.types.Symbol) DBRef(com.mongodb.DBRef) MaxKey(org.bson.types.MaxKey) BSONTimestamp(org.bson.types.BSONTimestamp) Decimal128(org.bson.types.Decimal128) Code(org.bson.types.Code) Date(java.util.Date) CodeWScope(org.bson.types.CodeWScope) MinKey(org.bson.types.MinKey) Binary(org.bson.types.Binary) UUID(java.util.UUID) Map(java.util.Map)

Example 3 with BSONTimestamp

use of org.bson.types.BSONTimestamp in project mongo-java-driver by mongodb.

the class DBCollectionTest method shouldAcceptDocumentsWithAllValidValueTypes.

@Test
public void shouldAcceptDocumentsWithAllValidValueTypes() {
    BasicDBObject doc = new BasicDBObject();
    doc.append("_id", new ObjectId());
    doc.append("bool", true);
    doc.append("int", 3);
    doc.append("short", (short) 4);
    doc.append("long", 5L);
    doc.append("str", "Hello MongoDB");
    doc.append("float", 6.0f);
    doc.append("double", 1.1);
    doc.append("date", new Date());
    doc.append("ts", new BSONTimestamp(5, 1));
    doc.append("pattern", Pattern.compile(".*"));
    doc.append("minKey", new MinKey());
    doc.append("maxKey", new MaxKey());
    doc.append("js", new Code("code"));
    doc.append("jsWithScope", new CodeWScope("code", new BasicDBObject()));
    doc.append("null", null);
    doc.append("uuid", UUID.randomUUID());
    doc.append("db ref", new com.mongodb.DBRef("test", new ObjectId()));
    doc.append("binary", new Binary((byte) 42, new byte[] { 10, 11, 12 }));
    doc.append("byte array", new byte[] { 1, 2, 3 });
    doc.append("int array", new int[] { 4, 5, 6 });
    doc.append("list", asList(7, 8, 9));
    doc.append("doc list", asList(new Document("x", 1), new Document("x", 2)));
    collection.insert(doc);
    DBObject found = collection.findOne();
    assertNotNull(found);
    assertEquals(ObjectId.class, found.get("_id").getClass());
    assertEquals(Boolean.class, found.get("bool").getClass());
    assertEquals(Integer.class, found.get("int").getClass());
    assertEquals(Integer.class, found.get("short").getClass());
    assertEquals(Long.class, found.get("long").getClass());
    assertEquals(String.class, found.get("str").getClass());
    assertEquals(Double.class, found.get("float").getClass());
    assertEquals(Double.class, found.get("double").getClass());
    assertEquals(Date.class, found.get("date").getClass());
    assertEquals(BSONTimestamp.class, found.get("ts").getClass());
    assertEquals(Pattern.class, found.get("pattern").getClass());
    assertEquals(MinKey.class, found.get("minKey").getClass());
    assertEquals(MaxKey.class, found.get("maxKey").getClass());
    assertEquals(Code.class, found.get("js").getClass());
    assertEquals(CodeWScope.class, found.get("jsWithScope").getClass());
    assertNull(found.get("null"));
    assertEquals(UUID.class, found.get("uuid").getClass());
    assertEquals(DBRef.class, found.get("db ref").getClass());
    assertEquals(Binary.class, found.get("binary").getClass());
    assertEquals(byte[].class, found.get("byte array").getClass());
    assertTrue(found.get("int array") instanceof List);
    assertTrue(found.get("list") instanceof List);
    assertTrue(found.get("doc list") instanceof List);
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) MaxKey(org.bson.types.MaxKey) BSONTimestamp(org.bson.types.BSONTimestamp) Document(org.bson.Document) Code(org.bson.types.Code) Date(java.util.Date) CodeWScope(org.bson.types.CodeWScope) MinKey(org.bson.types.MinKey) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) Binary(org.bson.types.Binary) Test(org.junit.Test)

Example 4 with BSONTimestamp

use of org.bson.types.BSONTimestamp in project mongo-java-driver by mongodb.

the class JSONCallbackTest method timestampParsing.

@Test
public void timestampParsing() {
    BSONTimestamp timestamp = (BSONTimestamp) JSON.parse(("{ \"$timestamp\" : { \"t\": 123, \"i\": 456 } }"));
    assertEquals(456, timestamp.getInc());
    assertEquals(123, timestamp.getTime());
}
Also used : BSONTimestamp(org.bson.types.BSONTimestamp) Test(org.junit.Test)

Example 5 with BSONTimestamp

use of org.bson.types.BSONTimestamp in project mongo-java-driver by mongodb.

the class JSONTest method testJSONEncoding.

@Test
public void testJSONEncoding() throws ParseException {
    String json = "{ 'str' : 'asdfasd' , 'long' : 123123123123 , 'int' : 5 , 'float' : 0.4 , 'bool' : false , " + "'date' : { '$date' : '2011-05-18T18:56:00Z'} , 'pat' : { '$regex' : '.*' , '$options' : ''} , " + "'oid' : { '$oid' : '4d83ab3ea39562db9c1ae2ae'} , 'ref' : { '$ref' : 'test.test' , " + "'$id' : { '$oid' : '4d83ab59a39562db9c1ae2af'}} , 'code' : { '$code' : 'asdfdsa'} , " + "'codews' : { '$code' : 'ggggg' , " + "'$scope' : { }} , 'ts' : { '$ts' : 1300474885 , '$inc' : 10} , 'null' :  null, " + "'uuid' : { '$uuid' : '60f65152-6d4a-4f11-9c9b-590b575da7b5' }}";
    BasicDBObject a = (BasicDBObject) JSON.parse(json);
    assertEquals("asdfasd", a.get("str"));
    assertEquals(5, a.get("int"));
    assertEquals(123123123123L, a.get("long"));
    assertEquals(0.4d, a.get("float"));
    assertEquals(false, a.get("bool"));
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
    assertEquals(format.parse("2011-05-18T18:56:00Z"), a.get("date"));
    Pattern pat = (Pattern) a.get("pat");
    Pattern pat2 = Pattern.compile(".*", BSON.regexFlags(""));
    assertEquals(pat.pattern(), pat2.pattern());
    assertEquals(pat.flags(), pat2.flags());
    ObjectId oid = (ObjectId) a.get("oid");
    assertEquals(new ObjectId("4d83ab3ea39562db9c1ae2ae"), oid);
    //        DBRef ref = (DBRef) a.get("ref");
    //        assertEquals(new DBRef(null, "test.test", new ObjectId("4d83ab59a39562db9c1ae2af")), ref);
    assertEquals(new Code("asdfdsa"), a.get("code"));
    assertEquals(new CodeWScope("ggggg", new BasicBSONObject()), a.get("codews"));
    assertEquals(new BSONTimestamp(1300474885, 10), a.get("ts"));
    assertEquals(UUID.fromString("60f65152-6d4a-4f11-9c9b-590b575da7b5"), a.get("uuid"));
    String json2 = JSON.serialize(a);
    BasicDBObject b = (BasicDBObject) JSON.parse(json2);
    assertEquals(JSON.serialize(b), JSON.serialize(b));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Pattern(java.util.regex.Pattern) BasicBSONObject(org.bson.BasicBSONObject) SimpleTimeZone(java.util.SimpleTimeZone) ObjectId(org.bson.types.ObjectId) GregorianCalendar(java.util.GregorianCalendar) BSONTimestamp(org.bson.types.BSONTimestamp) SimpleDateFormat(java.text.SimpleDateFormat) Code(org.bson.types.Code) CodeWScope(org.bson.types.CodeWScope) 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