Set algebra solver for Redis in Rust, based on Stal.
stal-rs
provide set operations and resolves them in Redis.
stal-rs
has no dependencies. It produces a vector of Redis operations that
have to be run by the user.
```rust extern crate stal;
let foobar = stal::Set::Inter(vec![stal::Set::Key(b"foo".tovec()), stal::Set::Key(b"bar".tovec())]); let foobarnobaz = stal::Set::Diff(vec![foobar, stal::Set::Key(b"baz".tovec())]); let foobarnobazandqux = stal::Set::Union(vec![stal::Set::Key(b"qux".tovec()), foobarnobaz]);
asserteq!( stal::Stal::new("SMEMBERS".tostring(), foobarnobazandqux).solve(), ( vec![ vec![b"MULTI".tovec()], vec![b"SINTERSTORE".tovec(), b"stal:2".tovec(), b"foo".tovec(), b"bar".tovec()], vec![b"SDIFFSTORE".tovec(), b"stal:1".tovec(), b"stal:2".tovec(), b"baz".tovec()], vec![b"SUNIONSTORE".tovec(), b"stal:0".tovec(), b"qux".tovec(), b"stal:1".tovec()], vec![b"SMEMBERS".tovec(), b"stal:0".tovec()], vec![b"DEL".tovec(), b"stal:0".tovec(), b"stal:1".tovec(), b"stal:2".tovec()], vec![b"EXEC".tovec()], ], 4 )); ```
stal-rs
translates the internal calls to SUNION
, SDIFF
and
SINTER
into SDIFFSTORE
, SINTERSTORE
and SUNIONSTORE
to
perform the underlying operations, and it takes care of generating
and deleting any temporary keys.
The outmost command can be any set operation, for example:
rust
extern crate stal;
let myset = stal::Set::Key(b"my set".to_vec());
stal::Stal::new("SCARD".to_string(), myset).solve();
If you want to preview the commands Stal
will send to generate
the results, you can use Stal.explain
:
```rust extern crate stal;
asserteq!( stal::Stal::new("SMEMBERS".tostring(), stal::Set::Inter(vec![ stal::Set::Union(vec![ stal::Set::Key(b"foo".tovec()), stal::Set::Key(b"bar".tovec()), ]), stal::Set::Key(b"baz".tovec()), ]) ).explain(), vec![ vec![b"SUNIONSTORE".tovec(), b"stal:1".tovec(), b"foo".tovec(), b"bar".tovec()], vec![b"SINTERSTORE".tovec(), b"stal:0".tovec(), b"stal:1".tovec(), b"baz".tovec()], vec![b"SMEMBERS".tovec(), b"stal:0".to_vec()], ] ) ```
All commands are wrapped in a MULTI/EXEC
transaction.
toml
[dependencies]
stal = "0.1.0"