public static BsonDocument getBsonDocument() { BsonDocument bsonObj = new BsonDocument().append("testDouble", new BsonDouble(20.777)); List<BsonDocument> list = new ArrayList<BsonDocument>(); list.add(bsonObj); list.add(bsonObj); byte[] bytes = new byte[3]; bytes[0] = 3; bytes[1] = 2; bytes[2] = 1; BsonDocument bsonDocument = new BsonDocument().append("testDouble", new BsonDouble(20.99)) .append("testString", new BsonString("testStringV")) .append("testArray", new BsonArray(list)); return new BsonDocument().append("testDouble", new BsonDouble(20.99)) .append("testString", new BsonString("testStringV")) .append("testArray", new BsonArray(list)) .append("bson_test", bsonDocument) .append("testBinary", new BsonBinary(bytes)) .append("testBsonUndefined", new BsonUndefined()) .append("testObjectId", new BsonObjectId()) .append("testStringObjectId", new BsonObjectId()) .append("testBoolean", new BsonBoolean(true)) .append("testDate", new BsonDateTime(time)) .append("testNull", new BsonNull()) .append("testInt", new BsonInt32(233)) .append("testLong", new BsonInt64(233332)); }
public BsonDocument putQuantityList(BsonDocument base, List<QuantityElement> quantityList) { BsonArray quantityArray = new BsonArray(); for (QuantityElement quantityElement : quantityList) { BsonDocument bsonQuantityElement = new BsonDocument("epcClass", new BsonString(quantityElement.getEpcClass())); if (quantityElement.getQuantity() != null) { bsonQuantityElement.put("quantity", new BsonDouble(quantityElement.getQuantity())); } if (quantityElement.getUom() != null) { bsonQuantityElement.put("uom", new BsonString(quantityElement.getUom())); } quantityArray.add(bsonQuantityElement); } base.put("quantityList", quantityArray); return base; }
public BsonDocument putChildQuantityList(BsonDocument base, List<QuantityElement> childQuantityList) { BsonArray quantityArray = new BsonArray(); for (QuantityElement quantityElement : childQuantityList) { BsonDocument bsonQuantityElement = new BsonDocument("epcClass", new BsonString(quantityElement.getEpcClass())); if (quantityElement.getQuantity() != null) { bsonQuantityElement.put("quantity", new BsonDouble(quantityElement.getQuantity())); } if (quantityElement.getUom() != null) { bsonQuantityElement.put("uom", new BsonString(quantityElement.getUom())); } quantityArray.add(bsonQuantityElement); } base.put("childQuantityList", quantityArray); return base; }
public BsonDocument putInputQuantityList(BsonDocument base, List<QuantityElement> inputQuantityList) { BsonArray quantityArray = new BsonArray(); for (QuantityElement quantityElement : inputQuantityList) { BsonDocument bsonQuantityElement = new BsonDocument("epcClass", new BsonString(quantityElement.getEpcClass())); if (quantityElement.getQuantity() != null) { bsonQuantityElement.put("quantity", new BsonDouble(quantityElement.getQuantity())); } if (quantityElement.getUom() != null) { bsonQuantityElement.put("uom", new BsonString(quantityElement.getUom())); } quantityArray.add(bsonQuantityElement); } base.put("inputQuantityList", quantityArray); return base; }
public BsonDocument putOutputQuantityList(BsonDocument base, List<QuantityElement> outputQuantityList) { BsonArray quantityArray = new BsonArray(); for (QuantityElement quantityElement : outputQuantityList) { BsonDocument bsonQuantityElement = new BsonDocument("epcClass", new BsonString(quantityElement.getEpcClass())); if (quantityElement.getQuantity() != null) { bsonQuantityElement.put("quantity", new BsonDouble(quantityElement.getQuantity())); } if (quantityElement.getUom() != null) { bsonQuantityElement.put("uom", new BsonString(quantityElement.getUom())); } quantityArray.add(bsonQuantityElement); } base.put("outputQuantityList", quantityArray); return base; }
static BsonArray getQuantityObjectList(List<QuantityElementType> qetList, Integer gcpLength) { BsonArray quantityList = new BsonArray(); for (int i = 0; i < qetList.size(); i++) { BsonDocument quantity = new BsonDocument(); QuantityElementType qet = qetList.get(i); if (qet.getEpcClass() != null) quantity.put("epcClass", new BsonString(getClassEPC(qet.getEpcClass().toString(), gcpLength))); if (qet.getQuantity().doubleValue() != 0) { quantity.put("quantity", new BsonDouble(qet.getQuantity().doubleValue())); } if (qet.getUom() != null) quantity.put("uom", new BsonString(qet.getUom().toString())); quantityList.add(quantity); } return quantityList; }
public static BsonDocument getBsonGeoPoint(String pointString) { try { BsonDocument pointDoc = new BsonDocument(); pointDoc.put("type", new BsonString("Point")); String[] pointArr = pointString.split(","); if (pointArr.length != 2) return null; BsonArray arr = new BsonArray(); arr.add(new BsonDouble(Double.parseDouble(pointArr[0]))); arr.add(new BsonDouble(Double.parseDouble(pointArr[1]))); pointDoc.put("coordinates", arr); return pointDoc; } catch (NumberFormatException e) { e.printStackTrace(); return null; } }
private static BsonValue converseType(String value) { String[] valArr = value.split("\\^"); if (valArr.length != 2) { return new BsonString(value); } try { String type = valArr[1]; if (type.equals("int")) { return new BsonInt32(Integer.parseInt(valArr[0])); } else if (type.equals("long")) { return new BsonInt64(Long.parseLong(valArr[0])); } else if (type.equals("double")) { return new BsonDouble(Double.parseDouble(valArr[0])); } else if (type.equals("boolean")) { return new BsonBoolean(Boolean.parseBoolean(valArr[0])); } else { return new BsonString(value); } } catch (NumberFormatException e) { return new BsonString(value); } }
static BsonValue converseType(String value) { String[] valArr = value.split("\\^"); if (valArr.length != 2) { return new BsonString(value); } try { String type = valArr[1]; if (type.equals("int")) { return new BsonInt32(Integer.parseInt(valArr[0])); } else if (type.equals("long")) { return new BsonInt64(Long.parseLong(valArr[0])); } else if (type.equals("double")) { return new BsonDouble(Double.parseDouble(valArr[0])); } else if (type.equals("boolean")) { return new BsonBoolean(Boolean.parseBoolean(valArr[0])); } else { return new BsonString(value); } } catch (NumberFormatException e) { return new BsonString(value); } }
static BsonArray getQuantityObjectList(List<QuantityElementType> qetList, Integer gcpLength) { BsonArray quantityList = new BsonArray(); for (int i = 0; i < qetList.size(); i++) { BsonDocument quantity = new BsonDocument(); QuantityElementType qet = qetList.get(i); if (qet.getEpcClass() != null) quantity.put("epcClass", new BsonString(getClassEPC(qet.getEpcClass().toString(), gcpLength))); if (qet.getQuantity() != 0) { quantity.put("quantity", new BsonDouble(qet.getQuantity())); } if (qet.getUom() != null) quantity.put("uom", new BsonString(qet.getUom().toString())); quantityList.add(quantity); } return quantityList; }
/** * Geospatial query * * @param key * should be indexed by 2dsphere * db.vertices.createIndex({"urn:oliot:ubv:mda:gps" : "2dsphere"}) * @param lon * @param lat * @param radius * in metres db.vertices.find({ "urn:oliot:ubv:mda:gps" : { $near : { * $geometry: { type: "Point", coordinates: [ -1.1673,52.93]}, * $maxDistance: 50000}}}) * * @return */ public HashSet<ChronoVertex> getChronoVertexSet(String key, double lon, double lat, double radius) { HashSet<ChronoVertex> ret = new HashSet<ChronoVertex>(); BsonArray coordinates = new BsonArray(); coordinates.add(new BsonDouble(lon)); coordinates.add(new BsonDouble(lat)); BsonDocument geometry = new BsonDocument(); geometry.put("type", new BsonString("Point")); geometry.put("coordinates", coordinates); BsonDocument near = new BsonDocument(); near.put("$geometry", geometry); near.put("$maxDistance", new BsonDouble(radius)); BsonDocument geoquery = new BsonDocument(); geoquery.put("$near", near); BsonDocument queryDoc = new BsonDocument(); queryDoc.put(key, geoquery); MongoCursor<BsonDocument> cursor = vertices.find(queryDoc).projection(Tokens.PRJ_ONLY_ID).iterator(); while (cursor.hasNext()) { BsonDocument v = cursor.next(); ret.add(new ChronoVertex(v.getString(Tokens.ID).getValue(), this)); } return ret; }
/** * Geospatial query * * @param key * should be indexed by 2dsphere * db.vertices.createIndex({"urn:oliot:ubv:mda:gps" : "2dsphere"}) * @param lon * @param lat * @param radius * in metres db.vertices.find({ "urn:oliot:ubv:mda:gps" : { $near : { * $geometry: { type: "Point", coordinates: [ -1.1673,52.93]}, * $maxDistance: 50000}}}) * * @return */ public Stream<ChronoVertex> getChronoVertexStream(String key, double lon, double lat, double radius) { HashSet<ChronoVertex> ret = new HashSet<ChronoVertex>(); BsonArray coordinates = new BsonArray(); coordinates.add(new BsonDouble(lon)); coordinates.add(new BsonDouble(lat)); BsonDocument geometry = new BsonDocument(); geometry.put("type", new BsonString("Point")); geometry.put("coordinates", coordinates); BsonDocument near = new BsonDocument(); near.put("$geometry", geometry); near.put("$maxDistance", new BsonDouble(radius)); BsonDocument geoquery = new BsonDocument(); geoquery.put("$near", near); BsonDocument queryDoc = new BsonDocument(); queryDoc.put(key, geoquery); MongoCursor<BsonDocument> cursor = vertices.find(queryDoc).projection(Tokens.PRJ_ONLY_ID).iterator(); while (cursor.hasNext()) { BsonDocument v = cursor.next(); ret.add(new ChronoVertex(v.getString(Tokens.ID).getValue(), this)); } return ret.parallelStream(); }
private BsonDocument generateHistogram(Histogram histogram) { BsonDocument document = new BsonDocument(); final Snapshot snapshot = histogram.getSnapshot(); document.append("count", new BsonInt64(histogram.getCount())); document.append("max", new BsonDouble(snapshot.getMax())); document.append("mean", new BsonDouble(snapshot.getMean())); document.append("min", new BsonDouble(snapshot.getMin())); document.append("p50", new BsonDouble(snapshot.getMedian())); document.append("p75", new BsonDouble(snapshot.get75thPercentile())); document.append("p95", new BsonDouble(snapshot.get95thPercentile())); document.append("p98", new BsonDouble(snapshot.get98thPercentile())); document.append("p99", new BsonDouble(snapshot.get99thPercentile())); document.append("p999", new BsonDouble(snapshot.get999thPercentile())); if (showSamples) { document.append("values", new BsonArray( Arrays.stream(snapshot.getValues()) .mapToObj(BsonInt64::new) .collect(Collectors.toList()) )); } document.append("stddev", new BsonDouble(snapshot.getStdDev())); return document; }
@Test public void testGetUriWithFilterMany() { BsonValue[] ids = new BsonValue[]{ new BsonInt32(1), new BsonDouble(20.0d), new BsonString("id")}; RequestContext context = prepareRequestContext(); String expResult = "/dbName/collName?filter={'_id':{'$in':[1,20.0,\'id\']}}"; String result; try { result = URLUtils.getUriWithFilterMany(context, "dbName", "collName", ids); assertEquals(expResult, result); } catch (UnsupportedDocumentIdException ex) { fail(ex.getMessage()); } }
@Test public void testGetUriWithFilterManyString() { BsonValue[] ids = new BsonValue[]{ new BsonInt32(1), new BsonDouble(20.0d), new BsonString("id")}; RequestContext context = prepareRequestContext(); String expResult = "/dbName/collName?filter={'_id':{'$in':[1,20.0,'id']}}"; String result; try { result = URLUtils.getUriWithFilterMany(context, "dbName", "collName", ids); assertEquals(expResult, result); } catch (UnsupportedDocumentIdException ex) { fail(ex.getMessage()); } }
/** * Reading from BSON to GSON */ @Test public void bsonToGson() throws Exception { BsonDocument document = new BsonDocument(); document.append("boolean", new BsonBoolean(true)); document.append("int32", new BsonInt32(32)); document.append("int64", new BsonInt64(64)); document.append("double", new BsonDouble(42.42D)); document.append("string", new BsonString("foo")); document.append("null", new BsonNull()); document.append("array", new BsonArray()); document.append("object", new BsonDocument()); JsonElement element = TypeAdapters.JSON_ELEMENT.read(new BsonReader(new BsonDocumentReader(document))); check(element.isJsonObject()); check(element.getAsJsonObject().get("boolean").getAsJsonPrimitive().isBoolean()); check(element.getAsJsonObject().get("boolean").getAsJsonPrimitive().getAsBoolean()); check(element.getAsJsonObject().get("int32").getAsJsonPrimitive().isNumber()); check(element.getAsJsonObject().get("int32").getAsJsonPrimitive().getAsNumber().intValue()).is(32); check(element.getAsJsonObject().get("int64").getAsJsonPrimitive().isNumber()); check(element.getAsJsonObject().get("int64").getAsJsonPrimitive().getAsNumber().longValue()).is(64L); check(element.getAsJsonObject().get("double").getAsJsonPrimitive().isNumber()); check(element.getAsJsonObject().get("double").getAsJsonPrimitive().getAsNumber().doubleValue()).is(42.42D); check(element.getAsJsonObject().get("string").getAsJsonPrimitive().isString()); check(element.getAsJsonObject().get("string").getAsJsonPrimitive().getAsString()).is("foo"); check(element.getAsJsonObject().get("null").isJsonNull()); check(element.getAsJsonObject().get("array").isJsonArray()); check(element.getAsJsonObject().get("object").isJsonObject()); }
@Override public Double getDouble(Object key) { BsonDouble aDouble = innerBsonDocument.getDouble(key); if (aDouble != null) { return (Double) BsonValueConverterRepertory.getValueConverterByBsonType(aDouble.getBsonType()).decode(aDouble); } else { return null; } }
private BsonDocument getBsonDocumentHasDeepLayer() { BsonDocument bsonObj = new BsonDocument().append("testDouble", new BsonDouble(20.777)); List<BsonDocument> list = new ArrayList<BsonDocument>(); list.add(bsonObj); list.add(bsonObj); BsonDocument deepBsonDocument = new BsonDocument().append("testDouble", new BsonDouble(20.99)) .append("testString", new BsonString("testStringV")) .append("testArray", new BsonArray(list)); BsonDocument bsonDocument1 = new BsonDocument().append("testDouble", new BsonDouble(20.99)) .append("testString", new BsonString("testStringV")) .append("bson_test", deepBsonDocument); BsonDocument bsonDocument2 = new BsonDocument().append("testDouble", new BsonDouble(20.99)) .append("testString", new BsonString("testStringV")) .append("bson_test", bsonDocument1); BsonDocument bsonDocument3 = new BsonDocument().append("testDouble", new BsonDouble(20.99)) .append("testString", new BsonString("testStringV")) .append("bson_test", bsonDocument2) .append("testArray", new BsonArray(list)); BsonDocument bsonDocument4 = new BsonDocument().append("testDouble", new BsonDouble(20.99)) .append("testString", new BsonString("testStringV")) .append("bson_test", bsonDocument3) .append("testArray", new BsonArray(list)); BsonDocument bsonDocument5 = new BsonDocument().append("testDouble", new BsonDouble(20.99)) .append("testString", new BsonString("testStringV")) .append("bson_test", bsonDocument4) .append("testArray", new BsonArray(list)); return new BsonDocument().append("testDouble", new BsonDouble(20.99)) .append("bson_test", bsonDocument5) .append("testLong", new BsonInt64(233332)); }
@Test public void testDoubleType() throws IOException { BsonDocument bsonDoc = new BsonDocument(); bsonDoc.append("doubleKey", new BsonDouble(12.35)); writer.reset(); bsonReader.write(writer, new BsonDocumentReader(bsonDoc)); SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader(); assertEquals(12.35d, mapReader.reader("doubleKey").readDouble().doubleValue(), 0.00001); }
public static BsonValue converseType(String value) { String[] valArr = value.split("\\^"); if (valArr.length != 2) { return new BsonString(value); } try { String type = valArr[1]; if (type.equals("int")) { return new BsonInt32(Integer.parseInt(valArr[0])); } else if (type.equals("long")) { return new BsonInt64(Long.parseLong(valArr[0])); } else if (type.equals("double")) { return new BsonDouble(Double.parseDouble(valArr[0])); } else if (type.equals("boolean")) { return new BsonBoolean(Boolean.parseBoolean(valArr[0])); } else if (type.equals("float")) { return new BsonDouble(Double.parseDouble(valArr[0])); } else if (type.equals("dateTime")) { BsonDateTime time = getBsonDateTime(valArr[0]); if (time != null) return time; return new BsonString(value); } else if (type.equals("geoPoint")) { BsonDocument point = getBsonGeoPoint(valArr[0]); if (point == null) return new BsonString(value); return point; } else if (type.equals("geoArea")) { BsonDocument area = getBsonGeoArea(valArr[0]); if (area == null) return new BsonString(value); return area; } else { return new BsonString(value); } } catch (NumberFormatException e) { return new BsonString(value); } }
static BsonDocument getNearQueryObject(String key, String paramValues) { try { // Prepare Query Values String[] paramValueArr = paramValues.split(","); if (paramValueArr.length < 2) return null; Double lon = Double.parseDouble(paramValueArr[0]); Double lat = Double.parseDouble(paramValueArr[1]); Integer min = null; if (paramValueArr.length > 2) min = Integer.parseInt(paramValueArr[2]); Integer max = null; if (paramValueArr.length > 3) max = Integer.parseInt(paramValueArr[3]); BsonDocument near = new BsonDocument(); BsonDocument geometry = new BsonDocument("type", new BsonString("Point")); BsonArray coordinates = new BsonArray(); coordinates.add(new BsonDouble(lon)); coordinates.add(new BsonDouble(lat)); geometry.put("coordinates", coordinates); near.put("$geometry", geometry); if (min != null) near.put("$minDistance", new BsonInt32(min)); if (max != null) near.put("$maxDistance", new BsonInt32(max)); return new BsonDocument("any." + key, new BsonDocument("$near", near)); } catch (NumberFormatException e) { e.printStackTrace(); return null; } }
static BsonValue converseType(String value) { String[] valArr = value.split("\\^"); if (valArr.length != 2) { return new BsonString(value); } try { String type = valArr[1].trim(); if (type.equals("int")) { return new BsonInt32(Integer.parseInt(valArr[0])); } else if (type.equals("long")) { return new BsonInt64(Long.parseLong(valArr[0])); } else if (type.equals("double")) { return new BsonDouble(Double.parseDouble(valArr[0])); } else if (type.equals("boolean")) { return new BsonBoolean(Boolean.parseBoolean(valArr[0])); } else if (type.equals("regex")) { return new BsonRegularExpression("^" + valArr[0] + "$"); } else if (type.equals("float")) { return new BsonDouble(Double.parseDouble(valArr[0])); } else if (type.equals("dateTime")) { BsonDateTime time = MongoQueryService.getTimeMillis(valArr[0]); if (time != null) return time; return new BsonString(value); } else { return new BsonString(value); } } catch (NumberFormatException e) { return new BsonString(value); } }