add NotFoundError to store
[epoint] / store / store.go
index 2595f88..2a6d2f8 100644 (file)
@@ -8,16 +8,23 @@ package store
 // TODO: this is a toy implementation
 
 import (
+       "io/ioutil"
        "os"
        "path/filepath"
-       "io/ioutil"
 )
 
-
 type Conn struct {
        path string
 }
 
+type NotFoundError struct {
+       path string
+}
+
+func (e NotFoundError) Error() string {
+       return "not found: " + e.path
+}
+
 func Open(root string) (c *Conn, err error) {
        c = new(Conn)
        c.path, err = filepath.Abs(root)
@@ -32,7 +39,13 @@ func Open(root string) (c *Conn, err error) {
 }
 
 func (c *Conn) Get(name, k string) (v []byte, err error) {
-       return ioutil.ReadFile(filepath.Join(c.path, name, k))
+       v, err = ioutil.ReadFile(filepath.Join(c.path, name, k))
+       if err != nil {
+               if p, ok := err.(*os.PathError); ok && p.Err == os.ENOENT {
+                       err = NotFoundError{name+"/"+k}
+               }
+       }
+       return
 }
 
 func (c *Conn) Ensure(name string) (err error) {
@@ -41,8 +54,7 @@ func (c *Conn) Ensure(name string) (err error) {
 
 func (c *Conn) Set(name, k string, v []byte) (err error) {
        fn := filepath.Join(c.path, name, k)
-       // os.O_SYNC
-       f, err := os.Create(fn+".tmp")
+       f, err := os.OpenFile(fn+".tmp", os.O_CREATE|os.O_TRUNC|os.O_WRONLY|os.O_SYNC, 0666)
        if err != nil {
                return
        }
@@ -55,6 +67,17 @@ func (c *Conn) Set(name, k string, v []byte) (err error) {
        return
 }
 
+func (c *Conn) Append(name, k string, v []byte) (err error) {
+       fn := filepath.Join(c.path, name, k)
+       f, err := os.OpenFile(fn, os.O_CREATE|os.O_APPEND|os.O_WRONLY|os.O_SYNC, 0666)
+       if err != nil {
+               return
+       }
+       defer f.Close()
+       _, err = f.Write(v)
+       return
+}
+
 func (c *Conn) Close() (err error) {
        return
 }