def parent(self, tr, key): # find parent for k, v in tr.get_range(fdb.last_less_than(fdb.tuple_to_key(self._path, key)), fdb.first_greater_than(fdb.tuple_to_key(self._path, key)) + 1, 2): parentValue = fdb.key_to_tuple(v) if parentValue[0] == key or parentValue[1] == key: return tuple(fdb.key_to_tuple(k)[1], parentValue) return None
def setKey(self, tr, key, value, metric): isNew = True isRoot = True child = tuple(key, tuple("", "", random.random(), metric, value)) parent = tuple() # find self or parent for k, v in tr.get_range(fdb.last_less_than(fdb.tuple_to_key(self._path, key)), fdb.first_greater_than(fdb.tuple_to_key(self._path, key)) + 1, 2): isRoot = False node = tuple(fdb.key_to_tuple(k)[1], fdb.key_to_tuple(v)) if node[0] == key: isNew = False child = node node[1][4] = value break elif node[0] < key and node[1][1] == "": isRoot = False parent = node parent[1][1] = key break elif node[0] > key and node[1][0] == "": isRoot = False parent = node parent[1][0] = key break # insert root if isRoot: self.updateRoot(tr, child) # update parent if isNew: self.updateNode(tr, parent) # insert self self.updateNode(tr, child) # balance self.balance(tr, parent, child)
def key_for_feed_message(feed, message): return fdb.tuple_to_key('f', struct.pack('>Q', feed), 'message', struct.pack('>Q', message))
def key_for_feed_watcher(feed, inbox): return fdb.tuple_to_key('f', struct.pack('>Q', feed), 'watchers', struct.pack('>Q', inbox))
def prefix_for_feed_subscribers(feed): return fdb.tuple_to_key('f', struct.pack('>Q', feed), 'subs')
def key_for_feed_subscriber(feed, inbox): return fdb.tuple_to_key('f', struct.pack('>Q', feed), 'subs', struct.pack('>Q', inbox))
def key_for_feed_message_count(feed): return fdb.tuple_to_key('f', struct.pack('>Q', feed), 'messCount')
def key_for_feed_subscriber_count(feed): return fdb.tuple_to_key('f', struct.pack('>Q', feed), 'subCount')
def updateRoot(self, tr, node): tr[self._rootKey] = fdb.tuple_to_key(self._path, node[0])
def key_for_message(message): return fdb.tuple_to_key('m', struct.pack('>Q', message))
def key_for_inbox_stale_feed(inbox, feed): return fdb.tuple_to_key('i', struct.pack('>Q', inbox), 'stale', struct.pack('>Q', feed))
def prefix_for_inbox_subscriptions(inbox): return fdb.tuple_to_key('i', struct.pack('>Q', inbox), 'subs')
def key_for_inbox_subscription(inbox, feed): return fdb.tuple_to_key('i', struct.pack('>Q', inbox), 'subs', struct.pack('>Q', feed))
def key_for_inbox_subscription_count(inbox): return fdb.tuple_to_key('i', struct.pack('>Q', inbox), 'subCount')
def prefix_for_feed_messages(feed): return fdb.tuple_to_key('f', struct.pack('>Q', feed), 'message')
def key_for_feed(feed): return fdb.tuple_to_key('f', struct.pack('>Q', feed))
def key_for_inbox(inbox): return fdb.tuple_to_key('i', struct.pack('>Q', inbox))
def updateNode(self, tr, node): tr[fdb.tuple_to_key(self._path, node[0])] = fdb.tuple_to_key(node[1])