func (d *Diskv) KeysPrefix(prefix string, cancel <-chan struct{}) <-chan string {
	var prepath string
	if prefix == "" {
		prepath = d.BasePath
	} else {
		prepath = d.pathFor(prefix)
	}
	c := make(chan string)
	go func() {
		filepath.Walk(prepath, walker(c, prefix, cancel))
		close(c)
	}()
	return c
}
KeysPrefix returns a channel that will yield every key accessible by the
store with the given prefix, in undefined order. If a cancel channel is
provided, closing it will terminate and close the keys channel. If the
provided prefix is the empty string, all keys will be yielded.
package diskv
import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"sync"
	"syscall"
)
const (
	defaultBasePath             = "diskv"
	defaultFilePerm os.FileMode = 0666
	defaultPathPerm os.FileMode = 0777
)
var (
	defaultTransform   = func(s string) []string { return []string{} }
	errCanceled        = errors.New("canceled")
	errEmptyKey        = errors.New("empty key")
	errBadKey          = errors.New("bad key")
	errImportDirectory = errors.New("can't import a directory")
)
type TransformFunction func(s string) []string
type Options struct {
	BasePath     string
	Transform    TransformFunction
	CacheSizeMax uint64  
	PathPerm     os.FileMode
	FilePerm     os.FileMode
	TempDir string
	Index     Index
	IndexLess LessFunction
	Compression Compression
}
type Diskv struct {
	Options
	mu        sync.RWMutex
	cache     map[string][]byte
	cacheSize uint64
}
func New(o Options) *Diskv {
	if o.BasePath == "" {
		o.BasePath = defaultBasePath
	}
	if o.Transform == nil {
		o.Transform = defaultTransform
	}
	if o.PathPerm == 0 {
		o.PathPerm = defaultPathPerm
	}
	if o.FilePerm == 0 {
		o.FilePerm = defaultFilePerm
	}
	d := &Diskv{
		Options:   o,
		cache:     map[string][]byte{},
		cacheSize: 0,
	}
	if d.Index != nil && d.IndexLess != nil {
		d.Index.Initialize(d.IndexLess, d.Keys(nil))
	}
	return d
}
func (d *Diskv) Write(key string, val []byte) error {
	return d.WriteStream(key, bytes.NewBuffer(val), false)
}
func (d *Diskv) WriteStream(key string, r io.Reader, sync bool) error {
	if len(key) <= 0 {
		return errEmptyKey
	}
	d.mu.Lock()
	defer d.mu.Unlock()
	return d.writeStreamWithLock(key, r, sync)
}
func (d *Diskv) createKeyFileWithLock(key string) (*os.File, error) {
	if d.TempDir != "" {
		if err := os.MkdirAll(d.TempDir, d.PathPerm); err != nil {
			return nil, fmt.Errorf("temp mkdir: %s", err)
		}
		f, err := ioutil.TempFile(d.TempDir, "")
		if err != nil {
			return nil, fmt.Errorf("temp file: %s", err)
		}
		if err := f.Chmod(d.FilePerm); err != nil {
			f.Close()            
			os.Remove(f.Name())  
			return nil, fmt.Errorf("chmod: %s", err)
		}
		return f, nil
	}
	mode := os.O_WRONLY | os.O_CREATE | os.O_TRUNC  
	f, err := os.OpenFile(d.completeFilename(key), mode, d.FilePerm)
	if err != nil {
		return nil, fmt.Errorf("open file: %s", err)
	}
	return f, nil
}
func (d *Diskv) writeStreamWithLock(key string, r io.Reader, sync bool) error {
	if err := d.ensurePathWithLock(key); err != nil {
		return fmt.Errorf("ensure path: %s", err)
	}
	f, err := d.createKeyFileWithLock(key)
	if err != nil {
		return fmt.Errorf("create key file: %s", err)
	}
	wc := io.WriteCloser(&nopWriteCloser{f})
	if d.Compression != nil {
		wc, err = d.Compression.Writer(f)
		if err != nil {
			f.Close()            
			os.Remove(f.Name())  
			return fmt.Errorf("compression writer: %s", err)
		}
	}
	if _, err := io.Copy(wc, r); err != nil {
		f.Close()            
		os.Remove(f.Name())  
		return fmt.Errorf("i/o copy: %s", err)
	}
	if err := wc.Close(); err != nil {
		f.Close()            
		os.Remove(f.Name())  
		return fmt.Errorf("compression close: %s", err)
	}
	if sync {
		if err := f.Sync(); err != nil {
			f.Close()            
			os.Remove(f.Name())  
			return fmt.Errorf("file sync: %s", err)
		}
	}
	if err := f.Close(); err != nil {
		return fmt.Errorf("file close: %s", err)
	}
	if f.Name() != d.completeFilename(key) {
		if err := os.Rename(f.Name(), d.completeFilename(key)); err != nil {
			os.Remove(f.Name())  
			return fmt.Errorf("rename: %s", err)
		}
	}
	if d.Index != nil {
		d.Index.Insert(key)
	}
	d.bustCacheWithLock(key)  
	return nil
}
func (d *Diskv) Import(srcFilename, dstKey string, move bool) (err error) {
	if dstKey == "" {
		return errEmptyKey
	}
	if fi, err := os.Stat(srcFilename); err != nil {
		return err
	} else if fi.IsDir() {
		return errImportDirectory
	}
	d.mu.Lock()
	defer d.mu.Unlock()
	if err := d.ensurePathWithLock(dstKey); err != nil {
		return fmt.Errorf("ensure path: %s", err)
	}
	if move {
		if err := syscall.Rename(srcFilename, d.completeFilename(dstKey)); err == nil {
			d.bustCacheWithLock(dstKey)
			return nil
		} else if err != syscall.EXDEV {
			return err
		}
	}
	f, err := os.Open(srcFilename)
	if err != nil {
		return err
	}
	defer f.Close()
	err = d.writeStreamWithLock(dstKey, f, false)
	if err == nil && move {
		err = os.Remove(srcFilename)
	}
	return err
}
func (d *Diskv) Read(key string) ([]byte, error) {
	rc, err := d.ReadStream(key, false)
	if err != nil {
		return []byte{}, err
	}
	defer rc.Close()
	return ioutil.ReadAll(rc)
}
func (d *Diskv) ReadStream(key string, direct bool) (io.ReadCloser, error) {
	d.mu.RLock()
	defer d.mu.RUnlock()
	if val, ok := d.cache[key]; ok {
		if !direct {
			buf := bytes.NewBuffer(val)
			if d.Compression != nil {
				return d.Compression.Reader(buf)
			}
			return ioutil.NopCloser(buf), nil
		}
		go func() {
			d.mu.Lock()
			defer d.mu.Unlock()
			d.uncacheWithLock(key, uint64(len(val)))
		}()
	}
	return d.readWithRLock(key)
}
func (d *Diskv) readWithRLock(key string) (io.ReadCloser, error) {
	filename := d.completeFilename(key)
	fi, err := os.Stat(filename)
	if err != nil {
		return nil, err
	}
	if fi.IsDir() {
		return nil, os.ErrNotExist
	}
	f, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	var r io.Reader
	if d.CacheSizeMax > 0 {
		r = newSiphon(f, d, key)
	} else {
		r = &closingReader{f}
	}
	var rc = io.ReadCloser(ioutil.NopCloser(r))
	if d.Compression != nil {
		rc, err = d.Compression.Reader(r)
		if err != nil {
			return nil, err
		}
	}
	return rc, nil
}
type closingReader struct {
	rc io.ReadCloser
}
func (cr closingReader) Read(p []byte) (int, error) {
	n, err := cr.rc.Read(p)
	if err == io.EOF {
		if closeErr := cr.rc.Close(); closeErr != nil {
			return n, closeErr  
		}
	}
	return n, err
}
type siphon struct {
	f   *os.File
	d   *Diskv
	key string
	buf *bytes.Buffer
}
func newSiphon(f *os.File, d *Diskv, key string) io.Reader {
	return &siphon{
		f:   f,
		d:   d,
		key: key,
		buf: &bytes.Buffer{},
	}
}
func (s *siphon) Read(p []byte) (int, error) {
	n, err := s.f.Read(p)
	if err == nil {
		return s.buf.Write(p[0:n])  
	}
	if err == io.EOF {
		s.d.cacheWithoutLock(s.key, s.buf.Bytes())  
		if closeErr := s.f.Close(); closeErr != nil {
			return n, closeErr  
		}
		return n, err
	}
	return n, err
}
func (d *Diskv) Erase(key string) error {
	d.mu.Lock()
	defer d.mu.Unlock()
	d.bustCacheWithLock(key)
	if d.Index != nil {
		d.Index.Delete(key)
	}
	filename := d.completeFilename(key)
	if s, err := os.Stat(filename); err == nil {
		if s.IsDir() {
			return errBadKey
		}
		if err = os.Remove(filename); err != nil {
			return err
		}
	} else {
		return err
	}
	d.pruneDirsWithLock(key)
	return nil
}
func (d *Diskv) EraseAll() error {
	d.mu.Lock()
	defer d.mu.Unlock()
	d.cache = make(map[string][]byte)
	d.cacheSize = 0
	if d.TempDir != "" {
		os.RemoveAll(d.TempDir)  
	}
	return os.RemoveAll(d.BasePath)
}
func (d *Diskv) Has(key string) bool {
	d.mu.Lock()
	defer d.mu.Unlock()
	if _, ok := d.cache[key]; ok {
		return true
	}
	filename := d.completeFilename(key)
	s, err := os.Stat(filename)
	if err != nil {
		return false
	}
	if s.IsDir() {
		return false
	}
	return true
}
func (d *Diskv) Keys(cancel <-chan struct{}) <-chan string {
	return d.KeysPrefix("", cancel)
}