server transactions: channel based sync for each account
[epoint] / pkg / store / store.go
index 21603fe..7ea1ded 100644 (file)
@@ -40,13 +40,16 @@ func Open(root string) (c *Conn, err error) {
        if err != nil {
                return
        }
-       err = os.MkdirAll(c.path, 0755)
+       fn := filepath.Join(c.path, ".journal")
+       err = os.MkdirAll(fn, 0755)
        if err != nil {
                return
        }
        return
 }
 
+// TODO: list .journal for recovery after a crash
+
 // Get the value of k
 func (c *Conn) Get(name, k string) (v []byte, err error) {
        v, err = ioutil.ReadFile(filepath.Join(c.path, name, k))
@@ -106,6 +109,24 @@ func (c *Conn) Append(name, k string, v []byte) (err error) {
        return
 }
 
+// Begin transaction identified by k
+func (c *Conn) Begin(k string) (err error) {
+       fn := filepath.Join(c.path, ".journal", k)
+       f, err := os.OpenFile(fn, os.O_CREATE|os.O_TRUNC|os.O_WRONLY|os.O_SYNC, 0666)
+       if err != nil {
+               return
+       }
+       err = f.Close()
+       return
+}
+
+// End transaction identified by k
+func (c *Conn) End(k string) (err error) {
+       fn := filepath.Join(c.path, ".journal", k)
+       err = os.Remove(fn)
+       return
+}
+
 // Close db connection
 func (c *Conn) Close() (err error) {
        return