Browse Source

KVS readme

Maxim Sokhatsky 11 years ago
parent
commit
5fd7638e8e
3 changed files with 91 additions and 5 deletions
  1. 87 1
      README.md
  2. 2 2
      include/config.hrl
  3. 2 2
      src/kvs.erl

+ 87 - 1
README.md

@@ -34,7 +34,93 @@ Currently kvs includes following store backends:
 
 * Mnesia
 * Riak
-* CouchDB
+* KAI
+
+Configuring
+-----------
+
+First of all you need to tune your backend in the kvs application:
+
+    {kvs, {dba,store_kai}},
+
+Try to check it:
+
+    1> kvs:config(dba).
+    store_kai
+    2> kvs:version().
+    {version,"KVS KAI PURE XEN"}
+
+Check table packages included into the schema:
+
+    3> kvs:dir().
+    [kvs_user,kvs_product,kvs_membership,kvs_payment,kvs_feed,
+     kvs_acl,kvs_account,kvs_group]
+
+Operations
+----------
+
+Try to add some data:
+
+    1> rr(kvs).
+    2> kvs:put(#user{id="maxim@synrc.com"}).
+    ok
+    3> kvs:get(user,"maxim@synrc.com").
+    #user{id = "maxim@synrc.com",container = feed,...}
+    4> kvs:put(#user{id="doxtop@synrc.com"}).
+    5> length(kvs:all(user)).
+    2
+
+Polymorphic Records
+-------------------
+
+The data in KVS represented as plain Erlang records. The first element of the tuple
+as usual indicates the name of bucket. And the second element usually corresponds
+to the index key field. Additional secondary indexes could be applied for stores
+that supports 2i, e.g. kai, mnesia, riak.
+
+    1 record_name -- user, groups, acl, etc... table name -- element(1, Rec).
+    2 id          -- index key -- element(2, Rec).
+
+Iterators
+---------
+
+All record could be chained into the double-linked lists in the database.
+So you can inherit from the ITERATOR record just like that:
+
+    -record(acl_entry, {?ITERATOR(acl),
+        entry_id,
+        acl_id,
+        accessor,
+        action}).
+
+The layout of iterators are following:
+
+    1 record_name -- table name, like
+    2 id          -- index key
+    3 container   -- container name
+    4 feed_id     -- feed id
+    5 prev        -- poniter to previous object in list
+    6 next        -- next
+    7 feeds       -- subfeeds
+    8 guard,      -- aux field
+    9 ...
+
+This means your table will support add/remove operations to lists.
+
+    1> kvs:add(#user{id="mes@ua.fm"}).
+    2> kvs:add(#user{id="dox@ua.fm"}).
+
+Containers
+----------
+
+If you are using iterators records this automatically means you are using containers.
+Containers are just boxes for storing top/heads of the linked lists. Here is layout
+of containers:
+
+    1 record_name   -- container name
+    2 id            -- unique id
+    3 top           -- pointer to the list's head
+    4 entries_count -- number of elements in list
 
 Credits
 -------

+ 2 - 2
include/config.hrl

@@ -1,4 +1,4 @@
 -record(config, {key, value}).
 
--define(DBA, store_mnesia).
-
+-define(DBA, store_kai).
+%-define(DBA, kvs:config(dba)).

+ 2 - 2
src/kvs.erl

@@ -55,7 +55,8 @@ add(Record) when is_tuple(Record) ->
             R3 = setelement(#iterator.feed_id, R2, element(#container.id, Container)),
             kvs:put(R3),
             error_logger:info_msg("[kvs] PUT: ~p", [element(#container.id,R3)]),
-            {ok, R3} end end.
+            {ok, R3} end;
+    E ->  error_logger:info_msg("Entry exist: ~p", [E]),{error, exist} end.
 
 remove(RecordName, RecordId) ->
     case kvs:get(RecordName, RecordId) of {error, not_found} -> error_logger:info_msg("not found");
@@ -312,4 +313,3 @@ modules() -> Modules = case kvs:config(schema) of
                 kvs_payment, kvs_feed, kvs_acl,
                 kvs_account, kvs_group ];
         E  -> E end.
-