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.

134 lines
3.5 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 greater than end, or the Iterator is invalid.
  31. // If start is nil, iterates from the last/greatest item (inclusive).
  32. // If end is nil, iterates up to the first/least 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. type Batch interface {
  48. SetDeleter
  49. Write()
  50. WriteSync()
  51. }
  52. type SetDeleter interface {
  53. Set(key, value []byte) // CONTRACT: key, value readonly []byte
  54. Delete(key []byte) // CONTRACT: key readonly []byte
  55. }
  56. //----------------------------------------
  57. // Iterator
  58. /*
  59. Usage:
  60. var itr Iterator = ...
  61. defer itr.Close()
  62. for ; itr.Valid(); itr.Next() {
  63. k, v := itr.Key(); itr.Value()
  64. // ...
  65. }
  66. */
  67. type Iterator interface {
  68. // The start & end (exclusive) limits to iterate over.
  69. // If end < start, then the Iterator goes in reverse order.
  70. //
  71. // A domain of ([]byte{12, 13}, []byte{12, 14}) will iterate
  72. // over anything with the prefix []byte{12, 13}.
  73. //
  74. // The smallest key is the empty byte array []byte{} - see BeginningKey().
  75. // The largest key is the nil byte array []byte(nil) - see EndingKey().
  76. // CONTRACT: start, end readonly []byte
  77. Domain() (start []byte, end []byte)
  78. // Valid returns whether the current position is valid.
  79. // Once invalid, an Iterator is forever invalid.
  80. Valid() bool
  81. // Next moves the iterator to the next sequential key in the database, as
  82. // defined by order of iteration.
  83. //
  84. // If Valid returns false, this method will panic.
  85. Next()
  86. // Key returns the key of the cursor.
  87. // If Valid returns false, this method will panic.
  88. // CONTRACT: key readonly []byte
  89. Key() (key []byte)
  90. // Value returns the value of the cursor.
  91. // If Valid returns false, this method will panic.
  92. // CONTRACT: value readonly []byte
  93. Value() (value []byte)
  94. // Close releases the Iterator.
  95. Close()
  96. }
  97. // For testing convenience.
  98. func bz(s string) []byte {
  99. return []byte(s)
  100. }
  101. // We defensively turn nil keys or values into []byte{} for
  102. // most operations.
  103. func nonNilBytes(bz []byte) []byte {
  104. if bz == nil {
  105. return []byte{}
  106. }
  107. return bz
  108. }