store_kai.erl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. -module(store_kai).
  2. -author('Maxim Sokhatsky <maxim@synrc.com>').
  3. -copyright('Synrc Research Center s.r.o.').
  4. -include_lib("kai/include/kai.hrl").
  5. -include_lib("kvs/include/config.hrl").
  6. -include_lib("kvs/include/users.hrl").
  7. -include_lib("kvs/include/groups.hrl").
  8. -include_lib("kvs/include/feeds.hrl").
  9. -include_lib("kvs/include/acls.hrl").
  10. -include_lib("kvs/include/invites.hrl").
  11. -include_lib("kvs/include/meetings.hrl").
  12. -include_lib("kvs/include/membership.hrl").
  13. -include_lib("kvs/include/payments.hrl").
  14. -include_lib("kvs/include/purchases.hrl").
  15. -include_lib("kvs/include/accounts.hrl").
  16. -include_lib("stdlib/include/qlc.hrl").
  17. -compile(export_all).
  18. start() -> kai_store:start(), ok.
  19. stop() -> kai_store:stop(), ok.
  20. version() -> {version,"KVS KAI PURE XEN"}.
  21. join() -> initialize(), ok.
  22. join(Node) -> initialize(), ok.
  23. initialize() -> ok.
  24. dir() -> kvs:modules().
  25. put(Records) when is_list(Records) -> lists:foreach(fun kai_put/1, Records);
  26. put(Record) -> kai_put(Record).
  27. kai_put(Record) ->
  28. Data = #data{key = element(2,Record), bucket = table_to_num(element(1,Record)),
  29. last_modified = now(), checksum = erlang:md5(term_to_binary(Record)),
  30. vector_clocks = vclock:fresh(), value = Record },
  31. kai_store:put(Data).
  32. update(Record, Object) -> ok.
  33. get(Tab, Key) ->
  34. Data = #data{key=Key,bucket=table_to_num(Tab)},
  35. kai_get(Data).
  36. kai_get(Data) ->
  37. case kai_store:get(Data) of
  38. #data{value=Value} -> Value;
  39. undefined -> {error,not_found};
  40. E -> {error,E} end.
  41. delete(Tab, Key) ->
  42. ok.
  43. key_to_bin(Key) ->
  44. if is_integer(Key) -> erlang:list_to_binary(integer_to_list(Key));
  45. is_list(Key) -> erlang:list_to_binary(Key);
  46. is_atom(Key) -> erlang:list_to_binary(erlang:atom_to_list(Key));
  47. is_binary(Key) -> Key;
  48. true -> [ListKey] = io_lib:format("~p", [Key]), erlang:list_to_binary(ListKey) end.
  49. all(RecordName) ->
  50. {list_of_data,List} = kai_store:list(table_to_num(RecordName)),
  51. [ kai_get(Data) || Data <- List ].
  52. all_by_index(Tab, IndexId, IndexVal) -> [].
  53. % index funs
  54. products(UId) -> all_by_index(user_product, <<"user_bin">>, list_to_binary(UId)).
  55. subscriptions(UId) -> all_by_index(subsciption, <<"subs_who_bin">>, list_to_binary(UId)).
  56. subscribed(Who) -> all_by_index(subscription, <<"subs_whom_bin">>, list_to_binary(Who)).
  57. participate(UserName) -> all_by_index(group_subscription, <<"who_bin">>, UserName).
  58. members(GroupName) -> all_by_index(group_subscription, <<"where_bin">>, GroupName).
  59. user_tournaments(UId) -> all_by_index(play_record, <<"play_record_who_bin">>, list_to_binary(UId)).
  60. tournament_users(TId) -> all_by_index(play_record, <<"play_record_tournament_bin">>, list_to_binary(integer_to_list(TId))).
  61. author_comments(Who) ->
  62. EIDs = [E || #comment{entry_id=E} <- all_by_index(comment,<<"author_bin">>, Who) ],
  63. lists:flatten([ all_by_index(entry,<<"entry_bin">>,EID) || EID <- EIDs]).
  64. table_to_num(user) -> 10;
  65. table_to_num(user_status) -> 20;
  66. table_to_num(subscription) -> 30;
  67. table_to_num(group) -> 40;
  68. table_to_num(group_subscription) -> 50;
  69. table_to_num(payment) -> 60;
  70. table_to_num(user_payment) -> 70;
  71. table_to_num(account) -> 80;
  72. table_to_num(transaction) -> 90;
  73. table_to_num(id_seq) -> 100;
  74. table_to_num(team) -> 110;
  75. table_to_num(membership) -> 120;
  76. table_to_num(product) -> 130;
  77. table_to_num(product_category) -> 140;
  78. table_to_num(user_product) -> 150;
  79. table_to_num(acl) -> 160;
  80. table_to_num(acl_entry) -> 170;
  81. table_to_num(feed) -> 180;
  82. table_to_num(entry) -> 190;
  83. table_to_num(comment) -> 200.