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

Modifying values in-place that are already inserted into MemDB is not supported behavior #157

Open
ApisecCopilot opened this issue May 30, 2024 · 1 comment

Comments

@ApisecCopilot
Copy link

i have a question abort txn.Insert() function. it has a describe “Modifying values in-place that are already inserted into MemDB is not supported behavior.”

` // Create a new data base
db, err := memdb.NewMemDB(schema)
if err != nil {
panic(err)
}
txn := db.Txn(true)
p := Person{"[email protected]", "Joe", 30}
if err := txn.Insert("person", p); err != nil {
panic(err)
}
txn.Commit()

txn = db.Txn(false)
defer txn.Abort()

p.Name = "Janeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
p.Age = 1111111
p.Email = "[email protected]"

// List all the people
it, err := txn.Get("person", "id")
if err != nil {
	panic(err)
}
for obj := it.Next(); obj != nil; obj = it.Next() {
	x := obj.(Person)
	fmt.Printf("  %+v\n", x)
}

`
aftern Insert and commit, i change object'value
and result is : &{Email:[email protected] Name:Janeeeeeeeeeeeeeeeeeeeeeeeeeeeeee Age:1111111}

is that correctly?

@i4ki
Copy link

i4ki commented Jan 12, 2025

This behavior is documented here:

go-memdb/memdb.go

Lines 24 to 28 in 0f077dd

// Objects inserted into MemDB are not copied. It is **extremely important**
// that objects are not modified in-place after they are inserted since they
// are stored directly in MemDB. It remains unsafe to modify inserted objects
// even after they've been deleted from MemDB since there may still be older
// snapshots of the DB being read from other goroutines.

you have to implement a "copy" strategy for each data structure if you intend to modify them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants