#!/usr/bin/python2
import sys
import os
import redis
import time
import datetime


def rename_keys(source, ScanIndex):
    ScanIndex, keys = source.execute_command('scan', ScanIndex, "count",20000)
    keys_count = len(keys)
    print "Key Count is:",keys_count
    pipe = source.pipeline(transaction=False)
    #for key in keys:
    index=0
    pipe_size=20000
    while index < keys_count:
        num=0
        while (index < keys_count) and (num < pipe_size):
            if len(keys[index]) > 7 and keys[index][0:6].isdigit() == True and keys[index][6] == "." :
                pipe.rename(keys[index], keys[index][7:])
            index +=1
            num +=1
        results=pipe.execute()
    return ScanIndex

if __name__=='__main__':
    argc = len(sys.argv)
    if argc != 3:
        print "usage: %s sourceIP sourcePort" % (sys.argv[0])
        exit(1)
    SrcIP = sys.argv[1]
    SrcPort = int(sys.argv[2])

    start=datetime.datetime.now()
    source=redis.Redis(host=SrcIP,port=SrcPort)

    print "Begin Read Keys"
    index=0
    times=1
    while index != '0' or times == 1:
        print "Times: ", times
        times += 1
        index=rename_keys(source, index)

    stop=datetime.datetime.now()
    diff=stop-start
    print "Finish, token time:",str(diff)
