···3030 defmacro server_tags_prefix, do: "\xFF/st/"
3131 defmacro server_tags_end, do: "\xFF/st0"
32323333+ defmacro key_storage_prefix, do: "\xFF/ks/"
3434+ defmacro key_storage_end, do: "\xFF/ks0"
3535+3336 defmacro key_servers_prefix, do: "\xFF/key_servers/"
3437 defmacro key_servers_end, do: "\xFF/key_servers0"
3538 defmacro server_keys_prefix, do: "\xFF/sk/"
···91949295 # Special range clears are not currently supported
9396 def special_mutation?({:clear_range, _sk, _ek}), do: false
9797+9898+ @spec pack_storage_team_pair(non_neg_integer, [non_neg_integer]) :: {binary, binary}
9999+ def pack_storage_team_pair(storage_team_id, storage_server_ids)
100100+ when is_integer(storage_team_id) and is_list(storage_server_ids) do
101101+ {
102102+ storage_teams_prefix() <> Keyset.pack([storage_team_id]),
103103+ Keyset.pack(storage_server_ids),
104104+ }
105105+ end
106106+107107+ def pack_key_storage_pair(shard_start_key, from_storage_team_id, to_storage_team_id)
108108+ when is_binary(shard_start_key) and is_integer(from_storage_team_id) and (is_integer(to_storage_team_id) or is_nil(to_storage_team_id)) do
109109+ {
110110+ key_storage_prefix() <> shard_start_key,
111111+ Keyset.pack([from_storage_team_id, to_storage_team_id]),
112112+ }
113113+ end
114114+115115+ @spec unpack_key_storage_value(binary) :: [non_neg_integer | nil]
116116+ def unpack_key_storage_value(value) when is_binary(value) do
117117+ [_to_storage_team_id, _from_storage_team_id] = Keyset.unpack(value)
118118+ end
9411995120 @spec pack_key_servers([integer], [integer]) :: binary
96121 def pack_key_servers(from_ids, to_ids) when is_list(from_ids) and is_list(to_ids) do
-18
test/shard_tag_map_test.exs
···11defmodule Hobbes.ShardTagMapTest do
22 use ExUnit.Case, async: true
3344- alias Hobbes.ShardTagMap
55-64 @moduletag :shard_tag_map
77-88- describe "tlog_ids_for_servers/2" do
99- test "returns tlog ids" do
1010- # TODO: this test will be flaky if it ever fails, it should really be a fuzz test
1111- # but for now the function is so simple it's not worth testing further
1212- tlog_ids = [0, 1, 2, 3, 4, 5]
1313-1414- assert [0, 1, 2] = ShardTagMap.tlog_ids_for_servers(tlog_ids, [0, 1, 2], 3)
1515-1616- assert [1, 2, _] = ids1 = ShardTagMap.tlog_ids_for_servers(tlog_ids, [1, 7, 8], 3)
1717- assert length(Enum.uniq(ids1)) == 3
1818-1919- assert [1, _, _] = ids2 = ShardTagMap.tlog_ids_for_servers(tlog_ids, [1, 7, 13], 3)
2020- assert length(Enum.uniq(ids2)) == 3
2121- end
2222- end
235end