backend.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright (C) 2019 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package backend
  7. import (
  8. "sync"
  9. )
  10. // The Reader interface specifies the read-only operations available on the
  11. // main database and on read-only transactions (snapshots). Note that when
  12. // called directly on the database handle these operations may take implicit
  13. // transactions and performance may suffer.
  14. type Reader interface {
  15. Get(key []byte) ([]byte, error)
  16. NewPrefixIterator(prefix []byte) (Iterator, error)
  17. NewRangeIterator(first, last []byte) (Iterator, error)
  18. }
  19. // The Writer interface specifies the mutating operations available on the
  20. // main database and on writable transactions. Note that when called
  21. // directly on the database handle these operations may take implicit
  22. // transactions and performance may suffer.
  23. type Writer interface {
  24. Put(key, val []byte) error
  25. Delete(key []byte) error
  26. }
  27. // The ReadTransaction interface specifies the operations on read-only
  28. // transactions. Every ReadTransaction must be released when no longer
  29. // required.
  30. type ReadTransaction interface {
  31. Reader
  32. Release()
  33. }
  34. // The WriteTransaction interface specifies the operations on writable
  35. // transactions. Every WriteTransaction must be either committed or released
  36. // (i.e., discarded) when no longer required. No further operations must be
  37. // performed after release or commit (regardless of whether commit succeeded),
  38. // with one exception -- it's fine to release an already committed or released
  39. // transaction.
  40. //
  41. // A Checkpoint is a potential partial commit of the transaction so far, for
  42. // purposes of saving memory when transactions are in-RAM. Note that
  43. // transactions may be checkpointed *anyway* even if this is not called, due to
  44. // resource constraints, but this gives you a chance to decide when.
  45. type WriteTransaction interface {
  46. ReadTransaction
  47. Writer
  48. Checkpoint() error
  49. Commit() error
  50. }
  51. // The Iterator interface specifies the operations available on iterators
  52. // returned by NewPrefixIterator and NewRangeIterator. The iterator pattern
  53. // is to loop while Next returns true, then check Error after the loop. Next
  54. // will return false when iteration is complete (Error() == nil) or when
  55. // there is an error preventing iteration, which is then returned by
  56. // Error(). For example:
  57. //
  58. // it, err := db.NewPrefixIterator(nil)
  59. // if err != nil {
  60. // // problem preventing iteration
  61. // }
  62. // defer it.Release()
  63. // for it.Next() {
  64. // // ...
  65. // }
  66. // if err := it.Error(); err != nil {
  67. // // there was a database problem while iterating
  68. // }
  69. //
  70. // An iterator must be Released when no longer required. The Error method
  71. // can be called either before or after Release with the same results. If an
  72. // iterator was created in a transaction (whether read-only or write) it
  73. // must be released before the transaction is released (or committed).
  74. type Iterator interface {
  75. Next() bool
  76. Key() []byte
  77. Value() []byte
  78. Error() error
  79. Release()
  80. }
  81. // The Backend interface represents the main database handle. It supports
  82. // both read/write operations and opening read-only or writable
  83. // transactions. Depending on the actual implementation, individual
  84. // read/write operations may be implicitly wrapped in transactions, making
  85. // them perform quite badly when used repeatedly. For bulk operations,
  86. // consider always using a transaction of the appropriate type. The
  87. // transaction isolation level is "read committed" - there are no dirty
  88. // reads.
  89. type Backend interface {
  90. Reader
  91. Writer
  92. NewReadTransaction() (ReadTransaction, error)
  93. NewWriteTransaction() (WriteTransaction, error)
  94. Close() error
  95. Compact() error
  96. }
  97. type Tuning int
  98. const (
  99. // N.b. these constants must match those in lib/config.Tuning!
  100. TuningAuto Tuning = iota
  101. TuningSmall
  102. TuningLarge
  103. )
  104. func Open(path string, tuning Tuning) (Backend, error) {
  105. return OpenLevelDB(path, tuning)
  106. }
  107. func OpenMemory() Backend {
  108. return OpenLevelDBMemory()
  109. }
  110. type errClosed struct{}
  111. func (errClosed) Error() string { return "database is closed" }
  112. type errNotFound struct{}
  113. func (errNotFound) Error() string { return "key not found" }
  114. func IsClosed(err error) bool {
  115. if _, ok := err.(errClosed); ok {
  116. return true
  117. }
  118. if _, ok := err.(*errClosed); ok {
  119. return true
  120. }
  121. return false
  122. }
  123. func IsNotFound(err error) bool {
  124. if _, ok := err.(errNotFound); ok {
  125. return true
  126. }
  127. if _, ok := err.(*errNotFound); ok {
  128. return true
  129. }
  130. return false
  131. }
  132. // releaser manages counting on top of a waitgroup
  133. type releaser struct {
  134. wg *closeWaitGroup
  135. once *sync.Once
  136. }
  137. func newReleaser(wg *closeWaitGroup) (*releaser, error) {
  138. if err := wg.Add(1); err != nil {
  139. return nil, err
  140. }
  141. return &releaser{
  142. wg: wg,
  143. once: new(sync.Once),
  144. }, nil
  145. }
  146. func (r releaser) Release() {
  147. // We use the Once because we may get called multiple times from
  148. // Commit() and deferred Release().
  149. r.once.Do(func() {
  150. r.wg.Done()
  151. })
  152. }
  153. // closeWaitGroup behaves just like a sync.WaitGroup, but does not require
  154. // a single routine to do the Add and Wait calls. If Add is called after
  155. // CloseWait, it will return an error, and both are safe to be used concurrently.
  156. type closeWaitGroup struct {
  157. sync.WaitGroup
  158. closed bool
  159. closeMut sync.RWMutex
  160. }
  161. func (cg *closeWaitGroup) Add(i int) error {
  162. cg.closeMut.RLock()
  163. defer cg.closeMut.RUnlock()
  164. if cg.closed {
  165. return errClosed{}
  166. }
  167. cg.WaitGroup.Add(i)
  168. return nil
  169. }
  170. func (cg *closeWaitGroup) CloseWait() {
  171. cg.closeMut.Lock()
  172. cg.closed = true
  173. cg.closeMut.Unlock()
  174. cg.WaitGroup.Wait()
  175. }