backend.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. }
  96. type Tuning int
  97. const (
  98. // N.b. these constants must match those in lib/config.Tuning!
  99. TuningAuto Tuning = iota
  100. TuningSmall
  101. TuningLarge
  102. )
  103. func Open(path string, tuning Tuning) (Backend, error) {
  104. return OpenLevelDB(path, tuning)
  105. }
  106. func OpenMemory() Backend {
  107. return OpenLevelDBMemory()
  108. }
  109. type errClosed struct{}
  110. func (errClosed) Error() string { return "database is closed" }
  111. type errNotFound struct{}
  112. func (errNotFound) Error() string { return "key not found" }
  113. func IsClosed(err error) bool {
  114. if _, ok := err.(errClosed); ok {
  115. return true
  116. }
  117. if _, ok := err.(*errClosed); ok {
  118. return true
  119. }
  120. return false
  121. }
  122. func IsNotFound(err error) bool {
  123. if _, ok := err.(errNotFound); ok {
  124. return true
  125. }
  126. if _, ok := err.(*errNotFound); ok {
  127. return true
  128. }
  129. return false
  130. }
  131. // releaser manages counting on top of a waitgroup
  132. type releaser struct {
  133. wg *sync.WaitGroup
  134. once *sync.Once
  135. }
  136. func newReleaser(wg *sync.WaitGroup) *releaser {
  137. wg.Add(1)
  138. return &releaser{
  139. wg: wg,
  140. once: new(sync.Once),
  141. }
  142. }
  143. func (r releaser) Release() {
  144. // We use the Once because we may get called multiple times from
  145. // Commit() and deferred Release().
  146. r.once.Do(func() {
  147. r.wg.Done()
  148. })
  149. }