/** * Gets a table's row-key hash resolution (in number of bytes) * for use in evenly spacing HBase row keys. * * @param tableLayout the layout of the table * @return the table's hash resolution. */ public static int getRowKeyResolution(TableLayoutDesc tableLayout) { // Get hashSize from layout. int hashSize = 16; // No assumptions make about the RKF. if (RowKeyFormat.class.equals(tableLayout.getKeysFormat().getClass())) { hashSize = ((RowKeyFormat) tableLayout.getKeysFormat()).getHashSize(); } else if (RowKeyFormat2.class.equals(tableLayout.getKeysFormat().getClass())) { RowKeyFormat2 format = (RowKeyFormat2) tableLayout.getKeysFormat(); if (null == format.getSalt()) { throw new IllegalArgumentException( "This table layout defines an entityId format without hashing enabled."); } hashSize = ((RowKeyFormat2) tableLayout.getKeysFormat()).getSalt().getHashSize(); } return hashSize; }
/** * Gets the RowKeyFormat2 of the provided layout, if it exists. Otherwise, null. * * @param layout of the table to find the RowKeyFormat2. * @return the RowKeyFormat2, null if the layout has RowKeyFormat1. * @throws IOException if the keys format can not be ascertained. */ private static RowKeyFormat2 getRKF2(final FijiTableLayout layout) throws IOException { if (null != layout && RowKeyEncoding.FORMATTED == getEncoding(layout.getDesc().getKeysFormat())) { return (RowKeyFormat2) layout.getDesc().getKeysFormat(); } else { return null; } }
/** * Gets the RowKeyFormat2 of the provided layout, if it exists. Otherwise, null. * * @param layout of the table to find the RowKeyFormat2. * @return the RowKeyFormat2, null if the layout has RowKeyFormat1. * @throws IOException if the keys format can not be ascertained. */ private static RowKeyFormat2 getRKF2(final FijiTableLayout layout) throws IOException { if (null != layout && RowKeyEncoding.FORMATTED == getEncoding(layout.getDesc().getKeysFormat())) { return (RowKeyFormat2) layout.getDesc().getKeysFormat(); } else { return null; } }
/** * Create a new {@code FormattedRowKeyDecoder}. * * @param layout The table layout. */ private FormattedRowKeyDecoder(final FijiTableLayout layout) { mTokenColumn = CQLUtils.getTokenColumn(layout); mKeyFormat = (RowKeyFormat2) layout.getDesc().getKeysFormat(); }
/** * Creates an entity ID factory for the row key format given by the specified * table layout. * * @param fijiTableLayout the layout of the fiji table. * @return a new entity ID factory. */ public static EntityIdFactory getFactory(FijiTableLayout fijiTableLayout) { Object rowKeyFormat = fijiTableLayout.getDesc().getKeysFormat(); if (rowKeyFormat instanceof RowKeyFormat) { return getFactory((RowKeyFormat) rowKeyFormat); } else if (rowKeyFormat instanceof RowKeyFormat2) { return getFactory((RowKeyFormat2) rowKeyFormat); } else { throw new RuntimeException("Fiji Table has unknown RowKeyFormat" + rowKeyFormat.getClass().getName()); } }
/** * Constructs an Entity ID factory from a layout capsule. * * @param layout layout to construct an entity ID factory from. * @return a new entity ID factory as described from the table layout. */ private static EntityIdFactory createEntityIdFactory(final FijiTableLayout layout) { final Object format = layout.getDesc().getKeysFormat(); if (format instanceof RowKeyFormat) { return EntityIdFactory.getFactory((RowKeyFormat) format); } else if (format instanceof RowKeyFormat2) { return EntityIdFactory.getFactory((RowKeyFormat2) format); } else { throw new RuntimeException("Invalid Row Key format found in Fiji Table: " + 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())); } }
/** {@inheritDoc} */ @Override public void createTable(TableLayoutDesc tableLayout, int numRegions) throws IOException { Preconditions.checkArgument((numRegions >= 1), "numRegions must be positive: " + numRegions); if (numRegions > 1) { if (FijiTableLayout.getEncoding(tableLayout.getKeysFormat()) == RowKeyEncoding.RAW) { throw new IllegalArgumentException( "May not use numRegions > 1 if row key hashing is disabled in the layout"); } createTable(tableLayout, FijiRowKeySplitter.get().getSplitKeys(numRegions, FijiRowKeySplitter.getRowKeyResolution(tableLayout))); } else { createTable(tableLayout, null); } }
/** * 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())); } }
/** * 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; }
/** * 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)); } }
switch (FijiTableLayout.getEncoding(tableLayout.getKeysFormat())) { case HASH: case HASH_PREFIX: throw new RuntimeException( "Unexpected row key encoding: " + FijiTableLayout.getEncoding(tableLayout.getKeysFormat()));
final EntityId entityId, final FijiTableLayout layout) throws IOException { final Object keysFormat = layout.getDesc().getKeysFormat(); final RowKeyEncoding encoding = getEncoding(keysFormat); switch (encoding) {
switch (FijiTableLayout.getEncoding(table.getLayout().getDesc().getKeysFormat())) { case RAW: { + FijiTableLayout.getEncoding(table.getLayout().getDesc().getKeysFormat()));
mRowKeyFormat = (RowKeyFormat2) mLayoutMonitor.getLayout().getDesc().getKeysFormat(); mEntityIdFactory = EntityIdFactory.getFactory(mRowKeyFormat);
final EntityId entityId, final FijiTableLayout layout) throws IOException { final Object keysFormat = layout.getDesc().getKeysFormat(); final RowKeyEncoding encoding = getEncoding(keysFormat);
if (tableLayout.getKeysFormat() instanceof RowKeyFormat) { throw new InvalidLayoutException( "CassandraFiji does not support 'RowKeyFormat', instead use 'RowKeyFormat2'.");
/** * Construct eid from a entity id string. * Formatted entity ids mustn't have wildcards in order to resolve. * * @param layout of table in which to construct the eid. * @return the eid. * @throws IOException if construction of eid fails due to incorrect user input. */ public EntityId resolve(final FijiTableLayout layout) throws IOException { if (this.hasComponents()) { if (this.isWildcarded()) { throw new IllegalArgumentException( "Entity id must be fully specified for resolution, i.e. without wildcards."); } final RowKeyEncoding encoding = getEncoding(layout.getDesc().getKeysFormat()); switch (encoding) { case FORMATTED: return EntityIdFactory.getFactory(layout).getEntityId(mComponents); default: return EntityIdFactory.getFactory(layout).getEntityId(mComponents[0]); } } else { final EntityIdFactory factory = EntityIdFactory.getFactory(layout); return factory.getEntityIdFromHBaseRowKey(parseBytes(mStringEntityId)); } }
if (tableLayout.getKeysFormat() instanceof RowKeyFormat) { LOG.warn("Usage of 'RowKeyFormat' is deprecated. New tables should use 'RowKeyFormat2'.");