Skip to content

Commit

Permalink
function example
Browse files Browse the repository at this point in the history
  • Loading branch information
chenby committed Jan 11, 2024
1 parent 3bdefd9 commit 258dcfc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.moilioncircle.examples.migration;

import static redis.clients.jedis.Protocol.Command.AUTH;
import static redis.clients.jedis.Protocol.Command.RESTORE;
import static redis.clients.jedis.Protocol.Command.SELECT;
import static redis.clients.jedis.Protocol.toByteArray;

Expand All @@ -40,13 +39,16 @@
import com.moilioncircle.redis.replicator.event.EventListener;
import com.moilioncircle.redis.replicator.rdb.datatype.DB;
import com.moilioncircle.redis.replicator.rdb.dump.DumpRdbVisitor;
import com.moilioncircle.redis.replicator.rdb.dump.datatype.DumpFunction;
import com.moilioncircle.redis.replicator.rdb.dump.datatype.DumpKeyValuePair;
import com.moilioncircle.redis.replicator.util.Strings;

import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.args.FunctionRestorePolicy;
import redis.clients.jedis.params.RestoreParams;

/**
* @author Leon Chen
Expand Down Expand Up @@ -84,6 +86,15 @@ public static void sync(String sourceUri, String targetUri) throws IOException,
r.addEventListener(new EventListener() {
@Override
public void onEvent(Replicator replicator, Event event) {

// function since redis 7.0
if (event instanceof DumpFunction) {
DumpFunction dfn = (DumpFunction) event;
Object r = target.restoreFunction(dfn.getSerialized(), true);
System.out.println(r);
}

// key value
if (event instanceof DumpKeyValuePair) {
DumpKeyValuePair dkv = (DumpKeyValuePair) event;
// Step1: select db
Expand All @@ -107,6 +118,7 @@ public void onEvent(Replicator replicator, Event event) {
}
}

// incremental commands
if (event instanceof DefaultCommand) {
// Step3: sync aof command
DefaultCommand dc = (DefaultCommand) event;
Expand Down Expand Up @@ -255,9 +267,17 @@ public Object send(final byte[] cmd, final byte[]... args) {

public Object restore(byte[] key, long expired, byte[] dumped, boolean replace) {
if (!replace) {
return send(RESTORE, key, toByteArray(expired), dumped);
return jedis.restore(key, expired, dumped);
} else {
return jedis.restore(key, expired, dumped, RestoreParams.restoreParams().replace());
}
}

public Object restoreFunction(byte[] dumped, boolean replace) {
if (!replace) {
return jedis.functionRestore(dumped, FunctionRestorePolicy.APPEND);
} else {
return send(RESTORE, key, toByteArray(expired), dumped, "REPLACE".getBytes());
return jedis.functionRestore(dumped, FunctionRestorePolicy.REPLACE);
}
}

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.4.2</version>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down

0 comments on commit 258dcfc

Please sign in to comment.