kvstore.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright (c) 2014 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package store
  15. import "encoding/json"
  16. // KVStore is an abstraction for working with KV stores. Note that
  17. // in order to be used with the bleve.registry, it must also implement
  18. // a constructor function of the registry.KVStoreConstructor type.
  19. type KVStore interface {
  20. // Writer returns a KVWriter which can be used to
  21. // make changes to the KVStore. If a writer cannot
  22. // be obtained a non-nil error is returned.
  23. Writer() (KVWriter, error)
  24. // Reader returns a KVReader which can be used to
  25. // read data from the KVStore. If a reader cannot
  26. // be obtained a non-nil error is returned.
  27. Reader() (KVReader, error)
  28. // Close closes the KVStore
  29. Close() error
  30. }
  31. // KVReader is an abstraction of an **ISOLATED** reader
  32. // In this context isolated is defined to mean that
  33. // writes/deletes made after the KVReader is opened
  34. // are not observed.
  35. // Because there is usually a cost associated with
  36. // keeping isolated readers active, users should
  37. // close them as soon as they are no longer needed.
  38. type KVReader interface {
  39. // Get returns the value associated with the key
  40. // If the key does not exist, nil is returned.
  41. // The caller owns the bytes returned.
  42. Get(key []byte) ([]byte, error)
  43. // MultiGet retrieves multiple values in one call.
  44. MultiGet(keys [][]byte) ([][]byte, error)
  45. // PrefixIterator returns a KVIterator that will
  46. // visit all K/V pairs with the provided prefix
  47. PrefixIterator(prefix []byte) KVIterator
  48. // RangeIterator returns a KVIterator that will
  49. // visit all K/V pairs >= start AND < end
  50. RangeIterator(start, end []byte) KVIterator
  51. // Close closes the iterator
  52. Close() error
  53. }
  54. // KVIterator is an abstraction around key iteration
  55. type KVIterator interface {
  56. // Seek will advance the iterator to the specified key
  57. Seek(key []byte)
  58. // Next will advance the iterator to the next key
  59. Next()
  60. // Key returns the key pointed to by the iterator
  61. // The bytes returned are **ONLY** valid until the next call to Seek/Next/Close
  62. // Continued use after that requires that they be copied.
  63. Key() []byte
  64. // Value returns the value pointed to by the iterator
  65. // The bytes returned are **ONLY** valid until the next call to Seek/Next/Close
  66. // Continued use after that requires that they be copied.
  67. Value() []byte
  68. // Valid returns whether or not the iterator is in a valid state
  69. Valid() bool
  70. // Current returns Key(),Value(),Valid() in a single operation
  71. Current() ([]byte, []byte, bool)
  72. // Close closes the iterator
  73. Close() error
  74. }
  75. // KVWriter is an abstraction for mutating the KVStore
  76. // KVWriter does **NOT** enforce restrictions of a single writer
  77. // if the underlying KVStore allows concurrent writes, the
  78. // KVWriter interface should also do so, it is up to the caller
  79. // to do this in a way that is safe and makes sense
  80. type KVWriter interface {
  81. // NewBatch returns a KVBatch for performing batch operations on this kvstore
  82. NewBatch() KVBatch
  83. // NewBatchEx returns a KVBatch and an associated byte array
  84. // that's pre-sized based on the KVBatchOptions. The caller can
  85. // use the returned byte array for keys and values associated with
  86. // the batch. Once the batch is either executed or closed, the
  87. // associated byte array should no longer be accessed by the
  88. // caller.
  89. NewBatchEx(KVBatchOptions) ([]byte, KVBatch, error)
  90. // ExecuteBatch will execute the KVBatch, the provided KVBatch **MUST** have
  91. // been created by the same KVStore (though not necessarily the same KVWriter)
  92. // Batch execution is atomic, either all the operations or none will be performed
  93. ExecuteBatch(batch KVBatch) error
  94. // Close closes the writer
  95. Close() error
  96. }
  97. // KVBatchOptions provides the KVWriter.NewBatchEx() method with batch
  98. // preparation and preallocation information.
  99. type KVBatchOptions struct {
  100. // TotalBytes is the sum of key and value bytes needed by the
  101. // caller for the entire batch. It affects the size of the
  102. // returned byte array of KVWrite.NewBatchEx().
  103. TotalBytes int
  104. // NumSets is the number of Set() calls the caller will invoke on
  105. // the KVBatch.
  106. NumSets int
  107. // NumDeletes is the number of Delete() calls the caller will invoke
  108. // on the KVBatch.
  109. NumDeletes int
  110. // NumMerges is the number of Merge() calls the caller will invoke
  111. // on the KVBatch.
  112. NumMerges int
  113. }
  114. // KVBatch is an abstraction for making multiple KV mutations at once
  115. type KVBatch interface {
  116. // Set updates the key with the specified value
  117. // both key and value []byte may be reused as soon as this call returns
  118. Set(key, val []byte)
  119. // Delete removes the specified key
  120. // the key []byte may be reused as soon as this call returns
  121. Delete(key []byte)
  122. // Merge merges old value with the new value at the specified key
  123. // as prescribed by the KVStores merge operator
  124. // both key and value []byte may be reused as soon as this call returns
  125. Merge(key, val []byte)
  126. // Reset frees resources for this batch and allows reuse
  127. Reset()
  128. // Close frees resources
  129. Close() error
  130. }
  131. // KVStoreStats is an optional interface that KVStores can implement
  132. // if they're able to report any useful stats
  133. type KVStoreStats interface {
  134. // Stats returns a JSON serializable object representing stats for this KVStore
  135. Stats() json.Marshaler
  136. StatsMap() map[string]interface{}
  137. }