|
| 1 | +/* |
| 2 | + * Copyright 2022 dengliming. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.github.dengliming.redismodule.redisgraph; |
| 18 | + |
| 19 | +import io.github.dengliming.redismodule.common.util.RAssert; |
| 20 | +import io.github.dengliming.redismodule.redisgraph.model.ResultSet; |
| 21 | +import io.github.dengliming.redismodule.redisgraph.model.SlowLogItem; |
| 22 | +import org.redisson.api.RFuture; |
| 23 | +import org.redisson.client.codec.Codec; |
| 24 | +import org.redisson.client.codec.StringCodec; |
| 25 | +import org.redisson.command.CommandAsyncExecutor; |
| 26 | + |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +import static io.github.dengliming.redismodule.redisgraph.protocol.Keywords.__COMPACT; |
| 31 | +import static io.github.dengliming.redismodule.redisgraph.protocol.RedisCommands.GRAPH_CONFIG_GET; |
| 32 | +import static io.github.dengliming.redismodule.redisgraph.protocol.RedisCommands.GRAPH_CONFIG_SET; |
| 33 | +import static io.github.dengliming.redismodule.redisgraph.protocol.RedisCommands.GRAPH_DELETE; |
| 34 | +import static io.github.dengliming.redismodule.redisgraph.protocol.RedisCommands.GRAPH_EXPLAIN; |
| 35 | +import static io.github.dengliming.redismodule.redisgraph.protocol.RedisCommands.GRAPH_LIST; |
| 36 | +import static io.github.dengliming.redismodule.redisgraph.protocol.RedisCommands.GRAPH_PROFILE; |
| 37 | +import static io.github.dengliming.redismodule.redisgraph.protocol.RedisCommands.GRAPH_QUERY; |
| 38 | +import static io.github.dengliming.redismodule.redisgraph.protocol.RedisCommands.GRAPH_READ_ONLY_QUERY; |
| 39 | +import static io.github.dengliming.redismodule.redisgraph.protocol.RedisCommands.GRAPH_SLOWLOG; |
| 40 | + |
| 41 | +public class RedisGraph { |
| 42 | + |
| 43 | + private final CommandAsyncExecutor commandExecutor; |
| 44 | + private final Codec codec; |
| 45 | + |
| 46 | + public RedisGraph(CommandAsyncExecutor commandExecutor) { |
| 47 | + this(commandExecutor, commandExecutor.getConnectionManager().getCodec()); |
| 48 | + } |
| 49 | + |
| 50 | + public RedisGraph(CommandAsyncExecutor commandExecutor, Codec codec) { |
| 51 | + this.commandExecutor = commandExecutor; |
| 52 | + this.codec = codec; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Retrieves the current value of a RedisGraph configuration parameter. |
| 57 | + * <p> |
| 58 | + * GRAPH.CONFIG GET name |
| 59 | + * |
| 60 | + * @param parameter |
| 61 | + * @return |
| 62 | + */ |
| 63 | + public Map<String, Object> getConfig(String parameter) { |
| 64 | + return commandExecutor.get(getConfigAsync(parameter)); |
| 65 | + } |
| 66 | + |
| 67 | + public RFuture<Map<String, Object>> getConfigAsync(String parameter) { |
| 68 | + RAssert.notEmpty(parameter, "parameter must not be empty"); |
| 69 | + |
| 70 | + return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, GRAPH_CONFIG_GET, parameter); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Set the value of a RedisGraph configuration parameter. |
| 75 | + * <p> |
| 76 | + * GRAPH.CONFIG SET name value |
| 77 | + * |
| 78 | + * @param name |
| 79 | + * @param value |
| 80 | + * @return |
| 81 | + */ |
| 82 | + public Boolean setConfig(String name, Object value) { |
| 83 | + return commandExecutor.get(setConfigAsync(name, value)); |
| 84 | + } |
| 85 | + |
| 86 | + public RFuture<Boolean> setConfigAsync(String name, Object value) { |
| 87 | + RAssert.notEmpty(name, "name must not be empty"); |
| 88 | + RAssert.notNull(value, "value must not be null"); |
| 89 | + |
| 90 | + return commandExecutor.readAsync(getName(), codec, GRAPH_CONFIG_SET, name, value); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Completely removes the graph and all of its entities. |
| 95 | + * <p> |
| 96 | + * GRAPH.DELETE graph |
| 97 | + * |
| 98 | + * @param name |
| 99 | + * @return |
| 100 | + */ |
| 101 | + public String delete(String name) { |
| 102 | + return commandExecutor.get(deleteAsync(name)); |
| 103 | + } |
| 104 | + |
| 105 | + public RFuture<String> deleteAsync(String name) { |
| 106 | + RAssert.notEmpty(name, "name must not be empty"); |
| 107 | + |
| 108 | + return commandExecutor.writeAsync(name, StringCodec.INSTANCE, GRAPH_DELETE, name); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Lists all graph keys in the keyspace. |
| 113 | + * <p> |
| 114 | + * GRAPH.LIST |
| 115 | + * |
| 116 | + * @return |
| 117 | + */ |
| 118 | + public List<String> list() { |
| 119 | + return commandExecutor.get(listAsync()); |
| 120 | + } |
| 121 | + |
| 122 | + public RFuture<List<String>> listAsync() { |
| 123 | + return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, GRAPH_LIST); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Executes a query and produces an execution plan augmented with metrics for each operation's execution. |
| 128 | + * <p> |
| 129 | + * GRAPH.PROFILE graph query [TIMEOUT timeout] |
| 130 | + * |
| 131 | + * @return |
| 132 | + */ |
| 133 | + public List<String> profile(String graphName, String query, long timeout) { |
| 134 | + return commandExecutor.get(profileAsync(graphName, query, timeout)); |
| 135 | + } |
| 136 | + |
| 137 | + public RFuture<List<String>> profileAsync(String graphName, String query, long timeout) { |
| 138 | + if (timeout > 0) { |
| 139 | + return commandExecutor.readAsync(graphName, StringCodec.INSTANCE, GRAPH_PROFILE, graphName, query, timeout); |
| 140 | + } |
| 141 | + return commandExecutor.readAsync(graphName, StringCodec.INSTANCE, GRAPH_PROFILE, graphName, query); |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | + /** |
| 146 | + * Constructs a query execution plan but does not run it. Inspect this execution plan to better understand how |
| 147 | + * your query will get executed. |
| 148 | + * <p> |
| 149 | + * GRAPH.EXPLAIN graph query |
| 150 | + * |
| 151 | + * @return |
| 152 | + */ |
| 153 | + public List<String> explain(String graphName, String query) { |
| 154 | + return commandExecutor.get(explainAsync(graphName, query)); |
| 155 | + } |
| 156 | + |
| 157 | + public RFuture<List<String>> explainAsync(String graphName, String query) { |
| 158 | + return commandExecutor.readAsync(graphName, codec, GRAPH_EXPLAIN, graphName, query); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Returns a list containing up to 10 of the slowest queries issued against the given graph ID. |
| 163 | + * <p> |
| 164 | + * GRAPH.SLOWLOG graph |
| 165 | + * |
| 166 | + * @return |
| 167 | + */ |
| 168 | + public List<SlowLogItem> slowLog(String graphName) { |
| 169 | + return commandExecutor.get(slowLogAsync(graphName)); |
| 170 | + } |
| 171 | + |
| 172 | + public RFuture<List<SlowLogItem>> slowLogAsync(String graphName) { |
| 173 | + return commandExecutor.readAsync(graphName, StringCodec.INSTANCE, GRAPH_SLOWLOG, graphName); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Executes the given query against a specified graph. |
| 178 | + * <p> |
| 179 | + * GRAPH.QUERY graph query [TIMEOUT timeout] |
| 180 | + * |
| 181 | + * @return |
| 182 | + */ |
| 183 | + public ResultSet query(String graphName, String query, long timeout) { |
| 184 | + return commandExecutor.get(queryAsync(graphName, query, timeout)); |
| 185 | + } |
| 186 | + |
| 187 | + public RFuture<ResultSet> queryAsync(String graphName, String query, long timeout) { |
| 188 | + if (timeout > 0) { |
| 189 | + return commandExecutor.readAsync(graphName, StringCodec.INSTANCE, GRAPH_QUERY, graphName, query, timeout, __COMPACT.getAlias()); |
| 190 | + } |
| 191 | + return commandExecutor.readAsync(graphName, StringCodec.INSTANCE, GRAPH_QUERY, graphName, query, __COMPACT.getAlias()); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Executes a given read only query against a specified graph. |
| 196 | + * <p> |
| 197 | + * GRAPH.RO_QUERY graph query [TIMEOUT timeout] |
| 198 | + * |
| 199 | + * @return |
| 200 | + */ |
| 201 | + public ResultSet readOnlyQuery(String graphName, String query, long timeout) { |
| 202 | + return commandExecutor.get(readOnlyQueryAsync(graphName, query, timeout)); |
| 203 | + } |
| 204 | + |
| 205 | + public RFuture<ResultSet> readOnlyQueryAsync(String graphName, String query, long timeout) { |
| 206 | + if (timeout > 0) { |
| 207 | + return commandExecutor.readAsync(graphName, StringCodec.INSTANCE, GRAPH_READ_ONLY_QUERY, graphName, query, timeout, __COMPACT.getAlias()); |
| 208 | + } |
| 209 | + return commandExecutor.readAsync(graphName, StringCodec.INSTANCE, GRAPH_READ_ONLY_QUERY, graphName, query, __COMPACT.getAlias()); |
| 210 | + } |
| 211 | + |
| 212 | + public String getName() { |
| 213 | + return ""; |
| 214 | + } |
| 215 | +} |
0 commit comments