From c1d638aae11e8107a146a74efdcc34ac660c9fd1 Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Fri, 17 Jul 2020 17:33:09 -0500 Subject: [PATCH] iddb.rho: add readOnly facet fixes: #32 --- rchat/chain_replica.js | 4 ++-- rchat/iddb.rho | 53 +++++++++++++++++++++++++++++++++++------- rchat/myzulipdb.rho | 16 +++++++++---- 3 files changed, 59 insertions(+), 14 deletions(-) diff --git a/rchat/chain_replica.js b/rchat/chain_replica.js index 865c7cb..34b8f03 100644 --- a/rchat/chain_replica.js +++ b/rchat/chain_replica.js @@ -106,9 +106,9 @@ function notice_as_rho({ op, table_name, OLD = undefined, NEW = undefined}) { // KLUDGE: replacing null with Nil in string form has false positives const lit = val => val ? JSON.stringify(val).replace(/\bnull\b/g, 'Nil') : 'Nil'; - // ISSUE: sync "zulip_iddb3" with myzulipdb.rho + // ISSUE: sync "zulip_iddb4" with myzulipdb.rho return `new deployerId(\`rho:rchain:deployerId\`) in { - for(db <<- @{[*deployerId, "zulip_iddb3"]}) { + for(db <<- @{[*deployerId, "zulip_iddb4"]}) { // ISSUE: Nil return channel: no sync db!(${lit(op)}, ${lit(table_name)}, ${lit(OLD)}, ${lit(NEW)}, Nil) } diff --git a/rchat/iddb.rho b/rchat/iddb.rho index 0e6a6e8..ed65578 100644 --- a/rchat/iddb.rho +++ b/rchat/iddb.rho @@ -18,8 +18,10 @@ in { } | contract IdDB(return) = { - new self, tablesCh in { - return!(*self) | + new self, readOnly, tablesCh in { + return!(*self, *readOnly) | + + contract self(@"readOnly", return) = { return!(*readOnly) } | tablesCh!({}) | // table_name -> channel with Set of keys // rows are stored at @{[self, table_name, key]} @@ -37,7 +39,13 @@ in { } | // TODO: drop table: delete from map @tablesCh; consume keysCh, all rows - contract self(@"keys", @table_name /\ String, return) = { + contract readOnly(@"tables", return) = { + for (@tables <<- tablesCh) { + return!(tables) + } + } | + + contract readOnly(@"keys", @table_name /\ String, return) = { for (@tables <<- tablesCh) { match tables.get(table_name) { Nil => { return!((false, "no such table")) } @@ -50,8 +58,37 @@ in { } } } | - // TODO: select method to get record from table_name, key - // TODO: move keys, select to read-only facet + + contract readOnly(@"select", @table_name /\ String, @wanted /\ List, return) = { + for (@tables <<- tablesCh) { + match tables.get(table_name) { + Nil => { return!((false, "no such table")) } + {*keysCh} => { + for (@keys <<- keysCh) { + log!({"select: keys for": table_name, "qty": keys.size()}) | + new loop in { + loop!(wanted, {}) | + contract loop(@todo, @acc) = { + match todo { + [] => return!((true, acc)) + [target, ...rest] => { + if (keys.contains(target)) { + for (@record <<- @{[*self, table_name, target]}) { + loop!(rest, acc.put(target, record)) + } + } else { + // TODO: warn? + loop!(rest, acc) + } + } + } + } + } + } + } + } + } + } | contract self(@"INSERT", @table_name /\ String, _OLD, @NEW, return) = { for (@tables <<- tablesCh) { @@ -133,12 +170,12 @@ in { // testing new ch, logKeys in { IdDB!(*ch) | - for (db1 <- ch) { - log!({"db1": *db1}) | + for (db1, ro1 <- ch) { + log!({"db1": *db1, "readOnly": *ro1}) | contract logKeys(@label, @key) = { new keysCh in { log!({label: key}) | - db1!("keys", "player", *keysCh) | + ro1!("keys", "player", *keysCh) | for (@{(true, keys)} <- keysCh) { log!({label: key, "keys": keys}) } diff --git a/rchat/myzulipdb.rho b/rchat/myzulipdb.rho index b11a470..528e6f6 100644 --- a/rchat/myzulipdb.rho +++ b/rchat/myzulipdb.rho @@ -5,19 +5,27 @@ new deployId, //(`rho:rchain:deployId`), deployerId, //(`rho:rchain:deployerId`), lookup(`rho:registry:lookup`), +insertArbitrary(`rho:registry:insertArbitrary`), IdDBCh, log(`rho:io:stderr`) in { - lookup!(`rho:id:ka9r5c1ha5fk7y8qwgqjmhcfrm53pz1kxjq4of9qm1mhbt8dsgzp3r`, *IdDBCh) | + lookup!(`rho:id:FIXME` /* ISSUE: how to sync IdDB registry URI? */, *IdDBCh) | for (IdDB <- IdDBCh) { log!({"IdDB": *IdDB}) | new dbCh in { IdDB!(*dbCh) | - for (db <- dbCh) { - log!({"db": *db}) | + for (db, readOnly <- dbCh) { + log!({"db": *db, "readOnly": *readOnly}) | + new uriCh in { + insertArbitrary!(*readOnly, *uriCh)| + for (@uri <- uriCh) { + log!({"db readOnly facet registered at": uri}) + } + } | db!("create_table", "zerver_message", Nil) | db!("create_table", "zerver_usermessage", Nil) | - @{[*deployerId, "zulip_iddb3"]}!(*db) + // ISSUE: sync zulip_iddb4 with chain_replica.js + @{[*deployerId, "zulip_iddb4"]}!(*db) } } }