IndexMaintainerHelper { public static List<BoundStatement> generateDocumentCreateIndexEntriesStatements(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) { BoundStatement bs = generateDocumentCreateIndexEntryStatement(session, index, entity, bucketLocator); if (bs != null) { statementList.add(bs); } } 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 testGenerateDocumentCreateIndexEntriesStatements() throws IndexParseException { System.out.println("generateDocumentCreateIndexEntriesStatements"); Document entity = Fixtures.createTestDocument2(); List<BoundStatement> result = IndexMaintainerHelper.generateDocumentCreateIndexEntriesStatements(f.getSession(), entity, PrimaryIndexBucketLocatorImpl.getInstance()); assertEquals(result.size(), 2); BoundStatement one = result.get(0); assertNotNull(one); for (int i = 0; i < 5; i++) { assertTrue(one.isSet(i)); } 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("docussandra", two.getKeyspace()); assertEquals("INSERT INTO mydb_mytable_myindexwithtwofields (bucket, id, object, created_at, updated_at, myindexedfield1, myindexedfield2) VALUES (?, ?, ?, ?, ?, ?, ?);", two.preparedStatement().getQueryString()); }
@Test public void testGenerateDocumentCreateIndexEntriesStatementsWithDataTypes() throws IndexParseException { System.out.println("generateDocumentCreateIndexEntriesStatementsWithDataTypes"); Document entity = Fixtures.createTestDocument3(); f.insertIndex(index3); List<BoundStatement> result = IndexMaintainerHelper.generateDocumentCreateIndexEntriesStatements(f.getSession(), entity, PrimaryIndexBucketLocatorImpl.getInstance()); assertEquals(result.size(), 1); BoundStatement one = result.get(0); assertNotNull(one); for (int i = 0; i < 12; i++) { assertTrue(one.isSet(i)); } assertNotNull(one.getLong(0)); assertNotNull(one.getUUID(1)); assertNotNull(one.getBytes(2)); assertNotNull(one.getDate(3)); assertNotNull(one.getDate(4)); assertNotNull(one.getUUID(5)); assertNotNull(one.getString(6)); assertNotNull(one.getInt(7)); assertNotNull(one.getDouble(8)); assertNotNull(one.getBytes(9)); assertNotNull(one.getBool(10)); assertNotNull(one.getDate(11)); assertEquals("docussandra", one.getKeyspace()); assertEquals("INSERT INTO mydb_mytable_myindexallfields (bucket, id," + " object, created_at, updated_at, thisisauudid, thisisastring, " + "thisisanint, thisisadouble, thisisbase64, thisisaboolean, thisisadate, thisisalong)" + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", one.preparedStatement().getQueryString()); }
@Test public void testGenerateDocumentCreateIndexEntriesStatementsNoIndexField() throws IndexParseException { System.out.println("testGenerateDocumentCreateIndexEntriesStatementsNoIndexField"); Document entity = Fixtures.createTestDocument2(); entity.setObjectAsString("{}"); List<BoundStatement> result = IndexMaintainerHelper.generateDocumentCreateIndexEntriesStatements(f.getSession(), entity, PrimaryIndexBucketLocatorImpl.getInstance()); assertTrue(result.isEmpty()); }
@Test public void testGenerateDocumentCreateIndexEntriesStatementsBadIndexField() { System.out.println("testGenerateDocumentCreateIndexEntriesStatementsBadIndexField"); Document entity = Fixtures.createTestDocument3(); f.insertIndex(Fixtures.createTestIndexAllFieldTypes()); entity.setObjectAsString("{\"thisisastring\":\"hello\", \"thisisanint\": \"five\", \"thisisadouble\":\"five point five five five\"," + " \"thisisbase64\":\"nope!\", \"thisisaboolean\":\"blah!\"," + " \"thisisadate\":\"day 0\", \"thisisauudid\":\"z\"}"); boolean expectedExceptionThrown = false; try { List<BoundStatement> result = IndexMaintainerHelper.generateDocumentCreateIndexEntriesStatements(f.getSession(), entity, PrimaryIndexBucketLocatorImpl.getInstance()); } catch (IndexParseException e) { expectedExceptionThrown = true; } assertTrue("Expected exception was not thrown.", expectedExceptionThrown); }