def encode_query_key(self, txid, namespace, ancestor_path): if not isinstance(ancestor_path, tuple): ancestor_path = Path.flatten(ancestor_path) section_prefix = self._txid_prefix(txid) + self.QUERIES encoded_ancestor = Text.encode(namespace) + Path.pack(ancestor_path[:2]) return section_prefix + encoded_ancestor
def encode_key(self, group_path): """ Encodes a key for a given entity group. Args: group_path: A tuple containing path elements. Returns: A byte string containing the relevant FDB key. """ return b''.join([ self.directory.rawPrefix, hash_tuple(group_path), Text.encode(group_path[0]) + Path.encode_id_or_name(group_path[1])])
def _unpack_keys(self, blob): keys = [] pos = 0 while pos < len(blob): namespace, pos = Text.decode(blob, pos) path, pos = Path.unpack(blob, pos) key = entity_pb.Reference() key.set_app(self.project_id) key.set_name_space(namespace) key.mutable_path().MergeFrom(Path.decode(path)) keys.append(key) return keys
def decode_metadata(self, txid, kvs): lookup_rpcs = defaultdict(list) queried_groups = set() mutation_rpcs = [] rpc_type_index = len(self._txid_prefix(txid)) current_versionstamp = None for kv in kvs: rpc_type = kv.key[rpc_type_index] pos = rpc_type_index + 1 if rpc_type == self.QUERIES: namespace, pos = Text.decode(kv.key, pos) group_path = Path.unpack(kv.key, pos)[0] queried_groups.add((namespace, group_path)) continue rpc_versionstamp = kv.key[pos:pos + VERSIONSTAMP_SIZE] if rpc_type == self.LOOKUPS: lookup_rpcs[rpc_versionstamp].append(kv.value) elif rpc_type in (self.PUTS, self.DELETES): if current_versionstamp == rpc_versionstamp: mutation_rpcs[-1].append(kv.value) else: current_versionstamp = rpc_versionstamp mutation_rpcs.append([rpc_type, kv.value]) else: raise InternalError(u'Unrecognized RPC type') lookups = dict() mutations = [] for chunks in six.itervalues(lookup_rpcs): lookups.update([(key.SerializeToString(), key) for key in self._unpack_keys(b''.join(chunks))]) for rpc_info in mutation_rpcs: rpc_type = rpc_info[0] blob = b''.join(rpc_info[1:]) if rpc_type == self.PUTS: mutations.extend(self._unpack_entities(blob)) else: mutations.extend(self._unpack_keys(blob)) return list(six.itervalues(lookups)), queried_groups, mutations
def _encode_keys(self, keys): return b''.join( [Text.encode(decode_str(key.name_space())) + Path.pack(key.path()) for key in keys])