def __init__(self, config, usethreads=False, writeback=False): "Initialize the datastore." encoding = config.get(C.DATASTORE, C.DATASTORE_ENCODING) if encoding == C.JSON: self.decode = decode_json self.encode = encode_json elif encoding == C.PROTOBUF: self.decode = decode_protobuf self.encode = encode_protobuf bound = config.getint(C.DATASTORE, C.SLAB_LRU_SIZE) if bound <= 0: raise ValueError, "Illegal SLAB LRU size %d" % bound if writeback: if usethreads: nthreads = config.getint(C.DATASTORE, C.SLAB_LRU_THREADS) else: nthreads = 0 self.nthreads = nthreads if nthreads: self.threads = [] self.workqueue = Queue(nthreads) for n in xrange(nthreads): t = threading.Thread(target=self._worker) self.threads.append(t) t.daemon = True t.name = "DS-%d" % n t.start() callback = self._cbthreaded else: callback = self._cbwrite else: callback = None self.cache = LRUIOCache(bound=bound, callback=callback)