Search in sources :

Example 11 with IndexSchema

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();
    }
}
Also used : AvroTableSchema(com.nearinfinity.honeycomb.mysql.gen.AvroTableSchema) TableSchema(com.nearinfinity.honeycomb.mysql.schema.TableSchema) IndexSchema(com.nearinfinity.honeycomb.mysql.schema.IndexSchema)

Example 12 with IndexSchema

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();
}
Also used : IndexSchema(com.nearinfinity.honeycomb.mysql.schema.IndexSchema)

Example 13 with IndexSchema

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();
}
Also used : IndexSchema(com.nearinfinity.honeycomb.mysql.schema.IndexSchema) ByteBuffer(java.nio.ByteBuffer)

Example 14 with IndexSchema

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;
}
Also used : IndexSchema(com.nearinfinity.honeycomb.mysql.schema.IndexSchema)

Example 15 with IndexSchema

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());
}
Also used : TableSchema(com.nearinfinity.honeycomb.mysql.schema.TableSchema) IndexSchema(com.nearinfinity.honeycomb.mysql.schema.IndexSchema) Test(org.junit.Test)

Aggregations

IndexSchema (com.nearinfinity.honeycomb.mysql.schema.IndexSchema)22 TableSchema (com.nearinfinity.honeycomb.mysql.schema.TableSchema)12 Test (org.junit.Test)10 ColumnSchema (com.nearinfinity.honeycomb.mysql.schema.ColumnSchema)5 QueryKey (com.nearinfinity.honeycomb.mysql.QueryKey)3 AvroTableSchema (com.nearinfinity.honeycomb.mysql.gen.AvroTableSchema)3 HoneycombIntegrationTest (integrationtests.HoneycombIntegrationTest)3 Scanner (com.nearinfinity.honeycomb.Scanner)2 ByteBuffer (java.nio.ByteBuffer)2 UnsignedBytes (com.google.common.primitives.UnsignedBytes)1 Table (com.nearinfinity.honeycomb.Table)1 IndexRowKeyBuilder (com.nearinfinity.honeycomb.hbase.rowkey.IndexRowKeyBuilder)1 RowKey (com.nearinfinity.honeycomb.hbase.rowkey.RowKey)1 HandlerProxy (com.nearinfinity.honeycomb.mysql.HandlerProxy)1 Row (com.nearinfinity.honeycomb.mysql.Row)1 ArrayList (java.util.ArrayList)1 Bytes (org.apache.hadoop.hbase.util.Bytes)1