RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 #!/usr/bin/python 00002 # Wolfgang Sourdeau - march 2003 00003 # Copyright (c) 2013 Datacratic. All rights reserved. 00004 00005 # restore script for the Banker database 00006 00007 # note: this scripts does a destructive restore 00008 00009 import redis 00010 import json 00011 import sys 00012 00013 def empty_db(r): 00014 storedKeys = tuple(r.keys()) 00015 apply(redis.Redis.delete, (r,) + storedKeys) 00016 00017 storedKeys = r.keys() 00018 if len(storedKeys) > 0: 00019 raise Exception("database was not properly emptied") 00020 00021 def load_json(filename): 00022 inf = open(filename) 00023 d = json.loads(inf.read()) 00024 inf.close() 00025 00026 return d 00027 00028 def fill_db(r, d): 00029 account_names = [] 00030 for key in d: 00031 value = d[key] 00032 if type(value) == dict: 00033 r.hmset(key, value) 00034 elif isinstance(value, basestring): 00035 if key.startswith("banker-"): 00036 account_names.append(key[7:]) 00037 r.set(key, value) 00038 else: 00039 raise Exception("unsupported type '%s'" % str(type(key))) 00040 00041 # TODO: backup files do not contain "banker:accounts" so we need to 00042 # rebuild it, which is wrong 00043 if len(account_names) > 0: 00044 apply(redis.Redis.sadd, (r, "banker:accounts") + tuple(account_names)) 00045 00046 if __name__ == "__main__": 00047 if len(sys.argv) < 2: 00048 raise Exception("missing filename parameter") 00049 00050 r = redis.Redis(host='localhost', port=6380) 00051 00052 empty_db(r) 00053 d = load_json(sys.argv[1]) 00054 fill_db(r, d)