/** * Construct a new Formatted Entity ID factory. * * @param format The row key format required for creating the factory. */ private FormattedEntityIdFactory(RowKeyFormat2 format) { Preconditions.checkNotNull(format); Preconditions.checkArgument(format.getEncoding() == RowKeyEncoding.FORMATTED); mRowKeyFormat = format; }
/** * Creates a RawEntityIdFactory. * * @param format Row key format. */ private RawEntityIdFactory(RowKeyFormat2 format) { Preconditions.checkNotNull(format); Preconditions.checkArgument(format.getEncoding() == RowKeyEncoding.RAW); }
/** * Find the encoding of the row key given the format. * * @param rowKeyFormat Format of row keys of type RowKeyFormat or RowKeyFormat2. * @return The specific row key encoding, e.g. RAW, HASH, etc. */ public static RowKeyEncoding getEncoding(Object rowKeyFormat) { if (rowKeyFormat instanceof RowKeyFormat) { return ((RowKeyFormat) rowKeyFormat).getEncoding(); } else if (rowKeyFormat instanceof RowKeyFormat2) { return ((RowKeyFormat2) rowKeyFormat).getEncoding(); } else { throw new RuntimeException("Unsupported Row Key Format"); } }
/** * Gets row key encoding of a row key format. * * @param keysFormat row key format. * @return row key encoding. * @throws IOException if row key format is unrecognized. */ private static RowKeyEncoding getEncoding(final Object keysFormat) throws IOException { if (keysFormat instanceof RowKeyFormat) { return ((RowKeyFormat) keysFormat).getEncoding(); } else if (keysFormat instanceof RowKeyFormat2) { return ((RowKeyFormat2) keysFormat).getEncoding(); } else { throw new IOException( String.format("Unrecognized row key format: %s", keysFormat.getClass())); } }
/** * Gets row key encoding of a row key format. * * @param keysFormat row key format. * @return row key encoding. * @throws IOException if row key format is unrecognized. */ private static RowKeyEncoding getEncoding(final Object keysFormat) throws IOException { if (keysFormat instanceof RowKeyFormat) { return ((RowKeyFormat) keysFormat).getEncoding(); } else if (keysFormat instanceof RowKeyFormat2) { return ((RowKeyFormat2) keysFormat).getEncoding(); } else { throw new IOException( String.format("Unrecognized row key format: %s", keysFormat.getClass())); } }
/** * Get the entity ID component values from an Entity ID. * * @param entityID The entity ID. * @return The entity ID's component values. */ private List<Object> getEntityIDComponents( final EntityId entityID ) { switch (mRowKeyFormat.getEncoding()) { case RAW: { return ImmutableList.<Object>of(ByteBuffer.wrap(entityID.getHBaseRowKey())); } case FORMATTED: { return entityID.getComponents(); } default: throw new IllegalArgumentException( String.format("Unknown row key encoding %s.", mRowKeyFormat.getEncoding())); } }
/** * Creates an entity ID factory for the specified row key format. * * @param format Row key format of type RowKeyFormat2 that determines * the type of EntityIdFactory that's created. * @return a new entity ID factory for the specified row key format. */ public static EntityIdFactory getFactory(RowKeyFormat2 format) { Preconditions.checkNotNull(format); // HASH and HASH_PREFIX encoding is legal here, but not supported by RowKeyFormat2. switch (format.getEncoding()) { case RAW: return new RawEntityIdFactory(format); case FORMATTED: return new FormattedEntityIdFactory(format); case HASH: case HASH_PREFIX: throw new RuntimeException( "Row key format encodings HASH and HASH_PREFIX are not supported with RowKeyFormat2" + " specifications. Use FORMATTED instead."); default: throw new RuntimeException(String.format("Unhandled row key format: '%s'.", format)); } }
/** * Return the ordered list of columns in the partition key for the table layout. * * @param layout to return partition key columns for. * @return the primary key columns for the layout. */ public static List<String> getPartitionKeyColumns(FijiTableLayout layout) { RowKeyFormat2 keyFormat = (RowKeyFormat2) layout.getDesc().getKeysFormat(); switch (keyFormat.getEncoding()) { case RAW: return Lists.newArrayList(RAW_KEY_COL); case FORMATTED: return transformToColumns( keyFormat.getComponents().subList(0, keyFormat.getRangeScanStartIndex())); default: throw new IllegalArgumentException( String.format("Unknown row key encoding %s.", keyFormat.getEncoding())); } }
/** * Get a function for decoding row keys and tokens from Cassandra rows. * * @param layout The table layout. * @return A function to decode row keys and tokens for the table. */ public static Function<Row, TokenRowKeyComponents> getRowKeyDecoderFunction( final FijiTableLayout layout ) { final RowKeyFormat2 keyFormat = (RowKeyFormat2) layout.getDesc().getKeysFormat(); switch (keyFormat.getEncoding()) { case RAW: return new RawRowKeyDecoder(layout); case FORMATTED: return new FormattedRowKeyDecoder(layout); default: throw new IllegalArgumentException( String.format("Unknown row key encoding %s.", keyFormat.getEncoding())); } }
mRowKeyFormat = rowKeyFormat; switch (rowKeyFormat.getEncoding()) { case RAW: { mEntityIDColumns = ImmutableList.of(CQLUtils.RAW_KEY_COL); String.format("Unknown row key encoding %s.", mRowKeyFormat.getEncoding()));
/** * Creates a new FormattedEntityId. * @param format Format of the row key as specified in the layout file. * @param hbaseRowKey Byte array containing the hbase row key. * @param fijiRowKey An ordered list of row key components. */ private FormattedEntityId(RowKeyFormat2 format, byte[] hbaseRowKey, List<Object> fijiRowKey) { mRowKeyFormat = Preconditions.checkNotNull(format); Preconditions.checkArgument(format.getEncoding() == RowKeyEncoding.FORMATTED); Preconditions.checkNotNull(format.getSalt(), "Formatted entityIds may not specify a null 'salt' field in RowKeyFormat2."); mHBaseRowKey = hbaseRowKey; if (format.getSalt().getSuppressKeyMaterialization()) { mComponentValues = null; } else { mComponentValues = fijiRowKey; } }
/** * Get the names and types of Entity ID columns in the Cassandra table. * * @param layout The table layout. * @return The names and types of Entity ID columns. */ private static LinkedHashMap<String, String> getEntityIdColumnTypes( final FijiTableLayout layout ) { LinkedHashMap<String, String> columns = Maps.newLinkedHashMap(); RowKeyFormat2 keyFormat = (RowKeyFormat2) layout.getDesc().getKeysFormat(); switch (keyFormat.getEncoding()) { case RAW: { columns.put(RAW_KEY_COL, BYTES_TYPE); break; } case FORMATTED: { for (RowKeyComponent component : keyFormat.getComponents()) { columns.put( translateEntityIDComponentNameToColumnName(component.getName()), getCQLType(component.getType())); } break; } default: throw new IllegalArgumentException( String.format("Unknown row key encoding %s.", keyFormat.getEncoding())); } return columns; }
/** * Get the ordered list of cluster columns originating from the entity ID. This is the set of * 'scannable' entity ID components. * * @param layout The layou of the table. * @return the cluster columns of the table from the entity ID. */ private static List<String> getEntityIdClusterColumns(FijiTableLayout layout) { RowKeyFormat2 keyFormat = (RowKeyFormat2) layout.getDesc().getKeysFormat(); switch (keyFormat.getEncoding()) { case RAW: { return Lists.newArrayList(); } case FORMATTED: { int size = keyFormat.getComponents().size(); int start = keyFormat.getRangeScanStartIndex(); if (start == size) { return Lists.newArrayList(); } else { return transformToColumns( keyFormat .getComponents() .subList(keyFormat.getRangeScanStartIndex(), keyFormat.getComponents().size())); } } default: throw new IllegalArgumentException( String.format("Unknown row key encoding %s.", keyFormat.getEncoding())); } }
Preconditions.checkArgument(rowKeyFormat.getEncoding().equals(RowKeyEncoding.FORMATTED), "FormattedEntityIdRowFilter only works with formatted entity IDs"); if (null != rowKeyFormat.getSalt()) {
/** * Parses a Fiji row key specification from a command-line flag. * * @param rowKeySpec Fiji row key specification. * @param factory Factory for entity IDs. * @param layout Layout of the table to parse the entity ID of. * @return the parsed entity ID. * @throws IOException on I/O error. */ public static EntityId parseFijiRowKey( String rowKeySpec, EntityIdFactory factory, FijiTableLayout layout) throws IOException { final Object keysFormat = layout.getDesc().getKeysFormat(); if (keysFormat instanceof RowKeyFormat) { // Former, deprecated, unformatted row key specification: return factory.getEntityId(rowKeySpec); } else if (keysFormat instanceof RowKeyFormat2) { final RowKeyFormat2 format = (RowKeyFormat2) keysFormat; switch (format.getEncoding()) { case RAW: return factory.getEntityIdFromHBaseRowKey(parseBytesFlag(rowKeySpec)); case FORMATTED: return parseJsonFormattedKeySpec(rowKeySpec, format, factory); default: throw new RuntimeException(String.format( "Invalid layout for table '%s' with unsupported keys format: '%s'.", layout.getName(), format)); } } else { throw new RuntimeException(String.format("Unknown row key format: '%s'.", keysFormat)); } }
if (format.getEncoding() != RowKeyEncoding.RAW && format.getEncoding() != RowKeyEncoding.FORMATTED) { throw new InvalidLayoutException("RowKeyFormat2 only supports RAW or FORMATTED encoding." + "Found " + format.getEncoding().name()); if (format.getEncoding() == RowKeyEncoding.RAW) { return;