Ejemplo n.º 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)
	}
}
Ejemplo n.º 2
0
func BenchmarkBitcaskCurrency(b *testing.B) {
	storagePath := "benchMarkBitcask"
	os.RemoveAll(storagePath)
	opts := &Options{
		MaxFileSize: 1 << 12,
	}
	bc, err := Open(storagePath, opts)
	if err != nil {
		logger.Fatal(err)
	}

	keyValues := make(map[int]string)

	for i := 0; i < b.N/2; i++ {
		key := strconv.Itoa(i)
		value := strconv.Itoa(int(time.Now().Unix()))
		bc.Put([]byte(key), []byte(value))
		keyValues[i] = value
	}
	logger.Warn(b.N)
	logger.Info("Put all Data")
	for i := 0; i < b.N/2; i++ {
		k := strconv.Itoa(i)
		v, _ := bc.Get([]byte(k))
		if string(v) != keyValues[i] {
			logger.Error(string(v), keyValues[i])
			os.Exit(-1)
		}
	}
	logger.Info("Get all data")
	// delete all data
	for i := 0; i < b.N/2; i++ {
		k := strconv.Itoa(i)
		//v, _ := bc.Get([]byte(k))
		err := bc.Del([]byte(k))
		if err != nil {
			logger.Error(err)
		}
	}
	logger.Info("Delete all data")
	// Get all data
	for i := 0; i < b.N/2; i++ {
		k := strconv.Itoa(i)
		v, err := bc.Get([]byte(k))
		if err != ErrNotFound {
			logger.Error(string(v), keyValues[i])
		}
	}
	logger.Info("all data is not found, pass test")
	//mergeWorker.Staop()
	bc.Close()
}
Ejemplo n.º 3
0
func bitcaskPutHandle(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	key := vars["key"]
	if len(key) <= 0 {
		w.Write([]byte("key invalid"))
		return
	}
	value, err := ioutil.ReadAll(r.Body)
	if err != nil {
		logger.Error(err)
		w.WriteHeader(500)
		return
	}

	bc.Put([]byte(key), value)
	w.Write([]byte("Success"))
}
Ejemplo n.º 4
0
func bitcaskDelHandle(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	key := vars["key"]
	if len(key) <= 0 {
		w.Write([]byte("key invalid"))
		return
	}

	err := bc.Del([]byte(key))
	if err != nil && err != bitcask.ErrNotFound {
		logger.Error(err)
		w.WriteHeader(500)
		return
	}
	if err == bitcask.ErrNotFound {
		w.WriteHeader(404)
		w.Write([]byte(bitcask.ErrNotFound.Error()))
		return
	}

	w.Write([]byte("Success"))
}