/** * Execute the query again and set the reuslts */ private void advanceIterator() { //run producing the values within a hystrix command. This way we'll time out if the read takes too long try { sourceIterator = rowQuery.execute().getResult().iterator(); } catch ( ConnectionException e ) { throw new RuntimeException( "Unable to get next page", e ); } } }
@Override public List<MvccLogEntry> load( final ApplicationScope collectionScope, final Id entityId, final UUID version, final int maxSize ) { Preconditions.checkNotNull( collectionScope, "collectionScope is required" ); Preconditions.checkNotNull( entityId, "entity id is required" ); Preconditions.checkNotNull( version, "version is required" ); Preconditions.checkArgument( maxSize > 0, "max Size must be greater than 0" ); ColumnList<UUID> columns; try { final Id applicationId = collectionScope.getApplication(); final ScopedRowKey<K> rowKey = createKey( applicationId, entityId ); columns = keyspace.prepareQuery( CF_ENTITY_LOG ).getKey( rowKey ).withColumnRange( version, null, false, maxSize ) .execute().getResult(); } catch ( ConnectionException e ) { throw new RuntimeException( "Unable to load log entries", e ); } return parseResults( columns, entityId ); }
@Override public List<MvccLogEntry> loadReversed( final ApplicationScope applicationScope, final Id entityId, final UUID minVersion, final int maxSize ) { ColumnList<UUID> columns; try { final Id applicationId = applicationScope.getApplication(); final ScopedRowKey<K> rowKey = createKey( applicationId, entityId ); columns = keyspace.prepareQuery( CF_ENTITY_LOG ).getKey( rowKey ) .withColumnRange( minVersion, null, true, maxSize ).execute().getResult(); } catch ( ConnectionException e ) { throw new RuntimeException( "Unable to load log entries", e ); } return parseResults( columns, entityId ); }
@Override public SortedMap<String, String> getCheckpoints() throws ConnectionException { SortedMap<String, String> checkpoints = Maps.newTreeMap(tokenComparator); for (Column<String> column : keyspace.prepareQuery(columnFamily).getKey(bbKey).execute().getResult()) { checkpoints.put(column.getName(), column.getStringValue()); } return checkpoints; }
@Test public void testGetSingleKey() throws ConnectionException { for (char key = 'A'; key <= 'Z'; key++) { String keyName = Character.toString(key); OperationResult<ColumnList<String>> result = keyspace.prepareQuery(CF_STANDARD1).getKey(keyName).execute(); Assert.assertNotNull(result.getResult()); Assert.assertFalse(result.getResult().isEmpty()); } }
private void verifyPresent(Keyspace ks, int rowKey) throws ConnectionException { ColumnList<Long> result = ks.prepareQuery(CF_DUAL_WRITES).getRow(rowKey).execute().getResult(); if (result.isEmpty()) { throw new RuntimeException("Row: " + rowKey + " missing from keysapce: " + ks.getKeyspaceName()); } else { System.out.println("Verified Row: " + rowKey + " present in ks: " + ks.getKeyspaceName()); } }
private void verifyNotPresent(Keyspace ks, int rowKey) throws ConnectionException { ColumnList<Long> result = ks.prepareQuery(CF_DUAL_WRITES).getRow(rowKey).execute().getResult(); if (!result.isEmpty()) { throw new RuntimeException("Row: " + rowKey + " present in keysapce: " + ks.getKeyspaceName()); } else { System.out.println("Verified Row: " + rowKey + " NOT present in ks: " + ks.getKeyspaceName()); } }
private void performSimpleRowQueryForRow(String rowKey, boolean rowDeleted, String expectedChar) throws Exception { ColumnList<String> result = keyspace.prepareQuery(CF_ACCOUNTS).getRow(rowKey).execute().getResult(); if (rowDeleted) { Assert.assertTrue(result.isEmpty()); } else { Assert.assertFalse(result.isEmpty()); Column<String> col = result.getColumnByName("user"); Assert.assertEquals("user" + expectedChar, col.getStringValue()); col = result.getColumnByName("pswd"); Assert.assertEquals("pswd" + expectedChar, col.getStringValue()); } }
private void testReadSingleRowAllColumns(boolean rowDeleted) throws Exception { for (int year = 2001; year <= 2014; year++) { ColumnList<Population> result = keyspace.prepareQuery(CF_POPULATION) .getRow(year) .execute().getResult(); if (rowDeleted) { Assert.assertTrue(result.isEmpty()); continue; } else { checkResult(result, SanDiego, SanFrancisco, NewYork, Seattle); } } }
@Test public void testGetColumnSlice() throws ConnectionException { LOG.info("Starting testGetColumnSlice..."); OperationResult<ColumnList<String>> result = keyspace .prepareQuery(CF_STANDARD1) .getKey("A") .withColumnSlice( new ColumnSlice<String>("c", "h").setLimit(5)) .execute(); Assert.assertNotNull(result.getResult()); Assert.assertEquals(5, result.getResult().size()); }
@Test public void testCopy() throws ConnectionException { String keyName = "A"; keyspace.prepareQuery(CF_STANDARD1).getKey(keyName).copyTo(CF_STANDARD2, keyName).execute(); ColumnList<String> list1 = keyspace.prepareQuery(CF_STANDARD1).getKey(keyName).execute().getResult(); ColumnList<String> list2 = keyspace.prepareQuery(CF_STANDARD2).getKey(keyName).execute().getResult(); Iterator<Column<String>> iter1 = list1.iterator(); Iterator<Column<String>> iter2 = list2.iterator(); while (iter1.hasNext()) { Column<String> column1 = iter1.next(); Column<String> column2 = iter2.next(); Assert.assertEquals(column1.getName(), column2.getName()); Assert.assertEquals(column1.getByteBufferValue(),column2.getByteBufferValue()); } Assert.assertFalse(iter2.hasNext()); }
@Test public void testColumnSlice() throws ConnectionException { OperationResult<ColumnList<String>> r1 = keyspace .prepareQuery(CF_STANDARD1).getKey("A") .withColumnSlice("a", "b").execute(); Assert.assertEquals(2, r1.getResult().size()); }
@Override public FeatureState getFeatureState(Feature feature) { try { final ColumnList<String> state = keyspace .prepareQuery(columnFamily) .getRow(feature.name()) .execute() .getResult(); return state.isEmpty() ? null : toFeatureState(feature, state); } catch (ConnectionException e) { throw new RuntimeException(e); } }
@Test public void testUniquenessWithCustomMutation() throws Exception { ColumnList<String> result = keyspace.prepareQuery(CF_UNIQUE_CONSTRAINT).getRow(10L).execute().getResult(); Assert.assertTrue(result.isEmpty()); ColumnPrefixUniquenessConstraint<Long> unique = new ColumnPrefixUniquenessConstraint<Long>(keyspace, CF_UNIQUE_CONSTRAINT, 3L) .withConsistencyLevel(ConsistencyLevel.CL_ONE); unique.acquireAndApplyMutation(new Function<MutationBatch, Boolean>() { public Boolean apply(MutationBatch input) { input.withRow(CF_UNIQUE_CONSTRAINT, 10L).putEmptyColumn("MyCustomColumn", null); return true; } }); result = keyspace.prepareQuery(CF_UNIQUE_CONSTRAINT).getRow(10L).execute().getResult(); Assert.assertFalse(result.isEmpty()); } }
@Test public void testSingleRowColumnSliceQueryWithCollection() throws Exception { for (int i=0; i<TestRowCount; i++) { ColumnList<String> result = keyspace.prepareQuery(CF_USER_INFO) .withCaching(true) .getRow("acct_" + i) .withColumnSlice("firstname", "lastname", "address", "age") .execute() .getResult(); Assert.assertNotNull(result); Assert.assertTrue(4 == result.size()); Assert.assertEquals("john_" + i, result.getColumnByName("firstname").getStringValue()); Assert.assertEquals("smith_" + i, result.getColumnByName("lastname").getStringValue()); Assert.assertEquals("john smith address " + i, result.getColumnByName("address").getStringValue()); Assert.assertTrue(30 + i == result.getColumnByName("age").getIntegerValue()); } }
@Test public void testSingleRowAllColumnsQuery() throws Exception { for (int i=0; i<TestRowCount; i++) { ColumnList<String> result = keyspace.prepareQuery(CF_USER_INFO) .withCaching(true) .getRow("acct_" + i) .execute() .getResult(); super.testAllColumnsForRow(result, i); } }
private MutationBatch getMutationBatch() throws ConnectionException { ColumnList<C> columnList = rowQuery.execute().getResult(); CqlKeyspaceImpl ksImpl = new CqlKeyspaceImpl(ksContext); MutationBatch mBatch = ksImpl.prepareMutationBatch(); CqlColumnListMutationImpl<K,C> colListMutation = (CqlColumnListMutationImpl<K, C>)mBatch.withRow(cf, rowKey); Iterator<Column<C>> iter = columnList.iterator(); boolean first = true; while(iter.hasNext()) { CqlColumnImpl<C> col = (CqlColumnImpl<C>) iter.next(); if (first && useOriginalTimestamp) { colListMutation.setTimestamp(col.getTimestamp()); first = false; } colListMutation.putColumnWithGenericValue(col.getName(), col.getGenericValue(), null); } return mBatch; } }
@Test public void testTtlValues() throws Exception { MutationBatch mb = keyspace.prepareMutationBatch(); mb.withRow(CF_TTL, "row") .putColumn("TTL0", "TTL0", 0) .putColumn("TTLNULL", "TTLNULL", null) .putColumn("TTL1", "TTL1", 1); mb.execute(); Thread.sleep(2000); ColumnList<String> result = keyspace.prepareQuery(CF_TTL) .getRow("row") .execute().getResult(); Assert.assertEquals(2, result.size()); Assert.assertNotNull(result.getColumnByName("TTL0")); Assert.assertNotNull(result.getColumnByName("TTLNULL")); } }
@Override public ObjectMetadata readMetadata(String objectName) throws Exception, NotFoundException { ColumnList<String> columns = keyspace.prepareQuery(cf).getKey(objectName).execute().getResult(); if (columns.isEmpty()) { throw new NotFoundException(objectName); } return new ObjectMetadata().setObjectSize(columns.getLongValue(getColumnName(Columns.OBJECTSIZE), null)) .setChunkSize(columns.getIntegerValue(getColumnName(Columns.CHUNKSIZE), null)) .setChunkCount(columns.getIntegerValue(getColumnName(Columns.CHUNKCOUNT), null)) .setAttributes(columns.getStringValue(getColumnName(Columns.ATTRIBUTES), null)); }
@Override public ObjectMetadata readMetadata(String objectName) throws Exception, NotFoundException { ColumnList<String> columns = keyspace.prepareQuery(cf).getKey(objectName).execute().getResult(); if (columns.isEmpty()) { throw new NotFoundException(objectName); } return new ObjectMetadata().setObjectSize(columns.getLongValue(getColumnName(Columns.OBJECTSIZE), null)) .setChunkSize(columns.getIntegerValue(getColumnName(Columns.CHUNKSIZE), null)) .setChunkCount(columns.getIntegerValue(getColumnName(Columns.CHUNKCOUNT), null)) .setAttributes(columns.getStringValue(getColumnName(Columns.ATTRIBUTES), null)); }