golemcloud / golem

Golem is an open source durable computing platform that makes it easy to build and deploy highly reliable distributed systems.
https://learn.golem.cloud/
Apache License 2.0
519 stars 59 forks source link

Plan an Cassandra implementation of the KeyValueStorage trait #897

Closed irach-ramos closed 1 month ago

irach-ramos commented 2 months ago

Examine the interface KeyValueStorage and figure out the exact Cassandra queries necessary for each function of the trait.

This is a preparation step before actually writing the implementation. Document the plan as a comment of this issue. Make sure to also plan the Cassandra statements required to initialize the database (create tables but only if does not exists, etc). The trait implementation will have to run these if needed so can be transparently used in place of the other implementations.

irach-ramos commented 1 month ago

KeyValueStorage in Cassandra

Table definition

Key value storage tables
CREATE TABLE kv_store (
    namespace TEXT,
    key TEXT,
    value BLOB,
    PRIMARY KEY (namespace, key)
);

CREATE TABLE kv_sets (
    namespace TEXT,
    key TEXT,
    value BLOB,
    PRIMARY KEY ((namespace, key), value)
);

CREATE TABLE kv_sorted_sets (
    namespace TEXT,
    key TEXT,
    score DOUBLE,
    value BLOB,
    PRIMARY KEY ((namespace, key), score, value)
);

Queries by function

trait function statement
set INSERT INTO kv_store (namespace, key, value) VALUES (?, ?, ?);
set_many BEGIN BATCH
    INSERT INTO kv_storage (namespace, key, value) VALUES ('NS', 'key1', 'value1');
    INSERT INTO kv_storage (namespace, key, value) VALUES ('NS', 'key2', 'value2');
    INSERT INTO kv_storage (namespace, key, value) VALUES ('NS', 'key3', 'value3');
APPLY BATCH;
set_if_not_exists INSERT INTO kv_store (namespace, key, value) VALUES (?, ?, ?) IF NOT EXISTS;
get SELECT value FROM kv_store WHERE namespace \= ? AND key \= ?;
get_many SELECT key, value FROM kv_store WHERE namespace \= ? AND key IN (?, ?, ...);
del DELETE FROM kv_store WHERE namespace \= ? AND key \= ?;
del_many DELETE FROM kv_store WHERE namespace \= ? AND key IN (?, ?, ...);
exists SELECT COUNT(*) FROM kv_store WHERE namespace \= ? AND key \= ?;
keys SELECT key FROM kv_store WHERE namespace \= ?;
add_to_set INSERT INTO kv_sets (namespace, key, value) VALUES (?, ?, ?);
remove_from_set DELETE FROM kv_sets WHERE namespace \= ? AND key \= ? AND value \= ?;
members_of_set SELECT value FROM kv_sets WHERE namespace \= ? AND key \= ?;
add_to_sorted_set INSERT INTO kv_sorted_sets (namespace, key, score, value) VALUES (?, ?, ?, ?);
remove_from_sorted_set DELETE FROM kv_sorted_sets WHERE namespace \= ? AND key \= ? AND value \= ?;
get_sorted_set SELECT score, value FROM kv_sorted_sets WHERE namespace \= ? AND key \= ? ORDER BY score ASC;
query_sorted_set SELECT score, value FROM kv_sorted_sets WHERE namespace \= ? AND key \= ? AND score >= ? AND score \<= ? ORDER BY score ASC;