3 // persistent key-value store
4 // multiple key-value store can be managed by a single db connection
5 // each store has a name, before usage the name of the store must be
8 // TODO: this is a toy implementation
20 type NotFoundError struct {
24 type AlreadyExistsError struct {
28 func (e NotFoundError) Error() string {
29 return "not found: " + e.Path
32 func (e AlreadyExistsError) Error() string {
33 return "already exists: " + e.Path
37 func Open(root string) (c *Conn, err error) {
39 c.path, err = filepath.Abs(root)
43 err = os.MkdirAll(c.path, 0755)
51 func (c *Conn) Get(name, k string) (v []byte, err error) {
52 v, err = ioutil.ReadFile(filepath.Join(c.path, name, k))
54 if p, ok := err.(*os.PathError); ok && p.Err == os.ENOENT {
55 err = NotFoundError{name + "/" + k}
61 // Ensure k-v store with the given name exists
62 func (c *Conn) Ensure(name string) (err error) {
63 return os.MkdirAll(filepath.Join(c.path, name), 0755)
67 func (c *Conn) Set(name, k string, v []byte) (err error) {
68 fn := filepath.Join(c.path, name, k)
69 f, err := os.OpenFile(fn+".tmp", os.O_CREATE|os.O_TRUNC|os.O_WRONLY|os.O_SYNC, 0666)
78 err = os.Rename(fn+".tmp", fn)
82 // Set k to v, but fail if k already exists
83 func (c *Conn) Insert(name, k string, v []byte) (err error) {
84 fn := filepath.Join(c.path, name, k)
85 f, err := os.OpenFile(fn, os.O_CREATE|os.O_EXCL|os.O_WRONLY|os.O_SYNC, 0666)
87 if p, ok := err.(*os.PathError); ok && p.Err == os.EEXIST {
88 err = AlreadyExistsError{name + "/" + k}
97 // Append v to value of k
98 func (c *Conn) Append(name, k string, v []byte) (err error) {
99 fn := filepath.Join(c.path, name, k)
100 f, err := os.OpenFile(fn, os.O_CREATE|os.O_APPEND|os.O_WRONLY|os.O_SYNC, 0666)
109 // Close db connection
110 func (c *Conn) Close() (err error) {