

import redis
import time
import os
from queue import Queue
import traceback

def set_enterprise_to_redis(filepath):
    from BaseDataMaintenance.dataSource.source import getConnect_redis_baseline
    from BaseDataMaintenance.common.Utils import log
    from BaseDataMaintenance.common.multiThread import MultiThreadHandler
    from BaseDataMaintenance.dataSource.pool import ConnectorPool

    def handle(item,result_queue):
        _db = pool_db.getConnector()
        try:
            _db.set(item,1)
        except Exception as e:
            traceback.print_exc()
        finally:
            pool_db.putConnector(_db)

    if os.path.exists(filepath):

        pool_db = ConnectorPool(10,30,getConnect_redis_baseline)
        _start_time = time.time()
        task_queue = Queue()

        with open(filepath,"r",encoding="UTF8") as f:
            for _e in f:
                if not _e:
                    continue
                _e = _e.strip()
                if len(_e)>=4:
                    task_queue.put(_e)
                    if task_queue.qsize()>=100*10000:
                        _mt = MultiThreadHandler(task_queue,handle,None,30)
                        _mt.run()
                        _count = 0
            if task_queue.qsize()>=0:
                _mt = MultiThreadHandler(task_queue,handle,None,30)
                _mt.run()
                _count = 0

        log("enterprise to redis takes %.5f"%(time.time()-_start_time))
    else:
        log("enterprise filepath not exists")

def remove_enterprise_key(filename,_keys):
    from BaseDataMaintenance.dataSource.source import getConnect_redis_baseline
    db = getConnect_redis_baseline()
    if filename is not None:
        with open(filename,"r",encoding="utf8") as f:
            while 1:
                _line = f.readline()
                if not _line:
                    break
                _key = _line.strip()
                _v = db.delete(_key)
                print("delete key %s %s"%(_key,str(_v)))

    if _keys is not None:
        for _key in _keys.split(","):
            _v = db.delete(_key)
            print("delete key %s %s"%(_key,str(_v)))
    del db



if __name__ == '__main__':
    set_enterprise_to_redis("F:\Workspace2016\BiddingKG\BiddingKG\dl\LEGAL_ENTERPRISE.txt")