@Test public void testDeserializeValidSerializedSchema() { final TableSchema actualSchema = TableSchema.deserialize(tableSchema.serialize()); assertEquals(COLUMNS.size(), tableSchema.getColumns().size()); assertEquals(INDICES.size(), tableSchema.getIndices().size()); assertEquals(tableSchema, actualSchema); }
@Test public void testAddIndicesValidIndex() { final List<IndexSchema> indices = Lists.newArrayList(); final TableSchema schema = new TableSchema(COLUMNS, indices); assertEquals(0, schema.getIndices().size()); schema.addIndices(INDICES); assertEquals(INDICES.size(), schema.getIndices().size()); }
@Test public void testGetAutoIncColumnNoAutoIncColumns() { final TableSchema schema = new TableSchema(ImmutableList.<ColumnSchema>of(FAKE_COL_SCHEMA), INDICES); assertEquals(null, schema.getAutoIncrementColumn()); }
/** * Parse a line of a CSV into a Honeycomb {@link Row} * * @param line CSV line * @return Honeycomb {@link Row} * @throws IOException The CSV line was improperly formed * @throws ParseException The CSV line contained invalid data. */ public Row parseRow(String line) throws IOException, ParseException { checkNotNull(line); String[] unparsedFields = csvParser.parseLine(line); checkArgument((schema.getColumns().size() == unparsedFields.length), "Line contains wrong number of columns."); ImmutableMap.Builder<String, ByteBuffer> fields = ImmutableMap.builder(); for (int i = 0; i < columns.length; i++) { fields.put(columns[i], FieldParser.parse(unparsedFields[i], schema.getColumnSchema(columns[i]))); } return new Row(fields.build(), UUID.randomUUID()); } }
/** * Performs all metadata operations necessary to create a table * * @param tableName The name of the table to create, not null or empty * @param schema The schema details of the table to create, not null */ public void createTable(final String tableName, final TableSchema schema) { Verify.isNotNullOrEmpty(tableName); checkNotNull(schema); final List<Put> puts = Lists.newArrayList(); // Fetch the next table id to use for this table final long tableId = getNextTableId(); puts.add(putTableId(tableName, tableId)); puts.add(putColumnIds(tableId, schema.getColumns())); puts.add(putTableSchema(tableId, schema)); if (schema.hasIndices()) { puts.add(putIndices(tableId, schema.getIndices())); } performMutations(ImmutableList.<Delete>of(), puts); }
/** * Performs all metadata operations necessary to create a table index * * @param tableId The id of the table to create the index * @param indexSchema The {@link com.nearinfinity.honeycomb.mysql.schema.IndexSchema} representing the index details, not null */ public void createTableIndex(final long tableId, final IndexSchema indexSchema) { Verify.isValidId(tableId); checkNotNull(indexSchema, "The index schema is invalid"); final List<Put> puts = Lists.newArrayList(); final List<IndexSchema> indexDetailMap = ImmutableList.of(indexSchema); // Update the table schema to store the new index schema details final TableSchema existingSchema = getSchema(tableId); final TableSchema updatedSchema = existingSchema.schemaCopy(); updatedSchema.addIndices(indexDetailMap); // Write the updated table schema and created index puts.add(putTableSchema(tableId, updatedSchema)); puts.add(putIndices(tableId, indexDetailMap)); performMutations(ImmutableList.<Delete>of(), puts); }
@Test public void testRemoveIndexValidIndexName() { final TableSchema schema = new TableSchema(COLUMNS, INDICES); assertEquals(INDICES.size(), schema.getIndices().size()); // Remove all of the indexes from the schema for(final IndexSchema indexSchema : INDICES ) { schema.removeIndex(indexSchema.getIndexName()); } assertEquals(0, schema.getIndices().size()); }
public void updateRow(byte[] oldRowBytes, byte[] rowBytes) { checkTableOpen(); checkNotNull(rowBytes); Row updatedRow = Row.deserialize(rowBytes); TableSchema schema = store.getSchema(tableName); Row oldRow = Row.deserialize(oldRowBytes); oldRow.setUUID(updatedRow.getUUID()); ImmutableList<IndexSchema> changedIndices = Util.getChangedIndices(schema.getIndices(), oldRow.getRecords(), updatedRow.getRecords()); table.updateRow(oldRow, updatedRow, changedIndices); if (schema.hasUniqueIndices()) { table.flush(); } }
@Test(expected = NullPointerException.class) public void testDeserializeNullSerializedSchema() { TableSchema.deserialize(null); }
/** * Deserialize a byte array into a table schema. * * @param serializedTableSchema Byte array of the table schema [Not null] * @return An table schema for the byte array. */ public static TableSchema deserialize(byte[] serializedTableSchema) { checkNotNull(serializedTableSchema); checkArgument(serializedTableSchema.length > 0); SchemaVersionUtils.processSchemaVersion(serializedTableSchema[0], TableSchemaInfo.VER_CURRENT); return new TableSchema(Util.deserializeAvroObject(serializedTableSchema, reader)); }
List<byte[]> encodedRecords = Lists.newArrayList(); if (fields != null) { for (String column : tableSchema.getIndexSchema(indexName).getColumns()) { if (!fields.containsKey(column)) { continue; if (record != null) { byte[] encodedRecord = encodeValue(record, tableSchema.getColumnSchema(column)); encodedRecords.add(order == SortOrder.Ascending ? encodedRecord
@Test public void testHasNoUniqueIndices() { final TableSchema schema = new TableSchema(COLUMNS, ImmutableList.<IndexSchema>of()); assertFalse(schema.hasUniqueIndices()); }
@Test public void testHasNoIndices() { final TableSchema schema = new TableSchema(COLUMNS, ImmutableList.<IndexSchema>of()); assertFalse(schema.hasIndices()); }
/** * Build put list for a row insert with indices * * @param tableId * @param row * @return The list of put mutations */ public List<Put> insert(long tableId, final Row row) { return insert(tableId, row, store.getSchema(tableId).getIndices()); }
/** * Insert row into table. * * @param rowBytes Serialized row to be written */ public void insertRow(byte[] rowBytes) { checkTableOpen(); checkNotNull(rowBytes); TableSchema schema = store.getSchema(tableName); Row row = Row.deserialize(rowBytes); row.setRandomUUID(); String auto_inc_col = schema.getAutoIncrementColumn(); if (auto_inc_col != null) { ByteBuffer bb = row.getRecords().get(auto_inc_col); if (bb != null) { long auto_inc = bb.getLong(); long next_auto_inc = auto_inc + 1; if (auto_inc > next_auto_inc) { // The autoincrement will wrap around. MySQL says don't wrap. next_auto_inc = auto_inc; } bb.rewind(); store.setAutoInc(tableName, next_auto_inc); } } table.insertRow(row); if (schema.hasUniqueIndices()) { table.flush(); } }
@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); }
/** * Remove an index schema from the table schema * * @param indexName Name of index schema [Not null, Not empty] */ public void removeIndex(String indexName) { Verify.isNotNullOrEmpty(indexName); IndexSchema schema = getIndexSchema(indexName); checkNotNull(schema); indices.remove(schema); avroTableSchema.getIndices().remove(indexName); if (schema.getIsUnique()) { uniqueIndexCount--; } }
/** * Verifies that the provided table schema has an auto increment column name * * @param schema The table schema to verify * @return True if the schema has an auto increment column name, false otherwise */ public static boolean hasAutoIncrementColumn(final TableSchema schema) { return schema.getAutoIncrementColumn() != 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); } }
@Test(expected = NullPointerException.class) public void testGetColumnSchemaInvalidColumnName() { tableSchema.getColumnSchema(null); }