Skip to content

Commit cfe4f22

Browse files
committed
DEV: update gears v1 example
1 parent 1d88617 commit cfe4f22

File tree

1 file changed

+15
-17
lines changed
  • content/operate/oss_and_stack/stack-with-enterprise/gears-v1/jvm/classes/readers

1 file changed

+15
-17
lines changed

content/operate/oss_and_stack/stack-with-enterprise/gears-v1/jvm/classes/readers/commandoverrider.md

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,24 @@ CommandOverrider overrider = new CommandOverrider();
5151
overrider.setCommand("HSET");
5252
// Only override HSET for keys that start with "user:"
5353
overrider.setPrefix("user:");
54-
54+
5555
// Create the data pipe builder
5656
GearsBuilder.CreateGearsBuilder(overrider).map(r-> {
57-
// Extract the key from the command arguments
58-
String keyName = new String((byte[]) r[1], StandardCharsets.UTF_8);
59-
60-
// Add a last_modified timestamp to the user's profile
61-
GearsBuilder.execute("HSET", keyName, "last_modified", Long.toString(System.currentTimeMillis()));
62-
63-
// Get the original HSET arguments
64-
ArrayList<String> commandArray = new ArrayList<String>();
65-
for (int i=1; i < r.length; i++) {
66-
commandArray.add(new String((byte[]) r[i], StandardCharsets.UTF_8));
57+
// Get the operation arguments
58+
ArrayList<String> operationArguments = new ArrayList<String>();
59+
for (int i = 1; i < r.length; i++) {
60+
operationArguments.add(new String((byte[]) r[i], StandardCharsets.UTF_8));
6761
}
68-
69-
// Run the original HSET command
70-
GearsBuilder.callNext(commandArray.toArray(new String[0]));
71-
72-
return "OK";
73-
}).register();
62+
63+
// Add a last-modified field and a corresponding timestamp to the operation arguments
64+
operationArguments.add("last-modified");
65+
operationArguments.add(Long.toString(System.currentTimeMillis()));
66+
67+
// Run the enriched HSET operation
68+
Object reply = GearsBuilder.callNext(operationArguments.toArray(new String[0]));
69+
70+
return reply.toString();
71+
}).register(ExecutionMode.SYNC);
7472
```
7573

7674
After you register the previous code with the `RG.JEXECUTE` command, try to update the user's data with `HSET` to test it:

0 commit comments

Comments
 (0)