IndexMaintainerHelper { public static List<BoundStatement> generateDocumentUpdateIndexEntriesStatements(Session session, Document entity, BucketLocator bucketLocator) throws IndexParseException { List<Index> indices = getIndexForDocument(session, entity); ArrayList<BoundStatement> statementList = new ArrayList<>(indices.size()); for (Index index : indices) { List<IndexField> fields = index.getFields(); BSONObject oldObject = getOldObjectForUpdate(session, entity); if (hasIndexedFieldChanged(oldObject, index, entity)) { BoundStatement createBS = generateDocumentCreateIndexEntryStatement(session, index, entity, bucketLocator); if (createBS != null) { statementList.add(createBS); } BoundStatement deleteBS = generateDocumentDeleteIndexEntryStatement(session, index, oldObject, bucketLocator); if (deleteBS != null) { statementList.add(deleteBS); } } else { String finalCQL = getCQLStatementForWhereClauses(ITABLE_UPDATE_CQL, index); PreparedStatement ps = PreparedStatementFactory.getPreparedStatement(finalCQL, session); BoundStatement updateBS = new BoundStatement(ps); BSONObject bson = (BSONObject) entity.getObject(); updateBS.setBytes(0, ByteBuffer.wrap(BSON.encode(bson))); updateBS.setDate(1, entity.getUpdatedAt()); DBObject jsonObject = (DBObject) entity.getObject(); Object bucketField = jsonObject.get(fields.get(0).getField()); if (bucketField == null) { break; } Long bucketId; try { bucketId = bucketLocator.getBucket(bucketField, fields.get(0).getType()); } catch (IndexParseFieldException ex) { throw new IndexParseException(fields.get(0), ex); } if (logger.isTraceEnabled()) { logger.trace("Bucket ID for entity: " + entity.toString() + " for index: " + index.toString() + " is: " + bucketId); } updateBS.setLong(2, bucketId); updateBS.setUUID(3, entity.getUuid()); boolean normal = true; for (int i = 0; i < fields.size(); i++) { normal = Utils.setField(jsonObject, fields.get(i), updateBS, i + 4); if (!normal) { logger.debug("Unable to update index for null field. For index: " + index.toString()); break; } } if (normal) { statementList.add(updateBS); } } } return statementList; } private  IndexMaintainerHelper(); static List<BoundStatement> generateDocumentCreateIndexEntriesStatements(Session session, Document entity, BucketLocator bucketLocator); static BoundStatement generateDocumentCreateIndexEntryStatement(Session session, Index index, Document entity, BucketLocator bucketLocator); static List<BoundStatement> generateDocumentUpdateIndexEntriesStatements(Session session, Document entity, BucketLocator bucketLocator); static List<BoundStatement> generateDocumentDeleteIndexEntriesStatements(Session session, Document entity, BucketLocator bucketLocator); static List<Index> getIndexForDocument(Session session, Document entity); static boolean hasIndexedFieldChanged(BSONObject oldObject, Index index, Document entity); static BSONObject getOldObjectForUpdate(Session session, Document entity); static String getCQLStatementForInsert(Index index); static String generateCQLStatementForInsert(Index index); static String getCQLStatementForWhereClauses(String CQL, Index index); static String generateCQLStatementForWhereClauses(String CQL, Index index); static final String ITABLE_INSERT_CQL; static final String ITABLE_UPDATE_CQL; static final String ITABLE_DELETE_CQL; }
@Test public void testGenerateDocumentUpdateIndexEntriesStatements() throws IndexParseException { System.out.println("generateDocumentUpdateIndexEntriesStatements"); Document entity = Fixtures.createTestDocument2(); tableRepo.create(table); docRepo.create(entity); List<BoundStatement> result = IndexMaintainerHelper.generateDocumentUpdateIndexEntriesStatements(f.getSession(), entity, PrimaryIndexBucketLocatorImpl.getInstance()); assertEquals(2, result.size()); BoundStatement one = result.get(0); assertNotNull(one); for (int i = 0; i < 3; i++) { assertTrue(one.isSet(i)); } assertEquals("docussandra", one.getKeyspace()); assertEquals("UPDATE mydb_mytable_myindexwithonefield SET object = ?, updated_at = ? WHERE bucket = ? AND id = ? AND myindexedfield = ?;", one.preparedStatement().getQueryString()); BoundStatement two = result.get(1); assertNotNull(two); for (int i = 0; i < 4; i++) { assertTrue(two.isSet(i)); } assertEquals("docussandra", two.getKeyspace()); assertEquals("UPDATE mydb_mytable_myindexwithtwofields SET object = ?, updated_at = ? WHERE bucket = ? AND id = ? AND myindexedfield1 = ? AND myindexedfield2 = ?;", two.preparedStatement().getQueryString()); }
@Test public void testGenerateDocumentUpdateIndexEntriesStatementsWithDataTypes() throws IndexParseException { System.out.println("generateDocumentUpdateIndexEntriesStatements"); Document entity = Fixtures.createTestDocument3(); tableRepo.create(table); f.insertIndex(index3); docRepo.create(entity); List<BoundStatement> result = IndexMaintainerHelper.generateDocumentUpdateIndexEntriesStatements(f.getSession(), entity, PrimaryIndexBucketLocatorImpl.getInstance()); assertEquals(1, result.size()); BoundStatement one = result.get(0); assertNotNull(one); for (int i = 0; i < 11; i++) { assertTrue(one.isSet(i)); } assertNotNull(one.getBytes(0)); assertNotNull(one.getDate(1)); assertNotNull(one.getLong(2)); assertNotNull(one.getUUID(3)); assertNotNull(one.getUUID(4)); assertNotNull(one.getString(5)); assertNotNull(one.getInt(6)); assertNotNull(one.getDouble(7)); assertNotNull(one.getBytes(8)); assertNotNull(one.getBool(9)); assertNotNull(one.getDate(10)); assertEquals("docussandra", one.getKeyspace()); assertEquals("UPDATE mydb_mytable_myindexallfields SET object = ?, updated_at = ? " + "WHERE bucket = ? AND id = ? AND thisisauudid = ? AND thisisastring = ? AND thisisanint = ?" + " AND thisisadouble = ? AND thisisbase64 = ? AND thisisaboolean = ? AND thisisadate = ? AND thisisalong = ?;", one.preparedStatement().getQueryString()); }
@Test public void testGenerateDocumentUpdateIndexEntriesStatementsIndexChanged() throws IndexParseException { System.out.println("generateDocumentUpdateIndexEntriesStatementsIndexChanged"); Document entity = Fixtures.createTestDocument2(); tableRepo.create(table); docRepo.create(entity); String changedMyindexedfield = "this is NOT my field"; entity.setObjectAsString("{'greeting':'hello', 'myindexedfield': '" + changedMyindexedfield + "', 'myindexedfield1':'my second field', 'myindexedfield2':'my third field'}"); List<BoundStatement> result = IndexMaintainerHelper.generateDocumentUpdateIndexEntriesStatements(f.getSession(), entity, PrimaryIndexBucketLocatorImpl.getInstance()); assertEquals(3, result.size()); BoundStatement one = result.get(0); assertNotNull(one); for (int i = 0; i < 6; i++) { assertTrue(one.isSet(i)); } assertEquals(entity.getObject(), BSON.decode(one.getBytes(2).array())); assertEquals(changedMyindexedfield, one.getString(5)); assertEquals("docussandra", one.getKeyspace()); assertEquals("INSERT INTO mydb_mytable_myindexwithonefield (bucket, id, object, created_at, updated_at, myindexedfield) VALUES (?, ?, ?, ?, ?, ?);", one.preparedStatement().getQueryString()); BoundStatement two = result.get(1); assertNotNull(two); assertTrue(two.isSet(0)); assertTrue(two.isSet(1)); assertEquals("this is my field", two.getString(1)); assertEquals("docussandra", two.getKeyspace()); assertEquals("DELETE FROM mydb_mytable_myindexwithonefield WHERE bucket = ? AND myindexedfield = ?;", two.preparedStatement().getQueryString()); BoundStatement three = result.get(2); assertNotNull(three); for (int i = 0; i < 6; i++) { assertTrue(three.isSet(i)); } assertEquals(entity.getObject(), BSON.decode(three.getBytes(0).array())); assertEquals(entity.getUuid(), three.getUUID(3)); assertEquals("my second field", three.getString(4)); assertEquals("my third field", three.getString(5)); 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 public void testGenerateDocumentUpdateIndexEntriesStatementsIndexChangedNewIndexNull() throws IndexParseException { System.out.println("generateDocumentUpdateIndexEntriesStatementsIndexChangedNewIndexNull"); Document entity = Fixtures.createTestDocument2(); tableRepo.create(table); docRepo.create(entity); entity.setObjectAsString("{'greeting':'hello', 'myindexedfield': null, 'myindexedfield1':'my second field', 'myindexedfield2':'my third field'}"); List<BoundStatement> result = IndexMaintainerHelper.generateDocumentUpdateIndexEntriesStatements(f.getSession(), entity, PrimaryIndexBucketLocatorImpl.getInstance()); assertEquals(2, result.size()); BoundStatement one = result.get(0); assertNotNull(one); assertTrue(one.isSet(0)); assertTrue(one.isSet(1)); assertEquals("this is my field", one.getString(1)); assertEquals("docussandra", one.getKeyspace()); assertEquals("DELETE FROM mydb_mytable_myindexwithonefield WHERE bucket = ? AND myindexedfield = ?;", one.preparedStatement().getQueryString()); BoundStatement two = result.get(1); assertNotNull(two); for (int i = 0; i < 6; i++) { assertTrue(two.isSet(i)); } assertEquals(entity.getObject(), BSON.decode(two.getBytes(0).array())); assertEquals(entity.getUuid(), two.getUUID(3)); assertEquals("my second field", two.getString(4)); assertEquals("my third field", two.getString(5)); assertEquals("docussandra", two.getKeyspace()); assertEquals("UPDATE mydb_mytable_myindexwithtwofields SET object = ?, updated_at = ? WHERE bucket = ? AND id = ? AND myindexedfield1 = ? AND myindexedfield2 = ?;", two.preparedStatement().getQueryString()); }
@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'}"); tableRepo.create(table); docRepo.create(entity); entity = Fixtures.createTestDocument2(); List<BoundStatement> result = IndexMaintainerHelper.generateDocumentUpdateIndexEntriesStatements(f.getSession(), entity, PrimaryIndexBucketLocatorImpl.getInstance()); assertEquals(2, result.size()); BoundStatement one = result.get(0); assertNotNull(one); for (int i = 0; i < 6; i++) { assertTrue(one.isSet(i)); } assertEquals(entity.getObject(), BSON.decode(one.getBytes(2).array())); assertEquals("this is my field", one.getString(5)); assertEquals("docussandra", one.getKeyspace()); assertEquals("INSERT INTO mydb_mytable_myindexwithonefield (bucket, id, object, created_at, updated_at, myindexedfield) VALUES (?, ?, ?, ?, ?, ?);", one.preparedStatement().getQueryString()); BoundStatement two = result.get(1); assertNotNull(two); for (int i = 0; i < 6; i++) { assertTrue(two.isSet(i)); } assertEquals(entity.getObject(), BSON.decode(two.getBytes(0).array())); assertEquals(entity.getUuid(), two.getUUID(3)); assertEquals("my second field", two.getString(4)); assertEquals("my third field", two.getString(5)); assertEquals("docussandra", two.getKeyspace()); assertEquals("UPDATE mydb_mytable_myindexwithtwofields SET object = ?, updated_at = ? WHERE bucket = ? AND id = ? AND myindexedfield1 = ? AND myindexedfield2 = ?;", two.preparedStatement().getQueryString()); }