@Override public int getSystemVersion() { try { return keyspace.prepareQuery( CF_MIGRATION_INFO ).getKey( LEGACY_ROW_KEY ).getColumn( COLUMN_VERSION ) .execute().getResult().getIntegerValue(); } //swallow, it doesn't exist catch ( NotFoundException nfe ) { return 0; } catch ( ConnectionException e ) { throw new DataMigrationException( "Unable to retrieve status", e ); } }
@Override public Optional<Long> getMaxVersion( final ApplicationScope scope, final Id node ) { ValidationUtils.validateApplicationScope( scope ); ValidationUtils.verifyIdentity( node ); ColumnFamilyQuery<ScopedRowKey<Id>, Boolean> query = keyspace.prepareQuery( GRAPH_DELETE ).setConsistencyLevel( fig.getReadCL() ); Column<Boolean> result = null; try { result = query.getKey( ScopedRowKey.fromKey( scope.getApplication(), node ) ).getColumn( COLUMN_NAME ).execute() .getResult(); } catch(NotFoundException nfe){ //swallow, there's just no column return Optional.absent(); } catch ( ConnectionException e ) { throw new RuntimeException( "Unable to connect to casandra", e ); } return Optional.of( result.getLongValue() ); }
@Override public String getStatusMessage(final String pluginName) { final ScopedRowKey<String> rowKey = ScopedRowKey.fromKey( STATIC_ID, pluginName); try { return keyspace.prepareQuery( CF_MIGRATION_INFO ).getKey( rowKey ).getColumn( COL_STATUS_MESSAGE ) .execute().getResult().getStringValue(); } //swallow, it doesn't exist catch ( NotFoundException nfe ) { return null; } catch ( ConnectionException e ) { throw new DataMigrationException( "Unable to retrieve status", e ); } }
@Override public int getStatusCode(final String pluginName) { final ScopedRowKey<String> rowKey = ScopedRowKey.fromKey( STATIC_ID, pluginName); try { return keyspace.prepareQuery( CF_MIGRATION_INFO ).getKey( rowKey ).getColumn( COLUMN_STATUS_CODE ) .execute().getResult().getIntegerValue(); } //swallow, it doesn't exist catch ( NotFoundException nfe ) { return 0; } catch ( ConnectionException e ) { throw new DataMigrationException( "Unable to retrieve status", e ); } }
@Override public int getVersion(final String pluginName) { final ScopedRowKey<String> rowKey = ScopedRowKey.fromKey( STATIC_ID, pluginName); try { return keyspace.prepareQuery( CF_MIGRATION_INFO ).getKey( rowKey ).getColumn( COLUMN_VERSION ).execute() .getResult().getIntegerValue(); } //swallow, it doesn't exist catch ( NotFoundException nfe ) { return 0; } catch ( ConnectionException e ) { AstyanaxUtils.isSchemaMissing("Unable to connect to cassandra to retrieve status", e); return 0; } }
Column<String> column = keyspace.prepareQuery( cf ).getKey( rowKey ).getColumn( colName ).execute().getResult(); column = keyspace.prepareQuery( cf ).getKey( rowKey ).getColumn( colName ).execute().getResult(); fail( "I shouldn't return a value" ); column = keyspace.prepareQuery( cf ).getKey( rowKey ).getColumn( colName ).execute().getResult(); fail( "I shouldn't return a value" );
Column<String> col = keyspace.prepareQuery( testCf ).getKey( key ).getColumn( colname ).execute().getResult(); keyspace.prepareQuery( testCf ).getKey( key ).getColumn( colname ).execute().getResult(); batch.execute(); col = keyspace.prepareQuery( testCf ).getKey( key ).getColumn( colname ).execute().getResult(); col = keyspace.prepareQuery( testCf ).getKey( key ).getColumn( colname ).execute().getResult(); keyspace.prepareQuery( testCf ).getKey( key ).getColumn( colname ).execute().getResult();
private void performSimpleRowSingleColumnQueryForRow(String rowKey, boolean rowDeleted, String expectedChar) throws Exception { Column<String> col = keyspace.prepareQuery(CF_ACCOUNTS).getRow(rowKey).getColumn("user").execute().getResult(); if (rowDeleted) { Assert.assertNull(col); } else { Assert.assertTrue(col.hasValue()); Assert.assertEquals("user" + expectedChar, col.getStringValue()); } col = keyspace.prepareQuery(CF_ACCOUNTS).getRow(rowKey).getColumn("pswd").execute().getResult(); if (rowDeleted) { Assert.assertNull(col); } else { Assert.assertTrue(col.hasValue()); Assert.assertEquals("pswd" + expectedChar, col.getStringValue()); } }
@Test public void testGetSingleColumnNotExists() throws ConnectionException { Column<String> column = keyspace.prepareQuery(CF_STANDARD1).getRow("A").getColumn("DoesNotExist").execute().getResult(); Assert.assertNull(column); }
private void testReadSingleRowSingleColumn(boolean rowDeleted) throws Exception { for (int year = 2001; year <= 2014; year++) { Column<Population> result = keyspace.prepareQuery(CF_POPULATION) .getRow(year) .getColumn(SanFrancisco.clone()) .execute().getResult(); if (rowDeleted) { Assert.assertNull(result); continue; } else { Assert.assertTrue(result.hasValue()); } Assert.assertEquals(SanFrancisco, result.getName()); } }
@Test public void testGetSingleKeyNotExists() throws Exception { Column<String> column = keyspace.prepareQuery(CF_STANDARD1).getRow("AA").getColumn("ab").execute().getResult(); Assert.assertNull(column); }
@Test public void testSingleRowSingleColumnQuery() throws Exception { for (int i=0; i<TestRowCount; i++) { Column<String> result = keyspace.prepareQuery(CF_USER_INFO) .withCaching(true) .getRow("acct_" + i) .getColumn("address") .execute() .getResult(); Assert.assertNotNull(result); Assert.assertEquals("address", result.getName()); Assert.assertNotNull(result.getStringValue()); Assert.assertEquals("john smith address " + i, result.getStringValue()); } }
@Override public String getCheckpoint(String startToken) throws ConnectionException { try { return keyspace.prepareQuery(columnFamily).getKey(bbKey).getColumn(startToken).execute().getResult().getStringValue(); } catch (NotFoundException e) { return startToken; } }
@Test public void testGetSingleColumn() throws Exception { keyspace.prepareColumnMutation(CF_STANDARD1, "A", "a").putValue(1, null).execute(); Column<String> column = keyspace.prepareQuery(CF_STANDARD1).getRow("A").getColumn("a").execute().getResult(); Assert.assertNotNull(column); Assert.assertEquals(1, column.getIntegerValue()); }
@Override public ResponseData get(Object rkey, Object colName) throws OperationException { StringBuffer response = new StringBuffer(); int bytes = 0; OperationResult<Column<Object>> opResult = null; try { opResult = AstyanaxConnection.instance.keyspace().prepareQuery(cfs).getKey(rkey).getColumn(colName).execute(); bytes = opResult.getResult().getRawName().capacity(); bytes += opResult.getResult().getByteBufferValue().capacity(); String value = SystemUtils.convertToString(valueSerializer, opResult.getResult().getByteBufferValue()); response.append(value); } catch (NotFoundException ex) { // ignore this because nothing is available to show response.append("...Not found..."); } catch (ConnectionException e) { throw new OperationException(e); } return new AstyanaxResponseData(response.toString(), bytes, opResult, rkey, colName, null); }
@Override public <K, N> long getCounter(String keyspace, String columnFamily, K rowKey, N columnName, CmbSerializer keySerializer, CmbSerializer columnNameSerializer) throws PersistenceException { long ts1 = System.currentTimeMillis(); logger.debug("event=get_counter column_family=" + columnFamily); try { Column<N> result = getKeyspace(keyspace). prepareQuery((ColumnFamily<K, N>)getColumnFamily(columnFamily)). getKey(rowKey). getColumn(columnName). execute(). getResult(); Long counterValue = result.getLongValue(); return counterValue; } catch (ConnectionException ex) { return 0; } finally { long ts2 = System.currentTimeMillis(); CMBControllerServlet.valueAccumulator.addToCounter(AccumulatorName.CassandraTime, (ts2 - ts1)); CMBControllerServlet.valueAccumulator.addToCounter(AccumulatorName.CassandraRead, 1L); } }
@Test public void testDelete() throws Exception { LOG.info("Starting testDelete..."); String rowKey = "DeleteMe_testDelete"; MutationBatch m = keyspace.prepareMutationBatch(); m.withRow(CF_STANDARD1, rowKey).putColumn("Column1", "X", null).putColumn("Column2", "X", null); m.execute(); Column<String> column = keyspace.prepareQuery(CF_STANDARD1).getRow(rowKey).getColumn("Column1").execute().getResult(); Assert.assertEquals("X", column.getStringValue()); m = keyspace.prepareMutationBatch(); m.withRow(CF_STANDARD1, rowKey).deleteColumn("Column1"); m.execute(); column = keyspace.prepareQuery(CF_STANDARD1).getRow(rowKey).getColumn("Column1").execute().getResult(); Assert.assertNull(column); LOG.info("... testDelete"); }
@Override public ByteBuffer readChunk(String objectName, int chunkId) throws Exception { return keyspace.prepareQuery(cf).setConsistencyLevel(readConsistencyLevel).withRetryPolicy(retryPolicy) .getKey(getRowKey(objectName, chunkId)).getColumn(getColumnName(Columns.DATA)).execute().getResult() .getByteBufferValue(); }
@Override public ByteBuffer readChunk(String objectName, int chunkId) throws Exception { return keyspace.prepareQuery(cf).setConsistencyLevel(readConsistencyLevel).withRetryPolicy(retryPolicy) .getKey(getRowKey(objectName, chunkId)).getColumn(getColumnName(Columns.DATA)).execute().getResult() .getByteBufferValue(); }
@Test public void testSerializedClassValue() throws Exception { UserInfo smo = new UserInfo(); smo.setLastName("Landau"); smo.setFirstName("Eran"); ByteBuffer bb = ObjectSerializer.get().toByteBuffer(smo); keyspace.prepareColumnMutation(CF_STANDARD1, "Key_SerializeTest", "Column1").putValue(bb, null).execute(); UserInfo smo2 = (UserInfo) keyspace.prepareQuery(CF_STANDARD1) .getKey("Key_SerializeTest").getColumn("Column1").execute() .getResult().getValue(ObjectSerializer.get()); Assert.assertEquals(smo, smo2); }