Example #1
0
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{}

	var keys []string
	for k := range hash {
		keys = append(keys, k)
	}
	sort.Strings(keys)

	for _, field := range keys {
		value := hash[field]
		arr = append(arr, resp.BulkString([]byte(field)), resp.BulkString(value))
	}

	return arr.WriteTo(ex.Buffer)
}
Example #2
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 := v[1:].ToBytes()
	hash := ex.DB.GetHashFields(v[0], fields)

	arr := resp.Array{}

	var keys []string
	for k := range hash {
		keys = append(keys, k)
	}
	sort.Strings(keys)

	for _, field := range keys {
		value := hash[field]
		arr = append(arr, resp.BulkString(value))
	}
	return arr.WriteTo(ex.Buffer)
}
Example #3
0
func hvals(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 _, value := range hash {
		arr = append(arr, resp.BulkString([]byte(value)))
	}
	return arr.WriteTo(ex.Buffer)
}
Example #4
0
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)
}