All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.flipkart.hbaseobjectmapper.WrappedHBTable Maven / Gradle / Ivy

Go to download

HBase ORM is a light-weight, thread-safe and performant library that enables: [1] object-oriented access of HBase rows (Data Access Object) with minimal code and good testability [2] reading from and/or writing to HBase tables in Hadoop MapReduce jobs

There is a newer version: 1.19
Show newest version
package com.flipkart.hbaseobjectmapper;

import com.flipkart.hbaseobjectmapper.exceptions.DuplicateCodecFlagForRowKeyException;
import com.flipkart.hbaseobjectmapper.exceptions.ImproperHBTableAnnotationExceptions;
import org.apache.hadoop.hbase.TableName;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * A wrapper for {@link HBTable} annotation (for internal use only)
 *
 * @param  Data type of row key
 * @param  Entity type
 */
class WrappedHBTable, T extends HBRecord> {

    private final TableName tableName;
    private final Map families; // This should evolve to Map
    private final Map codecFlags;
    private final Class clazz;

    WrappedHBTable(Class clazz) {
        this.clazz = clazz;
        final HBTable hbTable = clazz.getAnnotation(HBTable.class);
        if (hbTable == null) {
            throw new ImproperHBTableAnnotationExceptions.MissingHBTableAnnotationException(String.format("Class %s is missing %s annotation", clazz.getName(), HBTable.class.getSimpleName()));
        }
        if (hbTable.name().isEmpty()) {
            throw new ImproperHBTableAnnotationExceptions.EmptyTableNameOnHBTableAnnotationException(String.format("Annotation %s on class %s has empty name", HBTable.class.getName(), clazz.getName()));
        }
        tableName = TableName.valueOf(hbTable.name().getBytes());
        codecFlags = toMap(hbTable.rowKeyCodecFlags());
        families = new HashMap<>(hbTable.families().length, 1.0f);
        for (Family family : hbTable.families()) {
            if (family.name().isEmpty()) {
                throw new ImproperHBTableAnnotationExceptions.EmptyColumnFamilyOnHBTableAnnotationException(String.format("The %s annotation on class %s has a column family with empty name", HBTable.class.getSimpleName(), clazz.getName()));
            }
            if (family.versions() < 1) {
                throw new ImproperHBTableAnnotationExceptions.InvalidValueForVersionsOnHBTableAnnotationException(String.format("The %s annotation on class %s has a column family '%s' which has 'versions' less than 1", HBTable.class.getSimpleName(), clazz.getName(), family.name()));
            }
            final Integer prevValue = families.put(family.name(), family.versions());
            if (prevValue != null) {
                throw new ImproperHBTableAnnotationExceptions.DuplicateColumnFamilyNamesOnHBTableAnnotationException(String.format("The %s annotation on class %s has two or more column families with same name '%s' (Note: column family names must be unique)", HBTable.class.getSimpleName(), clazz.getName(), family.name()));
            }
        }
    }

    private Map toMap(Flag[] codecFlags) {
        Map flagsMap = new HashMap<>(codecFlags.length, 1.0f);
        for (Flag flag : codecFlags) {
            String previousValue = flagsMap.put(flag.name(), flag.value());
            if (previousValue != null) {
                throw new DuplicateCodecFlagForRowKeyException(clazz, flag.name());
            }
        }
        return flagsMap;
    }

    Map getFamiliesAndVersions() {
        return families;
    }

    boolean isColumnFamilyPresent(String familyName) {
        return families.containsKey(familyName);
    }

    TableName getName() {
        return tableName;
    }

    public Map getCodecFlags() {
        return codecFlags;
    }

    @Override
    public String toString() {
        return tableName.getNameAsString();
    }

    static , T extends HBRecord> Map getCodecFlags(Class clazz) {
        return new WrappedHBTable<>(clazz).codecFlags;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy