Exemple #1
0
func main() {
	flag.StringVar(&addr, "addr", "127.0.0.1:80", "bitcask http listen addr")
	flag.StringVar(&storagePath, "s", "bitcaskStorage", "data storage path")
	flag.Uint64Var(&maxSize, "ms", 1<<32, "single data file maxsize")
	flag.IntVar(&logLevel, "l", 0, "logger level")
	flag.Parse()

	logger.SetLevel(1)
	opts := &bitcask.Options{
		MaxFileSize: maxSize,
	}
	var err error
	bc, err = bitcask.Open(storagePath, opts)
	if err != nil {
		logger.Fatal(err)
	}
	defer bc.Close()

	defer func() {
		if err := recover(); err != nil {
			logger.Error(err)
			debug.PrintStack()
		}
	}()

	r := mux.NewRouter()
	r.HandleFunc("/{key}", bitcaskGetHandle).Methods("GET")
	r.HandleFunc("/{key}", bitcaskDelHandle).Methods("DELETE")
	r.HandleFunc("/{key}", bitcaskPutHandle).Methods("POST")
	logger.Info("bitcask server listen:", addr)
	if err := http.ListenAndServe(addr, r); err != nil {
		logger.Error(err)
	}
}
Exemple #2
0
func main() {
	os.RemoveAll("exampleBitcaskDir")
	bc, err := bitcask.Open("exampleBitcaskDir", nil)
	if err != nil {
		logger.Fatal(err)
	}
	defer bc.Close()
	k1 := []byte("xiaoMing")
	v1 := []byte("毕业于新东方推土机学院")

	k2 := []byte("zhanSan")
	v2 := []byte("毕业于新东方厨师学院")

	bc.Put(k1, v1)
	bc.Put(k2, v2)

	v1, _ = bc.Get(k1)
	v2, _ = bc.Get(k2)
	logger.Info(string(k1), string(v1))
	logger.Info(string(k2), string(v2))
	// override
	v2 = []byte("毕业于新东方美容美发学院")
	bc.Put(k2, v2)
	v2, _ = bc.Get(k2)
	logger.Info(string(k2), string(v2))

	bc.Del(k1)
	bc.Del(k2)
	logger.Info("毕业后的数据库:")
	v1, e := bc.Get(k1)
	if e != bitcask.ErrNotFound {
		logger.Info(string(k1), "shoud be:", bitcask.ErrNotFound)
	} else {
		logger.Info(string(k1), "已经毕业.")
	}
	v2, e = bc.Get(k2)
	if e != bitcask.ErrNotFound {
		logger.Info(string(k1), "shoud be:", bitcask.ErrNotFound)
	} else {
		logger.Info(string(k2), "已经毕业.")
	}

}