mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-24 16:30:21 +08:00
added RANDOMKEY embedded spec and test
This commit is contained in:
@@ -552,3 +552,14 @@ func (server *EchoVault) Rename(oldKey string, newKey string) (string, error) {
|
|||||||
// Parse the simple string response
|
// Parse the simple string response
|
||||||
return internal.ParseStringResponse(b)
|
return internal.ParseStringResponse(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Randomkey returns a random key
|
||||||
|
//If no keys present in db returns an emtpy string
|
||||||
|
func (server *EchoVault) Randomkey() (string, error) {
|
||||||
|
b, err := server.handleCommand(server.context, internal.EncodeCommand([]string{"RANDOMKEY"}), nil, false, true)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return internal.ParseStringResponse(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1308,3 +1308,37 @@ func TestEchoVault_Rename(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEchoVault_RANDOMKEY(t *testing.T) {
|
||||||
|
server := createEchoVault()
|
||||||
|
|
||||||
|
// test without keys
|
||||||
|
got, err := server.Randomkey()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if got != "" {
|
||||||
|
t.Errorf("RANDOMKEY error, expected emtpy string (%v), got (%v)", []byte(""), []byte(got))
|
||||||
|
}
|
||||||
|
|
||||||
|
// test with keys
|
||||||
|
testkeys := []string{"key1", "key2", "key3"}
|
||||||
|
for _, k := range testkeys {
|
||||||
|
err := presetValue(server, context.Background(), k, "")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
actual, err := server.Randomkey()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.Contains(actual, "key") {
|
||||||
|
t.Errorf("RANDOMKEY error, expected one of %v, got %s", testkeys, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -290,27 +290,6 @@ func (server *EchoVault) deleteKey(ctx context.Context, key string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *EchoVault) randomKey(ctx context.Context) string {
|
|
||||||
server.storeLock.RLock()
|
|
||||||
defer server.storeLock.RUnlock()
|
|
||||||
|
|
||||||
database := ctx.Value("Database").(int)
|
|
||||||
|
|
||||||
randnum := rand.Intn(len(server.store[database]))
|
|
||||||
i := 0
|
|
||||||
var randkey string
|
|
||||||
|
|
||||||
for key, _ := range server.store[database] {
|
|
||||||
if i == randnum {
|
|
||||||
randkey = key
|
|
||||||
} else {
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return randkey
|
|
||||||
}
|
|
||||||
|
|
||||||
func (server *EchoVault) createDatabase(database int) {
|
func (server *EchoVault) createDatabase(database int) {
|
||||||
// Create database store.
|
// Create database store.
|
||||||
server.store[database] = make(map[string]internal.KeyData)
|
server.store[database] = make(map[string]internal.KeyData)
|
||||||
@@ -657,3 +636,29 @@ func (server *EchoVault) evictKeysWithExpiredTTL(ctx context.Context) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (server *EchoVault) randomKey(ctx context.Context) string {
|
||||||
|
server.storeLock.RLock()
|
||||||
|
defer server.storeLock.RUnlock()
|
||||||
|
|
||||||
|
database := ctx.Value("Database").(int)
|
||||||
|
|
||||||
|
_max := len(server.store[database])
|
||||||
|
if _max == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
randnum := rand.Intn(_max)
|
||||||
|
i := 0
|
||||||
|
var randkey string
|
||||||
|
|
||||||
|
for key, _ := range server.store[database] {
|
||||||
|
if i == randnum {
|
||||||
|
randkey = key
|
||||||
|
} else {
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return randkey
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user