Search in sources :

Example 1 with BsonTimestamp

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

the class JsonWriterTest method testTimestampStrict.

//
@Test
public void testTimestampStrict() {
    writer.writeStartDocument();
    writer.writeTimestamp("timestamp", new BsonTimestamp(1000, 1));
    writer.writeEndDocument();
    String expected = "{ \"timestamp\" : { \"$timestamp\" : { \"t\" : 1000, \"i\" : 1 } } }";
    assertEquals(expected, stringWriter.toString());
}
Also used : BsonTimestamp(org.bson.BsonTimestamp) Test(org.junit.Test)

Example 2 with BsonTimestamp

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

the class CollectionAcceptanceTest method shouldAcceptDocumentsWithAllValidValueTypes.

@Test
public void shouldAcceptDocumentsWithAllValidValueTypes() {
    Document doc = new Document();
    doc.append("_id", new ObjectId());
    doc.append("bool", true);
    doc.append("int", 3);
    doc.append("long", 5L);
    doc.append("str", "Hello MongoDB");
    doc.append("double", 1.1);
    doc.append("date", new Date());
    doc.append("ts", new BsonTimestamp(5, 1));
    doc.append("pattern", new BsonRegularExpression("abc"));
    doc.append("minKey", new MinKey());
    doc.append("maxKey", new MaxKey());
    doc.append("js", new Code("code"));
    doc.append("jsWithScope", new CodeWithScope("code", new Document()));
    doc.append("null", null);
    doc.append("binary", new Binary((byte) 42, new byte[] { 10, 11, 12 }));
    doc.append("list", Arrays.asList(7, 8, 9));
    doc.append("doc list", Arrays.asList(new Document("x", 1), new Document("x", 2)));
    collection.insertOne(doc);
    Document found = collection.find().first();
    assertNotNull(found);
    assertEquals(ObjectId.class, found.get("_id").getClass());
    assertEquals(Boolean.class, found.get("bool").getClass());
    assertEquals(Integer.class, found.get("int").getClass());
    assertEquals(Long.class, found.get("long").getClass());
    assertEquals(String.class, found.get("str").getClass());
    assertEquals(Double.class, found.get("double").getClass());
    assertEquals(Date.class, found.get("date").getClass());
    assertEquals(BsonTimestamp.class, found.get("ts").getClass());
    assertEquals(BsonRegularExpression.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(CodeWithScope.class, found.get("jsWithScope").getClass());
    assertNull(found.get("null"));
    assertEquals(Binary.class, found.get("binary").getClass());
    assertTrue(found.get("list") instanceof List);
    assertTrue(found.get("doc list") instanceof List);
}
Also used : MinKey(org.bson.types.MinKey) ObjectId(org.bson.types.ObjectId) MaxKey(org.bson.types.MaxKey) CodeWithScope(org.bson.types.CodeWithScope) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Binary(org.bson.types.Binary) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonRegularExpression(org.bson.BsonRegularExpression) Code(org.bson.types.Code) Date(java.util.Date) BsonTimestamp(org.bson.BsonTimestamp) Test(org.junit.Test)

Example 3 with BsonTimestamp

use of org.bson.BsonTimestamp in project drill by apache.

the class TestBsonRecordReader method testTimeStampType.

@Test
public void testTimeStampType() throws IOException {
    BsonDocument bsonDoc = new BsonDocument();
    bsonDoc.append("ts", new BsonTimestamp(1000, 10));
    writer.reset();
    bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
    SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader();
    assertEquals(1000l, mapReader.reader("ts").readDateTime().getMillis());
}
Also used : BsonDocument(org.bson.BsonDocument) SingleMapReaderImpl(org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl) BsonDocumentReader(org.bson.BsonDocumentReader) BsonTimestamp(org.bson.BsonTimestamp) Test(org.junit.Test)

Example 4 with BsonTimestamp

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

the class JsonReader method visitTimestampExtendedJson.

private BsonTimestamp visitTimestampExtendedJson() {
    verifyToken(JsonTokenType.COLON);
    JsonToken nextToken = popToken();
    if (nextToken.getType() == JsonTokenType.STRING) {
        BsonTimestamp value = new BsonTimestamp(UnsignedLongs.parse(nextToken.getValue(String.class)));
        verifyToken(JsonTokenType.END_OBJECT);
        return value;
    } else if (nextToken.getType() == JsonTokenType.BEGIN_OBJECT) {
        verifyString("t");
        verifyToken(JsonTokenType.COLON);
        JsonToken timeToken = popToken();
        int time;
        if (timeToken.getType() == JsonTokenType.INT32) {
            time = timeToken.getValue(Integer.class);
        } else {
            throw new JsonParseException("JSON reader expected an integer but found '%s'.", timeToken.getValue());
        }
        verifyToken(JsonTokenType.COMMA);
        verifyString("i");
        verifyToken(JsonTokenType.COLON);
        JsonToken incrementToken = popToken();
        int increment;
        if (incrementToken.getType() == JsonTokenType.INT32) {
            increment = incrementToken.getValue(Integer.class);
        } else {
            throw new JsonParseException("JSON reader expected an integer but found '%s'.", timeToken.getValue());
        }
        verifyToken(JsonTokenType.END_OBJECT);
        return new BsonTimestamp(time, increment);
    } else {
        throw new JsonParseException("JSON reader expected a string or start object, but found '%s'.", nextToken.getValue());
    }
}
Also used : BsonTimestamp(org.bson.BsonTimestamp)

Example 5 with BsonTimestamp

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

the class JsonReaderTest method testTimestampShell.

@Test
public void testTimestampShell() {
    String json = "Timestamp(1234, 1)";
    bsonReader = new JsonReader(json);
    assertEquals(BsonType.TIMESTAMP, bsonReader.readBsonType());
    assertEquals(new BsonTimestamp(1234, 1), bsonReader.readTimestamp());
    assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
}
Also used : BsonTimestamp(org.bson.BsonTimestamp) Test(org.junit.Test)

Aggregations

BsonTimestamp (org.bson.BsonTimestamp)7 Test (org.junit.Test)6 BsonDocument (org.bson.BsonDocument)2 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Date (java.util.Date)1 List (java.util.List)1 SingleMapReaderImpl (org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl)1 BsonDocumentReader (org.bson.BsonDocumentReader)1 BsonRegularExpression (org.bson.BsonRegularExpression)1 Document (org.bson.Document)1 Binary (org.bson.types.Binary)1 Code (org.bson.types.Code)1 CodeWithScope (org.bson.types.CodeWithScope)1 MaxKey (org.bson.types.MaxKey)1 MinKey (org.bson.types.MinKey)1 ObjectId (org.bson.types.ObjectId)1