Search in sources :

Example 1 with PrimaryKey

use of com.hortonworks.registries.storage.PrimaryKey in project streamline by hortonworks.

the class MLModel method getPrimaryKey.

@JsonIgnore
@Override
public PrimaryKey getPrimaryKey() {
    Map<Field, Object> fieldObjectMap = new HashMap<>();
    fieldObjectMap.put(new Schema.Field(ID, Type.LONG), this.id);
    return new PrimaryKey(fieldObjectMap);
}
Also used : Field(com.hortonworks.registries.common.Schema.Field) HashMap(java.util.HashMap) Schema(com.hortonworks.registries.common.Schema) PrimaryKey(com.hortonworks.registries.storage.PrimaryKey) Field(com.hortonworks.registries.common.Schema.Field) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore)

Example 2 with PrimaryKey

use of com.hortonworks.registries.storage.PrimaryKey in project registry by hortonworks.

the class JdbcStorageManager method buildStorableKey.

// private helper methods
/**
 * Query parameters are typically specified for a column or key in a database table or storage namespace. Therefore, we build
 * the {@link StorableKey} from the list of query parameters, and then can use {@link SqlSelectQuery} builder to generate the query using
 * the query parameters in the where clause
 *
 * @return {@link StorableKey} with all query parameters that match database columns <br/>
 * null if none of the query parameters specified matches a column in the DB
 */
private StorableKey buildStorableKey(String namespace, List<QueryParam> queryParams) throws Exception {
    final Map<Schema.Field, Object> fieldsToVal = new HashMap<>();
    StorableKey storableKey = null;
    try {
        CaseAgnosticStringSet columnNames = queryExecutor.getColumnNames(namespace);
        for (QueryParam qp : queryParams) {
            if (!columnNames.contains(qp.getName())) {
                log.warn("Query parameter [{}] does not exist for namespace [{}]. Query parameter ignored.", qp.getName(), namespace);
            } else {
                final String val = qp.getValue();
                final Schema.Type typeOfVal = Schema.Type.getTypeOfVal(val);
                fieldsToVal.put(new Schema.Field(qp.getName(), typeOfVal), // instantiates object of the appropriate type
                typeOfVal.getJavaType().getConstructor(String.class).newInstance(val));
            }
        }
        // it is empty when none of the query parameters specified matches a column in the DB
        if (!fieldsToVal.isEmpty()) {
            final PrimaryKey primaryKey = new PrimaryKey(fieldsToVal);
            storableKey = new StorableKey(namespace, primaryKey);
        }
        log.debug("Building StorableKey from QueryParam: \n\tnamespace = [{}]\n\t queryParams = [{}]\n\t StorableKey = [{}]", namespace, queryParams, storableKey);
    } catch (Exception e) {
        log.debug("Exception occurred when attempting to generate StorableKey from QueryParam", e);
        throw new IllegalQueryParameterException(e);
    }
    return storableKey;
}
Also used : HashMap(java.util.HashMap) Schema(com.hortonworks.registries.common.Schema) PrimaryKey(com.hortonworks.registries.storage.PrimaryKey) AlreadyExistsException(com.hortonworks.registries.storage.exception.AlreadyExistsException) StorageException(com.hortonworks.registries.storage.exception.StorageException) IllegalQueryParameterException(com.hortonworks.registries.storage.exception.IllegalQueryParameterException) OrderByField(com.hortonworks.registries.storage.OrderByField) CaseAgnosticStringSet(com.hortonworks.registries.storage.impl.jdbc.util.CaseAgnosticStringSet) QueryParam(com.hortonworks.registries.common.QueryParam) StorableKey(com.hortonworks.registries.storage.StorableKey) IllegalQueryParameterException(com.hortonworks.registries.storage.exception.IllegalQueryParameterException)

Example 3 with PrimaryKey

use of com.hortonworks.registries.storage.PrimaryKey in project registry by hortonworks.

the class InMemoryStorageManager method addOrUpdate.

@Override
public void addOrUpdate(Storable storable) {
    String namespace = storable.getNameSpace();
    PrimaryKey id = storable.getPrimaryKey();
    if (!storageMap.containsKey(namespace)) {
        storageMap.putIfAbsent(namespace, new ConcurrentHashMap<PrimaryKey, Storable>());
        nameSpaceClassMap.putIfAbsent(namespace, storable.getClass());
    }
    if (!storageMap.get(namespace).containsKey(id)) {
        nextId(namespace);
    }
    storageMap.get(namespace).put(id, storable);
}
Also used : PrimaryKey(com.hortonworks.registries.storage.PrimaryKey) Storable(com.hortonworks.registries.storage.Storable)

Example 4 with PrimaryKey

use of com.hortonworks.registries.storage.PrimaryKey in project registry by hortonworks.

the class InMemoryStorageManager method find.

@Override
public <T extends Storable> Collection<T> find(final String namespace, final List<QueryParam> queryParams, final List<OrderByField> orderByFields) throws StorageException {
    List<T> storables = new ArrayList<>();
    if (queryParams == null) {
        Collection<T> collection = list(namespace);
        storables = Lists.newArrayList(collection);
    } else {
        Class<?> clazz = nameSpaceClassMap.get(namespace);
        if (clazz != null) {
            Map<PrimaryKey, Storable> storableMap = storageMap.get(namespace);
            if (storableMap != null) {
                for (Storable val : storableMap.values()) {
                    if (matches(val, queryParams, clazz)) {
                        storables.add((T) val);
                    }
                }
            }
        }
    }
    if (orderByFields != null && !orderByFields.isEmpty()) {
        storables.sort((storable1, storable2) -> {
            try {
                for (OrderByField orderByField : orderByFields) {
                    Comparable value1 = ReflectionHelper.invokeGetter(orderByField.getFieldName(), storable1);
                    Comparable value2 = ReflectionHelper.invokeGetter(orderByField.getFieldName(), storable2);
                    int compareTo;
                    // same values continue
                    if (value1 == value2) {
                        continue;
                    } else if (value1 == null) {
                        // value2 is non null
                        compareTo = -1;
                    } else if (value2 == null) {
                        // value1 is non null
                        compareTo = 1;
                    } else {
                        // both value and value2 non null
                        compareTo = value1.compareTo(value2);
                    }
                    if (compareTo == 0) {
                        continue;
                    }
                    return orderByField.isDescending() ? -compareTo : compareTo;
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            // all group by fields are matched means equal
            return 0;
        });
    }
    return storables;
}
Also used : ArrayList(java.util.ArrayList) PrimaryKey(com.hortonworks.registries.storage.PrimaryKey) Storable(com.hortonworks.registries.storage.Storable) AlreadyExistsException(com.hortonworks.registries.storage.exception.AlreadyExistsException) StorageException(com.hortonworks.registries.storage.exception.StorageException) InvocationTargetException(java.lang.reflect.InvocationTargetException) OrderByField(com.hortonworks.registries.storage.OrderByField)

Example 5 with PrimaryKey

use of com.hortonworks.registries.storage.PrimaryKey in project registry by hortonworks.

the class MySqlSelectQueryTest method testSelectQueryWithOrderBy.

@Test
public void testSelectQueryWithOrderBy() throws Exception {
    List<OrderByField> orderByFields = Arrays.asList(OrderByField.of("foo", true), OrderByField.of("bar"));
    MySqlSelectQuery mySqlSelectQuery = new MySqlSelectQuery("topic", orderByFields);
    String parametrizedSql = mySqlSelectQuery.getParametrizedSql();
    Assert.assertEquals("SELECT * FROM topic ORDER BY `foo` DESC, ORDER BY `bar` ASC", parametrizedSql);
    Map<Schema.Field, Object> fieldToObjectMap = new HashMap<>();
    fieldToObjectMap.put(new Schema.Field("foo", Schema.Type.LONG), 1);
    mySqlSelectQuery = new MySqlSelectQuery(new StorableKey(nameSpace, new PrimaryKey(fieldToObjectMap)), orderByFields);
    parametrizedSql = mySqlSelectQuery.getParametrizedSql();
    Assert.assertEquals("SELECT * FROM topic WHERE `foo` = ? ORDER BY `foo` DESC, ORDER BY `bar` ASC", parametrizedSql);
}
Also used : OrderByField(com.hortonworks.registries.storage.OrderByField) OrderByField(com.hortonworks.registries.storage.OrderByField) HashMap(java.util.HashMap) StorableKey(com.hortonworks.registries.storage.StorableKey) Schema(com.hortonworks.registries.common.Schema) PrimaryKey(com.hortonworks.registries.storage.PrimaryKey) MySqlSelectQuery(com.hortonworks.registries.storage.impl.jdbc.provider.mysql.query.MySqlSelectQuery) Test(org.junit.Test)

Aggregations

PrimaryKey (com.hortonworks.registries.storage.PrimaryKey)13 HashMap (java.util.HashMap)10 Schema (com.hortonworks.registries.common.Schema)9 OrderByField (com.hortonworks.registries.storage.OrderByField)7 StorableKey (com.hortonworks.registries.storage.StorableKey)6 Test (org.junit.Test)4 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)3 Field (com.hortonworks.registries.common.Schema.Field)3 StorageException (com.hortonworks.registries.storage.exception.StorageException)3 Storable (com.hortonworks.registries.storage.Storable)2 AlreadyExistsException (com.hortonworks.registries.storage.exception.AlreadyExistsException)2 MySqlSelectQuery (com.hortonworks.registries.storage.impl.jdbc.provider.mysql.query.MySqlSelectQuery)2 PostgresqlSelectQuery (com.hortonworks.registries.storage.impl.jdbc.provider.postgresql.query.PostgresqlSelectQuery)2 QueryParam (com.hortonworks.registries.common.QueryParam)1 IllegalQueryParameterException (com.hortonworks.registries.storage.exception.IllegalQueryParameterException)1 CaseAgnosticStringSet (com.hortonworks.registries.storage.impl.jdbc.util.CaseAgnosticStringSet)1 OrderBy (com.hortonworks.registries.storage.search.OrderBy)1 Predicate (com.hortonworks.registries.storage.search.Predicate)1 PredicateCombinerPair (com.hortonworks.registries.storage.search.PredicateCombinerPair)1 WhereClause (com.hortonworks.registries.storage.search.WhereClause)1