func (self *PersistentStorage) Set(kv *trib.KeyValue, succ *bool) error { fmt.Println("Calling Set") s := strings.Split(kv.Key, "::") binname := colon.Unescape(s[0]) fmt.Println("Bin name is " + binname) dir, _ := filepath.Abs(filepath.Dir(os.Args[0])) path := dir + "/cse223test/" + binname os.MkdirAll(path, 0777) filename := colon.Unescape(s[1]) fpath := path + "/" + filename fmt.Println("file path ", fpath) if onDisk(fpath) { os.Remove(fpath) } if kv.Value != "" { f, _ := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0777) _, err := f.WriteString(kv.Value) if err != nil { *succ = false } } *succ = true if Logging { log.Printf("Set(%q, %q)", kv.Key, kv.Value) } /* f, _ := os.OpenFile(fpath, os.O_WRONLY | os.O_CREATE | os.O_TRUNC, 0777) if kv.Value != "" { //self.strs[kv.Key] = kv.Value _, err2 := f.WriteString(kv.Value + "\n") if err2 != nil { return err2 } } else { delete(self.strs, kv.Key) } *succ = true if Logging { log.Printf("Set(%q, %q)", kv.Key, kv.Value) } */ return nil }
func (self *PersistentStorage) Get(key string, value *string) error { fmt.Println("Calling Get") s := strings.Split(key, "::") binname := colon.Unescape(s[0]) fmt.Println("Bin name is " + binname) dir, _ := filepath.Abs(filepath.Dir(os.Args[0])) path := dir + "/cse223test/" + binname //os.MkdirAll(path, 0777) filename := colon.Unescape(s[1]) fpath := path + "/" + filename fmt.Println("file path ", fpath) if onDisk(fpath) { data, err := ioutil.ReadFile(fpath) if err != nil { fmt.Println("read error") *value = "" return nil } *value = string(data) /*f, err := os.Open(fpath) if err != nil { log.Fatal(err) } defer f.Close() scanner := bufio.NewScanner(f) for scanner.Scan(){ *value = scanner.Text() fmt.Println("The value is " + scanner.Text()) } if err := scanner.Err(); err !=nil{ log.Fatal(err) }*/ } else { *value = "" } if Logging { log.Printf("Get(%q) => %q", key, *value) } return nil }
func (self *clientWithPrefix) ListKeys(p *trib.Pattern, list *trib.List) error { // escape colon binName := colon.Escape(self.bin) p.Prefix = binName + "::" + colon.Escape(p.Prefix) p.Suffix = colon.Escape(p.Suffix) // RPC call err := self.originalClient.ListKeys(p, list) if err != nil { return err } // unescape and trim for i, str := range list.L { str = colon.Unescape(str) list.L[i] = strings.TrimPrefix(str, self.bin+"::") } return nil }