Java 类org.bson.BsonObjectId 实例源码

项目:BsonMapper    文件:TestUtil.java   
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));
}
项目:mongo-obj-framework    文件:ObjectParser.java   
private BsonValue fromGridRef(SmofGridRef fileRef, PrimaryField fieldOpts) {
    if(fileRef.isEmpty()) {
        return new BsonNull();
    }
    if(fileRef.getId() == null) {
        final SmofObject annotation = fieldOpts.getSmofAnnotationAs(SmofObject.class);
        if(fileRef.getBucketName() == null) {
            fileRef.setBucketName(annotation.bucketName());
        }
        //TODO test if upload file adds id to fileRef
        if(!annotation.preInsert() && !fieldOpts.isForcePreInsert()) {
            return new BsonLazyObjectId(fieldOpts.getName(), fileRef);
        }
        fieldOpts.setForcePreInsert(false);
        dispatcher.insert(fileRef);
    }
    final BsonDocument bsonRef = new BsonDocument("id", new BsonObjectId(fileRef.getId()))
            .append("bucket", new BsonString(fileRef.getBucketName()));
    return bsonRef;
}
项目:epcis    文件:ObjectEventWriteConverter.java   
public void capture(BsonObjectId dataID, Long eventTime, Set<String> epcList, BsonArray epcQuantities,
        String readPoint, String bizLocation, BsonArray sourceList, BsonArray destinationList) {

    ChronoGraph pg = Configuration.persistentGraph;

    if (epcList != null && !epcList.isEmpty()) {
        epcList.stream().forEach(object -> {
            MongoWriterUtil.addBasicTimestampProperties(pg, eventTime, object, readPoint, bizLocation, sourceList,
                    destinationList);
            pg.addTimestampVertexProperty(object, eventTime, "data", dataID);
        });
    }

    if (epcQuantities != null && !epcQuantities.isEmpty()) {
        epcQuantities.stream().forEach(classElem -> {
            MongoWriterUtil.addBasicTimestampProperties(pg, eventTime, classElem, readPoint, bizLocation,
                    sourceList, destinationList);
            pg.addTimestampVertexProperty(classElem.asDocument().getString("epcClass").getValue(), eventTime,
                    "data", dataID);
        });
    }
    return;
}
项目:epcis    文件:TransactionEventWriteConverter.java   
public void capture(BsonObjectId dataID, Long eventTime, Set<String> epcList, BsonArray epcQuantities,
        String readPoint, String bizLocation, BsonArray sourceList, BsonArray destinationList) {

    ChronoGraph pg = Configuration.persistentGraph;

    if (epcList != null && !epcList.isEmpty()) {
        epcList.stream().forEach(object -> {
            MongoWriterUtil.addBasicTimestampProperties(pg, eventTime, object, readPoint, bizLocation, sourceList,
                    destinationList);
            pg.addTimestampVertexProperty(object, eventTime, "data", dataID);
        });
    }

    if (epcQuantities != null && !epcQuantities.isEmpty()) {
        epcQuantities.stream().forEach(classElem -> {
            MongoWriterUtil.addBasicTimestampProperties(pg, eventTime, classElem, readPoint, bizLocation,
                    sourceList, destinationList);
            pg.addTimestampVertexProperty(classElem.asDocument().getString("epcClass").getValue(), eventTime,
                    "data", dataID);
        });
    }
}
项目:restheart    文件:ValidOidsStringsAsOidsTransformer.java   
private void _transform(BsonDocument data, Set<String> propertiesNames) {
    data.keySet().stream().forEach(key -> {
        BsonValue value = data.get(key);

        if (shouldTransform(key, propertiesNames)) {
            if (value.isString()
                    && ObjectId.isValid(value
                            .asString()
                            .getValue())) {
                data.put(key,
                        new BsonObjectId(
                                new ObjectId(value
                                        .asString()
                                        .getValue())));
            }
        }

        if (value instanceof BsonDocument) {
            _transform(value.asDocument(), propertiesNames);
        }
    });
}
项目:cherimodata    文件:EntityCodec.java   
@Override
public BsonValue getDocumentId( T document )
{
    Object value = _getId( document );
    if ( value != null )
    {
        if ( ObjectId.class.isInstance( value ) )
        {
            return new BsonObjectId( (ObjectId) value );
        }
        else if ( String.class.isInstance( value ) )
        {
            return new BsonString( (String) value );
        }
    }
    return null;
}
项目:BsonMapper    文件:TDocument.java   
@Override
public ObjectId getObjectId(Object key) {
    BsonObjectId objectId = innerBsonDocument.getObjectId(key);
    if (objectId != null) {
        return (ObjectId) BsonValueConverterRepertory.getValueConverterByBsonType(objectId.getBsonType()).decode(objectId);
    } else {
        return null;
    }
}
项目:mongo-obj-framework    文件:ObjectParser.java   
@Override
public BsonValue toBson(Object value, SmofField fieldOpts) {
    if(contextContains(value, fieldOpts, serializationContext)) {
        return serializationContext.get(value, fieldOpts.getType());
    }
    final Class<?> type = value.getClass();
    final BsonDocument serValue;

    if(value instanceof ObjectId) {
        return new BsonObjectId((ObjectId) value);
    }
    else if(isMaster(fieldOpts)) {
        return fromMasterField((Element) value, fieldOpts, serializationContext);
    }
    else if(isSmofGridRef(type)) {
        return fromGridRef((SmofGridRef) value, (PrimaryField) fieldOpts);
    }
    else if(isElement(type)) {
        if(fieldOpts instanceof PrimaryField) {
            return fromElement((Element) value, (PrimaryField) fieldOpts, serializationContext);                
        }
        return fromElement((Element) value, serializationContext);
    }
    else if(isMap(type) && isPrimaryField(fieldOpts)) {
        return fromMap((Map<?, ?>) value, (PrimaryField) fieldOpts, serializationContext);
    }
    else if(isEnum(type)) {
        return fromEnum((Enum<?>) value, serializationContext);
    }
    serValue = fromObject(value);
    serializationContext.put(value, SmofType.OBJECT, serValue);
    return serValue;
}
项目:mongo-obj-framework    文件:ObjectParser.java   
private BsonValue fromElement(Element value, SerializationContext serContext) {
    final SmofOpOptions options = new SmofOpOptionsImpl();
    options.bypassCache(true);
    dispatcher.insert(value, options);
    final BsonObjectId id = BsonUtils.toBsonObjectId(value);
    serContext.put(value, SmofType.OBJECT, id);
    return id;
}
项目:drill    文件:TestBsonRecordReader.java   
@Test
public void testObjectIdType() throws IOException {
  BsonDocument bsonDoc = new BsonDocument();
  BsonObjectId value = new BsonObjectId(new ObjectId());
  bsonDoc.append("_idKey", value);
  writer.reset();
  bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
  SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader();
  byte[] readByteArray = mapReader.reader("_idKey").readByteArray();
  assertTrue(Arrays.equals(value.getValue().toByteArray(), readByteArray));
}
项目:core-ng-project    文件:EntityCodec.java   
@Override
public BsonValue getDocumentId(T document) {
    Object id = idHandler.get(document);
    if (id instanceof ObjectId) return new BsonObjectId((ObjectId) id);
    if (id instanceof String) new BsonString((String) id);
    throw Exceptions.error("unsupported id type, id={}", id);
}
项目:restheart    文件:RequestHelper.java   
/**
 *
 * @param exchange
 * @param etag
 * @return
 */
public static boolean checkReadEtag(HttpServerExchange exchange, BsonObjectId etag) {
    if (etag == null) {
        return false;
    }

    HeaderValues vs = exchange.getRequestHeaders().get(Headers.IF_NONE_MATCH);

    return vs == null || vs.getFirst() == null
            ? false
            : vs.getFirst().equals(etag.getValue().toString());
}
项目:restheart    文件:URLUtils.java   
private static BsonObjectId getIdAsObjectId(String id)
        throws IllegalArgumentException {
    if (!ObjectId.isValid(id)) {
        throw new IllegalArgumentException("The id is not a valid ObjectId " + id);
    }

    return new BsonObjectId(new ObjectId(id));
}
项目:restheart    文件:DocumentDAO.java   
/**
 * @param dbName
 * @param collName
 * @param documents
 * @param shardKeys
 * @return
 */
@Override
@SuppressWarnings("unchecked")
public BulkOperationResult bulkUpsertDocumentsPost(
        final String dbName,
        final String collName,
        final BsonArray documents,
        final BsonDocument filter,
        final BsonDocument shardKeys) {
    Objects.requireNonNull(documents);

    MongoDatabase mdb = client.getDatabase(dbName);
    MongoCollection<BsonDocument> mcoll
            = mdb.getCollection(collName, BsonDocument.class);

    BsonObjectId newEtag = new BsonObjectId(new ObjectId());

    documents
            .stream()
            .filter(d -> d != null && d.isDocument())
            .forEachOrdered(document -> {
                document.
                        asDocument()
                        .put("_etag", newEtag);
            });

    return DAOUtils.bulkUpsertDocuments(
            mcoll,
            documents,
            filter,
            shardKeys);
}
项目:restheart    文件:GetFileBinaryHandler.java   
private boolean checkEtag(HttpServerExchange exchange, GridFSFile dbsfile) {
    if (dbsfile != null) {
        Object etag;

        if (dbsfile.getMetadata() != null
                && dbsfile.getMetadata().containsKey("_etag")) {
            etag = dbsfile.getMetadata().get("_etag");
        } else {
            etag = null;
        }

        if (etag != null && etag instanceof ObjectId) {
            ObjectId _etag = (ObjectId) etag;

            BsonObjectId __etag = new BsonObjectId(_etag);

            // in case the request contains the IF_NONE_MATCH header with the current etag value,
            // just return 304 NOT_MODIFIED code
            if (RequestHelper.checkReadEtag(exchange, __etag)) {
                exchange.setStatusCode(HttpStatus.SC_NOT_MODIFIED);
                exchange.endExchange();
                return true;
            }
        }
    }

    return false;
}
项目:restheart    文件:URLUtilsTest.java   
@Test
public void testGetUriWithFilterOneObjectId() {
    BsonObjectId id = new BsonObjectId(new ObjectId());
    RequestContext context = prepareRequestContext();
    String expResult = "/dbName/collName?filter={'referenceField':{'$oid':'" + id.getValue().toString() + "'}}";
    String result;
    try {
        result = URLUtils.getUriWithFilterOne(context, "dbName", "collName", "referenceField", id);
        assertEquals(expResult, result);
    } catch (UnsupportedDocumentIdException ex) {
        fail(ex.getMessage());
    }
}
项目:restheart    文件:URLUtilsTest.java   
@Test
public void testGetUriWithFilterManyInverseObjectId() {
    RequestContext context = prepareRequestContext();
    BsonObjectId id = new BsonObjectId(new ObjectId());
    String expResult = "/dbName/collName?filter={'referenceField':{'$elemMatch':{'$eq':{'$oid':'" + id.getValue().toString() + "'}}}}";
    String result;
    try {
        result = URLUtils.getUriWithFilterManyInverse(context, "dbName", "collName", "referenceField", id);
        assertEquals(expResult, result);
    } catch (UnsupportedDocumentIdException ex) {
        fail(ex.getMessage());
    }
}
项目:octarine    文件:DeserialisationTest.java   
@Test public void
deserialise_object_id() {
    ObjectId value = new ObjectId(new Date(), random.nextInt(0xffffff));
    BsonDocument doc = new BsonDocument();
    doc.put("my-value", new BsonObjectId(value));
    Key<ObjectId> idKey = Key.named("my-value");

    Record record = BsonRecordDeserialiser.builder()
            .readObjectId(idKey)
            .get()
            .apply(doc);
    assertEquals("wrong ObjectId value", value, idKey.get(record).get());
}
项目:mongodb-rdbms-sync    文件:OrclToMngEventCodec.java   
@Override
public BsonValue getDocumentId(OracleToMongoEvent event) {
    return new BsonObjectId(event.getEventId());
}
项目:mongodb-rdbms-sync    文件:MngToOrclMapCodec.java   
@Override
public BsonValue getDocumentId(MongoToOracleMap map) {
    return new BsonObjectId(map.getMapId());
}
项目:mongodb-rdbms-sync    文件:OrclToMongoGridFsMapCodec.java   
@Override
public BsonValue getDocumentId(OracleToMongoGridFsMap map) {
    return new BsonObjectId(map.getMapId());
}
项目:mongodb-rdbms-sync    文件:OrclToMngMapCodec.java   
@Override
public BsonValue getDocumentId(OracleToMongoMap map) {
    return new BsonObjectId(map.getMapId());
}
项目:mongodb-rdbms-sync    文件:OrclToMngGridFsEventCodec.java   
@Override
public BsonValue getDocumentId(OracleToMongoGridFsEvent event) {
    return new BsonObjectId(event.getEventId());
}
项目:mongodb-rdbms-sync    文件:SyncEventCodec.java   
@Override
public BsonValue getDocumentId(SyncEvent event) {
    return new BsonObjectId(event.getEventId());
}
项目:mongodb-rdbms-sync    文件:SyncNodeCodec.java   
@Override
public BsonValue getDocumentId(SyncNode nodeMapper) {
    return new BsonObjectId(nodeMapper.getId());
}
项目:mongodb-rdbms-sync    文件:SyncMapCodec.java   
@Override
public BsonValue getDocumentId(SyncMap map) {
    return new BsonObjectId(map.getMapId());
}
项目:mongodb-rdbms-sync    文件:O2MSyncEventLogCodec.java   
@Override
public BsonValue getDocumentId(O2MSyncEventLog arg0) {
    return new BsonObjectId(arg0.getLogId());
}
项目:mongodb-rdbms-sync    文件:OrclToMngSyncEventCodec.java   
@Override
public BsonValue getDocumentId(OracleToMongoSyncEvent event) {
    return new BsonObjectId(event.getEventId());
}
项目:mongodb-rdbms-sync    文件:MngToOrclSyncEventCodec.java   
@Override
public BsonValue getDocumentId(MongoToOracleSyncEvent event) {
    return new BsonObjectId(event.getEventId());
}
项目:mongodb-rdbms-sync    文件:SyncEventGridFsCodec.java   
@Override
public BsonValue getDocumentId(OracleToMongoGridFsEvent event) {
    return new BsonObjectId(event.getEventId());
}
项目:mongodb-rdbms-sync    文件:MngToOrclEventCodec.java   
@Override
public BsonValue getDocumentId(MongoToOracleEvent event) {
    return new BsonObjectId(event.getEventId());
}
项目:mongodb-rdbms-sync    文件:SyncMapGridFsCodec.java   
@Override
public BsonValue getDocumentId(OracleToMongoGridFsMap map) {
    return new BsonObjectId(map.getMapId());
}
项目:polymorphia    文件:ObjectIdGenerator.java   
@Override
public BsonValue asBsonValue(ObjectId id, TypeCodecRegistry typeCodecRegistry) {
    return new BsonObjectId(id);
}
项目:BsonMapper    文件:BsonStringObjectIdConverter.java   
@Override
public BsonObjectId encode(StringObjectId object) {
    return new BsonObjectId(object.getInnerObjectId());
}
项目:BsonMapper    文件:BsonObjectIdConverter.java   
@Override
public BsonObjectId encode(ObjectId object) {
    return new BsonObjectId(object);
}
项目:BsonMapper    文件:BsonDocumentConverterTest.java   
@Test
public void decode() throws Exception {
    BsonDocument bsonObj = new BsonDocument().append("testDouble", new BsonDouble(20.777));
    List<BsonDocument> list = new ArrayList<BsonDocument>();
    list.add(bsonObj);
    list.add(bsonObj);
    List<BsonArray> arrayList = new ArrayList<BsonArray>();
    arrayList.add(new BsonArray(list));
    arrayList.add(new BsonArray(list));
    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));
    BsonDocument bsonDocument1 = 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(new Date().getTime()))
            .append("testNull", new BsonNull())
            .append("testInt", new BsonInt32(233))
            .append("testLong", new BsonInt64(233332));
    BsonTest bsonTest = BsonDocumentConverter.getInstance().decode(bsonDocument1, BsonTest.class, BsonMapperConfig.DEFALUT);
    System.out.println(bsonTest.getTestDouble());
    System.out.println(bsonTest.getTestString());
    System.out.println(bsonTest.getTestArray());
    System.out.println(Arrays.toString(bsonTest.getTestBinary().getData()));
    System.out.println(bsonTest.getTestObjectId());
    System.out.println(bsonTest.getTestStringObjectId());
    System.out.println(bsonTest.isTestBoolean());
    System.out.println(bsonTest.getTestDate());
    System.out.println(bsonTest.getTestNull());
    System.out.println(bsonTest.getTestInt());
    System.out.println(bsonTest.getTestLong());
    System.out.println(bsonTest);
}
项目:mongo-obj-framework    文件:BsonUtils.java   
public static BsonObjectId toBsonObjectId(Element value) {
    final ObjectId id = value.getId();
    return new BsonObjectId(id);
}
项目:mongo-obj-framework    文件:ObjectIdParser.java   
@Override
public BsonValue serializeToBson(Object value, SmofField fieldOpts) {
    return new BsonObjectId((ObjectId) value);
}
项目:mongo-obj-framework    文件:SmofUpdateQuery.java   
public void idEq(ObjectId id) {
    idEq(new BsonObjectId(id));
}
项目:mongo-obj-framework    文件:SmofUpdateQuery.java   
public void idEq(BsonObjectId id) {
    filter = new BsonDocument(Element.ID, id);
    execute();
}