Skip to content

Commit

Permalink
Add Keyspace retrieve and save
Browse files Browse the repository at this point in the history
Add Keyspace management based on persistent_term, with tests

Signed-off-by: Eddy Babetto <eddy.babetto@secomind.com>
  • Loading branch information
eddbbt committed Mar 6, 2024
1 parent 5335b0d commit 2d7dcf6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/astarte_core/cql_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,41 @@ defmodule Astarte.Core.CQLUtils do

eid
end

@spec realm_name_to_keyspace_name(nonempty_binary(), nonempty_binary()) :: nonempty_binary()
def realm_name_to_keyspace_name(realm_name, astarte_instance_id \\ "default")

def realm_name_to_keyspace_name("", _) do
raise "EmptyRealm"
end

def realm_name_to_keyspace_name(realm_name, astarte_instance_id)
when is_binary(realm_name) do
case :persistent_term.get(realm_name, nil) do
nil ->
if String.length(astarte_instance_id <> realm_name) < 48 do
keyspace_name =
(astarte_instance_id <> realm_name)
|> String.replace("-", "")
|> String.replace("_", "")
|> String.downcase()

:persistent_term.put(realm_name, keyspace_name)
keyspace_name
else
keyspace_name =
Base.url_encode64(astarte_instance_id <> realm_name, padding: false)
|> String.replace("-", "")
|> String.replace("_", "")
|> String.slice(0..47)
|> String.downcase()

:persistent_term.put(realm_name, keyspace_name)
keyspace_name
end

value ->
value
end
end
end
44 changes: 44 additions & 0 deletions test/astarte_core/cql_utils_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule CQLUtilsTest do
use ExUnit.Case
alias Astarte.Core.CQLUtils
alias Astarte.Core.Realm

test "interface name to table name" do
assert CQLUtils.interface_name_to_table_name("com.ispirata.Hemera.DeviceLog", 1) ==
Expand Down Expand Up @@ -148,4 +149,47 @@ defmodule CQLUtilsTest do
assert Astarte.Core.CQLUtils.endpoint_id("com.foo", 2, "/a/%{something}/foo") !=
Astarte.Core.CQLUtils.endpoint_id("com.foo", 2, "/a/%{something}/bar")
end

test "realm name does not get encoded, persistent_term exists" do
assert CQLUtils.realm_name_to_keyspace_name("example", "atestinstance") ==
"atestinstanceexample"

assert Realm.valid_name?(CQLUtils.realm_name_to_keyspace_name("example", "atestinstance")) ==
true

:persistent_term.erase("example")
end

test "realm name gets encoded, persistent_term exists" do
assert CQLUtils.realm_name_to_keyspace_name(
"averyveryverylongrealmnamejustforthistest",
"atestinstance"
) ==
"yxrlc3rpbnn0yw5jzwf2zxj5dmvyexzlcnlsb25ncmvhbg1u"

assert Realm.valid_name?(
CQLUtils.realm_name_to_keyspace_name(
"averyveryverylongrealmnamejustforthistest",
"atestinstance"
)
) == true

:persistent_term.erase("averyveryverylongrealmnamejustforthistest")
end

test "realm name is empty, persistent_term exists" do
assert_raise RuntimeError, ~r/^EmptyRealm/, fn ->
CQLUtils.realm_name_to_keyspace_name("", "atestinstance")
end
end

test "realm name is not empty, persistent_term does not exists" do
assert CQLUtils.realm_name_to_keyspace_name("example") ==
"defaultexample"

assert Realm.valid_name?(CQLUtils.realm_name_to_keyspace_name("example")) ==
true

:persistent_term.erase("example")
end
end

0 comments on commit 2d7dcf6

Please sign in to comment.