Files
SugarDB/pkg/modules/sorted_set/key_funcs.go

265 lines
6.1 KiB
Go

// Copyright 2024 Kelvin Clement Mwinuka
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sorted_set
import (
"errors"
"github.com/echovault/echovault/pkg/utils"
"slices"
"strings"
)
func zaddKeyFunc(cmd []string) ([]string, error) {
if len(cmd) < 4 {
return nil, errors.New(utils.WrongArgsResponse)
}
return cmd[1:2], nil
}
func zcardKeyFunc(cmd []string) ([]string, error) {
if len(cmd) != 2 {
return nil, errors.New(utils.WrongArgsResponse)
}
return cmd[1:], nil
}
func zcountKeyFunc(cmd []string) ([]string, error) {
if len(cmd) != 4 {
return nil, errors.New(utils.WrongArgsResponse)
}
return cmd[1:2], nil
}
func zdiffKeyFunc(cmd []string) ([]string, error) {
if len(cmd) < 2 {
return nil, errors.New(utils.WrongArgsResponse)
}
withscoresIndex := slices.IndexFunc(cmd, func(s string) bool {
return strings.EqualFold(s, "withscores")
})
if withscoresIndex == -1 {
return cmd[1:], nil
}
return cmd[1:withscoresIndex], nil
}
func zdiffstoreKeyFunc(cmd []string) ([]string, error) {
if len(cmd) < 3 {
return nil, errors.New(utils.WrongArgsResponse)
}
return cmd[2:], nil
}
func zincrbyKeyFunc(cmd []string) ([]string, error) {
if len(cmd) != 4 {
return nil, errors.New(utils.WrongArgsResponse)
}
return cmd[1:2], nil
}
func zinterKeyFunc(cmd []string) ([]string, error) {
if len(cmd) < 2 {
return nil, errors.New(utils.WrongArgsResponse)
}
endIdx := slices.IndexFunc(cmd[1:], func(s string) bool {
if strings.EqualFold(s, "WEIGHTS") ||
strings.EqualFold(s, "AGGREGATE") ||
strings.EqualFold(s, "WITHSCORES") {
return true
}
return false
})
if endIdx == -1 {
return cmd[1:], nil
}
if endIdx >= 1 {
return cmd[1:endIdx], nil
}
return nil, errors.New(utils.WrongArgsResponse)
}
func zinterstoreKeyFunc(cmd []string) ([]string, error) {
if len(cmd) < 2 {
return nil, errors.New(utils.WrongArgsResponse)
}
endIdx := slices.IndexFunc(cmd[1:], func(s string) bool {
if strings.EqualFold(s, "WEIGHTS") ||
strings.EqualFold(s, "AGGREGATE") ||
strings.EqualFold(s, "WITHSCORES") {
return true
}
return false
})
if endIdx == -1 {
return cmd[1:], nil
}
if endIdx >= 2 {
return cmd[1:endIdx], nil
}
return nil, errors.New(utils.WrongArgsResponse)
}
func zmpopKeyFunc(cmd []string) ([]string, error) {
if len(cmd) < 2 {
return nil, errors.New(utils.WrongArgsResponse)
}
endIdx := slices.IndexFunc(cmd, func(s string) bool {
return slices.Contains([]string{"MIN", "MAX", "COUNT"}, strings.ToUpper(s))
})
if endIdx == -1 {
return cmd[1:], nil
}
if endIdx >= 2 {
return cmd[1:endIdx], nil
}
return nil, errors.New(utils.WrongArgsResponse)
}
func zmscoreKeyFunc(cmd []string) ([]string, error) {
if len(cmd) < 3 {
return nil, errors.New(utils.WrongArgsResponse)
}
return cmd[1:2], nil
}
func zpopKeyFunc(cmd []string) ([]string, error) {
if len(cmd) < 2 || len(cmd) > 3 {
return nil, errors.New(utils.WrongArgsResponse)
}
return cmd[1:2], nil
}
func zrandmemberKeyFunc(cmd []string) ([]string, error) {
if len(cmd) < 2 || len(cmd) > 4 {
return nil, errors.New(utils.WrongArgsResponse)
}
return cmd[1:2], nil
}
func zrankKeyFunc(cmd []string) ([]string, error) {
if len(cmd) < 3 || len(cmd) > 4 {
return nil, errors.New(utils.WrongArgsResponse)
}
return cmd[1:2], nil
}
func zremKeyFunc(cmd []string) ([]string, error) {
if len(cmd) < 3 {
return nil, errors.New(utils.WrongArgsResponse)
}
return cmd[1:2], nil
}
func zrevrankKeyFunc(cmd []string) ([]string, error) {
if len(cmd) < 3 {
return nil, errors.New(utils.WrongArgsResponse)
}
return cmd[1:2], nil
}
func zscoreKeyFunc(cmd []string) ([]string, error) {
if len(cmd) != 3 {
return nil, errors.New(utils.WrongArgsResponse)
}
return cmd[1:2], nil
}
func zremrangebylexKeyFunc(cmd []string) ([]string, error) {
if len(cmd) != 4 {
return nil, errors.New(utils.WrongArgsResponse)
}
return cmd[1:2], nil
}
func zremrangebyrankKeyFunc(cmd []string) ([]string, error) {
if len(cmd) != 4 {
return nil, errors.New(utils.WrongArgsResponse)
}
return cmd[1:2], nil
}
func zremrangebyscoreKeyFunc(cmd []string) ([]string, error) {
if len(cmd) != 4 {
return nil, errors.New(utils.WrongArgsResponse)
}
return cmd[1:2], nil
}
func zlexcountKeyFunc(cmd []string) ([]string, error) {
if len(cmd) != 4 {
return nil, errors.New(utils.WrongArgsResponse)
}
return cmd[1:2], nil
}
func zrangeKeyCount(cmd []string) ([]string, error) {
if len(cmd) < 4 || len(cmd) > 10 {
return nil, errors.New(utils.WrongArgsResponse)
}
return cmd[1:2], nil
}
func zrangeStoreKeyFunc(cmd []string) ([]string, error) {
if len(cmd) < 5 || len(cmd) > 11 {
return nil, errors.New(utils.WrongArgsResponse)
}
return cmd[1:3], nil
}
func zunionKeyFunc(cmd []string) ([]string, error) {
if len(cmd) < 2 {
return nil, errors.New(utils.WrongArgsResponse)
}
endIdx := slices.IndexFunc(cmd[1:], func(s string) bool {
if strings.EqualFold(s, "WEIGHTS") ||
strings.EqualFold(s, "AGGREGATE") ||
strings.EqualFold(s, "WITHSCORES") {
return true
}
return false
})
if endIdx == -1 {
return cmd[1:], nil
}
if endIdx >= 1 {
return cmd[1:endIdx], nil
}
return nil, errors.New(utils.WrongArgsResponse)
}
func zunionstoreKeyFunc(cmd []string) ([]string, error) {
if len(cmd) < 2 {
return nil, errors.New(utils.WrongArgsResponse)
}
endIdx := slices.IndexFunc(cmd[1:], func(s string) bool {
if strings.EqualFold(s, "WEIGHTS") ||
strings.EqualFold(s, "AGGREGATE") ||
strings.EqualFold(s, "WITHSCORES") {
return true
}
return false
})
if endIdx == -1 {
return cmd[1:], nil
}
if endIdx >= 1 {
return cmd[1:endIdx], nil
}
return nil, errors.New(utils.WrongArgsResponse)
}