You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2020/06/10 03:35:16 UTC

[GitHub] [ignite] akuznetsov-gridgain commented on a change in pull request #7912: IGNITE-13133: Add support of QuerydslPredicateExecutor for ignite-spring-data

akuznetsov-gridgain commented on a change in pull request #7912:
URL: https://github.com/apache/ignite/pull/7912#discussion_r437840896



##########
File path: modules/spring-data-2.0/src/main/java/org/apache/ignite/springdata20/repository/support/QueryPredicateExecutorImpl.java
##########
@@ -0,0 +1,205 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.springdata20.repository.support;
+
+import com.querydsl.core.types.OrderSpecifier;
+import com.querydsl.core.types.Predicate;
+import com.querydsl.sql.Configuration;
+import com.querydsl.sql.SQLSerializer;
+import com.querydsl.sql.SQLTemplates;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cache.query.QueryCursor;
+import org.apache.ignite.cache.query.SqlFieldsQuery;
+import org.apache.ignite.springdata20.repository.query.IgniteQuery;
+import org.apache.ignite.springdata20.repository.query.IgniteQueryGenerator;
+import org.apache.ignite.springdata20.repository.query.ReturnStrategy;
+import org.apache.ignite.springdata20.repository.query.QueryUtils;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageImpl;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.querydsl.QuerydslPredicateExecutor;
+import org.springframework.data.repository.core.RepositoryMetadata;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Optional;
+
+import static java.util.stream.Collectors.toList;
+
+/**
+ * General Apache Ignite query predicate executor implementation.
+ * This bean shouldn't ever be loaded by context directly, only via {@link IgniteRepositoryFactory}
+ */
+public class QueryPredicateExecutorImpl<T> implements QuerydslPredicateExecutor<T> {
+    /** Config for SQL serializer. */
+    private final Configuration conf = new Configuration(SQLTemplates.DEFAULT);
+
+    /** Underlying cache to execute SQL queries. */
+    private final IgniteCache cache;
+
+    /** Result type. */
+    private final Class<?> type;
+
+    /**
+     * @param cache Cache.
+     * @param metadata Metadata.
+     */
+    public QueryPredicateExecutorImpl(IgniteCache cache, RepositoryMetadata metadata) {
+        this.cache = cache;
+        this.type = metadata.getDomainType();
+    }
+
+    /** {@inheritDoc} */
+    @Override public Optional<T> findOne(Predicate pred) {
+        return executeQuery(prepareSelectQuery(pred), PageRequest.of(0, 1)).stream().findFirst();
+    }
+
+    /** {@inheritDoc} */
+    @Override public Iterable<T> findAll(Predicate pred) {
+        return executeQuery(prepareSelectQuery(pred));
+    }
+
+    /** {@inheritDoc} */
+    @Override public Iterable<T> findAll(Predicate pred, Sort sort) {
+        return executeQuery(prepareSelectQuery(pred), sort);
+    }
+
+    /** {@inheritDoc} */
+    @Override public Iterable<T> findAll(Predicate pred, OrderSpecifier<?>... orders) {
+        Sort sort = Sort.by(mapQryDslOrderSpecifiersToOrders(orders));
+
+        return executeQuery(prepareSelectQuery(pred), sort);
+    }
+
+    /** {@inheritDoc} */
+    @Override public Iterable<T> findAll(OrderSpecifier<?>... orders) {
+        Sort sort = Sort.by(mapQryDslOrderSpecifiersToOrders(orders));
+
+        return executeQuery(prepareSqlQuery("SELECT * FROM " + type.getSimpleName()), sort);
+    }
+
+    /** {@inheritDoc} */
+    @Override public Page<T> findAll(Predicate pred, Pageable pageable) {
+        return new PageImpl<>(executeQuery(prepareSelectQuery(pred), pageable), pageable, 0);
+    }
+
+    /** {@inheritDoc} */
+    @Override public long count(Predicate pred) {
+        SQLSerializer ser = prepareSqlQuery("SELECT COUNT(*) FROM " + type.getSimpleName() + " WHERE ", pred);
+
+        SqlFieldsQuery qry = new SqlFieldsQuery(ser.toString());
+        qry.setArgs(ser.getConstants().toArray());
+
+        List<List<?>> res = cache.query(qry).getAll();
+
+        return (Long) res.get(0).get(0);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean exists(Predicate pred) {
+        return !executeQuery(prepareSelectQuery(pred), PageRequest.of(0, 1)).isEmpty();
+    }
+
+    /**
+     * @param pred Predicate.
+     * @return Prepared sql serializer.
+     */
+    private SQLSerializer prepareSelectQuery(Predicate pred) {
+        return prepareSqlQuery(
+            "SELECT * FROM " + type.getSimpleName() + " WHERE ",
+            pred
+        );
+    }
+
+    /**
+     * @param selectPart Select part.
+     * @return Prepared sql serializer.
+     */
+    private SQLSerializer prepareSqlQuery(String selectPart) {
+        return prepareSqlQuery(selectPart, null);
+    }
+
+    /**
+     * @param selectPart Select partition.
+     * @param pred Predicate.
+     * @return Prepared sql serializer.
+     */
+    private SQLSerializer prepareSqlQuery(String selectPart, Predicate pred) {
+        SQLSerializer res = new SQLSerializer(conf).append(selectPart + " ");
+
+        if (pred != null)
+            return res.handle(pred);
+
+        return res;
+    }
+
+    /**
+     * @param ser Serializable.
+     * @param additionalParams Additional params.
+     * @return List of results.
+     */
+    @SuppressWarnings({"unchecked", "rawtypes"})
+    private List<T> executeQuery(SQLSerializer ser, Object... additionalParams) {
+        List<Object> params = ser.getConstants();
+        params.addAll(Arrays.asList(additionalParams));
+
+        Object[] arrParams = params.toArray();
+
+        IgniteQuery qry = IgniteQueryGenerator.prepareQuery(type, ser.toString(), arrParams);
+
+        QueryCursor qryCursor = cache.query(QueryUtils.prepareQuery(qry, arrParams));
+
+        return (List<T>) QueryUtils.transformQueryCursor(qryCursor, arrParams, false, ReturnStrategy.LIST_OF_VALUES);

Review comment:
       I think you qryCursor should be wrapped with try-with-resource in order to close cursor after query execution.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org