1 /*
  2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 import jdk.incubator.code.Op;
 25 import jdk.incubator.code.dialect.java.ClassType;
 26 import jdk.incubator.code.dialect.java.JavaOp;
 27 import jdk.incubator.code.dialect.java.MethodRef;
 28 import jdk.incubator.code.dialect.java.JavaType;
 29 import java.util.stream.Stream;
 30 
 31 import static jdk.incubator.code.dialect.java.JavaType.parameterized;
 32 import static jdk.incubator.code.dialect.java.MethodRef.method;
 33 import static jdk.incubator.code.dialect.core.CoreOp.*;
 34 import static jdk.incubator.code.dialect.core.CoreType.functionType;
 35 import static jdk.incubator.code.dialect.java.JavaType.type;
 36 
 37 public interface Queryable<T> {
 38     JavaType TYPE = type(Queryable.class);
 39 
 40     QueryProvider provider();
 41 
 42     // T
 43     JavaType elementType();
 44 
 45     // Queryable<T> -> Queryable<U>
 46     FuncOp expression();
 47 
 48     @SuppressWarnings("unchecked")
 49     default Queryable<T> where(QuotablePredicate<T> f) {
 50         JavaOp.LambdaOp l = (JavaOp.LambdaOp) Op.ofQuotable(f).get().op();
 51         return (Queryable<T>) insertQuery(elementType(), "where", l);
 52     }
 53 
 54     @SuppressWarnings("unchecked")
 55     default <R> Queryable<R> select(QuotableFunction<T, R> f) {
 56         JavaOp.LambdaOp l = (JavaOp.LambdaOp) Op.ofQuotable(f).get().op();
 57         return (Queryable<R>) insertQuery((JavaType) l.invokableType().returnType(), "select", l);
 58     }
 59 
 60     private Queryable<?> insertQuery(JavaType elementType, String methodName, JavaOp.LambdaOp lambdaOp) {
 61         // Copy function expression, replacing return operation
 62         FuncOp queryExpression = expression();
 63         JavaType queryableType = parameterized(Queryable.TYPE, elementType);
 64         FuncOp nextQueryExpression = func("query",
 65                 functionType(queryableType, queryExpression.invokableType().parameterTypes()))
 66                 .body(b -> b.inline(queryExpression, b.parameters(), (block, query) -> {
 67                     Op.Result fi = block.op(lambdaOp);
 68 
 69                     MethodRef md = method(Queryable.TYPE, methodName,
 70                             functionType(Queryable.TYPE, ((ClassType) lambdaOp.functionalInterface()).rawType()));
 71                     Op.Result queryable = block.op(JavaOp.invoke(queryableType, md, query, fi));
 72 
 73                     block.op(return_(queryable));
 74                 }));
 75 
 76         return provider().createQuery(elementType, nextQueryExpression);
 77     }
 78 
 79     @SuppressWarnings("unchecked")
 80     default QueryResult<Stream<T>> elements() {
 81         JavaType resultType = parameterized(type(Stream.class), elementType());
 82         return (QueryResult<Stream<T>>) insertQueryResult(resultType, "elements");
 83     }
 84 
 85     @SuppressWarnings("unchecked")
 86     default QueryResult<Long> count() {
 87         JavaType resultType = JavaType.LONG;
 88         return (QueryResult<Long>) insertQueryResult(resultType, "count");
 89     }
 90 
 91     private QueryResult<?> insertQueryResult(JavaType resultType, String methodName) {
 92         // Copy function expression, replacing return operation
 93         FuncOp queryExpression = expression();
 94         JavaType queryResultType = parameterized(QueryResult.TYPE, resultType);
 95         FuncOp queryResultExpression = func("queryResult",
 96                 functionType(queryResultType, queryExpression.invokableType().parameterTypes()))
 97                 .body(b -> b.inline(queryExpression, b.parameters(), (block, query) -> {
 98                     MethodRef md = method(Queryable.TYPE, methodName,
 99                             functionType(QueryResult.TYPE));
100                     Op.Result queryResult = block.op(JavaOp.invoke(queryResultType, md, query));
101 
102                     block.op(return_(queryResult));
103                 }));
104 
105         return provider().createQueryResult(resultType, queryResultExpression);
106     }
107 }