1 // Diskv (disk-vee) is a simple, persistent, key-value store.
2 // It stores all data flatly on the filesystem.
20 defaultBasePath = "diskv"
21 defaultFilePerm os.FileMode = 0666
22 defaultPathPerm os.FileMode = 0777
26 defaultTransform = func(s string) []string { return []string{} }
27 errCanceled = errors.New("canceled")
28 errEmptyKey = errors.New("empty key")
29 errBadKey = errors.New("bad key")
30 errImportDirectory = errors.New("can't import a directory")
33 // TransformFunction transforms a key into a slice of strings, with each
34 // element in the slice representing a directory in the file path where the
35 // key's entry will eventually be stored.
37 // For example, if TransformFunc transforms "abcdef" to ["ab", "cde", "f"],
38 // the final location of the data file will be <basedir>/ab/cde/f/abcdef
39 type TransformFunction func(s string) []string
41 // Options define a set of properties that dictate Diskv behavior.
42 // All values are optional.
45 Transform TransformFunction
46 CacheSizeMax uint64 // bytes
49 // If TempDir is set, it will enable filesystem atomic writes by
50 // writing temporary files to that location before being moved
52 // Note that TempDir MUST be on the same device/partition as
57 IndexLess LessFunction
59 Compression Compression
62 // Diskv implements the Diskv interface. You shouldn't construct Diskv
63 // structures directly; instead, use the New constructor.
67 cache map[string][]byte
71 // New returns an initialized Diskv structure, ready to use.
72 // If the path identified by baseDir already contains data,
73 // it will be accessible, but not yet cached.
74 func New(o Options) *Diskv {
76 o.BasePath = defaultBasePath
78 if o.Transform == nil {
79 o.Transform = defaultTransform
82 o.PathPerm = defaultPathPerm
85 o.FilePerm = defaultFilePerm
90 cache: map[string][]byte{},
94 if d.Index != nil && d.IndexLess != nil {
95 d.Index.Initialize(d.IndexLess, d.Keys(nil))
101 // Write synchronously writes the key-value pair to disk, making it immediately
102 // available for reads. Write relies on the filesystem to perform an eventual
103 // sync to physical media. If you need stronger guarantees, see WriteStream.
104 func (d *Diskv) Write(key string, val []byte) error {
105 return d.WriteStream(key, bytes.NewBuffer(val), false)
108 // WriteStream writes the data represented by the io.Reader to the disk, under
109 // the provided key. If sync is true, WriteStream performs an explicit sync on
110 // the file as soon as it's written.
112 // bytes.Buffer provides io.Reader semantics for basic data types.
113 func (d *Diskv) WriteStream(key string, r io.Reader, sync bool) error {
121 return d.writeStreamWithLock(key, r, sync)
124 // createKeyFileWithLock either creates the key file directly, or
125 // creates a temporary file in TempDir if it is set.
126 func (d *Diskv) createKeyFileWithLock(key string) (*os.File, error) {
128 if err := os.MkdirAll(d.TempDir, d.PathPerm); err != nil {
129 return nil, fmt.Errorf("temp mkdir: %s", err)
131 f, err := ioutil.TempFile(d.TempDir, "")
133 return nil, fmt.Errorf("temp file: %s", err)
136 if err := f.Chmod(d.FilePerm); err != nil {
137 f.Close() // error deliberately ignored
138 os.Remove(f.Name()) // error deliberately ignored
139 return nil, fmt.Errorf("chmod: %s", err)
144 mode := os.O_WRONLY | os.O_CREATE | os.O_TRUNC // overwrite if exists
145 f, err := os.OpenFile(d.completeFilename(key), mode, d.FilePerm)
147 return nil, fmt.Errorf("open file: %s", err)
152 // writeStream does no input validation checking.
153 func (d *Diskv) writeStreamWithLock(key string, r io.Reader, sync bool) error {
154 if err := d.ensurePathWithLock(key); err != nil {
155 return fmt.Errorf("ensure path: %s", err)
158 f, err := d.createKeyFileWithLock(key)
160 return fmt.Errorf("create key file: %s", err)
163 wc := io.WriteCloser(&nopWriteCloser{f})
164 if d.Compression != nil {
165 wc, err = d.Compression.Writer(f)
167 f.Close() // error deliberately ignored
168 os.Remove(f.Name()) // error deliberately ignored
169 return fmt.Errorf("compression writer: %s", err)
173 if _, err := io.Copy(wc, r); err != nil {
174 f.Close() // error deliberately ignored
175 os.Remove(f.Name()) // error deliberately ignored
176 return fmt.Errorf("i/o copy: %s", err)
179 if err := wc.Close(); err != nil {
180 f.Close() // error deliberately ignored
181 os.Remove(f.Name()) // error deliberately ignored
182 return fmt.Errorf("compression close: %s", err)
186 if err := f.Sync(); err != nil {
187 f.Close() // error deliberately ignored
188 os.Remove(f.Name()) // error deliberately ignored
189 return fmt.Errorf("file sync: %s", err)
193 if err := f.Close(); err != nil {
194 return fmt.Errorf("file close: %s", err)
197 if f.Name() != d.completeFilename(key) {
198 if err := os.Rename(f.Name(), d.completeFilename(key)); err != nil {
199 os.Remove(f.Name()) // error deliberately ignored
200 return fmt.Errorf("rename: %s", err)
208 d.bustCacheWithLock(key) // cache only on read
213 // Import imports the source file into diskv under the destination key. If the
214 // destination key already exists, it's overwritten. If move is true, the
215 // source file is removed after a successful import.
216 func (d *Diskv) Import(srcFilename, dstKey string, move bool) (err error) {
221 if fi, err := os.Stat(srcFilename); err != nil {
223 } else if fi.IsDir() {
224 return errImportDirectory
230 if err := d.ensurePathWithLock(dstKey); err != nil {
231 return fmt.Errorf("ensure path: %s", err)
235 if err := syscall.Rename(srcFilename, d.completeFilename(dstKey)); err == nil {
236 d.bustCacheWithLock(dstKey)
238 } else if err != syscall.EXDEV {
239 // If it failed due to being on a different device, fall back to copying
244 f, err := os.Open(srcFilename)
249 err = d.writeStreamWithLock(dstKey, f, false)
250 if err == nil && move {
251 err = os.Remove(srcFilename)
256 // Read reads the key and returns the value.
257 // If the key is available in the cache, Read won't touch the disk.
258 // If the key is not in the cache, Read will have the side-effect of
259 // lazily caching the value.
260 func (d *Diskv) Read(key string) ([]byte, error) {
261 rc, err := d.ReadStream(key, false)
266 return ioutil.ReadAll(rc)
269 // ReadStream reads the key and returns the value (data) as an io.ReadCloser.
270 // If the value is cached from a previous read, and direct is false,
271 // ReadStream will use the cached value. Otherwise, it will return a handle to
272 // the file on disk, and cache the data on read.
274 // If direct is true, ReadStream will lazily delete any cached value for the
275 // key, and return a direct handle to the file on disk.
277 // If compression is enabled, ReadStream taps into the io.Reader stream prior
278 // to decompression, and caches the compressed data.
279 func (d *Diskv) ReadStream(key string, direct bool) (io.ReadCloser, error) {
283 if val, ok := d.cache[key]; ok {
285 buf := bytes.NewBuffer(val)
286 if d.Compression != nil {
287 return d.Compression.Reader(buf)
289 return ioutil.NopCloser(buf), nil
295 d.uncacheWithLock(key, uint64(len(val)))
299 return d.readWithRLock(key)
302 // read ignores the cache, and returns an io.ReadCloser representing the
303 // decompressed data for the given key, streamed from the disk. Clients should
304 // acquire a read lock on the Diskv and check the cache themselves before
306 func (d *Diskv) readWithRLock(key string) (io.ReadCloser, error) {
307 filename := d.completeFilename(key)
309 fi, err := os.Stat(filename)
314 return nil, os.ErrNotExist
317 f, err := os.Open(filename)
323 if d.CacheSizeMax > 0 {
324 r = newSiphon(f, d, key)
326 r = &closingReader{f}
329 var rc = io.ReadCloser(ioutil.NopCloser(r))
330 if d.Compression != nil {
331 rc, err = d.Compression.Reader(r)
340 // closingReader provides a Reader that automatically closes the
341 // embedded ReadCloser when it reaches EOF
342 type closingReader struct {
346 func (cr closingReader) Read(p []byte) (int, error) {
347 n, err := cr.rc.Read(p)
349 if closeErr := cr.rc.Close(); closeErr != nil {
350 return n, closeErr // close must succeed for Read to succeed
356 // siphon is like a TeeReader: it copies all data read through it to an
357 // internal buffer, and moves that buffer to the cache at EOF.
365 // newSiphon constructs a siphoning reader that represents the passed file.
366 // When a successful series of reads ends in an EOF, the siphon will write
367 // the buffered data to Diskv's cache under the given key.
368 func newSiphon(f *os.File, d *Diskv, key string) io.Reader {
373 buf: &bytes.Buffer{},
377 // Read implements the io.Reader interface for siphon.
378 func (s *siphon) Read(p []byte) (int, error) {
379 n, err := s.f.Read(p)
382 return s.buf.Write(p[0:n]) // Write must succeed for Read to succeed
386 s.d.cacheWithoutLock(s.key, s.buf.Bytes()) // cache may fail
387 if closeErr := s.f.Close(); closeErr != nil {
388 return n, closeErr // close must succeed for Read to succeed
396 // Erase synchronously erases the given key from the disk and the cache.
397 func (d *Diskv) Erase(key string) error {
401 d.bustCacheWithLock(key)
409 filename := d.completeFilename(key)
410 if s, err := os.Stat(filename); err == nil {
414 if err = os.Remove(filename); err != nil {
418 // Return err as-is so caller can do os.IsNotExist(err).
422 // clean up and return
423 d.pruneDirsWithLock(key)
427 // EraseAll will delete all of the data from the store, both in the cache and on
428 // the disk. Note that EraseAll doesn't distinguish diskv-related data from non-
429 // diskv-related data. Care should be taken to always specify a diskv base
430 // directory that is exclusively for diskv data.
431 func (d *Diskv) EraseAll() error {
434 d.cache = make(map[string][]byte)
437 os.RemoveAll(d.TempDir) // errors ignored
439 return os.RemoveAll(d.BasePath)
442 // Has returns true if the given key exists.
443 func (d *Diskv) Has(key string) bool {
447 if _, ok := d.cache[key]; ok {
451 filename := d.completeFilename(key)
452 s, err := os.Stat(filename)
463 // Keys returns a channel that will yield every key accessible by the store,
464 // in undefined order. If a cancel channel is provided, closing it will
465 // terminate and close the keys channel.
466 func (d *Diskv) Keys(cancel <-chan struct{}) <-chan string {
467 return d.KeysPrefix("", cancel)
470 // KeysPrefix returns a channel that will yield every key accessible by the
471 // store with the given prefix, in undefined order. If a cancel channel is
472 // provided, closing it will terminate and close the keys channel. If the
473 // provided prefix is the empty string, all keys will be yielded.
474 func (d *Diskv) KeysPrefix(prefix string, cancel <-chan struct{}) <-chan string {
479 prepath = d.pathFor(prefix)
481 c := make(chan string)
483 filepath.Walk(prepath, walker(c, prefix, cancel))
489 // walker returns a function which satisfies the filepath.WalkFunc interface.
490 // It sends every non-directory file entry down the channel c.
491 func walker(c chan<- string, prefix string, cancel <-chan struct{}) filepath.WalkFunc {
492 return func(path string, info os.FileInfo, err error) error {
497 if info.IsDir() || !strings.HasPrefix(info.Name(), prefix) {
502 case c <- info.Name():
511 // pathFor returns the absolute path for location on the filesystem where the
512 // data for the given key will be stored.
513 func (d *Diskv) pathFor(key string) string {
514 return filepath.Join(d.BasePath, filepath.Join(d.Transform(key)...))
517 // ensurePathWithLock is a helper function that generates all necessary
518 // directories on the filesystem for the given key.
519 func (d *Diskv) ensurePathWithLock(key string) error {
520 return os.MkdirAll(d.pathFor(key), d.PathPerm)
523 // completeFilename returns the absolute path to the file for the given key.
524 func (d *Diskv) completeFilename(key string) string {
525 return filepath.Join(d.pathFor(key), key)
528 // cacheWithLock attempts to cache the given key-value pair in the store's
529 // cache. It can fail if the value is larger than the cache's maximum size.
530 func (d *Diskv) cacheWithLock(key string, val []byte) error {
531 valueSize := uint64(len(val))
532 if err := d.ensureCacheSpaceWithLock(valueSize); err != nil {
533 return fmt.Errorf("%s; not caching", err)
536 // be very strict about memory guarantees
537 if (d.cacheSize + valueSize) > d.CacheSizeMax {
538 panic(fmt.Sprintf("failed to make room for value (%d/%d)", valueSize, d.CacheSizeMax))
542 d.cacheSize += valueSize
546 // cacheWithoutLock acquires the store's (write) mutex and calls cacheWithLock.
547 func (d *Diskv) cacheWithoutLock(key string, val []byte) error {
550 return d.cacheWithLock(key, val)
553 func (d *Diskv) bustCacheWithLock(key string) {
554 if val, ok := d.cache[key]; ok {
555 d.uncacheWithLock(key, uint64(len(val)))
559 func (d *Diskv) uncacheWithLock(key string, sz uint64) {
564 // pruneDirsWithLock deletes empty directories in the path walk leading to the
565 // key k. Typically this function is called after an Erase is made.
566 func (d *Diskv) pruneDirsWithLock(key string) error {
567 pathlist := d.Transform(key)
568 for i := range pathlist {
569 dir := filepath.Join(d.BasePath, filepath.Join(pathlist[:len(pathlist)-i]...))
571 // thanks to Steven Blenkinsop for this snippet
572 switch fi, err := os.Stat(dir); true {
576 panic(fmt.Sprintf("corrupt dirstate at %s", dir))
579 nlinks, err := filepath.Glob(filepath.Join(dir, "*"))
582 } else if len(nlinks) > 0 {
583 return nil // has subdirs -- do not prune
585 if err = os.Remove(dir); err != nil {
593 // ensureCacheSpaceWithLock deletes entries from the cache in arbitrary order
594 // until the cache has at least valueSize bytes available.
595 func (d *Diskv) ensureCacheSpaceWithLock(valueSize uint64) error {
596 if valueSize > d.CacheSizeMax {
597 return fmt.Errorf("value size (%d bytes) too large for cache (%d bytes)", valueSize, d.CacheSizeMax)
600 safe := func() bool { return (d.cacheSize + valueSize) <= d.CacheSizeMax }
602 for key, val := range d.cache {
607 d.uncacheWithLock(key, uint64(len(val)))
611 panic(fmt.Sprintf("%d bytes still won't fit in the cache! (max %d bytes)", valueSize, d.CacheSizeMax))
617 // nopWriteCloser wraps an io.Writer and provides a no-op Close method to
618 // satisfy the io.WriteCloser interface.
619 type nopWriteCloser struct {
623 func (wc *nopWriteCloser) Write(p []byte) (int, error) { return wc.Writer.Write(p) }
624 func (wc *nopWriteCloser) Close() error { return nil }