/** * Return the name of the auto increment column in the table, or null. * * @return ColumnSchema with an auto increment modifier */ public String getAutoIncrementColumn() { for (ColumnSchema entry : columns) { if (entry.getIsAutoIncrement()) { return entry.getColumnName(); } } return null; }
private void checkSqlColumnsMatch(String sqlTable) { Set<String> expectedColumns = Sets.newHashSet(); for (ColumnSchema column : schema.getColumns()) { expectedColumns.add(column.getColumnName()); } List<String> invalidColumns = Lists.newLinkedList(); for (String column : columns) { if (!expectedColumns.contains(column)) { LOG.error("Found non-existent column " + column); invalidColumns.add(column); } } if (invalidColumns.size() > 0) { String expectedColumnString = Joiner.on(",").join(expectedColumns); String invalidColumnString = Joiner.on(",").join(invalidColumns); String message = String.format("In table %s following columns (%s)" + " are not valid columns. Expected columns (%s)", sqlTable, invalidColumnString, expectedColumnString); throw new IllegalStateException(message); } }
/** * Verifies that the index schema only index columns for columns that are available * @param indices A mapping of the index details, not null * @param columns A mapping of column details, not null * @throws NullPointerException Thrown if the indices or columns container is null * @throws IllegalArgumentException Thrown if a {@link IndexSchema} indexes * a column that is not an available column */ public static void isValidIndexSchema(final Collection<IndexSchema> indices, final Collection<ColumnSchema> columns) { checkNotNull(indices); checkNotNull(columns); Set<String> columnNames = Sets.newHashSet(); for (ColumnSchema column : columns) { columnNames.add(column.getColumnName()); } for (final IndexSchema index : indices) { for (final String column : index.getColumns()) { if (!columnNames.contains(column)) { throw new IllegalArgumentException("Only columns in the table may be indexed."); } } } } }
private Put putColumnIds(long tableId, Collection<ColumnSchema> columns) { long columnId = getNextColumnId(tableId, columns.size()); Put put = new Put(new ColumnsRowKey(tableId).encode()); for (ColumnSchema columnEntry : columns) { put.add(columnFamily, serializeName(columnEntry.getColumnName()), serializeId(columnId--)); } return put; }
/** * Construct a table schema from a map of column schemas and index schemas. * * @param columns Column schemas map [Not null] * @param indices Index schema map [Not null] */ public TableSchema(Collection<ColumnSchema> columns, Collection<IndexSchema> indices) { checkArgument(columns.size() > 0, "Table must have at least one column."); checkNotNull(indices); Map<String, AvroColumnSchema> columnSchemaMap = Maps.newHashMap(); for (ColumnSchema columnSchema : columns) { columnSchemaMap.put(columnSchema.getColumnName(), columnSchema.getAvroValue()); } Map<String, AvroIndexSchema> indexSchemaMap = Maps.newHashMap(); for (IndexSchema indexSchema : indices) { indexSchemaMap.put(indexSchema.getIndexName(), indexSchema.getAvroValue()); if (indexSchema.getIsUnique()) { uniqueIndexCount++; } } avroTableSchema = AvroTableSchema.newBuilder() .setColumns(columnSchemaMap) .setIndices(indexSchemaMap) .build(); this.columns = new LinkedList<ColumnSchema>(columns); this.indices = new LinkedList<IndexSchema>(indices); }
public RowGenerator(TableSchema schema) { super(); ImmutableMap.Builder<String, Generator<ByteBuffer>> recordGenerators = ImmutableMap.builder(); for (ColumnSchema column : schema.getColumns()) { recordGenerators.put(column.getColumnName(), new FieldGenerator(column)); } this.recordGenerators = recordGenerators.build(); }
@Test public void testDeserializeValidSerializedSchemaAndColumnName() { final ColumnSchema actualSchema = ColumnSchema.deserialize(TEST_COL_SCHEMA.serialize(), TEST_COL_SCHEMA.getColumnName()); assertEquals(TEST_COL_SCHEMA.getColumnName(), actualSchema.getColumnName()); assertEquals(TEST_COL_SCHEMA, actualSchema); }
@Test public void testGetColumnSchemaValidColumnName() { final TableSchema schema = new TableSchema(ImmutableList.<ColumnSchema>of(FAKE_COL_SCHEMA), INDICES); final ColumnSchema actual = schema.getColumnSchema(FAKE_COL_SCHEMA.getColumnName()); assertEquals(FAKE_COL_SCHEMA.getColumnName(), actual.getColumnName()); assertEquals(FAKE_COL_SCHEMA, actual); }
@Override public TableSchema next() { final List<ColumnSchema> columnSchemas = COLUMNS_GEN.next(); ImmutableList.Builder<String> columns = ImmutableList.builder(); for (ColumnSchema columnSchema : columnSchemas) { columns.add(columnSchema.getColumnName()); } final Generator<List<IndexSchema>> indexGen = CombinedGenerators.lists( new IndexSchemaGenerator(columns.build()), numIndicesGen); return new TableSchema(columnSchemas, indexGen.next()); } }