Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create index method #125

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.13
require (
github.com/hashicorp/go-immutable-radix v1.3.0
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/stretchr/testify v1.8.4
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/hashicorp/go-immutable-radix v1.3.0 h1:8exGP7ego3OmkfksihtSouGMZ+hQrhxx+FVELeXpVPE=
github.com/hashicorp/go-immutable-radix v1.3.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-uuid v1.0.0 h1:RS8zrF7PhGwyNPOtxSClXXj9HA8feRnJzgnI1RJCSnM=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
72 changes: 37 additions & 35 deletions index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,30 @@ import (
)

type TestObject struct {
ID string
Foo string
Fu *string
Boo *string
Bar int
Baz string
Bam *bool
Empty string
Qux []string
QuxEmpty []string
Zod map[string]string
ZodEmpty map[string]string
Int int
Int8 int8
Int16 int16
Int32 int32
Int64 int64
Uint uint
Uint8 uint8
Uint16 uint16
Uint32 uint32
Uint64 uint64
Bool bool
ID string
Foo string
Fu *string
Boo *string
Bar int
Baz string
Bam *bool
Empty string
Qux []string
QuxEmpty []string
Zod map[string]string
ZodEmpty map[string]string
Int int
Int8 int8
Int16 int16
Int32 int32
Int64 int64
Uint uint
Uint8 uint8
Uint16 uint16
Uint32 uint32
Uint64 uint64
Bool bool
Uint32Map map[uint32]struct{}
}

func String(s string) *string {
Expand All @@ -47,7 +48,7 @@ func String(s string) *string {
func testObj() *TestObject {
b := true
obj := &TestObject{
ID: "my-cool-obj",
ID: fmt.Sprintf("my-cool-obj-%v", rand.Int()),
Foo: "Testing",
Fu: String("Fu"),
Boo: nil,
Expand All @@ -60,17 +61,18 @@ func testObj() *TestObject {
"instance_type": "m3.medium",
"": "asdf",
},
Int: int(1),
Int8: int8(-1 << 7),
Int16: int16(-1 << 15),
Int32: int32(-1 << 31),
Int64: int64(-1 << 63),
Uint: uint(1),
Uint8: uint8(1<<8 - 1),
Uint16: uint16(1<<16 - 1),
Uint32: uint32(1<<32 - 1),
Uint64: uint64(1<<64 - 1),
Bool: false,
Int: int(1),
Int8: int8(-1 << 7),
Int16: int16(-1 << 15),
Int32: int32(-1 << 31),
Int64: int64(-1 << 63),
Uint: uint(1),
Uint8: uint8(1<<8 - 1),
Uint16: uint16(1<<16 - 1),
Uint32: uint32(1<<32 - 1),
Uint64: uint64(1<<64 - 1),
Bool: false,
Uint32Map: map[uint32]struct{}{},
}
return obj
}
Expand Down
39 changes: 39 additions & 0 deletions memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,45 @@ func (db *MemDB) Snapshot() *MemDB {
return clone
}

// CreateIndexes is used for create indexes after the database already exists.
func (db *MemDB) CreateIndexes(table string, schema ...*IndexSchema) error {
transaction := db.Txn(true)
defer transaction.Abort()

// create an edge for each index to create
root := db.getRoot()
for _, indexSchema := range schema {
index := iradix.New()
path := indexPath(table, indexSchema.Name)
transaction.rootTxn.Insert(path, index)
//root, _, _ = root.Insert(path, index)
}
db.root = unsafe.Pointer(root)

//// now we can create the transaction
//transaction := &Txn{
// db: db,
// write: true,
// rootTxn: db.getRoot().Txn(),
//}
//defer transaction.Abort()

// create the requested indexes
if err := transaction.CreateIndexes(table, schema...); err != nil {
return err
}

transaction.Commit()

// copy the new created indexes into our schema
tableSchema := db.schema.Tables[table]
for _, indexSchema := range schema {
tableSchema.Indexes[indexSchema.Name] = indexSchema
}
Comment on lines +128 to +132
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm this seems like a race footgun that we should at least document in the doc comment for this method.

I think it's true that today we never expect db.schema to change after initialization therefore readers will read from it without taking the db.writer.Lock which would be a data race if there were any readers concurrent with this call.

I think that's probably OK for it's intended usage and adding a lock to every read would be too invasive and impact performance for all users, but we should at least point out the requirement in the doc comments that this MUST NOT be called concurrently with any reads. For it's intended purpose that would probably be a logical bug anyway since the indexes the readers presumably need wouldn't exist yet but it might not be obvious to other library users!


return nil
}

// initialize is used to setup the DB for use after creation. This should
// be called only once after allocating a MemDB.
func (db *MemDB) initialize() error {
Expand Down
Loading