README.md in rico-0.4.0 vs README.md in rico-0.5.0
- old
+ new
@@ -12,32 +12,10 @@
```ruby
gem "rico"
```
-## Usage
-
-Instantiate a Rico object with a **bucket** and a **key** then perform operations.
-
-Here's an example of how to use a set to manage a list of followed users:
-
-```ruby
-follows = Rico::Set.new "follows", @user.id
-
-follows.member? @other_user.id # => false
-
-follows.add @other_user.id
-
-follows.member? @other_user.id # => true
-follows.length # => 1
-
-follows.remove @other_user.id
-
-follows.member? @other_user.id # => false
-follows.length # => 0
-```
-
## Configuration
By default, Rico uses a generic Riak::Client instance for operations. You can specify your own options for the Riak client (perhaps inside of a rails initializer) like so:
```ruby
@@ -53,87 +31,85 @@
c.namespace = "development" # => "development:BUCKET:KEY"
c.namespace = ["my_app", "production"] # => "my_app:production:BUCKET:KEY"
end
```
-## Data Types
+## Supported Data Types
**Arrays** - sequence of values
```ruby
a = Rico::Array.new "bucket", "key"
-a.add [3, 1, 1, 4, 2]
-a.members # => [3, 1, 1, 4, 2]
-a.length # => 5
+a.add [3, 1, 1, 4, 2] # writes to riak
+a.members # => [3, 1, 1, 4, 2]
+a.length # => 5
+a.remove [2, 4] # writes to riak
+a.members # => [3, 1, 1]
+a.raw_data # => '{"_type":"array","_values":[3,1,1],"_deletes":[2,4]}'
```
**Lists** - sorted sequence of values
```ruby
l = Rico::List.new "bucket", "key"
l.add [3, 1, 1, 4, 2]
-l.members # => [1, 1, 2, 3, 4]
-l.length # => 5
+l.members # => [1, 1, 2, 3, 4]
```
**Sets** - unique sequence of values
```ruby
s = Rico::Set.new "bucket", "key"
s.add [3, 1, 1, 4, 2]
-s.members # => [3, 1, 4, 2]
-s.length # => 4
+s.members # => [3, 1, 4, 2]
```
**Sorted Sets** - unique, sorted sequence of values
```ruby
s = Rico::SortedSet.new "bucket", "key"
s.add [3, 1, 1, 4, 2]
-s.members # => [1, 2, 3, 4]
-s.length # => 4
+s.members # => [1, 2, 3, 4]
+s.reduce(&:+)
```
**Maps** - key-value mappings
```ruby
m = Rico::Map.new "bucket", "key"
m.add({"a" => 1})
m.add({"b" => 2, "c" => 3})
-m.members # => {"a" => 1, "b" => 2, "c" => 3}
-m.length # => 3
+m.members # => {"a"=>1, "b"=>2, "c"=>3}
```
**Sorted Maps** - key-value mappings sorted by key
```ruby
m = Rico::SortedMap.new "bucket", "key"
m.add({"b" => 2, "c" => 3})
m.add({"a" => 1})
-m.members # => {"a" => 1, "b" => 2, "c" => 3}
-m.length # => 3
+m.members # => {"a"=>1, "b"=>2, "c"=>3}
+m.raw_data # => '{"_type":"smap","_values":{"a":1,"b":2,"c":3}}'
```
**Capped Sorted Maps** - key-value mappings sorted by key and bound by size
```ruby
m = Rico::CappedSortedMap.new "bucket", "key", limit: 2
m.add({"b" => 2, "c" => 3})
m.add({"a" => 1})
-m.members # => {"b" => 2, "c" => 3}
-m.length # => 2
+m.members # => {"b"=>2, "c"=>3}
+m.length # => 2
```
**Values** - generic serialized values
```ruby
v = Rico::Value.new "bucket", "key"
-v.exists? # => false
-v.get # => nil
+v.get # => nil
v.set "bob"
-v.get # => "bob"
-v.exists? # => true
+v.get # => "bob"
```
## Content Types
**JSON**: Objects are serialized and stored as JSON by default, using a Content-Type header of `application/json`
@@ -142,26 +118,55 @@
```ruby
s = Rico::Set.new "bucket", "key"
s.content_type = "application/x-gzip"
s.add [1,2,3]
-s.get # => [1, 2, 3]
-s.raw_data # => "\u001F\x8B\b\u0000G...."
+s.members # => [1, 2, 3]
+s.data # => {"_type"=>"set", "_values"=>[1, 2, 3]}
+s.raw_data # => "\u001F\x8B\b\u0000G...."
```
+## Under The Hood
+
+Objects are stored in a simple map encoded to JSON.
+
+**Array, List, Set and SortedSet** types look like this:
+
+```json
+{ "_type": "sset", "_values": [1,2,3], "_deletes": [4] }
+```
+
+The *_deletes* field acts as a temporary tombstone for preserve deletes during conflict resolution. The field will normally contain the deletes processed during the last write, or the cumulative deletes processed during the last sibling merge. The value of _deletes is intentionally forgotten after a successful read and will not taint future operations.
+
+Conflict resolution works by adding the difference between value arrays, removing deleted values and returning the result. This implementation is susceptible to sticky deletes - if different clients delete and add the same value simultenously the delete will ultimately win and the value is left out.
+
+**Map, SortedMap and CappedSortedMap** types look like this:
+
+```json
+{ "_type": "csmap", "_values": {"a": 1, "b": 2, "c": 3}, "_deletes": ["d"] }
+```
+
+The *_deletes* field for map objects contains only the keys deleted.
+
+Conflict resolution works by merging value hashes, removing deleted keys and returning the result. This implementation is susceptible to sticky deletes - if different clients delete and add the same key simultenously the delete will ultimately win and the value is left out.
+
+**Value** type looks like this:
+
+```json
+{ "_type": "value", "_value": "Hipsters love modern folk rock" }
+```
+
+Conflict resolution is handled by last-write-wins.
+
## Notes
### Enumerable
Enumerable-looking types are indeed Enumerable
### Persistence
Data is persisted at operation time. For example, List#add(5) will immediately update the record in Riak. It'd generally be wise to compute a list of values to be added or removed and then issue a single operation.
-
-## TODO
-
-- Ability to provide custom sibling resolution callbacks
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)