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);
}
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;
}
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);
}
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;
}
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);
}
Aggregations