latestEntityColumns = keyspace.prepareQuery( CF_ENTITY_LOG ).getKeySlice( rowKeys ) .withColumnRange( maxVersion, null, false, 1 ).execute().getResult() .iterator();
final Iterator<Row<ScopedRowKey<Id>, Boolean>> latestEntityColumns = rows.iterator();
rows.iterator();
return new RowIterator(result.iterator(), sliceQuery);
@Override public KeyIterator getKeys(KeyRangeQuery query, StoreTransaction txh) throws BackendException { // this query could only be done when byte-ordering partitioner is used // because Cassandra operates on tokens internally which means that even contiguous // range of keys (e.g. time slice) with random partitioner could produce disjoint set of tokens // returning ambiguous results to the user. Partitioner partitioner = storeManager.getPartitioner(); if (partitioner != Partitioner.BYTEORDER) throw new PermanentBackendException("getKeys(KeyRangeQuery could only be used with byte-ordering partitioner."); ByteBuffer start = query.getKeyStart().asByteBuffer(), end = query.getKeyEnd().asByteBuffer(); RowSliceQuery rowSlice = keyspace.prepareQuery(columnFamily) .setConsistencyLevel(getTx(txh).getReadConsistencyLevel().getAstyanax()) .withRetryPolicy(retryPolicy.duplicate()) .getKeyRange(start, end, null, null, Integer.MAX_VALUE); // Astyanax is bad at builder pattern :( rowSlice.withColumnRange(query.getSliceStart().asByteBuffer(), query.getSliceEnd().asByteBuffer(), false, query.getLimit()); // Omit final the query's keyend from the result, if present in result final Rows<ByteBuffer, ByteBuffer> r; try { r = ((OperationResult<Rows<ByteBuffer, ByteBuffer>>) rowSlice.execute()).getResult(); } catch (ConnectionException e) { throw new TemporaryBackendException(e); } Iterator<Row<ByteBuffer, ByteBuffer>> i = Iterators.filter(r.iterator(), new KeySkipPredicate(query.getKeyEnd().asByteBuffer())); return new RowIterator(i, query); }
private <K, N, V> List<CmbRow<K, N, V>> getRows(Rows<K, N> rows) throws PersistenceException { List<CmbRow<K, N, V>> l = new ArrayList<CmbRow<K, N, V>>(); Iterator<Row<K, N>> iter = rows.iterator(); while (iter.hasNext()) { Row<K, N> r = iter.next(); l.add(new CmbAstyanaxRow<K, N, V>(r)); } return l; }
public List<FilterInfo> getFiltersForIndex(String index) { List<String> filterInfoList = getFilterIdsIndex(index); if (filterInfoList.isEmpty()) { return Collections.emptyList(); } Rows<String, String> result = cassandraGateway.getByFilterIds(filterInfoList); if (result == null || result.isEmpty()) { return Collections.emptyList(); } else { List<FilterInfo> filterInfos = new ArrayList<FilterInfo>(); Iterator<Row<String, String>> rows = result.iterator(); while (rows.hasNext()) { Row<String, String> row = rows.next(); FilterInfo script = getFilterScriptFromCassandraRow(row); if (script != null) { filterInfos.add(script); } } Collections.sort(filterInfos); return filterInfos; } }
public List<FilterInfo> getFiltersForIndex(String index) { List<String> filterInfoList = getFilterIdsIndex(index); if (filterInfoList.isEmpty()) { return Collections.emptyList(); } Rows<String, String> result = cassandraGateway.getByFilterIds(filterInfoList); if (result == null || result.isEmpty()) { return Collections.emptyList(); } else { List<FilterInfo> filterInfos = new ArrayList<FilterInfo>(); Iterator<Row<String, String>> rows = result.iterator(); while (rows.hasNext()) { Row<String, String> row = rows.next(); FilterInfo script = getFilterScriptFromCassandraRow(row); if (script != null) { filterInfos.add(script); } } Collections.sort(filterInfos); return filterInfos; } }
@SuppressWarnings("unchecked") @Test public void testGetFilterIdsRawIndex() { ZuulFilterDAO dao = new ZuulFilterDAOCassandra(gateway); // setup empty response String fids = "filter1|filter2|filter3"; /* create mock response data */ Row<String, String> row0 = mockRow(); ColumnList<String> columnList0 = mockColumnList(row0); mockColumn(columnList0, "filter_ids", fids); // when(response.getRowByIndex(0)).thenReturn(row0); Iterator<Row<String, String>> iterator = (Iterator<Row<String, String>>) mock(Iterator.class); when(response.iterator()).thenReturn(iterator); when(iterator.hasNext()).thenReturn(true, false); // 1 row when(iterator.next()).thenReturn(row0, (Row) null); when(gateway.select(anyString())).thenReturn(response); when(response.isEmpty()).thenReturn(false); when(response.size()).thenReturn(1); /* exercise the method we're testing */ String list = dao.getFilterIdsRaw("index"); /* validate responses */ assertEquals(fids, list); }
@SuppressWarnings("unchecked") @Test public void testGetFilterIdsRawIndex() { ZuulFilterDAO dao = new ZuulFilterDAOCassandra(gateway); // setup empty response String fids = "filter1|filter2|filter3"; /* create mock response data */ Row<String, String> row0 = mockRow(); ColumnList<String> columnList0 = mockColumnList(row0); mockColumn(columnList0, "filter_ids", fids); // when(response.getRowByIndex(0)).thenReturn(row0); Iterator<Row<String, String>> iterator = (Iterator<Row<String, String>>) mock(Iterator.class); when(response.iterator()).thenReturn(iterator); when(iterator.hasNext()).thenReturn(true, false); // 1 row when(iterator.next()).thenReturn(row0, (Row) null); when(gateway.select(anyString())).thenReturn(response); when(response.isEmpty()).thenReturn(false); when(response.size()).thenReturn(1); /* exercise the method we're testing */ String list = dao.getFilterIdsRaw("index"); /* validate responses */ assertEquals(fids, list); }
/** * We can't unit test the CQL query or how Cassandra will behave, that will have to be manually confirmed. * We can however test that whatever rows are returned do end up in the List<EndpointURI> that we expect. */ @SuppressWarnings("unchecked") @Test public void testGetAllEndpointsReturnsResults() { ZuulFilterDAO dao = new ZuulFilterDAOCassandra(gateway); // setup empty response String fids = "filter1|filter2"; /* create mock response data */ Row<String, String> row0 = mockRow(); ColumnList<String> columnList0 = mockColumnList(row0); mockColumn(columnList0, "filter_ids", fids); // when(response.getRowByIndex(0)).thenReturn(row0); Iterator<Row<String, String>> iterator = (Iterator<Row<String, String>>) mock(Iterator.class); when(response.iterator()).thenReturn(iterator); when(iterator.hasNext()).thenReturn(true, false); // 1 row when(iterator.next()).thenReturn(row0, (Row) null); when(gateway.select(anyString())).thenReturn(response); when(response.isEmpty()).thenReturn(false); when(response.size()).thenReturn(1); /* exercise the method we're testing */ List<String> list = dao.getAllFilterIDs(); /* validate responses */ assertEquals("filter1", list.get(0)); assertEquals("filter2", list.get(1)); }
@SuppressWarnings("unchecked") @Test public void testGetFilterIdsIndex() { ZuulFilterDAO dao = new ZuulFilterDAOCassandra(gateway); // setup empty response String fids = "filter1|filter2|filter3"; /* create mock response data */ Row<String, String> row0 = mockRow(); ColumnList<String> columnList0 = mockColumnList(row0); mockColumn(columnList0, "filter_ids", fids); // when(response.getRowByIndex(0)).thenReturn(row0); Iterator<Row<String, String>> iterator = (Iterator<Row<String, String>>) mock(Iterator.class); when(response.iterator()).thenReturn(iterator); when(iterator.hasNext()).thenReturn(true, false); // 1 row when(iterator.next()).thenReturn(row0, (Row) null); when(gateway.select(anyString())).thenReturn(response); when(response.isEmpty()).thenReturn(false); when(response.size()).thenReturn(1); /* exercise the method we're testing */ List<String> list = dao.getFilterIdsIndex("index"); /* validate responses */ assertEquals(list.size(), 3); assertEquals(list.get(0), "filter1"); assertEquals(list.get(1), "filter2"); assertEquals(list.get(2), "filter3"); }
@SuppressWarnings("unchecked") @Test public void testGetFilterIdsIndex() { ZuulFilterDAO dao = new ZuulFilterDAOCassandra(gateway); // setup empty response String fids = "filter1|filter2|filter3"; /* create mock response data */ Row<String, String> row0 = mockRow(); ColumnList<String> columnList0 = mockColumnList(row0); mockColumn(columnList0, "filter_ids", fids); // when(response.getRowByIndex(0)).thenReturn(row0); Iterator<Row<String, String>> iterator = (Iterator<Row<String, String>>) mock(Iterator.class); when(response.iterator()).thenReturn(iterator); when(iterator.hasNext()).thenReturn(true, false); // 1 row when(iterator.next()).thenReturn(row0, (Row) null); when(gateway.select(anyString())).thenReturn(response); when(response.isEmpty()).thenReturn(false); when(response.size()).thenReturn(1); /* exercise the method we're testing */ List<String> list = dao.getFilterIdsIndex("index"); /* validate responses */ assertEquals(list.size(), 3); assertEquals(list.get(0), "filter1"); assertEquals(list.get(1), "filter2"); assertEquals(list.get(2), "filter3"); }
@Override public String getFilterIdsRaw(String index) { Rows<String, String> result = cassandraGateway.select("select filter_ids from zuul_filter_indices where index_name = '" + index + "'"); if (result == null || result.isEmpty()) { return ""; } else { Iterator<Row<String, String>> iterator = result.iterator(); if (iterator.hasNext()) { Row<String, String> row = iterator.next(); try { String filter_ids = row.getColumns().getColumnByName("filter_ids").getStringValue(); if (filter_ids == null) return ""; return filter_ids; } catch (Exception e) { // unable to retrieve data for this row, could be missing the uri column (which shouldn't happen) logger.warn("Unable to retrieve uri for row", e); } } return ""; } }
public String getFilterIdsRaw(String index) { Rows<String, String> result = cassandraGateway.select("select filter_ids from zuul_filter_indices where index_name = '" + index + "'"); if (result == null || result.isEmpty()) { return ""; } else { Iterator<Row<String, String>> iterator = result.iterator(); if (iterator.hasNext()) { Row<String, String> row = iterator.next(); try { String filter_ids = row.getColumns().getColumnByName("filter_ids").getStringValue(); if (filter_ids == null) return ""; return filter_ids; } catch (Exception e) { // unable to retrieve data for this row, could be missing the uri column (which shouldn't happen) logger.warn("Unable to retrieve uri for row", e); } } return ""; } }
@Override public Iterator<String> listChannels() { final Iterator<Row<String, ByteBuffer>> rowIter = execute( _keyspace.prepareQuery(ColumnFamilies.MANIFEST, ConsistencyLevel.CL_LOCAL_QUORUM) .getAllRows() .setRowLimit(1000) .withColumnRange(new RangeBuilder().setLimit(1).build())) .iterator(); return new AbstractIterator<String>() { @Override protected String computeNext() { while (rowIter.hasNext()) { Row<String, ByteBuffer> row = rowIter.next(); if (!row.getColumns().isEmpty()) { return row.getKey(); } } return endOfData(); } }; }
@Override public Iterator<String> listChannels() { final Iterator<Row<String, ByteBuffer>> rowIter = execute( _keyspace.prepareQuery(ColumnFamilies.MANIFEST, ConsistencyLevel.CL_LOCAL_QUORUM) .getAllRows() .setRowLimit(1000) .withColumnRange(new RangeBuilder().setLimit(1).build())) .iterator(); return new AbstractIterator<String>() { @Override protected String computeNext() { while (rowIter.hasNext()) { Row<String, ByteBuffer> row = rowIter.next(); if (!row.getColumns().isEmpty()) { return row.getKey(); } } return endOfData(); } }; }
@Override public Iterator<String> listQueues() { final Iterator<Row<String, UUID>> rowIter = execute( _keyspace.prepareQuery(CF_DEDUP_MD, ConsistencyLevel.CL_LOCAL_QUORUM) .getAllRows() .setRowLimit(100) .withColumnRange(new RangeBuilder().setLimit(1).build())) .iterator(); return new AbstractIterator<String>() { @Override protected String computeNext() { while (rowIter.hasNext()) { Row<String, UUID> row = rowIter.next(); if (!row.getColumns().isEmpty()) { return row.getKey(); } } return endOfData(); } }; }
@Override public Iterator<String> listQueues() { final Iterator<Row<String, UUID>> rowIter = execute( _keyspace.prepareQuery(CF_DEDUP_MD, ConsistencyLevel.CL_LOCAL_QUORUM) .getAllRows() .setRowLimit(100) .withColumnRange(new RangeBuilder().setLimit(1).build())) .iterator(); return new AbstractIterator<String>() { @Override protected String computeNext() { while (rowIter.hasNext()) { Row<String, UUID> row = rowIter.next(); if (!row.getColumns().isEmpty()) { return row.getKey(); } } return endOfData(); } }; }
@Override public KeyIterator getKeys(KeyRangeQuery query, StoreTransaction txh) throws BackendException { // this query could only be done when byte-ordering partitioner is used // because Cassandra operates on tokens internally which means that even contiguous // range of keys (e.g. time slice) with random partitioner could produce disjoint set of tokens // returning ambiguous results to the user. Partitioner partitioner = storeManager.getPartitioner(); if (partitioner != Partitioner.BYTEORDER) throw new PermanentBackendException("getKeys(KeyRangeQuery could only be used with byte-ordering partitioner."); ByteBuffer start = query.getKeyStart().asByteBuffer(), end = query.getKeyEnd().asByteBuffer(); RowSliceQuery rowSlice = keyspace.prepareQuery(columnFamily) .setConsistencyLevel(getTx(txh).getReadConsistencyLevel().getAstyanax()) .withRetryPolicy(retryPolicy.duplicate()) .getKeyRange(start, end, null, null, Integer.MAX_VALUE); // Astyanax is bad at builder pattern :( rowSlice.withColumnRange(query.getSliceStart().asByteBuffer(), query.getSliceEnd().asByteBuffer(), false, query.getLimit()); // Omit final the query's keyend from the result, if present in result final Rows<ByteBuffer, ByteBuffer> r; try { r = ((OperationResult<Rows<ByteBuffer, ByteBuffer>>) rowSlice.execute()).getResult(); } catch (ConnectionException e) { throw new TemporaryBackendException(e); } Iterator<Row<ByteBuffer, ByteBuffer>> i = Iterators.filter(r.iterator(), new KeySkipPredicate(query.getKeyEnd().asByteBuffer())); return new RowIterator(i, query); }