package databases
import ( "fmt" "io/ioutil" "log" "os"
"github.com/xujiajun/nutsdb" )
var ( db *nutsdb.DB bucket string )
func init() { log.Println("NutsDbHandler init start") opt := nutsdb.DefaultOptions fileDir := "./databases"
files, _ := ioutil.ReadDir(fileDir) for _, f := range files { name := f.Name() if name != "" { fmt.Println(fileDir + "/" + name) err := os.RemoveAll(fileDir + "/" + name) if err != nil { panic(err) } } } opt.Dir = fileDir opt.SegmentSize = 1024 * 1024 // 1MB db, _ = nutsdb.Open(opt) bucket = "bucket" log.Println("NutsDbHandler init end") }
func delete(keyName string) { if err := db.Update( func(tx *nutsdb.Tx) error { key := []byte(keyName) return tx.Delete(bucket, key) }); err != nil { log.Fatal(err) } }
func put(keyName, keyValue string) { if err := db.Update( func(tx *nutsdb.Tx) error { key := []byte(keyName) val := []byte(keyValue) return tx.Put(bucket, key, val, 0) }); err != nil { log.Fatal(err) } }
func put2(keyName, keyValue string) { if err := db.Update( func(tx *nutsdb.Tx) error { key := []byte(keyName) val := []byte(keyValue) return tx.Put(bucket, key, val, 0) }); err != nil { log.Fatal(err) } }
func read(keyName string) { if err := db.View( func(tx *nutsdb.Tx) error { key := []byte(keyName) e, err := tx.Get(bucket, key) if err != nil { return err } fmt.Println("val:", string(e.Value)) return nil }); err != nil { log.Println(err) } }
// 사용방법 ex // import( db "src/databases")
// func main() { // //insert // db.put("name1", "value1")
// //read // db.read("name1")
// //delete // db.delete("name1")
// //read // db.read("name1")
// //insert // db.put("name1", "value1")
// //read // db.read("name1")
// //update // db.put2("name1", "value2")
// //read // db.read("name1")
// }
|