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

CreateTrigger and DropTrigger #79

Merged
merged 1 commit into from
Dec 3, 2022
Merged

CreateTrigger and DropTrigger #79

merged 1 commit into from
Dec 3, 2022

Conversation

kelindar
Copy link
Owner

@kelindar kelindar commented Dec 3, 2022

This PR adds trigger functionality for the cases where built-in capabilities aren't sufficient. Triggers allow you to add a callback function that will get called whether a value is inserted, updated or deleted. It functions similarly to the bitmap index.

players.CreateTrigger("on_balance", "balance", func(r Reader) {
	switch {
	case r.IsDelete():
		updates = append(updates, fmt.Sprintf("delete %d", r.Index()))
	case r.IsUpsert():
		updates = append(updates, fmt.Sprintf("upsert %d=%v", r.Index(), r.Float()))
	}
})

// Perform a few deletions and insertions
for i := 0; i < 3; i++ {
	players.DeleteAt(uint32(i))
	players.Insert(func(r Row) error {
		r.SetFloat64("balance", 50.0)
		return nil
	})
}

// Must keep track of all operations
assert.Len(t, updates, 6)
assert.Equal(t, []string{
	"delete 0", 
	"upsert 500=50", 
	"delete 1", 
	"upsert 501=50", 
	"delete 2",
	"upsert 502=50",
}, updates)

@kelindar kelindar merged commit 6cf7de3 into main Dec 3, 2022
@kelindar kelindar deleted the trigger branch December 3, 2022 18:15
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

Successfully merging this pull request may close these issues.

None yet

1 participant