|
1 | 1 | package io.vertx.tests.redis.client;
|
2 | 2 |
|
3 |
| -import io.vertx.core.AbstractVerticle; |
| 3 | +import io.vertx.core.Future; |
| 4 | +import io.vertx.core.VerticleBase; |
4 | 5 | import io.vertx.core.Vertx;
|
5 | 6 | import io.vertx.redis.client.Redis;
|
6 | 7 | import io.vertx.redis.client.Response;
|
|
14 | 15 | import static io.vertx.redis.client.Command.COMMAND;
|
15 | 16 | import static io.vertx.redis.client.Request.cmd;
|
16 | 17 |
|
17 |
| -public class CommandGenerator extends AbstractVerticle { |
| 18 | +public class CommandGenerator extends VerticleBase { |
18 | 19 |
|
19 | 20 | public static void main(String[] args) {
|
20 |
| - Vertx.vertx().deployVerticle(new CommandGenerator()); |
| 21 | + Vertx vertx = Vertx.vertx(); |
| 22 | + try { |
| 23 | + vertx |
| 24 | + .deployVerticle(new CommandGenerator()) |
| 25 | + .await(); |
| 26 | + } finally { |
| 27 | + vertx.close(); |
| 28 | + } |
21 | 29 | }
|
22 | 30 |
|
23 | 31 | @Override
|
24 |
| - public void start() { |
| 32 | + public Future<?> start() { |
25 | 33 | Redis client = Redis.createClient(vertx);
|
26 | 34 |
|
27 | 35 | Map<String, String> commandDocs = new HashMap<>();
|
28 | 36 |
|
29 |
| - client.send(cmd(COMMAND).arg("DOCS")) |
| 37 | + return client.send(cmd(COMMAND).arg("DOCS")) |
30 | 38 | .compose(resp -> {
|
31 | 39 | for (String key : resp.getKeys()) {
|
32 | 40 | Response doc = resp.get(key);
|
@@ -60,141 +68,141 @@ public void start() {
|
60 | 68 | }
|
61 | 69 |
|
62 | 70 | return client.send(cmd(COMMAND).arg("INFO"));
|
63 |
| - }).onSuccess(res -> { |
64 |
| - List<String> commands = new ArrayList<>(); |
65 |
| - Map<String, String> commandInstantiation = new HashMap<>(); |
66 |
| - Map<String, String> knownCommands = new HashMap<>(); |
| 71 | + }).andThen(ar -> { |
67 | 72 |
|
68 |
| - res.forEach(cmd -> { |
69 |
| - String commandName = cmd.get(0).toString(); |
| 73 | + if (ar.succeeded()) { |
70 | 74 |
|
71 |
| - Boolean ro = null; |
72 |
| - boolean getkeys = false; |
| 75 | + Response res = ar.result(); |
73 | 76 |
|
74 |
| - for (Response flag : cmd.get(2)) { |
75 |
| - if ("readonly".equalsIgnoreCase(flag.toString())) { |
76 |
| - ro = true; |
77 |
| - } |
78 |
| - if ("write".equalsIgnoreCase(flag.toString())) { |
79 |
| - ro = false; |
80 |
| - } |
81 |
| - } |
| 77 | + List<String> commands = new ArrayList<>(); |
| 78 | + Map<String, String> commandInstantiation = new HashMap<>(); |
| 79 | + Map<String, String> knownCommands = new HashMap<>(); |
82 | 80 |
|
83 |
| - String keyLocator = null; |
| 81 | + res.forEach(cmd -> { |
| 82 | + String commandName = cmd.get(0).toString(); |
84 | 83 |
|
85 |
| - if (cmd.get(8).size() > 0) { |
| 84 | + Boolean ro = null; |
| 85 | + boolean getkeys = false; |
86 | 86 |
|
87 |
| - for (Response hint : cmd.get(8)) { |
88 |
| - String beginSearch = null; |
89 |
| - String findKeys = null; |
90 |
| - Boolean flagRO = null; |
| 87 | + for (Response flag : cmd.get(2)) { |
| 88 | + if ("readonly".equalsIgnoreCase(flag.toString())) { |
| 89 | + ro = true; |
| 90 | + } |
| 91 | + if ("write".equalsIgnoreCase(flag.toString())) { |
| 92 | + ro = false; |
| 93 | + } |
| 94 | + } |
91 | 95 |
|
92 |
| - if (hint.size() > 0) { |
93 |
| - if (hint.containsKey("flags")) { |
94 |
| - for (Response flag : hint.get("flags")) { |
95 |
| - if ("RO".equalsIgnoreCase(flag.toString())) { |
96 |
| - flagRO = true; |
97 |
| - break; |
98 |
| - } |
99 |
| - if ("RW".equalsIgnoreCase(flag.toString()) || "OW".equalsIgnoreCase(flag.toString()) || "RM".equalsIgnoreCase(flag.toString())) { |
100 |
| - flagRO = false; |
101 |
| - break; |
| 96 | + String keyLocator = null; |
| 97 | + |
| 98 | + if (cmd.get(8).size() > 0) { |
| 99 | + |
| 100 | + for (Response hint : cmd.get(8)) { |
| 101 | + String beginSearch = null; |
| 102 | + String findKeys = null; |
| 103 | + Boolean flagRO = null; |
| 104 | + |
| 105 | + if (hint.size() > 0) { |
| 106 | + if (hint.containsKey("flags")) { |
| 107 | + for (Response flag : hint.get("flags")) { |
| 108 | + if ("RO".equalsIgnoreCase(flag.toString())) { |
| 109 | + flagRO = true; |
| 110 | + break; |
| 111 | + } |
| 112 | + if ("RW".equalsIgnoreCase(flag.toString()) || "OW".equalsIgnoreCase(flag.toString()) || "RM".equalsIgnoreCase(flag.toString())) { |
| 113 | + flagRO = false; |
| 114 | + break; |
| 115 | + } |
102 | 116 | }
|
103 | 117 | }
|
104 |
| - } |
105 |
| - if (hint.containsKey("begin_search")) { |
106 |
| - String type = hint.get("begin_search").get("type").toString(); |
107 |
| - Response spec = hint.get("begin_search").get("spec"); |
108 |
| - switch (type) { |
109 |
| - case "index": |
110 |
| - beginSearch = "new BeginSearchIndex(" + spec.get("index").toInteger() + ")"; |
111 |
| - break; |
112 |
| - case "keyword": |
113 |
| - beginSearch = "new BeginSearchKeyword(\"" + spec.get("keyword").toString() + "\", " + spec.get("startfrom").toInteger() + ")"; |
114 |
| - break; |
115 |
| - case "unknown": |
116 |
| - getkeys = true; |
117 |
| - System.err.println(cmd); |
118 |
| - break; |
| 118 | + if (hint.containsKey("begin_search")) { |
| 119 | + String type = hint.get("begin_search").get("type").toString(); |
| 120 | + Response spec = hint.get("begin_search").get("spec"); |
| 121 | + switch (type) { |
| 122 | + case "index": |
| 123 | + beginSearch = "new BeginSearchIndex(" + spec.get("index").toInteger() + ")"; |
| 124 | + break; |
| 125 | + case "keyword": |
| 126 | + beginSearch = "new BeginSearchKeyword(\"" + spec.get("keyword").toString() + "\", " + spec.get("startfrom").toInteger() + ")"; |
| 127 | + break; |
| 128 | + case "unknown": |
| 129 | + getkeys = true; |
| 130 | + System.err.println(cmd); |
| 131 | + break; |
| 132 | + } |
119 | 133 | }
|
120 |
| - } |
121 |
| - if (hint.containsKey("find_keys")) { |
122 |
| - String type = hint.get("find_keys").get("type").toString(); |
123 |
| - Response spec = hint.get("find_keys").get("spec"); |
124 |
| - switch (type) { |
125 |
| - case "range": |
126 |
| - findKeys = "new FindKeysRange(" + spec.get("lastkey").toInteger() + ", " + spec.get("keystep").toInteger() + ", " + spec.get("limit").toInteger() + ")"; |
127 |
| - break; |
128 |
| - case "keynum": |
129 |
| - findKeys = "new FindKeysKeynum(" + spec.get("keynumidx").toInteger() + ", " + spec.get("firstkey").toInteger() + ", " + spec.get("keystep").toInteger() + ")"; |
130 |
| - break; |
131 |
| - case "unknown": |
132 |
| - getkeys = true; |
133 |
| - System.err.println(cmd); |
134 |
| - break; |
| 134 | + if (hint.containsKey("find_keys")) { |
| 135 | + String type = hint.get("find_keys").get("type").toString(); |
| 136 | + Response spec = hint.get("find_keys").get("spec"); |
| 137 | + switch (type) { |
| 138 | + case "range": |
| 139 | + findKeys = "new FindKeysRange(" + spec.get("lastkey").toInteger() + ", " + spec.get("keystep").toInteger() + ", " + spec.get("limit").toInteger() + ")"; |
| 140 | + break; |
| 141 | + case "keynum": |
| 142 | + findKeys = "new FindKeysKeynum(" + spec.get("keynumidx").toInteger() + ", " + spec.get("firstkey").toInteger() + ", " + spec.get("keystep").toInteger() + ")"; |
| 143 | + break; |
| 144 | + case "unknown": |
| 145 | + getkeys = true; |
| 146 | + System.err.println(cmd); |
| 147 | + break; |
| 148 | + } |
135 | 149 | }
|
136 | 150 | }
|
137 |
| - } |
138 | 151 |
|
139 |
| - if (beginSearch != null && findKeys != null) { |
140 |
| - if (keyLocator == null) { |
141 |
| - keyLocator = "new KeyLocator(" + flagRO + ", " + beginSearch + ", " + findKeys + ")"; |
142 |
| - } else { |
143 |
| - keyLocator += ", new KeyLocator(" + flagRO + ", " + beginSearch + ", " + findKeys + ")"; |
| 152 | + if (beginSearch != null && findKeys != null) { |
| 153 | + if (keyLocator == null) { |
| 154 | + keyLocator = "new KeyLocator(" + flagRO + ", " + beginSearch + ", " + findKeys + ")"; |
| 155 | + } else { |
| 156 | + keyLocator += ", new KeyLocator(" + flagRO + ", " + beginSearch + ", " + findKeys + ")"; |
| 157 | + } |
144 | 158 | }
|
145 | 159 | }
|
146 | 160 | }
|
147 |
| - } |
148 | 161 |
|
149 |
| - boolean pubSub = false; |
| 162 | + boolean pubSub = false; |
150 | 163 |
|
151 |
| - for (Response flag : cmd.get(2)) { |
152 |
| - if ("pubsub".equals(flag.toString())) { |
153 |
| - // we exclude PUBSUB / PUBLISH / SPUBLISH from the flag |
154 |
| - if ("pubsub".equalsIgnoreCase(commandName) |
| 164 | + for (Response flag : cmd.get(2)) { |
| 165 | + if ("pubsub".equals(flag.toString())) { |
| 166 | + // we exclude PUBSUB / PUBLISH / SPUBLISH from the flag |
| 167 | + if ("pubsub".equalsIgnoreCase(commandName) |
155 | 168 | || "publish".equalsIgnoreCase(commandName)
|
156 | 169 | || "spublish".equalsIgnoreCase(commandName)) {
|
157 |
| - continue; |
| 170 | + continue; |
| 171 | + } |
| 172 | + pubSub = true; |
| 173 | + break; |
158 | 174 | }
|
159 |
| - pubSub = true; |
160 |
| - break; |
161 | 175 | }
|
162 |
| - } |
163 | 176 |
|
164 |
| - commands.add(commandName); |
165 |
| - |
166 |
| - commandInstantiation.put(commandName, |
167 |
| - generateCommand( |
168 |
| - commandName, |
169 |
| - cmd.get(1).toInteger(), |
170 |
| - ro, |
171 |
| - pubSub, |
172 |
| - getkeys, |
173 |
| - keyLocator |
174 |
| - )); |
175 |
| - |
176 |
| - knownCommands.put(commandName, generateCommandMap(commandName)); |
177 |
| - }); |
178 |
| - |
179 |
| - commands.sort(Comparator.comparing(this::toIdentifier)); |
180 |
| - for (String cmd : commands) { |
181 |
| - System.out.print(commandDocs.get(cmd)); |
182 |
| - System.out.println(commandInstantiation.get(cmd)); |
183 |
| - } |
| 177 | + commands.add(commandName); |
| 178 | + |
| 179 | + commandInstantiation.put(commandName, |
| 180 | + generateCommand( |
| 181 | + commandName, |
| 182 | + cmd.get(1).toInteger(), |
| 183 | + ro, |
| 184 | + pubSub, |
| 185 | + getkeys, |
| 186 | + keyLocator |
| 187 | + )); |
| 188 | + |
| 189 | + knownCommands.put(commandName, generateCommandMap(commandName)); |
| 190 | + }); |
| 191 | + |
| 192 | + commands.sort(Comparator.comparing(this::toIdentifier)); |
| 193 | + for (String cmd : commands) { |
| 194 | + System.out.print(commandDocs.get(cmd)); |
| 195 | + System.out.println(commandInstantiation.get(cmd)); |
| 196 | + } |
184 | 197 |
|
185 |
| - System.out.println(); |
186 |
| - System.out.println("----------------------------------------------------------------"); |
187 |
| - System.out.println(); |
| 198 | + System.out.println(); |
| 199 | + System.out.println("----------------------------------------------------------------"); |
| 200 | + System.out.println(); |
188 | 201 |
|
189 |
| - for (String cmd : commands) { |
190 |
| - System.out.println(knownCommands.get(cmd)); |
| 202 | + for (String cmd : commands) { |
| 203 | + System.out.println(knownCommands.get(cmd)); |
| 204 | + } |
191 | 205 | }
|
192 |
| - |
193 |
| - vertx.close(); |
194 |
| - }) |
195 |
| - .onFailure(err -> { |
196 |
| - err.printStackTrace(); |
197 |
| - System.exit(1); |
198 | 206 | });
|
199 | 207 | }
|
200 | 208 |
|
|
0 commit comments