Search in sources :

Example 1 with GCRule

use of com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule in project java-bigtable-hbase by googleapis.

the class ColumnDescriptorAdapter method adapt.

/**
 * Adapt a single instance of an HBase {@link org.apache.hadoop.hbase.HColumnDescriptor} to an
 * instance of {@link com.google.bigtable.admin.v2.ColumnFamily.Builder}.
 *
 * <p>NOTE: This method does not set the name of the ColumnFamily.Builder. The assumption is that
 * the CreateTableRequest or CreateColumnFamilyRequest takes care of the naming. As of now
 * (3/11/2015), the server insists on having a blank name.
 *
 * @param columnDescriptor a {@link org.apache.hadoop.hbase.HColumnDescriptor} object.
 * @return a {@link com.google.bigtable.admin.v2.ColumnFamily.Builder} object.
 */
public ColumnFamily adapt(HColumnDescriptor columnDescriptor) {
    throwIfRequestingUnknownFeatures(columnDescriptor);
    throwIfRequestingUnsupportedFeatures(columnDescriptor);
    ColumnFamily.Builder resultBuilder = ColumnFamily.newBuilder();
    GCRule gcRule = buildGarbageCollectionRule(columnDescriptor);
    if (gcRule != null) {
        resultBuilder.setGcRule(gcRule.toProto());
    }
    return resultBuilder.build();
}
Also used : GCRule(com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule) ColumnFamily(com.google.bigtable.admin.v2.ColumnFamily)

Example 2 with GCRule

use of com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule in project java-bigtable-hbase by googleapis.

the class ColumnDescriptorAdapter method buildGarbageCollectionRule.

/**
 * Construct an Bigtable {@link GCRule} from the given column descriptor.
 *
 * @param columnDescriptor a {@link org.apache.hadoop.hbase.HColumnDescriptor} object.
 * @return a {@link GCRule} object.
 */
public static GCRule buildGarbageCollectionRule(HColumnDescriptor columnDescriptor) {
    int maxVersions = columnDescriptor.getMaxVersions();
    int minVersions = columnDescriptor.getMinVersions();
    int ttlSeconds = columnDescriptor.getTimeToLive();
    Preconditions.checkState(minVersions < maxVersions, "HColumnDescriptor min versions must be less than max versions.");
    if (ttlSeconds == HColumnDescriptor.DEFAULT_TTL) {
        if (maxVersions == Integer.MAX_VALUE) {
            return null;
        } else {
            return GCRULES.maxVersions(maxVersions);
        }
    }
    // minVersions only comes into play with a TTL:
    GCRule ageRule = GCRULES.maxAge(Duration.ofSeconds(ttlSeconds));
    if (minVersions != HColumnDescriptor.DEFAULT_MIN_VERSIONS) {
        // The logic here is: only delete a cell if:
        // 1) the age is older than :ttlSeconds AND
        // 2) the cell's relative version number is greater than :minVersions
        // 
        // Bigtable's nomenclature for this is:
        // Intersection (AND)
        // - maxAge = :HBase_ttlSeconds
        // - maxVersions = :HBase_minVersion
        ageRule = GCRULES.intersection().rule(ageRule).rule(GCRULES.maxVersions(minVersions));
    }
    if (maxVersions == Integer.MAX_VALUE) {
        return ageRule;
    } else {
        return GCRULES.union().rule(ageRule).rule(GCRULES.maxVersions(maxVersions));
    }
}
Also used : GCRule(com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule)

Example 3 with GCRule

use of com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule in project java-bigtable-hbase by googleapis.

the class TestTableAdapter method testAdaptWithColumnDesc.

@Test
public void testAdaptWithColumnDesc() {
    HColumnDescriptor columnDesc = new HColumnDescriptor(COLUMN_FAMILY);
    HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(TABLE_ID));
    CreateTableRequest request = CreateTableRequest.of(TABLE_ID);
    desc.addFamily(columnDesc);
    TableAdapter.adapt(desc, request);
    GCRule gcRule = ColumnDescriptorAdapter.buildGarbageCollectionRule(columnDesc);
    CreateTableRequest expected = CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY, gcRule);
    Assert.assertEquals(request.toProto(PROJECT_ID, INSTANCE_ID), expected.toProto(PROJECT_ID, INSTANCE_ID));
}
Also used : HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) GCRule(com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule) CreateTableRequest(com.google.cloud.bigtable.admin.v2.models.CreateTableRequest) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) Test(org.junit.Test)

Example 4 with GCRule

use of com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule in project java-bigtable by googleapis.

the class GCRulesTest method duration.

@Test
public void duration() {
    DurationRule actual = GCRULES.maxAge(Duration.ofSeconds(61, 9));
    GcRule expected = buildAgeRule(61, 9);
    assertNotNull(actual.getMaxAge());
    assertThat(actual.toProto()).isEqualTo(expected);
}
Also used : GcRule(com.google.bigtable.admin.v2.GcRule) DurationRule(com.google.cloud.bigtable.admin.v2.models.GCRules.DurationRule) Test(org.junit.Test)

Example 5 with GCRule

use of com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule in project java-bigtable by googleapis.

the class GCRulesTest method intersectionOfUnions.

@Test
public void intersectionOfUnions() {
    IntersectionRule actual = GCRULES.intersection().rule(GCRULES.union().rule(GCRULES.maxVersions(1)).rule(GCRULES.maxAge(Duration.ofSeconds(1)))).rule(GCRULES.union().rule(GCRULES.maxVersions(1)).rule(GCRULES.maxAge(Duration.ofSeconds(1))));
    GcRule expected = GcRule.newBuilder().setIntersection(Intersection.newBuilder().addRules(GcRule.newBuilder().setUnion(Union.newBuilder().addRules(buildVersionsRule(1)).addRules(buildAgeRule(1, 0)))).addRules(GcRule.newBuilder().setUnion(Union.newBuilder().addRules(buildVersionsRule(1)).addRules(buildAgeRule(1, 0))))).build();
    assertEquals(2, actual.getRulesList().size());
    assertThat(actual.toProto()).isEqualTo(expected);
}
Also used : IntersectionRule(com.google.cloud.bigtable.admin.v2.models.GCRules.IntersectionRule) GcRule(com.google.bigtable.admin.v2.GcRule) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)10 GCRule (com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule)9 HColumnDescriptor (org.apache.hadoop.hbase.HColumnDescriptor)6 ColumnFamily (com.google.bigtable.admin.v2.ColumnFamily)4 GcRule (com.google.bigtable.admin.v2.GcRule)4 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)3 Table (com.google.bigtable.admin.v2.Table)2 DurationRule (com.google.cloud.bigtable.admin.v2.models.GCRules.DurationRule)2 ColumnFamily (com.google.cloud.bigtable.admin.v2.models.ColumnFamily)1 CreateTableRequest (com.google.cloud.bigtable.admin.v2.models.CreateTableRequest)1 GCRules (com.google.cloud.bigtable.admin.v2.models.GCRules)1 IntersectionRule (com.google.cloud.bigtable.admin.v2.models.GCRules.IntersectionRule)1 UnionRule (com.google.cloud.bigtable.admin.v2.models.GCRules.UnionRule)1 VersionRule (com.google.cloud.bigtable.admin.v2.models.GCRules.VersionRule)1 ByteString (com.google.protobuf.ByteString)1 TableDescriptor (org.apache.hadoop.hbase.client.TableDescriptor)1