/*
 * Copyright 2016-2018 Leon Chen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.moilioncircle.examples.migration;

import com.moilioncircle.examples.migration.cmd.DefaultCommand;
import com.moilioncircle.examples.migration.rdb.MigrationKeyValuePair;
import com.moilioncircle.redis.replicator.CloseListener;
import com.moilioncircle.redis.replicator.Configuration;
import com.moilioncircle.redis.replicator.Replicator;
import com.moilioncircle.redis.replicator.cmd.Command;
import com.moilioncircle.redis.replicator.cmd.CommandListener;
import com.moilioncircle.redis.replicator.rdb.RdbListener;
import com.moilioncircle.redis.replicator.rdb.datatype.KeyValuePair;
import redis.clients.jedis.Client;
import redis.clients.jedis.Connection;
import redis.clients.jedis.Protocol;

import java.io.IOException;

/**
 * @author Leon Chen
 * @since 2.4.3
 */
public class MigrationExample {
    @SuppressWarnings("resource")
    public static void main(String[] args) throws IOException {
        final MyClient target = new MyClient("127.0.0.1", 6380);
        Replicator r = new RedisMigrationReplicator("127.0.0.1", 6379, Configuration.defaultSetting());
        r.addRdbListener(new RdbListener.Adaptor() {
            @Override
            public void handle(Replicator replicator, KeyValuePair<?> kv) {
                if (kv instanceof MigrationKeyValuePair) {
                    MigrationKeyValuePair mkv = (MigrationKeyValuePair) kv;
                    if (mkv.getExpiredMs() == null) {
                        target.sendCommand(Protocol.Command.RESTORE, mkv.getRawKey(), "0".getBytes(), mkv.getValue(), "REPLACE".getBytes());
                        String r = target.getStatusCodeReply();
                        System.out.println(r);
                    } else {
                        long ms = mkv.getExpiredMs() - System.currentTimeMillis();
                        if (ms > 0) {
                            target.sendCommand(Protocol.Command.RESTORE, mkv.getRawKey(), String.valueOf(ms).getBytes(), mkv.getValue(), "REPLACE".getBytes());
                            String r = target.getStatusCodeReply();
                            System.out.println(r);
                        }
                    }
                }
            }
        });
        r.addCommandListener(new CommandListener() {
            @Override
            public void handle(Replicator replicator, Command command) {
                if (command instanceof DefaultCommand) {
                    DefaultCommand dc = (DefaultCommand) command;
                    target.sendCommand(dc.getCommand(), dc.getArgs());
                    String r = target.getStatusCodeReply();
                    System.out.println(r);
                }
            }
        });
        r.addCloseListener(new CloseListener() {
            @Override
            public void handle(Replicator replicator) {
                target.close();
            }
        });
        r.open();
    }

    public static class MyClient extends Client {
        public MyClient(final String host, final int port) {
            super(host, port);
        }

        public Connection sendCommand(final Protocol.Command cmd, final byte[]... args) {
            return super.sendCommand(cmd, args);
        }
    }
}

@@ -1843,16 +1843,18 @@
      if 
+(!
 (kv inst
@@ -1882,23 +1882,26 @@
 luePair)
- %7B%0A    
+) return;%0A
         
@@ -1976,28 +1976,24 @@
             
- 
-   
 if (mkv.getE
@@ -2010,28 +2010,24 @@
  == null) %7B%0A
-    
             
@@ -2163,36 +2163,32 @@
                 
-    
 String r = targe
@@ -2223,36 +2223,32 @@
                 
-    
 System.out.print
@@ -2270,20 +2270,16 @@
         
- 
-   
 %7D else %7B
@@ -2279,20 +2279,16 @@
  else %7B%0A
-    
         
@@ -2374,28 +2374,24 @@
             
-    
 if (ms %3E 0) 
@@ -2388,28 +2388,24 @@
  (ms %3E 0) %7B%0A
-    
             
@@ -2564,36 +2564,32 @@
                 
- 
-   
 String r = targe
@@ -2604,36 +2604,32 @@
 tusCodeReply();%0A
-    
                 
@@ -2675,34 +2675,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -2699,34 +2699,16 @@
       %7D%0A
-                %7D%0A
         
@@ -2892,16 +2892,18 @@
      if 
+(!
 (command
@@ -2925,30 +2925,34 @@
 aultCommand)
- %7B%0A   
+) return;%0A
             
@@ -2955,17 +2955,16 @@
         
- 
 DefaultC
@@ -3001,20 +3001,16 @@
 ommand;%0A
-    
         
@@ -3076,36 +3076,32 @@
                 
-   
- 
 String r = targe
@@ -3132,36 +3132,32 @@
                 
-    
 System.out.print
@@ -3163,34 +3163,16 @@
 tln(r);%0A
-                %7D%0A
         

fix example