kvs_stream.htm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <html>
  2. <head>
  3. <meta charset="utf-8" />
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <meta name="description" content="" />
  7. <meta name="author" content="Maxim Sokhatsky" />
  8. <title>STREAM</title>
  9. <link rel="stylesheet" href="https://n2o.space/n2o.css" />
  10. </head>
  11. <body>
  12. <header>
  13. <a href="../index.html"><img src="https://n2o.space/img/Synrc Neo.svg"></a>
  14. <h1>STREAM</h1>
  15. </header>
  16. <main>
  17. <section>
  18. <h3>INTRO</h3>
  19. <p>The <b>kvs_stream</b> is intended to store and retrieve doubly-linked lists using
  20. simple key-value access to different databases through its backends:
  21. redis, mongo, mnesia, riak, kai, fs.
  22. The main descriptor of the list is the cursor which holds the cached value of one of list elements
  23. and also two pointers to first and last elements along with default traversal direction.
  24. Cursor should be stored in databse, if there is no cursor for some data then this data is not alive yet.
  25. The data could be added only from list ends.
  26. The data in list could be removed by record id.
  27. The list could not contain duplicates or even records with the same id.
  28. When you consume the stream, the data is not deleted, you need to remove it manually.</p>
  29. </section>
  30. <section>
  31. <h3>RECORDS</h3>
  32. <figure><figcaption>KVS CORE</figcaption><code>
  33. #ok { data= [] :: term() }.
  34. #error { data= [] :: term() }.
  35. #cur { id= [] :: term(),
  36. val= [] :: [] | tuple(),
  37. dir= 0 :: 0 | 1,
  38. top= [] :: [] | integer(),
  39. bot= [] :: [] | integer()}).
  40. #iter { id= [] :: [] | integer(),
  41. prev= [] :: [] | integer(),
  42. next= [] :: [] | integer()).
  43. </code></figure>
  44. </section>
  45. <section>
  46. <h3>EXAMPLE</h3>
  47. <figure><code>
  48. create_destroy() ->
  49. Cur = new(),
  50. [A,B,C,D] = [ kvs:next_id(person,1)
  51. || _ <- lists:seq(1,4) ],
  52. S = kvs_stream:save(
  53. kvs_stream:add(#person{id=A},
  54. kvs_stream:down(
  55. kvs_stream:add(#person{id=B},
  56. kvs_stream:up(
  57. kvs_stream:add(#person{id=C},
  58. kvs_stream:down(
  59. kvs_stream:add(#person{id=D},
  60. kvs_stream:up(
  61. kvs_stream:new()))))))))),
  62. Y = kvs_stream:remove(B,
  63. kvs_stream:remove(D,
  64. kvs_stream:remove(A,
  65. kvs_stream:remove(C,S)))),
  66. [] = kvs_stream:take(-1,
  67. kvs_stream:down(
  68. kvs_stream:top(Y))).
  69. </code></figure>
  70. </section>
  71. <section>
  72. <h3>API</h3>
  73. <h4>new() -> #cur{}.</h4>
  74. <p>Creates a KVS cursor.</p>
  75. <h4>save(#cur{}) -> #cur{}.</h4>
  76. <p>Saves cursor to database.<?p>
  77. <h4>load() -> #ok{data::#cur{}} | #error{}.</h4>
  78. <p>Gets a curson from database.</p>
  79. <h4>next(#cur{}) -> #cur{}.</h4>
  80. <p>Moves cursor to next. Consume data top down.</p>
  81. <h4>prev(#cur{}) -> #cur{}.</h4>
  82. <p>Moves cursor to prev. Consume data bottom up.</p>
  83. <h4>seek(Id,#cur{}) -> #cur{} | #error{}.</h4>
  84. <p>Moves cursor to record by its id.
  85. If cursor has no cached value then function returns error.</p>
  86. <h4>top(#cur{}) -> #cur{}.</h4>
  87. <p>Moves cursor to top of the list.</p>
  88. <h4>bot(#cur{}) -> #cur{}.</h4>
  89. <p>Moves cursor to bottom of the list.</p>
  90. <h4>add(Message,#cur{}) -> #cur{}.</h4>
  91. <p>Adds message to datatabase and update cursor to new data.
  92. Message is linked on next prev fields with existed data under cursor.
  93. If cursor doesn't contain top or bottom value the additional
  94. seek to the end is performed according to cursor direction.</p>
  95. <h4>remove(Id,#cur{}) -> #cur{} | #error{}.</h4>
  96. <p>Removes record by id from database and unlink it from list.
  97. If cursor has no cached value then function returns error.</p>
  98. <h4>take(N,#cur{}) -> list().</h4>
  99. <p>Trying to consume N records from stream using its current value and direction. Returns consumed data.</p>
  100. </section>
  101. <section>
  102. <h3>CONFIG</h3>
  103. <p>In sys.config you should specify kvs backend and list of modules containing <b>metainfo/0</b> exported function.</p>
  104. <figure><code>
  105. [{kvs, [{dba, store_mnesia},
  106. {schema, [kvs]} ]}].
  107. </code></figure>
  108. </section>
  109. <section>
  110. <p>This module may refer to:
  111. <a href="http://erlang.org/doc/man/mnesia.html">mnesia</a></b>,
  112. <a href="kvs.htm"><b>kvs</b></a>.
  113. </p>
  114. </section>
  115. </main>
  116. <footer>
  117. 2005&mdash;2017 &copy; Synrc Research Center
  118. </footer>
  119. </body>
  120. </html>