Esempio n. 1
0
func incrbyfloat(v resp.CommandArgs, ex *CommandExtras) error {
	by, err := strconv.ParseFloat(v[1].String(), 64)
	if err != nil {
		return resp.NewError(ErrNotValidFloat).WriteTo(ex.Buffer)
	}

	ex.DB.Lock()
	defer ex.DB.Unlock()

	exists, tipe, expireAt := ex.DB.Has(v[0])
	if exists && tipe != storage.String {
		return resp.NewError(ErrWrongType).WriteTo(ex.Buffer)
	}

	newVal := 0.0
	if !exists {
		newVal += by
	} else {
		val := ex.DB.GetString(v[0])
		f, err := strconv.ParseFloat(string(val), 64)
		if err != nil {
			return resp.NewError(ErrNotValidFloat).WriteTo(ex.Buffer)
		}
		newVal = f + by
	}

	s := []byte(strconv.FormatFloat(newVal, 'f', -1, 64))
	ex.DB.PutString(v[0], s, expireAt)
	return resp.BulkString(s).WriteTo(ex.Buffer)
}
Esempio n. 2
0
func mget(v resp.CommandArgs, ex *CommandExtras) error {
	if len(v) < 1 {
		return resp.NewError(ErrFmtWrongNumberArgument, "mget").WriteTo(ex.Buffer)
	}

	ex.DB.RLock()
	defer ex.DB.RUnlock()

	arr := make(resp.Array, len(v))

	for i, g := range v {
		val, err := ex.DB.Get(g)

		if err != nil && err != leveldb.ErrNotFound {
			return err
		}
		if err == leveldb.ErrNotFound {
			arr[i] = resp.NilBulkString
		} else {
			arr[i] = resp.BulkString(val)
		}
	}

	return arr.WriteTo(ex.Buffer)
}
Esempio n. 3
0
func getrange(v resp.CommandArgs, ex *CommandExtras) error {
	start, err := strconv.Atoi(string(v[1]))
	if err != nil {
		return resp.NewError(ErrNotValidInt).WriteTo(ex.Buffer)
	}

	end, err := strconv.Atoi(string(v[2]))
	if err != nil {
		return resp.NewError(ErrNotValidInt).WriteTo(ex.Buffer)
	}

	ex.DB.RLock()
	defer ex.DB.RUnlock()

	exists, tipe, _ := ex.DB.Has(v[0])
	if !exists {
		return resp.EmptyBulkString.WriteTo(ex.Buffer)
	}
	if tipe != storage.String {
		return resp.NewError(ErrWrongType).WriteTo(ex.Buffer)
	}

	val := ex.DB.GetString(v[0])
	start, end = calcRange(start, end, len(val))
	if end <= start {
		return resp.EmptyBulkString.WriteTo(ex.Buffer)
	}

	return resp.BulkString(val[start:end]).WriteTo(ex.Buffer)
}
Esempio n. 4
0
func incrbyfloat(v resp.CommandArgs, ex *CommandExtras) error {
	by, err := strconv.ParseFloat(v[1].String(), 64)
	if err != nil {
		return resp.NewError(ErrNotValidFloat).WriteTo(ex.Buffer)
	}

	ex.DB.Lock()
	defer ex.DB.Unlock()

	s, err := ex.DB.Get(v[0])
	if err != nil && err != leveldb.ErrNotFound {
		return err
	}
	val := 0.0
	if err != nil && err == leveldb.ErrNotFound {
		val += by
	} else {
		val, err = strconv.ParseFloat(string(s), 64)
		if err != nil {
			return resp.NewError(ErrNotValidFloat).WriteTo(ex.Buffer)
		}
		val += by
	}

	s = []byte(strconv.FormatFloat(val, 'f', -1, 64))
	if err = ex.DB.Put(v[0], s); err != nil {
		return err
	}
	return resp.BulkString(s).WriteTo(ex.Buffer)
}
Esempio n. 5
0
File: hashes.go Progetto: rod6/rodis
func hincrbyfloat(v resp.CommandArgs, ex *CommandExtras) error {
	by, err := strconv.ParseFloat(v[2].String(), 64)
	if err != nil {
		return resp.NewError(ErrNotValidInt).WriteTo(ex.Buffer)
	}

	ex.DB.Lock()
	defer ex.DB.Unlock()

	keyExists, tipe, expireAt := ex.DB.Has(v[0])
	if keyExists && tipe != storage.Hash {
		return resp.NewError(ErrWrongType).WriteTo(ex.Buffer)
	}

	hash := ex.DB.GetHashFields(v[0], [][]byte{v[1]})

	newVal := 0.0
	if hash[string(v[1])] == nil {
		newVal += by
	} else {
		f, err := strconv.ParseFloat(string(hash[string(v[1])]), 64)
		if err != nil {
			return resp.NewError(ErrNotValidFloat).WriteTo(ex.Buffer)
		}
		newVal = f + by
	}
	hash[string(v[1])] = []byte(strconv.FormatFloat(newVal, 'f', -1, 64))

	ex.DB.PutHash(v[0], hash, expireAt)
	return resp.BulkString(hash[string(v[1])]).WriteTo(ex.Buffer)
}
Esempio n. 6
0
func getrange(v resp.CommandArgs, ex *CommandExtras) error {
	start, err := strconv.Atoi(string(v[1]))
	if err != nil {
		return resp.NewError(ErrNotValidInt).WriteTo(ex.Buffer)
	}

	end, err := strconv.Atoi(string(v[2]))
	if err != nil {
		return resp.NewError(ErrNotValidInt).WriteTo(ex.Buffer)
	}

	ex.DB.RLock()
	defer ex.DB.RUnlock()

	val, err := ex.DB.Get(v[0])
	if err != nil && err != leveldb.ErrNotFound {
		return err
	}
	if err == leveldb.ErrNotFound {
		return resp.EmptyBulkString.WriteTo(ex.Buffer)
	}

	start, end = calcRange(start, end, len(val))
	if end <= start {
		return resp.EmptyBulkString.WriteTo(ex.Buffer)
	}

	return resp.BulkString(val[start:end]).WriteTo(ex.Buffer)
}
Esempio n. 7
0
func hmget(v resp.CommandArgs, ex *CommandExtras) error {
	if len(v) < 2 {
		return resp.NewError(ErrFmtWrongNumberArgument, "hmget").WriteTo(ex.Buffer)
	}

	ex.DB.RLock()
	defer ex.DB.RUnlock()

	keyExists, tipe, _ := ex.DB.Has(v[0])
	if keyExists && tipe != storage.Hash {
		return resp.NewError(ErrWrongType).WriteTo(ex.Buffer)
	}

	fields := [][]byte{}
	for _, field := range v[1:] {
		fields = append(fields, []byte(field))
	}
	hash := ex.DB.GetHashFields(v[0], fields)

	arr := resp.Array{}
	for _, value := range hash {
		arr = append(arr, resp.BulkString(value))
	}
	return arr.WriteTo(ex.Buffer)
}
Esempio n. 8
0
File: hashes.go Progetto: rod6/rodis
func hgetall(v resp.CommandArgs, ex *CommandExtras) error {
	ex.DB.RLock()
	defer ex.DB.RUnlock()

	keyExists, tipe, _ := ex.DB.Has(v[0])
	if !keyExists {
		return resp.EmptyArray.WriteTo(ex.Buffer)
	}
	if keyExists && tipe != storage.Hash {
		return resp.NewError(ErrWrongType).WriteTo(ex.Buffer)
	}

	hash := ex.DB.GetHash(v[0])
	arr := resp.Array{}

	for field, value := range hash {
		arr = append(arr, resp.BulkString([]byte(field)), resp.BulkString(value))
	}
	return arr.WriteTo(ex.Buffer)
}
Esempio n. 9
0
func get(v resp.CommandArgs, ex *CommandExtras) error {
	ex.DB.RLock()
	defer ex.DB.RUnlock()

	val, err := ex.DB.Get(v[0])
	if err != nil && err != leveldb.ErrNotFound {
		return err
	}
	if err == leveldb.ErrNotFound {
		return resp.NilBulkString.WriteTo(ex.Buffer)
	}
	return resp.BulkString(val).WriteTo(ex.Buffer)
}
Esempio n. 10
0
func get(v resp.CommandArgs, ex *CommandExtras) error {
	ex.DB.RLock()
	defer ex.DB.RUnlock()

	exists, tipe, _ := ex.DB.Has(v[0])
	if !exists {
		return resp.NilBulkString.WriteTo(ex.Buffer)
	}
	if tipe != storage.String {
		return resp.NewError(ErrWrongType).WriteTo(ex.Buffer)
	}
	val := ex.DB.GetString(v[0])
	return resp.BulkString(val).WriteTo(ex.Buffer)
}
Esempio n. 11
0
File: hashes.go Progetto: rod6/rodis
func hget(v resp.CommandArgs, ex *CommandExtras) error {
	ex.DB.RLock()
	defer ex.DB.RUnlock()

	keyExists, tipe, _ := ex.DB.Has(v[0])
	if !keyExists {
		return resp.NilBulkString.WriteTo(ex.Buffer)
	}
	if keyExists && tipe != storage.Hash {
		return resp.NewError(ErrWrongType).WriteTo(ex.Buffer)
	}

	hash := ex.DB.GetHashFields(v[0], [][]byte{v[1]})
	return resp.BulkString(hash[string(v[1])]).WriteTo(ex.Buffer)
}
Esempio n. 12
0
File: hashes.go Progetto: rod6/rodis
func hkeys(v resp.CommandArgs, ex *CommandExtras) error {
	ex.DB.RLock()
	defer ex.DB.RUnlock()

	keyExists, tipe, _ := ex.DB.Has(v[0])
	if !keyExists {
		return resp.EmptyArray.WriteTo(ex.Buffer)
	}
	if keyExists && tipe != storage.Hash {
		return resp.NewError(ErrWrongType).WriteTo(ex.Buffer)
	}

	fields := ex.DB.GetHashFieldNames(v[0])
	arr := resp.Array{}

	for _, field := range fields {
		arr = append(arr, resp.BulkString(field))
	}
	return arr.WriteTo(ex.Buffer)
}
Esempio n. 13
0
func mget(v resp.CommandArgs, ex *CommandExtras) error {
	if len(v) < 1 {
		return resp.NewError(ErrFmtWrongNumberArgument, "mget").WriteTo(ex.Buffer)
	}

	ex.DB.RLock()
	defer ex.DB.RUnlock()

	arr := make(resp.Array, len(v))
	for i, g := range v {
		exists, tipe, _ := ex.DB.Has(g)
		if !exists || tipe != storage.String {
			arr[i] = resp.NilBulkString
		} else {
			val := ex.DB.GetString(g)
			arr[i] = resp.BulkString(val)
		}
	}

	return arr.WriteTo(ex.Buffer)
}
Esempio n. 14
0
func getset(v resp.CommandArgs, ex *CommandExtras) error {
	if len(v[1]) > STRLIMIT {
		return resp.NewError(ErrStringExccedLimit).WriteTo(ex.Buffer)
	}

	ex.DB.Lock()
	defer ex.DB.Unlock()

	exists, tipe, expireAt := ex.DB.Has(v[0])
	if exists && tipe != storage.String {
		return resp.NewError(ErrWrongType).WriteTo(ex.Buffer)
	}
	var oldValue []byte
	if exists {
		oldValue = ex.DB.GetString(v[0])
	}

	ex.DB.PutString(v[0], v[1], expireAt)

	if !exists {
		return resp.NilBulkString.WriteTo(ex.Buffer)
	}
	return resp.BulkString(oldValue).WriteTo(ex.Buffer)
}