Search in sources :

Example 1 with OrderByField

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;
}
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 2 with OrderByField

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()));
}
Also used : ObjectMapperUtils(com.hortonworks.registries.schemaregistry.utils.ObjectMapperUtils) SchemaBranchCache(com.hortonworks.registries.schemaregistry.cache.SchemaBranchCache) Arrays(java.util.Arrays) SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException) UnsupportedSchemaTypeException(com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException) InitializedStateDetails(com.hortonworks.registries.schemaregistry.state.details.InitializedStateDetails) SchemaVersionLifecycleStates(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStates) QueryParam(com.hortonworks.registries.common.QueryParam) InbuiltSchemaVersionLifecycleState(com.hortonworks.registries.schemaregistry.state.InbuiltSchemaVersionLifecycleState) LoggerFactory(org.slf4j.LoggerFactory) Storable(com.hortonworks.registries.storage.Storable) OrderByField(com.hortonworks.registries.storage.OrderByField) Hex(org.apache.commons.codec.binary.Hex) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) SchemaVersionLifecycleStateTransitionListener(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateTransitionListener) Map(java.util.Map) SchemaVersionLifecycleStateTransition(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateTransition) InvalidSchemaBranchVersionMapping(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchVersionMapping) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) Logger(org.slf4j.Logger) SchemaVersionLifecycleStateAction(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateAction) SchemaVersionService(com.hortonworks.registries.schemaregistry.state.SchemaVersionService) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) Collection(java.util.Collection) SchemaVersionMergeException(com.hortonworks.registries.schemaregistry.errors.SchemaVersionMergeException) SchemaLifecycleException(com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException) Set(java.util.Set) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) CustomSchemaStateExecutor(com.hortonworks.registries.schemaregistry.state.CustomSchemaStateExecutor) SchemaVersionLifecycleContext(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleContext) Collectors(java.util.stream.Collectors) StorageException(com.hortonworks.registries.storage.exception.StorageException) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) List(java.util.List) SchemaVersionInfoCache(com.hortonworks.registries.schemaregistry.cache.SchemaVersionInfoCache) SchemaVersionLifecycleStateMachine(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateMachine) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) Preconditions(com.google.common.base.Preconditions) SchemaVersionLifecycleState(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleState) StorableKey(com.hortonworks.registries.storage.StorableKey) StorageManager(com.hortonworks.registries.storage.StorageManager) Collections(java.util.Collections) QueryParam(com.hortonworks.registries.common.QueryParam) OrderByField(com.hortonworks.registries.storage.OrderByField) StorableKey(com.hortonworks.registries.storage.StorableKey) ArrayList(java.util.ArrayList) InvalidSchemaBranchVersionMapping(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchVersionMapping) SchemaLifecycleException(com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException)

Example 3 with OrderByField

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

Example 4 with OrderByField

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);
}
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) PostgresqlSelectQuery(com.hortonworks.registries.storage.impl.jdbc.provider.postgresql.query.PostgresqlSelectQuery) PrimaryKey(com.hortonworks.registries.storage.PrimaryKey) Test(org.junit.Test)

Example 5 with OrderByField

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;
}
Also used : QueryParam(com.hortonworks.registries.common.QueryParam) OrderByField(com.hortonworks.registries.storage.OrderByField) InvalidSchemaBranchVersionMapping(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchVersionMapping) ArrayList(java.util.ArrayList) InvalidSchemaBranchVersionMapping(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchVersionMapping)

Aggregations

OrderByField (com.hortonworks.registries.storage.OrderByField)7 ArrayList (java.util.ArrayList)5 QueryParam (com.hortonworks.registries.common.QueryParam)4 StorableKey (com.hortonworks.registries.storage.StorableKey)4 PrimaryKey (com.hortonworks.registries.storage.PrimaryKey)3 Storable (com.hortonworks.registries.storage.Storable)3 HashMap (java.util.HashMap)3 Preconditions (com.google.common.base.Preconditions)2 Schema (com.hortonworks.registries.common.Schema)2 SchemaBranchCache (com.hortonworks.registries.schemaregistry.cache.SchemaBranchCache)2 SchemaVersionInfoCache (com.hortonworks.registries.schemaregistry.cache.SchemaVersionInfoCache)2 IncompatibleSchemaException (com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException)2 InvalidSchemaBranchVersionMapping (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchVersionMapping)2 InvalidSchemaException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException)2 SchemaBranchNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException)2 SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)2 UnsupportedSchemaTypeException (com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException)2 SchemaLifecycleException (com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException)2 SchemaVersionLifecycleContext (com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleContext)2 SchemaVersionLifecycleStates (com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStates)2