use of com.hortonworks.registries.storage.OrderByField 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.OrderByField in project registry by hortonworks.
the class SchemaVersionLifecycleManager method deleteSchemaVersionBranchMapping.
private void deleteSchemaVersionBranchMapping(Long schemaVersionId) throws SchemaNotFoundException, SchemaLifecycleException {
List<QueryParam> schemaVersionMappingStorableQueryParams = Lists.newArrayList();
schemaVersionMappingStorableQueryParams.add(new QueryParam(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, schemaVersionId.toString()));
List<OrderByField> orderByFields = new ArrayList<>();
orderByFields.add(OrderByField.of(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, false));
Collection<SchemaBranchVersionMapping> storables = storageManager.find(SchemaBranchVersionMapping.NAMESPACE, schemaVersionMappingStorableQueryParams, orderByFields);
if (storables == null || storables.isEmpty()) {
LOG.debug("No need to delete schema version mapping as the database did a cascade delete");
return;
}
if (storables.size() > 1) {
List<String> branchNamesTiedToSchema = storables.stream().map(storable -> schemaBranchCache.get(SchemaBranchCache.Key.of(storable.getSchemaBranchId())).getName()).collect(Collectors.toList());
throw new SchemaLifecycleException(String.format("Schema version with id : '%s' is tied with more than one branch : '%s' ", schemaVersionId.toString(), Arrays.toString(branchNamesTiedToSchema.toArray())));
}
storageManager.remove(new StorableKey(SchemaBranchVersionMapping.NAMESPACE, storables.iterator().next().getPrimaryKey()));
}
use of com.hortonworks.registries.storage.OrderByField 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);
}
use of com.hortonworks.registries.storage.OrderByField in project registry by hortonworks.
the class PostgresSelectQueryTest method testSelectQueryWithOrderBy.
@Test
public void testSelectQueryWithOrderBy() throws Exception {
List<OrderByField> orderByFields = Arrays.asList(OrderByField.of("foo", true), OrderByField.of("bar"));
PostgresqlSelectQuery postgresqlSelectQuery = new PostgresqlSelectQuery("topic", orderByFields);
String parametrizedSql = postgresqlSelectQuery.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);
postgresqlSelectQuery = new PostgresqlSelectQuery(new StorableKey(nameSpace, new PrimaryKey(fieldToObjectMap)), orderByFields);
parametrizedSql = postgresqlSelectQuery.getParametrizedSql();
Assert.assertEquals("SELECT * FROM \"topic\" WHERE \"foo\" = ? ORDER BY \"foo\" DESC, ORDER BY \"bar\" ASC", parametrizedSql);
}
use of com.hortonworks.registries.storage.OrderByField in project registry by hortonworks.
the class SchemaVersionLifecycleManager method getSortedSchemaVersions.
private List<SchemaVersionInfo> getSortedSchemaVersions(Long schemaBranchId) throws SchemaNotFoundException, SchemaBranchNotFoundException {
List<QueryParam> schemaVersionMappingStorableQueryParams = Lists.newArrayList();
schemaVersionMappingStorableQueryParams.add(new QueryParam(SchemaBranchVersionMapping.SCHEMA_BRANCH_ID, schemaBranchId.toString()));
List<OrderByField> orderByFields = new ArrayList<>();
orderByFields.add(OrderByField.of(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, false));
List<SchemaVersionInfo> schemaVersionInfos = new ArrayList<>();
Collection<SchemaBranchVersionMapping> storables = storageManager.find(SchemaBranchVersionMapping.NAMESPACE, schemaVersionMappingStorableQueryParams, orderByFields);
if (storables == null || storables.size() == 0) {
if (schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchId)).getName().equals(SchemaBranch.MASTER_BRANCH))
return Collections.emptyList();
else
throw new InvalidSchemaBranchVersionMapping(String.format("No schema versions are attached to the schema branch id : '%s'", schemaBranchId));
}
for (SchemaBranchVersionMapping storable : storables) {
SchemaIdVersion schemaIdVersion = new SchemaIdVersion(storable.getSchemaVersionInfoId());
schemaVersionInfos.add(schemaVersionInfoCache.getSchema(SchemaVersionInfoCache.Key.of(schemaIdVersion)));
}
return schemaVersionInfos;
}
Aggregations