/** * Creates a new {@link CassandraColumnName} instance. * * @param family Cassandra column family, non-null. * @param qualifier Cassandra column qualifier, nullable (must be null if family is null). */ public CassandraColumnName(ByteBuffer family, ByteBuffer qualifier) { Preconditions.checkNotNull(family, "Family must not be null."); mFamily = ByteUtils.toBytes(family); mQualifier = qualifier == null ? null : ByteUtils.toBytes(qualifier); }
/** {@inheritDoc} */ @Override public SimpleEntry<String, byte[]> next() { Row next = mRowIterator.next(); String key = next.getString(KEY_COLUMN); byte[] value = ByteUtils.toBytes(next.getBytes(VALUE_COLUMN)); return new SimpleEntry<String, byte[]>(key, value); }
/** {@inheritDoc} */ @Override public List<byte[]> getValues(String table, String key, int numVersions) throws IOException { final List<Row> rows = getRows(table, key, numVersions); if (rows.isEmpty()) { throw new IOException(String.format( "Could not find any values associated with table %s and key %s", table, key)); } // Convert result into a list of bytes final List<byte[]> values = Lists.newArrayList(); for (Row row: rows) { final ByteBuffer blob = row.getBytes(KV_COLUMN_VALUE); values.add(ByteUtils.toBytes(blob)); } return values; }
/** {@inheritDoc} */ @Override public NavigableMap<Long, byte[]> getTimedValues(String table, String key, int numVersions) throws IOException { List<Row> rows = getRows(table, key, numVersions); if (0 == rows.size()) { throw new IOException(String.format( "Could not find any values associated with table %s and key %s", table, key)); } // Convert result into a map from timestamps to values final NavigableMap<Long, byte[]> timedValues = Maps.newTreeMap(); for (Row row: rows) { ByteBuffer blob = row.getBytes(KV_COLUMN_VALUE); final byte[] bytes = ByteUtils.toBytes(blob); Long timestamp = row.getDate(KV_COLUMN_TIME).getTime(); Preconditions.checkState(timedValues.put(timestamp, bytes) == null); } return timedValues; }
/** {@inheritDoc} */ @Override public List<FijiTableLayout> getTableLayoutVersions(String table, int numVersions) throws IOException { Preconditions.checkArgument(numVersions >= 1, "numVersions must be positive"); final List<Row> rows = getRows(table, numVersions); // Convert result into a list of bytes final List<FijiTableLayout> layouts = Lists.newArrayList(); for (Row row: rows) { ByteBuffer blob = row.getBytes(QUALIFIER_LAYOUT); byte[] bytes = ByteUtils.toBytes(blob); layouts.add(FijiTableLayout.newLayout(decodeTableLayoutDesc(bytes))); } return layouts; }
/** {@inheritDoc} */ @Override public byte[] getValue(String key) throws IOException { final State state = mState.get(); Preconditions.checkState(state == State.OPEN, "Cannot get value from SystemTable instance in state %s.", state); ResultSet resultSet = mAdmin.execute(mPreparedStatementGetValue.bind(key)); // Extra the value from the byte buffer, otherwise return this empty buffer // TODO: Some additional sanity checks here? List<Row> rows = resultSet.all(); Preconditions.checkArgument( rows.size() <= 1, "Expected to get 0 or 1 rows from system table, but got %s.", rows); if (rows.size() == 1) { Row row = rows.get(0); return ByteUtils.toBytes(row.getBytes(VALUE_COLUMN)); } return null; }
/** {@inheritDoc} */ @Override public NavigableMap<Long, FijiTableLayout> getTimedTableLayoutVersions( String table, int numVersions ) throws IOException { Preconditions.checkArgument(numVersions >= 1, "numVersions must be positive"); final List<Row> rows = getRows(table, numVersions); // Convert result into a map from timestamps to values final NavigableMap<Long, FijiTableLayout> timedValues = Maps.newTreeMap(); for (Row row: rows) { ByteBuffer blob = row.getBytes(QUALIFIER_LAYOUT); byte[] bytes = ByteUtils.toBytes(blob); FijiTableLayout layout = FijiTableLayout.newLayout(decodeTableLayoutDesc(bytes)); Long timestamp = row.getDate(QUALIFIER_TIME).getTime(); Preconditions.checkState(timedValues.put(timestamp, layout) == null); } return timedValues; }
/** * Fetches a schema entry from the tables given a schema hash. * * @param schemaHash schema hash * @return Avro schema entry, or null if the schema hash does not exist in the table * @throws IOException on I/O error. */ private SchemaTableEntry loadFromHashTable(BytesKey schemaHash) throws IOException { final ByteBuffer tableKey = ByteBuffer.wrap(schemaHash.getBytes()); final ResultSet resultSet = mAdmin.execute(mPreparedStatementReadHashTable.bind(tableKey)); final List<Row> rows = resultSet.all(); if (0 == rows.size()) { return null; } assert(rows.size() == 1); final byte[] schemaAsBytes = ByteUtils.toBytes(rows.get(0).getBytes(SCHEMA_COLUMN_VALUE)); return decodeSchemaEntry(schemaAsBytes); }
/** {@inheritDoc} */ @Override public TokenRowKeyComponents apply(final Row row) { final int token = row.getInt(mTokenColumn); final Object[] components = new Object[] { ByteUtils.toBytes(row.getBytes(CQLUtils.RAW_KEY_COL)) }; return new TokenRowKeyComponents(token, FijiRowKeyComponents.fromComponents(components)); } }
/** {@inheritDoc} */ @Override public FijiCell<T> apply(final Row row) { try { final DecodedCell<T> decodedCell = mCellDecoder.decodeCell(ByteUtils.toBytes(row.getBytes(CQLUtils.VALUE_COL))); return FijiCell.create(mColumnName, row.getLong(CQLUtils.VERSION_COL), decodedCell); } catch (IOException e) { throw new FijiIOException(e); } } }
/** {@inheritDoc} */ @Override public TableLayoutsBackup layoutsToBackup(String table) throws IOException { final List<TableLayoutBackupEntry> history = Lists.newArrayList(); final TableLayoutsBackup backup = TableLayoutsBackup.newBuilder().setLayouts(history).build(); final List<Row> rows = getRows(table, Integer.MAX_VALUE); if (rows.isEmpty()) { LOG.info(String.format( "There is no row in the MetaTable named '%s' or the row is empty.", table)); return backup; } for (Row row: rows) { final long timestamp = row.getDate(QUALIFIER_TIME).getTime(); final TableLayoutDesc layout = decodeTableLayoutDesc(ByteUtils.toBytes(row.getBytes(QUALIFIER_LAYOUT))); // TODO: May need some check here that the update is not null final TableLayoutDesc update = decodeTableLayoutDesc(ByteUtils.toBytes(row.getBytes(QUALIFIER_UPDATE))); history.add(TableLayoutBackupEntry.newBuilder() .setLayout(layout) .setUpdate(update) .setTimestamp(timestamp) .build()); } return backup; }
new CassandraColumnName( mFamilyColumn.getFamily(), ByteUtils.toBytes(qualifier))); mLastDecoder = mDecoderProvider.getDecoder(mLastColumn); } catch (NoSuchColumnException e) { final DecodedCell<T> decodedCell = mLastDecoder.decodeCell( ByteUtils.toBytes(row.getBytes(CQLUtils.VALUE_COL))); return FijiCell.create(mLastColumn, version, decodedCell); } catch (IOException e) {
new CassandraColumnName( mFamilyColumn.getFamily(), ByteUtils.toBytes(row.getBytes(CQLUtils.QUALIFIER_COL)))); } catch (NoSuchColumnException e) { final DecodedCell<T> decodedCell = mCellDecoder.decodeCell( ByteUtils.toBytes(row.getBytes(CQLUtils.VALUE_COL))); return FijiCell.create(mLastColumn, version, decodedCell); } catch (IOException e) {
/** * Fetches a schema entry from the tables given a schema ID. * * @param schemaId schema ID * @return Avro schema entry, or null if the schema ID does not exist in the table * @throws IOException on I/O error. */ private SchemaTableEntry loadFromIdTable(long schemaId) throws IOException { final Select select = QueryBuilder .select(SCHEMA_COLUMN_VALUE) .from(mSchemaIdTable.getKeyspace(), mSchemaIdTable.getTable()) .where(eq(SCHEMA_COLUMN_ID_KEY, schemaId)) .orderBy(desc(SCHEMA_COLUMN_TIME)) .limit(1); final ResultSet resultSet = mAdmin.execute(select); final List<Row> rows = resultSet.all(); if (0 == rows.size()) { return null; } assert(rows.size() == 1); final byte[] schemaAsBytes = ByteUtils.toBytes(rows.get(0).getBytes(SCHEMA_COLUMN_VALUE)); return decodeSchemaEntry(schemaAsBytes); }