Skip to content

Commit

Permalink
Add support for COPY command
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Kahn committed Feb 6, 2022
1 parent a034b08 commit 52eb7da
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
17 changes: 17 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ type Cmdable interface {
SetXX(ctx context.Context, key string, value interface{}, expiration time.Duration) *BoolCmd
SetRange(ctx context.Context, key string, offset int64, value string) *IntCmd
StrLen(ctx context.Context, key string) *IntCmd
Copy(ctx context.Context, sourceKey string, destKey string, db int, replace bool) *IntCmd

GetBit(ctx context.Context, key string, offset int64) *IntCmd
SetBit(ctx context.Context, key string, offset int64, value int) *IntCmd
Expand Down Expand Up @@ -1025,6 +1026,22 @@ func (c cmdable) StrLen(ctx context.Context, key string) *IntCmd {
return cmd
}

func (c cmdable) Copy(ctx context.Context, sourceKey, destKey string, db int, replace bool) *IntCmd {
var replaceArg string
if replace {
replaceArg = "REPLACE"
}
cmd := NewIntCmd(ctx,
"copy",
sourceKey,
destKey,
db,
replaceArg,
)
_ = c(ctx, cmd)
return cmd
}

//------------------------------------------------------------------------------

func (c cmdable) GetBit(ctx context.Context, key string, offset int64) *IntCmd {
Expand Down
29 changes: 29 additions & 0 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,35 @@ var _ = Describe("Commands", func() {
Expect(strLen.Err()).NotTo(HaveOccurred())
Expect(strLen.Val()).To(Equal(int64(0)))
})

It("should Copy", func() {
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))

copy := client.Copy(ctx, "key", "newKey", 0, false)
Expect(copy.Err()).NotTo(HaveOccurred())
Expect(copy.Val()).To(Equal(int64(1)))

getOld := client.Get(ctx, "key")
Expect(getOld.Err()).NotTo(HaveOccurred())
Expect(getOld.Result()).To(Equal(redis.Nil))

getNew := client.Get(ctx, "newKey")
Expect(getNew.Err()).NotTo(HaveOccurred())
Expect(getNew.Result()).To(Equal("hello"))

// Copy over existing key should return error
set = client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
overwrite := client.Copy(ctx, "newKey", "key", 0, false)
Expect(overwrite.Err()).To(HaveOccurred())

// Overwrite is allowed when replace=rue
replace := client.Copy(ctx, "newKey", "key", 0, true)
Expect(replace.Err()).NotTo(HaveOccurred())
})
})

Describe("hashes", func() {
Expand Down

0 comments on commit 52eb7da

Please sign in to comment.