Skip to content

Commit

Permalink
Merge pull request #89 from canonical/fix/kv-to-mkv
Browse files Browse the repository at this point in the history
Kv should be mkv
  • Loading branch information
neoaggelos authored Feb 26, 2024
2 parents 9571bd9 + 3fe5aff commit be803ca
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/kine/drivers/generic/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func Open(ctx context.Context, driverName, dataSourceName string, paramCharacter
SELECT (%s), COUNT(c.theid)
FROM (
%s
) c`, revSQL, fmt.Sprintf(listSQL, "AND kv.id <= ?")), paramCharacter, numbered),
) c`, revSQL, fmt.Sprintf(listSQL, "AND mkv.id <= ?")), paramCharacter, numbered),

AfterSQLPrefix: q(fmt.Sprintf(`
SELECT %s
Expand Down
4 changes: 0 additions & 4 deletions pkg/kine/logstructured/sqllog/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,6 @@ func canSkipRevision(rev, skip int64, skipTime time.Time) bool {
}

func (s *SQLLog) Count(ctx context.Context, prefix string, revision int64) (int64, int64, error) {
if strings.HasSuffix(prefix, "/") {
prefix += "%"
}

if revision == 0 {
return s.d.CountCurrent(ctx, prefix)
}
Expand Down
64 changes: 64 additions & 0 deletions test/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,70 @@ func TestList(t *testing.T) {
g.Expect(resp.Header.Revision).ToNot(BeZero())
g.Expect(resp.Kvs[0].Key).To(Equal([]byte("/key/1")))
})

t.Run("ListRevision", func(t *testing.T) {
t.Run("Create", func(t *testing.T) {
// Create some keys
keys := []string{"/revkey/1"}
for _, key := range keys {
resp, err := client.Txn(ctx).
If(clientv3.Compare(clientv3.ModRevision(key), "=", 0)).
Then(clientv3.OpPut(key, "value")).
Commit()

g.Expect(err).To(BeNil())
g.Expect(resp.Succeeded).To(BeTrue())
g.Expect(resp.Header.Revision).ToNot(BeZero())
}
})

t.Run("Update", func(t *testing.T) {
g := NewWithT(t)
var rev int64
for rev < 30 {
get, err := client.Get(ctx, "/revkey/1", clientv3.WithRange(""))
g.Expect(err).To(BeNil())
g.Expect(get.Kvs).To(HaveLen(1))
rev = get.Kvs[0].ModRevision

update, err := client.Txn(ctx).
If(clientv3.Compare(clientv3.ModRevision("/revkey/1"), "=", rev)).
Then(clientv3.OpPut("/revkey/1", fmt.Sprintf("val-%d", rev))).
Else(clientv3.OpGet("/revkey/1", clientv3.WithRange(""))).
Commit()

g.Expect(err).To(BeNil())
g.Expect(update.Succeeded).To(BeTrue())
}
})

t.Run("List", func(t *testing.T) {
t.Run("NoRevision", func(t *testing.T) {
g := NewWithT(t)
resp, err := client.Get(ctx, "/revkey/", clientv3.WithPrefix())
g.Expect(err).To(BeNil())
g.Expect(resp.Kvs).To(HaveLen(1))
g.Expect(resp.Kvs[0].ModRevision).To(Equal(int64(31)))
g.Expect(resp.Count).To(Equal(int64(1)))
})
t.Run("OldRevision", func(t *testing.T) {
g := NewWithT(t)
resp, err := client.Get(ctx, "/revkey/", clientv3.WithPrefix(), clientv3.WithRev(10))
g.Expect(err).To(BeNil())
g.Expect(resp.Kvs).To(HaveLen(1))
g.Expect(resp.Kvs[0].ModRevision).To(Equal(int64(10)))
g.Expect(resp.Count).To(Equal(int64(1)))
})
t.Run("LaterRevision", func(t *testing.T) {
g := NewWithT(t)
resp, err := client.Get(ctx, "/revkey/", clientv3.WithPrefix(), clientv3.WithRev(100))
g.Expect(err).To(BeNil())
g.Expect(resp.Kvs).To(HaveLen(1))
g.Expect(resp.Kvs[0].ModRevision).To(Equal(int64(31)))
g.Expect(resp.Count).To(Equal(int64(1)))
})
})
})
})
}

Expand Down

0 comments on commit be803ca

Please sign in to comment.