Skip to content

Commit

Permalink
Merge pull request #23 from anyproto/ensure-create-index
Browse files Browse the repository at this point in the history
Ensure/Create index
  • Loading branch information
cheggaaa authored Nov 26, 2024
2 parents ea36d20 + 9e15567 commit 258f7cc
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
6 changes: 3 additions & 3 deletions cmd/any-store-cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/anyproto/any-store/cmd/any-store-cli
go 1.22.1

require (
github.com/anyproto/any-store v0.1.2-0.20241111111229-b91543814476
github.com/anyproto/any-store v0.1.3-0.20241125123457-251299094be1
github.com/fatih/color v1.17.0
github.com/peterh/liner v1.2.2
github.com/robertkrimen/otto v0.4.0
Expand All @@ -27,9 +27,9 @@ require (
golang.org/x/text v0.14.0 // indirect
gopkg.in/sourcemap.v1 v1.0.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
modernc.org/libc v1.61.0 // indirect
modernc.org/libc v1.61.2 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.8.0 // indirect
modernc.org/sqlite v1.33.1 // indirect
modernc.org/sqlite v1.34.1 // indirect
zombiezen.com/go/sqlite v1.4.0 // indirect
)
20 changes: 10 additions & 10 deletions cmd/any-store-cli/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/anyproto/any-store v0.1.2-0.20241111111229-b91543814476 h1:18GPpZv1fkoxWFHC+Ar+0OG6Yqa0L7p61gqeDwSk/tI=
github.com/anyproto/any-store v0.1.2-0.20241111111229-b91543814476/go.mod h1:SDlN2AzysAfs8CDd28rrs6boUUtf5H/ydCvwmj+EbsE=
github.com/anyproto/any-store v0.1.3-0.20241125123457-251299094be1 h1:RcZyq/07mMzABepaW2fFvsmAXeUE1sK3lFwlQcXZoco=
github.com/anyproto/any-store v0.1.3-0.20241125123457-251299094be1/go.mod h1:SDlN2AzysAfs8CDd28rrs6boUUtf5H/ydCvwmj+EbsE=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
Expand Down Expand Up @@ -54,16 +54,16 @@ gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI=
gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
modernc.org/cc/v4 v4.21.4 h1:3Be/Rdo1fpr8GrQ7IVw9OHtplU4gWbb+wNgeoBMmGLQ=
modernc.org/cc/v4 v4.21.4/go.mod h1:HM7VJTZbUCR3rV8EYBi9wxnJ0ZBRiGE5OeGXNA0IsLQ=
modernc.org/ccgo/v4 v4.21.0 h1:kKPI3dF7RIag8YcToh5ZwDcVMIv6VGa0ED5cvh0LMW4=
modernc.org/ccgo/v4 v4.21.0/go.mod h1:h6kt6H/A2+ew/3MW/p6KEoQmrq/i3pr0J/SiwiaF/g0=
modernc.org/cc/v4 v4.23.1 h1:WqJoPL3x4cUufQVHkXpXX7ThFJ1C4ik80i2eXEXbhD8=
modernc.org/cc/v4 v4.23.1/go.mod h1:HM7VJTZbUCR3rV8EYBi9wxnJ0ZBRiGE5OeGXNA0IsLQ=
modernc.org/ccgo/v4 v4.22.3 h1:C7AW89Zw3kygesTQWBzApwIn9ldM+cb/plrTIKq41Os=
modernc.org/ccgo/v4 v4.22.3/go.mod h1:Dz7n0/UkBbH3pnYaxgi1mFSfF4REqUOZNziphZASx6k=
modernc.org/fileutil v1.3.0 h1:gQ5SIzK3H9kdfai/5x41oQiKValumqNTDXMvKo62HvE=
modernc.org/fileutil v1.3.0/go.mod h1:XatxS8fZi3pS8/hKG2GH/ArUogfxjpEKs3Ku3aK4JyQ=
modernc.org/gc/v2 v2.5.0 h1:bJ9ChznK1L1mUtAQtxi0wi5AtAs5jQuw4PrPHO5pb6M=
modernc.org/gc/v2 v2.5.0/go.mod h1:wzN5dK1AzVGoH6XOzc3YZ+ey/jPgYHLuVckd62P0GYU=
modernc.org/libc v1.61.0 h1:eGFcvWpqlnoGwzZeZe3PWJkkKbM/3SUGyk1DVZQ0TpE=
modernc.org/libc v1.61.0/go.mod h1:DvxVX89wtGTu+r72MLGhygpfi3aUGgZRdAYGCAVVud0=
modernc.org/libc v1.61.2 h1:dkO4DlowfClcJYsvf/RiK6fUwvzCQTmB34bJLt0CAGQ=
modernc.org/libc v1.61.2/go.mod h1:4QGjNyX3h+rn7V5oHpJY2yH0QN6frt1X+5BkXzwLPCo=
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
modernc.org/memory v1.8.0 h1:IqGTL6eFMaDZZhEWwcREgeMXYwmW83LYW8cROZYkg+E=
Expand All @@ -72,8 +72,8 @@ modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4=
modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0=
modernc.org/sortutil v1.2.0 h1:jQiD3PfS2REGJNzNCMMaLSp/wdMNieTbKX920Cqdgqc=
modernc.org/sortutil v1.2.0/go.mod h1:TKU2s7kJMf1AE84OoiGppNHJwvB753OYfNl2WRb++Ss=
modernc.org/sqlite v1.33.1 h1:trb6Z3YYoeM9eDL1O8do81kP+0ejv+YzgyFo+Gwy0nM=
modernc.org/sqlite v1.33.1/go.mod h1:pXV2xHxhzXZsgT/RtTFAPY6JJDEvOTcTdwADQCCWD4k=
modernc.org/sqlite v1.34.1 h1:u3Yi6M0N8t9yKRDwhXcyp1eS5/ErhPTBggxWFuR6Hfk=
modernc.org/sqlite v1.34.1/go.mod h1:pXV2xHxhzXZsgT/RtTFAPY6JJDEvOTcTdwADQCCWD4k=
modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA=
modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0=
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
Expand Down
15 changes: 15 additions & 0 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ type Collection interface {
// Returns the count of documents or an error if the operation fails.
Count(ctx context.Context) (count int, err error)

// CreateIndex creates a new index.
// Returns an error if index exists or the operation fails.
CreateIndex(ctx context.Context, info ...IndexInfo) (err error)

// EnsureIndex ensures an index exists on the specified fields.
// Returns an error if the operation fails.
EnsureIndex(ctx context.Context, info ...IndexInfo) (err error)
Expand Down Expand Up @@ -508,7 +512,15 @@ func (c *collection) Count(ctx context.Context) (count int, err error) {
return
}

func (c *collection) CreateIndex(ctx context.Context, info ...IndexInfo) (err error) {
return c.createIndexes(ctx, false, info...)
}

func (c *collection) EnsureIndex(ctx context.Context, info ...IndexInfo) (err error) {
return c.createIndexes(ctx, true, info...)
}

func (c *collection) createIndexes(ctx context.Context, ensure bool, info ...IndexInfo) (err error) {
buf := c.db.syncPool.GetDocBuf()
defer c.db.syncPool.ReleaseDocBuf(buf)
// TODO: validate fields
Expand All @@ -522,6 +534,9 @@ func (c *collection) EnsureIndex(ctx context.Context, info ...IndexInfo) (err er
)
for _, idxInfo := range info {
if idx, txErr = c.createIndex(ctx, cn, idxInfo); txErr != nil {
if ensure && errors.Is(txErr, ErrIndexExists) {
continue
}
return
}
if txErr = idx.checkStmts(ctx, cn); txErr != nil {
Expand Down
15 changes: 12 additions & 3 deletions collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,17 +263,17 @@ func TestCollection_DeleteOne(t *testing.T) {
})
}

func TestCollection_EnsureIndex(t *testing.T) {
func TestCollection_CreateIndex(t *testing.T) {
fx := newFixture(t)
coll, err := fx.CreateCollection(ctx, "test")
require.NoError(t, err)
require.NoError(t, coll.Insert(ctx, anyenc.MustParseJson(`{"id":1, "doc":"a"}`), anyenc.MustParseJson(`{"id":2, "doc":"b"}`)))
t.Run("err exists", func(t *testing.T) {
err = coll.EnsureIndex(ctx, IndexInfo{Fields: []string{"name"}}, IndexInfo{Fields: []string{"name"}})
err = coll.CreateIndex(ctx, IndexInfo{Fields: []string{"name"}}, IndexInfo{Fields: []string{"name"}})
assert.ErrorIs(t, err, ErrIndexExists)
})
t.Run("success", func(t *testing.T) {
require.NoError(t, coll.EnsureIndex(ctx, IndexInfo{Fields: []string{"doc"}}))
require.NoError(t, coll.CreateIndex(ctx, IndexInfo{Fields: []string{"doc"}}))
idxs := coll.GetIndexes()
require.Len(t, idxs, 1)
assert.Equal(t, "doc", idxs[0].Info().Name)
Expand All @@ -283,6 +283,15 @@ func TestCollection_EnsureIndex(t *testing.T) {
})
}

func TestCollection_EnsureIndex(t *testing.T) {
fx := newFixture(t)
coll, err := fx.CreateCollection(ctx, "test")
require.NoError(t, err)
require.NoError(t, coll.Insert(ctx, anyenc.MustParseJson(`{"id":1, "doc":"a"}`), anyenc.MustParseJson(`{"id":2, "doc":"b"}`)))
err = coll.EnsureIndex(ctx, IndexInfo{Fields: []string{"name"}}, IndexInfo{Fields: []string{"doc"}}, IndexInfo{Fields: []string{"name"}})
assert.NoError(t, err, ErrIndexExists)
}

func TestCollection_DropIndex(t *testing.T) {
fx := newFixture(t)
coll, err := fx.CreateCollection(ctx, "test")
Expand Down

0 comments on commit 258f7cc

Please sign in to comment.