github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/third_party/code.google.com/p/leveldb-go/leveldb/db/db.go (about) 1 // Copyright 2011 The LevelDB-Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package db defines the interfaces for a key/value store. 6 // 7 // A DB's basic operations (Get, Set, Delete) should be self-explanatory. Get 8 // and Delete will return ErrNotFound if the requested key is not in the store. 9 // Callers are free to ignore this error. 10 // 11 // A DB also allows for iterating over the key/value pairs in key order. If d 12 // is a DB, the code below prints all key/value pairs whose keys are 'greater 13 // than or equal to' k: 14 // 15 // iter := d.Find(k) 16 // for iter.Next() { 17 // fmt.Printf("key=%q value=%q\n", iter.Key(), iter.Value()) 18 // } 19 // return iter.Close() 20 // 21 // Other leveldb packages provide implementations of these interfaces. The 22 // Options struct in this package holds the optional parameters for these 23 // implementations, including a Comparer to define a 'less than' relationship 24 // over keys. It is always valid to pass a nil *Options, which means to use 25 // the default parameter values. Any zero field of a non-nil *Options also 26 // means to use the default value for that parameter. Thus, the code below 27 // uses a custom Comparer, but the default values for every other parameter: 28 // 29 // db := memdb.New(&db.Options{ 30 // Comparer: myComparer, 31 // }) 32 package db 33 34 import ( 35 "errors" 36 ) 37 38 // ErrNotFound means that a get or delete call did not find the requested key. 39 var ErrNotFound = errors.New("leveldb/db: not found") 40 41 // Iterator iterates over a DB's key/value pairs in key order. 42 // 43 // An iterator must be closed after use, but it is not necessary to read an 44 // iterator until exhaustion. 45 // 46 // An iterator is not necessarily goroutine-safe, but it is safe to use 47 // multiple iterators concurrently, with each in a dedicated goroutine. 48 // 49 // It is also safe to use an iterator concurrently with modifying its 50 // underlying DB, if that DB permits modification. However, the resultant 51 // key/value pairs are not guaranteed to be a consistent snapshot of that DB 52 // at a particular point in time. 53 type Iterator interface { 54 // Next moves the iterator to the next key/value pair. 55 // It returns whether the iterator is exhausted. 56 Next() bool 57 58 // Key returns the key of the current key/value pair, or nil if done. 59 // The caller should not modify the contents of the returned slice, and 60 // its contents may change on the next call to Next. 61 Key() []byte 62 63 // Value returns the value of the current key/value pair, or nil if done. 64 // The caller should not modify the contents of the returned slice, and 65 // its contents may change on the next call to Next. 66 Value() []byte 67 68 // Close closes the iterator and returns any accumulated error. Exhausting 69 // all the key/value pairs in a table is not considered to be an error. 70 // It is valid to call Close multiple times. Other methods should not be 71 // called after the iterator has been closed. 72 Close() error 73 } 74 75 // DB is a key/value store. 76 // 77 // It is safe to call Get and Find from concurrent goroutines. It is not 78 // necessarily safe to do so for Set and Delete. 79 // 80 // Some implementations may impose additional restrictions. For example: 81 // - Set calls may need to be in increasing key order. 82 // - a DB may be read-only or write-only. 83 type DB interface { 84 // Get gets the value for the given key. It returns ErrNotFound if the DB 85 // does not contain the key. 86 // 87 // The caller should not modify the contents of the returned slice, but 88 // it is safe to modify the contents of the argument after Get returns. 89 Get(key []byte, o *ReadOptions) (value []byte, err error) 90 91 // Set sets the value for the given key. It overwrites any previous value 92 // for that key; a DB is not a multi-map. 93 // 94 // It is safe to modify the contents of the arguments after Set returns. 95 Set(key, value []byte, o *WriteOptions) error 96 97 // Delete deletes the value for the given key. It returns ErrNotFound if 98 // the DB does not contain the key. 99 // 100 // It is safe to modify the contents of the arguments after Delete returns. 101 Delete(key []byte, o *WriteOptions) error 102 103 // Find returns an iterator positioned before the first key/value pair 104 // whose key is 'greater than or equal to' the given key. There may be no 105 // such pair, in which case the iterator will return false on Next. 106 // 107 // Any error encountered will be implicitly returned via the iterator. An 108 // error-iterator will yield no key/value pairs and closing that iterator 109 // will return that error. 110 // 111 // It is safe to modify the contents of the argument after Find returns. 112 Find(key []byte, o *ReadOptions) Iterator 113 114 // Close closes the DB. It may or may not close any underlying io.Reader 115 // or io.Writer, depending on how the DB was created. 116 // 117 // It is not safe to close a DB until all outstanding iterators are closed. 118 // It is valid to call Close multiple times. Other methods should not be 119 // called after the DB has been closed. 120 Close() error 121 }