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.

124 lines
2.8 KiB

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