You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.6 KiB

7 years ago
7 years ago
7 years ago
  1. package db
  2. // DBs are goroutine safe.
  3. type DB interface {
  4. // Get returns nil iff key doesn't exist.
  5. // A nil key is interpreted as an empty byteslice.
  6. // CONTRACT: key, value readonly []byte
  7. Get([]byte) []byte
  8. // Has checks if a key exists.
  9. // A nil key is interpreted as an empty byteslice.
  10. // CONTRACT: key, value readonly []byte
  11. Has(key []byte) bool
  12. // Set sets the key.
  13. // A nil key is interpreted as an empty byteslice.
  14. // CONTRACT: key, value readonly []byte
  15. Set([]byte, []byte)
  16. SetSync([]byte, []byte)
  17. // Delete deletes the key.
  18. // A nil key is interpreted as an empty byteslice.
  19. // CONTRACT: key readonly []byte
  20. Delete([]byte)
  21. DeleteSync([]byte)
  22. // Iterate over a domain of keys in ascending order. End is exclusive.
  23. // Start must be less than end, or the Iterator is invalid.
  24. // A nil start is interpreted as an empty byteslice.
  25. // If end is nil, iterates up to the last item (inclusive).
  26. // CONTRACT: No writes may happen within a domain while an iterator exists over it.
  27. // CONTRACT: start, end readonly []byte
  28. Iterator(start, end []byte) Iterator
  29. // Iterate over a domain of keys in descending order. End is exclusive.
  30. // Start must be less than end, or the Iterator is invalid.
  31. // If start is nil, iterates up to the first/least item (inclusive).
  32. // If end is nil, iterates from the last/greatest item (inclusive).
  33. // CONTRACT: No writes may happen within a domain while an iterator exists over it.
  34. // CONTRACT: start, end readonly []byte
  35. ReverseIterator(start, end []byte) Iterator
  36. // Closes the connection.
  37. Close()
  38. // Creates a batch for atomic updates.
  39. NewBatch() Batch
  40. // For debugging
  41. Print()
  42. // Stats returns a map of property values for all keys and the size of the cache.
  43. Stats() map[string]string
  44. }
  45. //----------------------------------------
  46. // Batch
  47. // Batch Close must be called when the program no longer needs the object.
  48. type Batch interface {
  49. SetDeleter
  50. Write()
  51. WriteSync()
  52. Close()
  53. }
  54. type SetDeleter interface {
  55. Set(key, value []byte) // CONTRACT: key, value readonly []byte
  56. Delete(key []byte) // CONTRACT: key readonly []byte
  57. }
  58. //----------------------------------------
  59. // Iterator
  60. /*
  61. Usage:
  62. var itr Iterator = ...
  63. defer itr.Close()
  64. for ; itr.Valid(); itr.Next() {
  65. k, v := itr.Key(); itr.Value()
  66. // ...
  67. }
  68. */
  69. type Iterator interface {
  70. // The start & end (exclusive) limits to iterate over.
  71. // If end < start, then the Iterator goes in reverse order.
  72. //
  73. // A domain of ([]byte{12, 13}, []byte{12, 14}) will iterate
  74. // over anything with the prefix []byte{12, 13}.
  75. //
  76. // The smallest key is the empty byte array []byte{} - see BeginningKey().
  77. // The largest key is the nil byte array []byte(nil) - see EndingKey().
  78. // CONTRACT: start, end readonly []byte
  79. Domain() (start []byte, end []byte)
  80. // Valid returns whether the current position is valid.
  81. // Once invalid, an Iterator is forever invalid.
  82. Valid() bool
  83. // Next moves the iterator to the next sequential key in the database, as
  84. // defined by order of iteration.
  85. //
  86. // If Valid returns false, this method will panic.
  87. Next()
  88. // Key returns the key of the cursor.
  89. // If Valid returns false, this method will panic.
  90. // CONTRACT: key readonly []byte
  91. Key() (key []byte)
  92. // Value returns the value of the cursor.
  93. // If Valid returns false, this method will panic.
  94. // CONTRACT: value readonly []byte
  95. Value() (value []byte)
  96. // Close releases the Iterator.
  97. Close()
  98. }
  99. // For testing convenience.
  100. func bz(s string) []byte {
  101. return []byte(s)
  102. }
  103. // We defensively turn nil keys or values into []byte{} for
  104. // most operations.
  105. func nonNilBytes(bz []byte) []byte {
  106. if bz == nil {
  107. return []byte{}
  108. }
  109. return bz
  110. }