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.

133 lines
3.5 KiB

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