Search in sources :

Example 46 with Decimal128

use of org.bson.types.Decimal128 in project realm-java by realm.

the class RealmQueryTests method populateTestRealm.

private void populateTestRealm(Realm testRealm, int dataSize) {
    testRealm.beginTransaction();
    testRealm.deleteAll();
    for (int i = 0; i < dataSize; ++i) {
        AllTypes allTypes = testRealm.createObject(AllTypes.class);
        allTypes.setColumnBoolean((i % 3) == 0);
        allTypes.setColumnBinary(new byte[] { 1, 2, 3 });
        allTypes.setColumnDate(new Date(DECADE_MILLIS * (i - (dataSize / 2))));
        allTypes.setColumnDouble(Math.PI);
        allTypes.setColumnFloat(1.2345f + i);
        allTypes.setColumnString("test data " + i);
        allTypes.setColumnLong(i);
        allTypes.setColumnObjectId(new ObjectId(TestHelper.generateObjectIdHexString(i)));
        allTypes.setColumnDecimal128(new Decimal128(new BigDecimal(i + ".23456789")));
        allTypes.setColumnUUID(UUID.fromString(TestHelper.generateUUIDString(i)));
        allTypes.setColumnRealmAny(RealmAny.valueOf(i));
        NonLatinFieldNames nonLatinFieldNames = testRealm.createObject(NonLatinFieldNames.class);
        nonLatinFieldNames.set델타(i);
        nonLatinFieldNames.setΔέλτα(i);
        nonLatinFieldNames.set베타(1.2345f + i);
        nonLatinFieldNames.setΒήτα(1.2345f + i);
        Dog dog = testRealm.createObject(Dog.class);
        dog.setAge(i);
        dog.setName("test data " + i);
        allTypes.setColumnRealmObject(dog);
    }
    testRealm.commitTransaction();
}
Also used : ObjectId(org.bson.types.ObjectId) NonLatinFieldNames(io.realm.entities.NonLatinFieldNames) AllTypes(io.realm.entities.AllTypes) DictionaryAllTypes(io.realm.entities.DictionaryAllTypes) Decimal128(org.bson.types.Decimal128) Dog(io.realm.entities.Dog) Date(java.util.Date) BigDecimal(java.math.BigDecimal)

Example 47 with Decimal128

use of org.bson.types.Decimal128 in project realm-java by realm.

the class DynamicRealmObject method setValueDictionary.

@SuppressWarnings("unchecked")
private <E> void setValueDictionary(String fieldName, RealmDictionary<E> sourceDictionary, RealmFieldType primitiveType) {
    long columnKey = proxyState.getRow$realm().getColumnKey(fieldName);
    OsMap osMap = proxyState.getRow$realm().getValueMap(columnKey, primitiveType);
    Class<E> elementClass;
    switch(primitiveType) {
        case STRING_TO_INTEGER_MAP:
            elementClass = (Class<E>) Long.class;
            break;
        case STRING_TO_BOOLEAN_MAP:
            elementClass = (Class<E>) Boolean.class;
            break;
        case STRING_TO_STRING_MAP:
            elementClass = (Class<E>) String.class;
            break;
        case STRING_TO_BINARY_MAP:
            elementClass = (Class<E>) byte[].class;
            break;
        case STRING_TO_DATE_MAP:
            elementClass = (Class<E>) Date.class;
            break;
        case STRING_TO_FLOAT_MAP:
            elementClass = (Class<E>) Float.class;
            break;
        case STRING_TO_DOUBLE_MAP:
            elementClass = (Class<E>) Double.class;
            break;
        case STRING_TO_DECIMAL128_MAP:
            elementClass = (Class<E>) Decimal128.class;
            break;
        case STRING_TO_OBJECT_ID_MAP:
            elementClass = (Class<E>) ObjectId.class;
            break;
        case STRING_TO_UUID_MAP:
            elementClass = (Class<E>) UUID.class;
            break;
        case STRING_TO_MIXED_MAP:
            elementClass = (Class<E>) RealmAny.class;
            break;
        default:
            throw new IllegalArgumentException("Unsupported type: " + primitiveType);
    }
    // Dictionary in the RealmObject
    RealmDictionary<E> targetDictionary = new RealmDictionary<>(proxyState.getRealm$realm(), osMap, elementClass);
    // We move the data in a auxiliary dictionary to prevent removing the values when the input and out dicts are
    // the same.
    RealmDictionary<E> auxiliaryDictionary = new RealmDictionary<>();
    for (Map.Entry<String, E> entry : sourceDictionary.entrySet()) {
        auxiliaryDictionary.put(entry.getKey(), entry.getValue());
    }
    // Now we can safely clear the target dictionary
    osMap.clear();
    // And now we move the data back in
    for (Map.Entry<String, E> entry : auxiliaryDictionary.entrySet()) {
        targetDictionary.put(entry.getKey(), entry.getValue());
    }
}
Also used : ObjectId(org.bson.types.ObjectId) Decimal128(org.bson.types.Decimal128) Date(java.util.Date) OsMap(io.realm.internal.OsMap) UUID(java.util.UUID) NativeRealmAny(io.realm.internal.core.NativeRealmAny) OsMap(io.realm.internal.OsMap) Map(java.util.Map)

Example 48 with Decimal128

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

the class JsonWriterTest method testDecimal128Strict.

@Test
@SuppressWarnings("deprecation")
public void testDecimal128Strict() {
    List<TestData<Decimal128>> tests = asList(new TestData<Decimal128>(Decimal128.parse("1.0"), "1.0"), new TestData<Decimal128>(Decimal128.POSITIVE_INFINITY, Decimal128.POSITIVE_INFINITY.toString()));
    for (final TestData<Decimal128> cur : tests) {
        stringWriter = new StringWriter();
        writer = new JsonWriter(stringWriter, JsonWriterSettings.builder().outputMode(JsonMode.STRICT).build());
        writer.writeStartDocument();
        writer.writeDecimal128("d", cur.value);
        writer.writeEndDocument();
        String expected = "{ \"d\" : { \"$numberDecimal\" : \"" + cur.expected + "\" } }";
        assertEquals(expected, stringWriter.toString());
    }
}
Also used : StringWriter(java.io.StringWriter) Decimal128(org.bson.types.Decimal128) Test(org.junit.Test)

Example 49 with Decimal128

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

the class JSONSerializersTest method testLegacySerialization.

@Test
public void testLegacySerialization() {
    ObjectSerializer serializer = JSONSerializers.getLegacy();
    BasicDBObject testObj = new BasicDBObject();
    // test  ARRAY
    BasicDBObject[] a = { new BasicDBObject("object1", "value1"), new BasicDBObject("object2", "value2") };
    testObj.put("array", a);
    StringBuilder buf = new StringBuilder();
    serializer.serialize(a, buf);
    assertEquals("[ { \"object1\" : \"value1\"} , { \"object2\" : \"value2\"}]", buf.toString());
    // test  BINARY
    byte[] b = { 1, 2, 3, 4 };
    testObj = new BasicDBObject("binary", new org.bson.types.Binary(b));
    buf = new StringBuilder();
    serializer.serialize(testObj, buf);
    assertEquals("{ \"binary\" : <Binary Data>}", buf.toString());
    // test  BOOLEAN
    testObj = new BasicDBObject("boolean", new Boolean(true));
    buf = new StringBuilder();
    serializer.serialize(testObj, buf);
    assertEquals(buf.toString(), "{ \"boolean\" : true}");
    // test  BSON_TIMESTAMP,
    testObj = new BasicDBObject("timestamp", new BSONTimestamp());
    buf = new StringBuilder();
    serializer.serialize(testObj, buf);
    assertEquals("{ \"timestamp\" : { \"$ts\" : 0 , \"$inc\" : 0}}", buf.toString());
    // test  BYTE_ARRAY
    testObj = new BasicDBObject("byte_array", b);
    buf = new StringBuilder();
    serializer.serialize(testObj, buf);
    assertEquals("{ \"byte_array\" : <Binary Data>}", buf.toString());
    // test  CODE
    testObj = new BasicDBObject("code", new Code("test code"));
    buf = new StringBuilder();
    serializer.serialize(testObj, buf);
    assertEquals("{ \"code\" : { \"$code\" : \"test code\"}}", buf.toString());
    // test  CODE_W_SCOPE
    testObj = new BasicDBObject("scope", "scope of code");
    CodeWScope codewscope = new CodeWScope("test code", testObj);
    buf = new StringBuilder();
    serializer.serialize(codewscope, buf);
    assertEquals("{ \"$code\" : \"test code\" , \"$scope\" : { \"scope\" : \"scope of code\"}}", buf.toString());
    // test  DATE
    Date d = new Date();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
    buf = new StringBuilder();
    serializer.serialize(d, buf);
    assertEquals("{ \"$date\" : \"" + format.format(d) + "\"}", buf.toString());
    // test  DB_OBJECT implicit in preceding tests
    // test  DB_REF_BASE
    DBRef dbref = new com.mongodb.DBRef("test.test", "4d83ab59a39562db9c1ae2af");
    buf = new StringBuilder();
    serializer.serialize(dbref, buf);
    assertEquals("{ \"$ref\" : \"test.test\" , \"$id\" : \"4d83ab59a39562db9c1ae2af\"}", buf.toString());
    DBRef dbrefWithDatabaseName = new com.mongodb.DBRef("mydb", "test.test", "4d83ab59a39562db9c1ae2af");
    buf = new StringBuilder();
    serializer.serialize(dbrefWithDatabaseName, buf);
    assertEquals("{ \"$ref\" : \"test.test\" , \"$id\" : \"4d83ab59a39562db9c1ae2af\" , \"$db\" : \"mydb\"}", buf.toString());
    // test  ITERABLE
    BasicBSONList testList = new BasicBSONList();
    testList.add(new BasicDBObject("key1", "val1"));
    testList.add(new BasicDBObject("key2", "val2"));
    buf = new StringBuilder();
    serializer.serialize(testList, buf);
    assertEquals("[ { \"key1\" : \"val1\"} , { \"key2\" : \"val2\"}]", buf.toString());
    // test  MAP
    Map<String, String> testMap = new TreeMap<String, String>();
    testMap.put("key1", "val1");
    testMap.put("key2", "val2");
    buf = new StringBuilder();
    serializer.serialize(testMap, buf);
    assertEquals("{ \"key1\" : \"val1\" , \"key2\" : \"val2\"}", buf.toString());
    // test  MAXKEY
    buf = new StringBuilder();
    serializer.serialize(new MaxKey(), buf);
    assertEquals("{ \"$maxKey\" : 1}", buf.toString());
    // test  MINKEY
    buf = new StringBuilder();
    serializer.serialize(new MinKey(), buf);
    assertEquals("{ \"$minKey\" : 1}", buf.toString());
    // test  NULL
    buf = new StringBuilder();
    serializer.serialize(null, buf);
    assertEquals(" null ", buf.toString());
    // test  NUMBER
    Random rand = new Random();
    long val = rand.nextLong();
    Long longVal = new Long(val);
    buf = new StringBuilder();
    serializer.serialize(longVal, buf);
    assertEquals(String.valueOf(val), buf.toString());
    // test  OBJECT_ID
    buf = new StringBuilder();
    serializer.serialize(new ObjectId("4d83ab3ea39562db9c1ae2ae"), buf);
    assertEquals("{ \"$oid\" : \"4d83ab3ea39562db9c1ae2ae\"}", buf.toString());
    // test  PATTERN
    buf = new StringBuilder();
    serializer.serialize(Pattern.compile("test"), buf);
    assertEquals("{ \"$regex\" : \"test\"}", buf.toString());
    // test  STRING
    buf = new StringBuilder();
    serializer.serialize("test string", buf);
    assertEquals("\"test string\"", buf.toString());
    // test  UUID;
    UUID uuid = UUID.randomUUID();
    buf = new StringBuilder();
    serializer.serialize(uuid, buf);
    assertEquals("{ \"$uuid\" : \"" + uuid.toString() + "\"}", buf.toString());
    // test Decimal128
    Decimal128 decimal128 = Decimal128.parse("3.140");
    buf = new StringBuilder();
    serializer.serialize(decimal128, buf);
    assertEquals("{ \"$numberDecimal\" : \"3.140\"}", buf.toString());
}
Also used : MaxKey(org.bson.types.MaxKey) BSONTimestamp(org.bson.types.BSONTimestamp) BasicDBObject(com.mongodb.BasicDBObject) MinKey(org.bson.types.MinKey) Random(java.util.Random) SimpleTimeZone(java.util.SimpleTimeZone) UUID(java.util.UUID) ObjectId(org.bson.types.ObjectId) BasicBSONList(org.bson.types.BasicBSONList) GregorianCalendar(java.util.GregorianCalendar) DBRef(com.mongodb.DBRef) Decimal128(org.bson.types.Decimal128) TreeMap(java.util.TreeMap) Code(org.bson.types.Code) Date(java.util.Date) CodeWScope(org.bson.types.CodeWScope) Binary(org.bson.types.Binary) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 50 with Decimal128

use of org.bson.types.Decimal128 in project pinpoint by naver.

the class MongoDBITBase method insertComlexBsonValueData34.

public void insertComlexBsonValueData34(PluginTestVerifier verifier, MongoCollection<Document> collection, Class<?> mongoDatabaseImpl, String collectionInfo, String collectionOption) {
    // insert Data
    Document document = createComplexDocument();
    document.append("decimal128", new BsonDecimal128(new Decimal128(55)));
    collection.insertOne(document);
    Method insertOneMethod = getMethod(mongoDatabaseImpl, "insertOne", Object.class);
    NormalizedBson parsedBson = parseBson(document);
    verifier.verifyTrace(event(MONGO_EXECUTE_QUERY, insertOneMethod, null, MongoDBITConstants.MONGODB_ADDRESS, null, new ExpectedAnnotation(MongoConstants.MONGO_COLLECTION_INFO.getName(), collectionInfo), new ExpectedAnnotation(MongoConstants.MONGO_COLLECTION_OPTION.getName(), collectionOption), new ExpectedAnnotation(MongoConstants.MONGO_JSON_DATA.getName(), new StringStringValue(parsedBson.getNormalizedBson(), parsedBson.getParameter()))));
}
Also used : ExpectedAnnotation(com.navercorp.pinpoint.bootstrap.plugin.test.ExpectedAnnotation) BsonDecimal128(org.bson.BsonDecimal128) StringStringValue(com.navercorp.pinpoint.common.util.StringStringValue) NormalizedBson(com.navercorp.pinpoint.plugin.mongo.NormalizedBson) BsonDecimal128(org.bson.BsonDecimal128) Decimal128(org.bson.types.Decimal128) Method(java.lang.reflect.Method) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument)

Aggregations

Decimal128 (org.bson.types.Decimal128)80 ObjectId (org.bson.types.ObjectId)50 Date (java.util.Date)46 Test (org.junit.Test)46 AllTypes (io.realm.entities.AllTypes)22 BigDecimal (java.math.BigDecimal)20 UUID (java.util.UUID)17 UiThreadTest (androidx.test.annotation.UiThreadTest)12 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)11 AllJavaTypes (io.realm.entities.AllJavaTypes)10 DictionaryAllTypes (io.realm.entities.DictionaryAllTypes)10 Dog (io.realm.entities.Dog)9 PrimaryKeyAsObjectId (io.realm.entities.PrimaryKeyAsObjectId)8 JSONObject (org.json.JSONObject)8 MappedAllJavaTypes (io.realm.entities.MappedAllJavaTypes)7 NonLatinFieldNames (io.realm.entities.NonLatinFieldNames)6 PrimaryKeyAsLong (io.realm.entities.PrimaryKeyAsLong)6 SimpleDateFormat (java.text.SimpleDateFormat)6 Calendar (java.util.Calendar)6 DefaultValueOfField (io.realm.entities.DefaultValueOfField)5