document_store.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*!
  2. * lunr.Store
  3. * Copyright (C) @YEAR Oliver Nightingale
  4. */
  5. /**
  6. * lunr.Store is a simple key-value store used for storing sets of tokens for
  7. * documents stored in index.
  8. *
  9. * @constructor
  10. * @module
  11. */
  12. lunr.Store = function () {
  13. this.store = {}
  14. this.length = 0
  15. }
  16. /**
  17. * Loads a previously serialised store
  18. *
  19. * @param {Object} serialisedData The serialised store to load.
  20. * @returns {lunr.Store}
  21. * @memberOf Store
  22. */
  23. lunr.Store.load = function (serialisedData) {
  24. var store = new this
  25. store.length = serialisedData.length
  26. store.store = Object.keys(serialisedData.store).reduce(function (memo, key) {
  27. memo[key] = lunr.SortedSet.load(serialisedData.store[key])
  28. return memo
  29. }, {})
  30. return store
  31. }
  32. /**
  33. * Stores the given tokens in the store against the given id.
  34. *
  35. * @param {Object} id The key used to store the tokens against.
  36. * @param {Object} tokens The tokens to store against the key.
  37. * @memberOf Store
  38. */
  39. lunr.Store.prototype.set = function (id, tokens) {
  40. if (!this.has(id)) this.length++
  41. this.store[id] = tokens
  42. }
  43. /**
  44. * Retrieves the tokens from the store for a given key.
  45. *
  46. * @param {Object} id The key to lookup and retrieve from the store.
  47. * @returns {Object}
  48. * @memberOf Store
  49. */
  50. lunr.Store.prototype.get = function (id) {
  51. return this.store[id]
  52. }
  53. /**
  54. * Checks whether the store contains a key.
  55. *
  56. * @param {Object} id The id to look up in the store.
  57. * @returns {Boolean}
  58. * @memberOf Store
  59. */
  60. lunr.Store.prototype.has = function (id) {
  61. return id in this.store
  62. }
  63. /**
  64. * Removes the value for a key in the store.
  65. *
  66. * @param {Object} id The id to remove from the store.
  67. * @memberOf Store
  68. */
  69. lunr.Store.prototype.remove = function (id) {
  70. if (!this.has(id)) return
  71. delete this.store[id]
  72. this.length--
  73. }
  74. /**
  75. * Returns a representation of the store ready for serialisation.
  76. *
  77. * @returns {Object}
  78. * @memberOf Store
  79. */
  80. lunr.Store.prototype.toJSON = function () {
  81. return {
  82. store: this.store,
  83. length: this.length
  84. }
  85. }