本文整理汇总了Java中org.bson.BSON类的典型用法代码示例。如果您正苦于以下问题:Java BSON类的具体用法?Java BSON怎么用?Java BSON使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BSON类属于org.bson包,在下文中一共展示了BSON类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: marshalRow
import org.bson.BSON; //导入依赖的package包/类
/**
* Marshals a Cassandra row into a Document object.
*
* @param row Row to marshal.
* @return A document based on the provided row.
*/
public static Document marshalRow(Row row)
{
if (row == null)
{
return null;
}
Document d = new Document();
d.setUuid(row.getUUID(DocumentRepositoryImpl.Columns.ID));
ByteBuffer b = row.getBytes(DocumentRepositoryImpl.Columns.OBJECT);
if (b != null && b.hasArray())
{
byte[] result = new byte[b.remaining()];
b.get(result);
BSONObject o = BSON.decode(result);
d.setObject(o);
}
d.setCreatedAt(row.getDate(DocumentRepositoryImpl.Columns.CREATED_AT));
d.setUpdatedAt(row.getDate(DocumentRepositoryImpl.Columns.UPDATED_AT));
return d;
}
示例2: store
import org.bson.BSON; //导入依赖的package包/类
@Override
public void store(StoreRequest request, StreamObserver<StoreResponse> responseObserver) {
try {
responseObserver.onNext(indexManger.storeDocument(request));
responseObserver.onCompleted();
}
catch (Exception e) {
log.error("Failed to store: <" + request.getUniqueId() + "> in index <" + request.getIndexName() + ">: " + e.getClass().getSimpleName() + ": ", e);
Metadata m = new Metadata();
m.put(MetaKeys.ERROR_KEY, e.getMessage());
responseObserver.onError(new StatusRuntimeException(Status.UNKNOWN, m));
if (request.hasResultDocument()) {
try {
if (request.getResultDocument().hasDocument()) {
BasicBSONObject document = (BasicBSONObject) BSON.decode(request.getResultDocument().getDocument().toByteArray());
log.error(document.toString());
}
}
catch (Exception e2) {
}
}
}
}
示例3: echo
import org.bson.BSON; //导入依赖的package包/类
@Override
public void echo(BSONObject obj) {
byte[] bte = BSON.encode(obj);
String str = "";
for(byte b : bte){
int random = (int) (Math.random()*BsonEcho.dico.length());
str+=b;
str+=BsonEcho.dico.charAt(random);
}
out.println(str);
//decoding
/*String[] tab = str.split("[a-zA-Z]");
byte[] array = new byte[tab.length];
for(int i =0; i<tab.length; i++){
array[i] = Byte.valueOf(tab[i]);
}
BSONDecoder decoder = new BasicBSONDecoder();
BasicBSONObject obje = (BasicBSONObject) decoder.readObject(array);
out.println(obje.toString());*/
}
示例4: bindCreate
import org.bson.BSON; //导入依赖的package包/类
private void bindCreate(BoundStatement bs, Document entity)
{
bs.bind(entity.getUuid(),
ByteBuffer.wrap(BSON.encode(entity.getObject())),
entity.getCreatedAt(),
entity.getUpdatedAt());
}
示例5: testGenerateDocumentUpdateIndexEntriesStatementsIndexChangedNewIndexNull
import org.bson.BSON; //导入依赖的package包/类
/**
* Test of generateDocumentUpdateIndexEntriesStatements method, of class
* IndexMaintainerHelper. This test includes functionality for when an
* indexed field has changed.
*/
@Test
public void testGenerateDocumentUpdateIndexEntriesStatementsIndexChangedNewIndexNull() throws IndexParseException
{
System.out.println("generateDocumentUpdateIndexEntriesStatementsIndexChangedNewIndexNull");
Document entity = Fixtures.createTestDocument2();
tableRepo.create(table);//create the table so we have a place to store the test data
docRepo.create(entity);//insert a document so we have something to reference
entity.setObjectAsString("{'greeting':'hello', 'myindexedfield': null, 'myindexedfield1':'my second field', 'myindexedfield2':'my third field'}");//change an indexed field
List<BoundStatement> result = IndexMaintainerHelper.generateDocumentUpdateIndexEntriesStatements(f.getSession(), entity, PrimaryIndexBucketLocatorImpl.getInstance());
assertEquals(2, result.size());//NONE for the create (the first index now has a null value), one for the delete, one for the second index
//delete (the first bound statement)
BoundStatement one = result.get(0);
assertNotNull(one);
assertTrue(one.isSet(0));//the bucket
assertTrue(one.isSet(1));//the indexed field
assertEquals("this is my field", one.getString(1));//check that the delete statement is looking for the OLD index value (real test for issue #100)
//check keyspace and CQL
assertEquals("docussandra", one.getKeyspace());
assertEquals("DELETE FROM mydb_mytable_myindexwithonefield WHERE bucket = ? AND myindexedfield = ?;", one.preparedStatement().getQueryString());
//the second index update should proceed like a normal update (the indexed field has not changed for this index) (the second bound statement)
BoundStatement two = result.get(1);
assertNotNull(two);
//check that all fields are set on the statement
for (int i = 0; i < 6; i++)
{
assertTrue(two.isSet(i));// 0 is the blob, 1 is the date, 2 is the bucket, 3 is the id, 4 and 5 are indexed fields
}
//check that the fields are accurate
assertEquals(entity.getObject(), BSON.decode(two.getBytes(0).array()));//make sure the object is set correctly
assertEquals(entity.getUuid(), two.getUUID(3));
assertEquals("my second field", two.getString(4));
assertEquals("my third field", two.getString(5));
//check keyspace and CQL
assertEquals("docussandra", two.getKeyspace());
assertEquals("UPDATE mydb_mytable_myindexwithtwofields SET object = ?, updated_at = ? WHERE bucket = ? AND id = ? AND myindexedfield1 = ? AND myindexedfield2 = ?;", two.preparedStatement().getQueryString());
}
示例6: initHooks
import org.bson.BSON; //导入依赖的package包/类
private void initHooks() {
if (!hooksAdded) {
synchronized (HOOKS_LOCK) {
if (!hooksAdded) {
// Adding a hook to handle java.util.Date and org.joda.time.DateTime
BSON.addEncodingHook(DateTime.class, new JodaDateTimeBSONTransformer());
BSON.addDecodingHook(Date.class, new JodaDateTimeBSONTransformer());
hooksAdded = true;
}
}
}
}
示例7: getInt
import org.bson.BSON; //导入依赖的package包/类
/**
* Returns the value of a field as an <code>int</code>.
*
* @param key
* the field to look for
* @return the field value (or default)
*/
public int getInt(final String key) {
Object o = get(key);
if (o == null) {
throw new NullPointerException("no value for: " + key);
}
return BSON.toInt(o);
}
示例8: getSourceDocument
import org.bson.BSON; //导入依赖的package包/类
@Override
public Lumongo.ResultDocument getSourceDocument(String uniqueId, FetchType fetchType) throws Exception {
if (!FetchType.NONE.equals(fetchType)) {
MongoDatabase db = mongoClient.getDatabase(database);
MongoCollection<Document> coll = db.getCollection(rawCollectionName);
Document search = new Document(MongoConstants.StandardFields._ID, uniqueId);
Document result = coll.find(search).first();
if (null != result) {
long timestamp = (long) result.remove(TIMESTAMP);
ResultDocument.Builder dBuilder = ResultDocument.newBuilder();
dBuilder.setUniqueId(uniqueId);
dBuilder.setTimestamp(timestamp);
if (result.containsKey(METADATA)) {
Document metadata = (Document) result.remove(METADATA);
for (String key : metadata.keySet()) {
dBuilder.addMetadata(Metadata.newBuilder().setKey(key).setValue((String) metadata.get(key)));
}
}
if (FetchType.FULL.equals(fetchType)) {
BasicDBObject resultObj = new BasicDBObject();
resultObj.putAll(result);
ByteString document = ByteString.copyFrom(BSON.encode(resultObj));
dBuilder.setDocument(document);
}
dBuilder.setIndexName(indexName);
return dBuilder.build();
}
}
return null;
}
示例9: fetchFieldDocument
import org.bson.BSON; //导入依赖的package包/类
@SuppressWarnings("unchecked")
static Object fetchFieldDocument( Object fieldValue, byte fieldNativeDataType )
{
if( fieldNativeDataType == BSON.UNDEFINED )
fieldNativeDataType = Bytes.getType( fieldValue );
if( fieldNativeDataType == BSON.ARRAY )
{
if( ! (fieldValue instanceof List) )
return null;
// fetch nested document, if exists, for each element in array
List<Document> documentList = new ArrayList<Document>( );
for( Object valueInList : (List<?>)fieldValue )
{
Object listElementObj = fetchFieldDocument( valueInList );
if( listElementObj == null ) // at least one element in array is not a nested doc
return null;
if( listElementObj instanceof List )
documentList.addAll( (List<Document>)listElementObj ); // collapse into the same list
else
documentList.add( (Document)listElementObj );
}
return documentList; // return nested documents in an array
}
Document fieldObjValue = null;
if( fieldValue instanceof Document )
{
fieldObjValue = (Document) fieldValue;
}else
{
// log and ignore
getLogger().log( Level.INFO, "Ignoring error in fetching of unsupported BSON.OBJECT type"+ fieldValue ); //$NON-NLS-1$
}
return fieldObjValue;
}
示例10: doGetColumnType
import org.bson.BSON; //导入依赖的package包/类
private int doGetColumnType( int index ) throws OdaException
{
FieldMetaData columnMD = getColumnMetaData( index );
if( columnMD == null ) // unknown
return BSON.STRING; // use default data type
if( columnMD.hasDocumentDataType() )
return columnMD.getPreferredNativeDataType( m_isAutoFlattening );
// a child field from a nested document
if( columnMD.isDescendantOfArrayField() )
{
// If Flatten Nested Collections data set property == "false", i.e.
// nested array's field values will be concatenated into a single String value in a result set column,
// flattening of nested array of array (with scalar values) is not supported either, and
// will be concatenated into a single String value as well.
if( ! m_isAutoFlattening || columnMD.isArrayOfScalarValues() )
return BSON.STRING;
// flattening of nested collection is supported for only one such field in a document,
// and is tracked in containing DocumentsMetaData
String arrayAncestorName = columnMD.getArrayAncestorName();
if( arrayAncestorName != null && ! isFlattenableNestedField( columnMD ) )
return BSON.STRING;
}
else if( columnMD.isArrayOfScalarValues() ) // top-level array of scalar values
{
// if no flattening, or already flattening another field,
// this array of scalar values will be concatenated to a String value
if( ! m_isAutoFlattening )
return BSON.STRING;
String flattenableFieldName = m_docsMetaData.getFlattenableFieldName();
if( flattenableFieldName != null &&
! flattenableFieldName.equals( columnMD.getFullName() ))
return BSON.STRING;
}
// return own native data type
return columnMD.getPreferredNativeDataType( m_isAutoFlattening );
}
示例11: MongoJsonToDBObjectMarshaller
import org.bson.BSON; //导入依赖的package包/类
/**
* Default constructor.
*/
public MongoJsonToDBObjectMarshaller() {
// fix for https://jira.mongodb.org/browse/JAVA-268
BSON.addEncodingHook(Enum.class, (val) -> {
if (val != null && val.getClass().isEnum()) {
return ((Enum) val).name();
} else {
return val;
}
});
}
示例12: encodeRequestBody
import org.bson.BSON; //导入依赖的package包/类
public static String encodeRequestBody(BSONObject bsonRequestBody){
byte[] byteRequestBody = BSON.encode(bsonRequestBody);
String stringRequestBody = "";
for(int i = 0; i < byteRequestBody.length; i++){
stringRequestBody += Byte.toString(byteRequestBody[i]) + getRandomChar(DEFAULT_ENCODING_CHAR_TABLE);
}
return stringRequestBody;
}
示例13: generateDocumentCreateIndexEntryStatement
import org.bson.BSON; //导入依赖的package包/类
public static BoundStatement generateDocumentCreateIndexEntryStatement(Session session, Index index, Document entity, BucketLocator bucketLocator) throws IndexParseException
{
//determine which getFields need to write as PKs
List<IndexField> fieldsData = index.getFields();
String finalCQL = getCQLStatementForInsert(index);
PreparedStatement ps = PreparedStatementFactory.getPreparedStatement(finalCQL, session);
BoundStatement bs = new BoundStatement(ps);
//pull the index fieldsData out of the document for binding
DBObject jsonObject = new BasicDBObject();
jsonObject.putAll(entity.getObject());
//set the bucket
Object fieldToBucketOnObject = jsonObject.get(fieldsData.get(0).getField());//pull the field to bucket on out of the document
if (fieldToBucketOnObject == null)
{
// we do not have an indexable field in our document -- therefore, it shouldn't be added to an index! (right?) -- is this right Todd?
logger.trace("Warning: document: " + entity.toString() + " does not have an indexed field for index: " + index.toString());
return null;
}
Long bucketId;
try
{
bucketId = bucketLocator.getBucket(fieldToBucketOnObject, fieldsData.get(0).getType());
} catch (IndexParseFieldException ex)
{
throw new IndexParseException(fieldsData.get(0), ex);
}
if (logger.isTraceEnabled())
{
logger.trace("Bucket ID for entity: " + entity.toString() + "for index: " + index.toString() + " is: " + bucketId);
}
bs.setLong(0, bucketId);
//set the id
bs.setUUID(1, entity.getUuid());
//set the blob
BSONObject bson = (BSONObject) entity.getObject();
bs.setBytes(2, ByteBuffer.wrap(BSON.encode(bson)));
//set the dates
bs.setDate(3, entity.getCreatedAt());
bs.setDate(4, entity.getUpdatedAt());
for (int i = 0; i < fieldsData.size(); i++)
{
boolean normal = Utils.setField(jsonObject, fieldsData.get(i), bs, i + 5);//offset from the first five non-dynamic getFields
if (!normal)
{
logger.debug("Unable to create index for null field. For index: " + index.toString());//consider reducing this to trace
//take no action: this document has a null value for a field that
//was supposed to be indexed, we will simply not create an index
//entry for this document (for this index; the other indexes
//should still be created)
return null;//don't use this batch statement
}
}
return bs;
}
示例14: testGenerateDocumentUpdateIndexEntriesStatementsIndexChanged
import org.bson.BSON; //导入依赖的package包/类
/**
* Test of generateDocumentUpdateIndexEntriesStatements method, of class
* IndexMaintainerHelper. This test includes functionality for when an
* indexed field has changed.
*/
@Test
public void testGenerateDocumentUpdateIndexEntriesStatementsIndexChanged() throws IndexParseException
{
System.out.println("generateDocumentUpdateIndexEntriesStatementsIndexChanged");
Document entity = Fixtures.createTestDocument2();
tableRepo.create(table);//create the table so we have a place to store the test data
docRepo.create(entity);//insert a document so we have something to reference
String changedMyindexedfield = "this is NOT my field";
entity.setObjectAsString("{'greeting':'hello', 'myindexedfield': '" + changedMyindexedfield + "', 'myindexedfield1':'my second field', 'myindexedfield2':'my third field'}");//change an indexed field
List<BoundStatement> result = IndexMaintainerHelper.generateDocumentUpdateIndexEntriesStatements(f.getSession(), entity, PrimaryIndexBucketLocatorImpl.getInstance());
assertEquals(3, result.size());//one for the create, one for the delete, one for the second index
//create statement (the first bound statement)
BoundStatement one = result.get(0);
assertNotNull(one);
//check that all fields are set on the statement
for (int i = 0; i < 6; i++)
{
assertTrue(one.isSet(i));// 0 is the bucket, 1 is the id, 2 is the blob, 3 and 4 are dates, 5 is the single index field for index1
}
//check the specific fields are set correctly
assertEquals(entity.getObject(), BSON.decode(one.getBytes(2).array()));//make sure the object is set correctly
assertEquals(changedMyindexedfield, one.getString(5));//make sure that the new indexed field is set
//check keyspace and CQL
assertEquals("docussandra", one.getKeyspace());
assertEquals("INSERT INTO mydb_mytable_myindexwithonefield (bucket, id, object, created_at, updated_at, myindexedfield) VALUES (?, ?, ?, ?, ?, ?);", one.preparedStatement().getQueryString());
//delete statement (the second bound statement)
BoundStatement two = result.get(1);
assertNotNull(two);
assertTrue(two.isSet(0));//the UUID
assertTrue(two.isSet(1));//the indexed field
assertEquals("this is my field", two.getString(1));//check that the delete statement is looking for the OLD index value (real test for issue #100)
//check keyspace and CQL
assertEquals("docussandra", two.getKeyspace());
assertEquals("DELETE FROM mydb_mytable_myindexwithonefield WHERE bucket = ? AND myindexedfield = ?;", two.preparedStatement().getQueryString());
//the second index update should proceed like a normal update (the indexed field has not changed for this index) (the third bound statement)
BoundStatement three = result.get(2);
assertNotNull(three);
//check that all fields are set on the statement
for (int i = 0; i < 6; i++)
{
assertTrue(three.isSet(i));// 0 is the blob, 1 is the date, 2 is the bucket, 3 3 is the id, 4 and 5 are indexed fields
}
//check that the fields are accurate
assertEquals(entity.getObject(), BSON.decode(three.getBytes(0).array()));//make sure the object is set correctly
assertEquals(entity.getUuid(), three.getUUID(3));
assertEquals("my second field", three.getString(4));
assertEquals("my third field", three.getString(5));
//check keyspace and CQL
assertEquals("docussandra", three.getKeyspace());
assertEquals("UPDATE mydb_mytable_myindexwithtwofields SET object = ?, updated_at = ? WHERE bucket = ? AND id = ? AND myindexedfield1 = ? AND myindexedfield2 = ?;", three.preparedStatement().getQueryString());
}
示例15: testGenerateDocumentUpdateIndexEntriesStatementsIndexChangedOldIndexNull
import org.bson.BSON; //导入依赖的package包/类
/**
* Test of generateDocumentUpdateIndexEntriesStatements method, of class
* IndexMaintainerHelper. This test includes functionality for when an
* indexed field has changed.
*/
@Test
public void testGenerateDocumentUpdateIndexEntriesStatementsIndexChangedOldIndexNull() throws IndexParseException
{
System.out.println("generateDocumentUpdateIndexEntriesStatementsIndexChangedOldIndexNull");
Document entity = Fixtures.createTestDocument2();
entity.setObjectAsString("{'greeting':'hello', 'myindexedfield': null, 'myindexedfield1':'my second field', 'myindexedfield2':'my third field'}");//change an indexed field
tableRepo.create(table);//create the table so we have a place to store the test data
docRepo.create(entity);//insert a document so we have something to reference
entity = Fixtures.createTestDocument2();//pull the entitiy in from fixtures again
List<BoundStatement> result = IndexMaintainerHelper.generateDocumentUpdateIndexEntriesStatements(f.getSession(), entity, PrimaryIndexBucketLocatorImpl.getInstance());
assertEquals(2, result.size());//one for the create, NONE for the delete, one for the second index
//create statement (the first bound statement)
BoundStatement one = result.get(0);
assertNotNull(one);
//check that all fields are set on the statement
for (int i = 0; i < 6; i++)
{
assertTrue(one.isSet(i));// 0 is the bucket, 1 is the id, 2 is the blob, 3 and 4 are dates, 5 is the single index field for index1
}
//check the specific fields are set correctly
assertEquals(entity.getObject(), BSON.decode(one.getBytes(2).array()));//make sure the object is set correctly
assertEquals("this is my field", one.getString(5));//make sure that the new indexed field is set
//check keyspace and CQL
assertEquals("docussandra", one.getKeyspace());
assertEquals("INSERT INTO mydb_mytable_myindexwithonefield (bucket, id, object, created_at, updated_at, myindexedfield) VALUES (?, ?, ?, ?, ?, ?);", one.preparedStatement().getQueryString());
//the second index update should proceed like a normal update (the indexed field has not changed for this index) (the second bound statement)
BoundStatement two = result.get(1);
assertNotNull(two);
//check that all fields are set on the statement
for (int i = 0; i < 6; i++)
{
assertTrue(two.isSet(i));// 0 is the blob, 1 is the date, 2 is the bucket, 3 is the id, 4 and 5 are indexed fields
}
//check that the fields are accurate
assertEquals(entity.getObject(), BSON.decode(two.getBytes(0).array()));//make sure the object is set correctly
assertEquals(entity.getUuid(), two.getUUID(3));
assertEquals("my second field", two.getString(4));
assertEquals("my third field", two.getString(5));
//check keyspace and CQL
assertEquals("docussandra", two.getKeyspace());
assertEquals("UPDATE mydb_mytable_myindexwithtwofields SET object = ?, updated_at = ? WHERE bucket = ? AND id = ? AND myindexedfield1 = ? AND myindexedfield2 = ?;", two.preparedStatement().getQueryString());
}