Source: database.js

  1. /*
  2. * Copyright 2017-2019, Intel Corporation
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * * Neither the name of the copyright holder nor the names of its
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. /**
  33. * Main Node.js pmemkv class.
  34. *
  35. * It contains Node.js pmemkv public enum *constants* and class *db* with its
  36. * functions and members.
  37. *
  38. * @link https://github.com/pmem/pmemkv-nodejs
  39. * @file This files defines the *db* class and exports *constants* enum.
  40. * @since 1.0
  41. */
  42. const pmemkv = require('bindings')('pmemkv');
  43. /** @class Main Node.js pmemkv class, it provides functions to operate on data in database.
  44. * If an error/exception is thrown from a method it will contain *status* variable.
  45. * Possible statuses are enumerated in constants.status.
  46. */
  47. class db {
  48. /**
  49. * Creates an instance of db.
  50. *
  51. * @constructor
  52. * @throws {Error} on any failure.
  53. * @param {string} engine Name of the engine to work with.
  54. * @param {object} config JSON like config with parameters specified for the engine.
  55. */
  56. constructor(engine, config) {
  57. this._stopped = false;
  58. this._db = new pmemkv.db(engine, config);
  59. Object.defineProperty(this, '_db', {configurable: false, writable: false});
  60. }
  61. /**
  62. * Stops the database.
  63. */
  64. stop() {
  65. if (!this._stopped) {
  66. this._stopped = true;
  67. Object.defineProperty(this, '_stopped', {configurable: false, writable: false});
  68. this._db.stop();
  69. }
  70. }
  71. /**
  72. * Returns value of *stopped* property.
  73. *
  74. * @return {boolean} true if stopped, false otherwise.
  75. */
  76. get stopped() {
  77. return this._stopped;
  78. }
  79. /**
  80. * Executes function for every record stored in db. Callback is called
  81. * only with the key of each record.
  82. *
  83. * @throws {Error} on any failure.
  84. * @param {Function} callback - function to be called for every element stored in db.
  85. * It has only one param - key (for each returned element).
  86. */
  87. get_keys(callback) {
  88. this._db.get_keys(callback);
  89. }
  90. /**
  91. * Executes function for every record stored in db, whose keys are greater
  92. * than the given *key*. Callback is called only with the key of each record.
  93. *
  94. * @throws {Error} on any failure.
  95. * @param {string} key - sets the lower bound for querying.
  96. * @param {Function} callback - function to be called for each returned element.
  97. * It has only one param - key (for each returned element).
  98. */
  99. get_keys_above(key, callback) {
  100. this._db.get_keys_above(key, callback);
  101. }
  102. /**
  103. * Executes function for every record stored in db, whose keys are less than
  104. * the given *key*. Callback is called only with the key of each record.
  105. *
  106. * @throws {Error} on any failure.
  107. * @param {string} key - sets the upper bound for querying.
  108. * @param {Function} callback - function to be called for each returned element.
  109. * It has only one param - key (for each returned element).
  110. */
  111. get_keys_below(key, callback) {
  112. this._db.get_keys_below(key, callback);
  113. }
  114. /**
  115. * Executes function for every record stored in db, whose keys are
  116. * greater than the *key1* and less than the *key2*. Callback is
  117. * called only with the key of each record.
  118. *
  119. * @throws {Error} on any failure.
  120. * @param {string} key1 - sets the lower bound for querying.
  121. * @param {string} key2 - sets the upper bound for querying.
  122. * @param {Function} callback - function to be called for each returned element.
  123. * It has only one param - key (for each returned element).
  124. */
  125. get_keys_between(key1, key2, callback) {
  126. this._db.get_keys_between(key1, key2, callback);
  127. }
  128. /**
  129. * Returns number of currently stored elements in db.
  130. *
  131. * @throws {Error} on any failure.
  132. * @return {number|object} Number of records stored in db
  133. * or empty Value() if error occurred.
  134. */
  135. get count_all() {
  136. return this._db.count_all();
  137. }
  138. /**
  139. * Returns number of currently stored elements in db, whose keys are
  140. * greater than the given *key*.
  141. *
  142. * @throws {Error} on any failure.
  143. * @param {string} key - sets the lower bound of counting.
  144. * @return {number|object} Number of records in db matching query
  145. * or empty Value() if error occurred.
  146. */
  147. count_above(key) {
  148. return this._db.count_above(key);
  149. }
  150. /**
  151. * Returns number of currently stored elements in db, whose keys are
  152. * less than the given *key*.
  153. *
  154. * @throws {Error} on any failure.
  155. * @param {string} key - sets the upper bound of counting.
  156. * @return {number|object} Number of records in db matching query
  157. * or empty Value() if error occurred.
  158. */
  159. count_below(key) {
  160. return this._db.count_below(key);
  161. }
  162. /**
  163. * Returns number of currently stored elements in db, whose keys are
  164. * greater than the *key1* and less than the *key2*.
  165. *
  166. * @throws {Error} on any failure.
  167. * @param {string} key1 - sets the lower bound of counting.
  168. * @param {string} key2 - sets the upper bound of counting.
  169. * @return {number|object} Number of records in db matching query
  170. * or empty Value() if error occurred.
  171. */
  172. count_between(key1, key2) {
  173. return this._db.count_between(key1, key2);
  174. }
  175. /**
  176. * Executes function for every record stored in db.
  177. *
  178. * @throws {Error} on any failure.
  179. * @param {Function} callback - function to be called for every element stored in db.
  180. */
  181. get_all(callback) {
  182. this._db.get_all(callback)
  183. }
  184. /**
  185. * Executes function for every record stored in db, whose keys are
  186. * greater than the given *key*.
  187. *
  188. * @throws {Error} on any failure.
  189. * @param {string} key - sets the lower bound for querying.
  190. * @param {Function} callback - function to be called for each returned element.
  191. */
  192. get_above(key, callback) {
  193. this._db.get_above(key, callback)
  194. }
  195. /**
  196. * Executes function for every record stored in db, whose keys are
  197. * less than the given *key*.
  198. *
  199. * @throws {Error} on any failure.
  200. * @param {string} key - sets the upper bound for querying.
  201. * @param {Function} callback - function to be called for each returned element.
  202. */
  203. get_below(key, callback) {
  204. this._db.get_below(key, callback)
  205. }
  206. /**
  207. * Executes function for every record stored in db, whose keys are
  208. * greater than the *key1* and less than the *key2*.
  209. *
  210. * @throws {Error} on any failure.
  211. * @param {string} key1 - sets the lower bound for querying.
  212. * @param {string} key2 - sets the upper bound for querying.
  213. * @param {Function} callback - function to be called for each returned element.
  214. */
  215. get_between(key1, key2, callback) {
  216. this._db.get_between(key1, key2, callback)
  217. }
  218. /**
  219. * Checks existence of record with given *key*.
  220. *
  221. * @throws {Error} on any failure.
  222. * @param {string} key - record's key to query for.
  223. * @return {boolean} True if record with given key exists, False otherwise.
  224. */
  225. exists(key) {
  226. return this._db.exists(key);
  227. }
  228. /**
  229. * Gets value of record with given *key*.
  230. *
  231. * @throws {Error} on any failure.
  232. * @param {string} key - record's key to query for.
  233. * @return {object} string with value stored for this key,
  234. * env.Undefined() if not found, or empty Value() if error.
  235. */
  236. get(key) {
  237. return this._db.get(key);
  238. }
  239. /**
  240. * Inserts a key-value pair into pmemkv database.
  241. *
  242. * @throws {Error} on any failure.
  243. * @param {string} key - record's key; record will be put into database under its name.
  244. * @param {string} value - data to be inserted into this new database record.
  245. */
  246. put(key, value) {
  247. this._db.put(key, value);
  248. }
  249. /**
  250. * Removes from database record with given *key*.
  251. *
  252. * @throws {Error} on any failure.
  253. * @param {string} key - record's key to query for, to be removed.
  254. * @return {boolean} True if pmemkv returned status OK, False if status NOT_FOUND.
  255. */
  256. remove(key) {
  257. return this._db.remove(key);
  258. }
  259. }
  260. module.exports = db;
  261. module.exports.constants = pmemkv.constants;