/** * 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; }
@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) { } } } }
@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());*/ }
private void bindCreate(BoundStatement bs, Document entity) { bs.bind(entity.getUuid(), ByteBuffer.wrap(BSON.encode(entity.getObject())), entity.getCreatedAt(), entity.getUpdatedAt()); }
/** * 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()); }
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; } } } }
/** * 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); }
@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; }
@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; }
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 ); }
/** * 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; } }); }
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; }
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; }
/** * 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()); }
/** * 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()); }
private void addDataType( Object fieldValue ) { // add the data type of given fieldValue to existing set, if exists; // the same named field set in a different doc may have a different data type byte nativeBSonDataTypeCode = Bytes.getType( fieldValue ); if( m_nativeDataTypes == null ) m_nativeDataTypes = new HashSet<Integer>(2); if ( nativeBSonDataTypeCode == -1 ) { if ( fieldValue instanceof Document ) { nativeBSonDataTypeCode = Bytes.OBJECT; } } m_nativeDataTypes.add( Integer.valueOf( nativeBSonDataTypeCode ) ); // check if field value contains a document, // iteratively get field document's nested metadata if( nativeBSonDataTypeCode == BSON.ARRAY ) { if( fieldValue instanceof java.util.List ) { java.util.List<?> listOfObjects = (java.util.List<?>)fieldValue; if( listOfObjects.size() > 0 ) { // use first element in array to determine metadata addDataType( listOfObjects.get(0) ); // handles nested arrays return; } } } Object fieldObjValue = ResultDataHandler.fetchFieldDocument( fieldValue, nativeBSonDataTypeCode ); if( fieldObjValue != null ) // contains nested document { if( m_childDocMetaData == null ) m_childDocMetaData = sm_factory.new DocumentsMetaData(); m_childDocMetaData.addDocumentMetaData( fieldObjValue, this ); } }
static Object fetchFieldDocument( Object fieldValue ) { return fetchFieldDocument( fieldValue, BSON.UNDEFINED ); }
/** * Returns the value of a field as an <code>int</code>. * * @param key * the field to look for * @param def * the default to return * @return the field value (or default) */ public int getInt(final String key, final int def) { Object foo = get(key); if (foo == null) { return def; } return BSON.toInt(foo); }
/** * Converts the given UUID into a Binary object that represents the underlying byte array in * Mongo. This is recommended by the mongo docs * to store UUIDs. * * @param uid * The object's UUID * @return a Binary representation of the given UUID. */ public static Binary convertUUIDtoBinary(UUID uuid) { ByteBuffer buff = ByteBuffer.allocate(16); buff.putLong(uuid.getMostSignificantBits()); buff.putLong(uuid.getLeastSignificantBits()); byte[] arr = buff.array(); Binary binary = new Binary(BSON.B_UUID, arr); return binary; }
/** * Converts the given UUID into a Binary object that represents the underlying byte array in * Mongo. This is recommended by the mongo docs to store UUIDs. * * @param uid * The object's UUID * @return a Binary representation of the given UUID. */ public static Binary convertUUIDtoBinary(UUID uuid) { ByteBuffer buff = ByteBuffer.allocate(16); buff.putLong(uuid.getMostSignificantBits()); buff.putLong(uuid.getLeastSignificantBits()); byte[] arr = buff.array(); Binary binary = new Binary(BSON.B_UUID, arr); return binary; }
/** * Creates a Binary object with the default binary type of 0 * @param data raw data */ public Binary( byte[] data ){ this(BSON.B_GENERAL, data); }
/** * Creates a Binary object with the default binary type of 0 * * @param data raw data */ public Binary(byte[] data) { this(BSON.B_GENERAL, data); }