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
21 func Open(root string) (c *Conn, err error) {
23 c.path, err = filepath.Abs(root)
27 err = os.MkdirAll(c.path, 0755)
34 func (c *Conn) Get(name, k string) (v []byte, err error) {
35 return ioutil.ReadFile(filepath.Join(c.path, name, k))
38 func (c *Conn) Ensure(name string) (err error) {
39 return os.MkdirAll(filepath.Join(c.path, name), 0755)
42 func (c *Conn) Set(name, k string, v []byte) (err error) {
43 fn := filepath.Join(c.path, name, k)
44 f, err := os.OpenFile(fn+".tmp", os.O_CREATE|os.O_TRUNC|os.O_WRONLY|os.O_SYNC, 0666)
53 err = os.Rename(fn+".tmp", fn)
57 func (c *Conn) Append(name, k string, v []byte) (err error) {
58 fn := filepath.Join(c.path, name, k)
59 f, err := os.OpenFile(fn, os.O_CREATE|os.O_APPEND|os.O_WRONLY|os.O_SYNC, 0666)
68 func (c *Conn) Close() (err error) {