kv_storage.go

package storage
//kv 存储引擎实现
import (
    "github.com/cznic/kv"
    "io"
)
//kv 存储结构体 并且实现了storage存储接口
type kvStorage struct {
    db *kv.DB
}
//打开存储引擎   即:引擎map集合对应的value 值  函数的实现  key为path
//返回存储引擎接口  和  err  代表文件打开错误
func openKVStorage(path string) (Storage, error) {
    options := &kv.Options{}
    db, errOpen := kv.Open(path, options)
    if errOpen != nil {
        var errCreate error
        db, errCreate = kv.Create(path, options)
        if errCreate != nil {
            return &kvStorage{db}, errCreate
        }
    }
    return &kvStorage{db}, nil
}

func (s *kvStorage) WALName() string {
    return s.db.WALName()
}

func (s *kvStorage) Set(k []byte, v []byte) error {
    return s.db.Set(k, v)
}

func (s *kvStorage) Get(k []byte) ([]byte, error) {
    return s.db.Get(nil, k)
}

func (s *kvStorage) Delete(k []byte) error {
    return s.db.Delete(k)
}

func (s *kvStorage) ForEach(fn func(k, v []byte) error) error {
    iter, err := s.db.SeekFirst()
    if err == io.EOF {
        return nil
    } else if err != nil {
        return err
    }
    for {
        key, value, err := iter.Next()
        if err == io.EOF {
            break
        } else if err != nil {
            return err
        }
        if err := fn(key, value); err != nil {
            return err
        }
    }
    return nil
}

func (s *kvStorage) Close() error {
    return s.db.Close()
}

上一篇:[转]hibernate三种状态详解


下一篇:转:HTTP 1.1与HTTP 1.0的比较