use of com.nearinfinity.honeycomb.mysql.schema.IndexSchema in project honeycomb by altamiracorp.
the class HandlerProxy method updateRow.
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();
}
}
use of com.nearinfinity.honeycomb.mysql.schema.IndexSchema in project honeycomb by altamiracorp.
the class HandlerProxy method addIndex.
/**
* Add the provided index information to the table. The table must be open
* before this operation can be performed.
*
* @param indexName The name of the index to add, not null or empty
* @param serializedSchema The byte representation of the {@link IndexSchema} for this index, not null
*/
public void addIndex(String indexName, byte[] serializedSchema) {
Verify.isNotNullOrEmpty(indexName, "The index name is invalid");
checkNotNull(serializedSchema, "Schema cannot be null");
checkTableOpen();
IndexSchema schema = IndexSchema.deserialize(serializedSchema, indexName);
checkArgument(!schema.getIsUnique(), "Honeycomb does not support adding unique indices without a table rebuild.");
store.addIndex(tableName, schema);
table.insertTableIndex(schema);
table.flush();
}
use of com.nearinfinity.honeycomb.mysql.schema.IndexSchema in project honeycomb by altamiracorp.
the class Util method getChangedIndices.
/**
* Retrieve from a list of indices which ones have been changed.
*
* @param indices Table indices
* @param oldRecords Old MySQL row
* @param newRecords New MySQL row
* @return List of changed indices
*/
public static ImmutableList<IndexSchema> getChangedIndices(Collection<IndexSchema> indices, Map<String, ByteBuffer> oldRecords, Map<String, ByteBuffer> newRecords) {
if (indices.isEmpty()) {
return ImmutableList.of();
}
MapDifference<String, ByteBuffer> diff = Maps.difference(oldRecords, newRecords);
Set<String> changedColumns = Sets.difference(Sets.union(newRecords.keySet(), oldRecords.keySet()), diff.entriesInCommon().keySet());
ImmutableList.Builder<IndexSchema> changedIndices = ImmutableList.builder();
for (IndexSchema index : indices) {
Set<String> indexColumns = ImmutableSet.copyOf(index.getColumns());
if (!Sets.intersection(changedColumns, indexColumns).isEmpty()) {
changedIndices.add(index);
}
}
return changedIndices.build();
}
use of com.nearinfinity.honeycomb.mysql.schema.IndexSchema in project honeycomb by altamiracorp.
the class HBaseMetadata method putIndices.
private Put putIndices(long tableId, Collection<IndexSchema> indices) {
checkState(!indices.isEmpty(), "putIndices requires 1 or more indices.");
long indexId = getNextIndexId(tableId, indices.size());
Put put = new Put(new IndicesRowKey(tableId).encode());
for (IndexSchema columnEntry : indices) {
put.add(columnFamily, serializeName(columnEntry.getIndexName()), serializeId(indexId--));
}
return put;
}
use of com.nearinfinity.honeycomb.mysql.schema.IndexSchema in project honeycomb by altamiracorp.
the class HBaseMetadataTest method testDeleteIndex.
@Test
public void testDeleteIndex() {
final TableSchema tableSchema = new TableSchema(COLUMN_SCHEMAS, ImmutableList.<IndexSchema>of(new IndexSchema(INDEX_NAME, Lists.newArrayList(COLUMN_NAME), false)));
// Create a new table with the configured details
hbaseMetadata.createTable(TABLE_NAME, tableSchema);
final long tableId = hbaseMetadata.getTableId(TABLE_NAME);
// Verify that the table schema contains indices after creation
final TableSchema schemaBefore = hbaseMetadata.getSchema(tableId);
assertNotNull(schemaBefore);
final Collection<IndexSchema> schemaIndices = schemaBefore.getIndices();
assertEquals(1, schemaIndices.size());
final IndexSchema newIndexDetails = Iterables.find(schemaIndices, indexPredicate);
assertNotNull(newIndexDetails);
final List<String> indexColumns = newIndexDetails.getColumns();
assertEquals(1, indexColumns.size());
assertEquals(COLUMN_NAME, indexColumns.get(0));
// Verify that the index exists after table creation
final Map<String, Long> tableIndexInfo = hbaseMetadata.getIndexIds(tableId);
assertEquals(1, tableIndexInfo.size());
assertTrue(tableIndexInfo.containsKey(INDEX_NAME));
// Remove an existing index from the table
hbaseMetadata.deleteTableIndex(tableId, INDEX_NAME);
// Verify that the table schema has been correctly updated
final TableSchema schemaAfter = hbaseMetadata.getSchema(tableId);
assertNotNull(schemaAfter);
assertTrue(schemaAfter.getIndices().isEmpty());
// Verify that the index has been removed correctly
assertTrue(hbaseMetadata.getIndexIds(tableId).isEmpty());
}
Aggregations