def __init__(self, g, initial_val=None, increment=None): if initial_val is None: initial_val = 1000 if increment is None: increment = 50 self.g = g self.increment = increment self.num_calls = 0 seq_vertex = self._get_or_create_vertex('cc', { 'lastval': initial_val }) initial_val = seq_vertex.lastval + increment max_id = max(filter(lambda x: x < 999999999999, map(CompactID._base_decode, self.g.g.node.keys()))) while initial_val < max_id: initial_val += increment seq_vertex.lastval = initial_val self.seq_vertex = seq_vertex self.compact_id = CompactID(initial_val)
class IDSequencer(object): def __init__(self, g, initial_val=None, increment=None): if initial_val is None: initial_val = 1000 if increment is None: increment = 50 self.g = g self.increment = increment self.num_calls = 0 seq_vertex = self._get_or_create_vertex('cc', { 'lastval': initial_val }) initial_val = seq_vertex.lastval + increment max_id = max(filter(lambda x: x < 999999999999, map(CompactID._base_decode, self.g.g.node.keys()))) while initial_val < max_id: initial_val += increment seq_vertex.lastval = initial_val self.seq_vertex = seq_vertex self.compact_id = CompactID(initial_val) def __call__(self, *args, **kwargs): self.num_calls += 1 if self.num_calls > self.increment: self.seq_vertex.lastval += self.increment self.num_calls = 0 return self.compact_id(*args, **kwargs) def current_value(self, encoded=False): return self.compact_id.current(encoded=encoded) def get_block(self, block_size=1000): block_begin, block_end = self.compact_id.block(block_size) self.seq_vertex.lastval = block_end return block_begin, block_end def set(self, new_val): self.seq_vertex.lastval = new_val new_val_encoded = self.compact_id._base_encode(new_val) return self.compact_id(encoded=new_val_encoded, inc=0) def _get_or_create_vertex(self, sid, properties=None): if not properties: properties = {} vertex = self.g.get_point(sid) if not vertex: self.g.add_point(sid, **properties) vertex = self.g.get_point(sid) return vertex