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

p/index for SQL-style indexes on datastore libraries (i.e., avl) #2194

Open
moul opened this issue May 25, 2024 · 3 comments
Open

p/index for SQL-style indexes on datastore libraries (i.e., avl) #2194

moul opened this issue May 25, 2024 · 3 comments

Comments

@moul
Copy link
Member

moul commented May 25, 2024

Propose creating a p/index package to set up an indexing mechanism that automatically maintains an index based on a configuration each time a data struct is updated.

Several packages in examples/ manually implement similar logic, storing pointers in multiple avl.Tree instances for different fields.

Details

  • Automatic Indexing: Automatically maintain an index based on configuration when a data struct is updated.
  • Separation of Concerns: Allow datastores to focus on storing data while the indexer library manages secondary indexes efficiently.
  • Simplified Logic: Simplifies writing, reading, and maintaining indexing logic.
  • Optimized Implementation: Allows for optimized implementations, such as using a slice for small datasets and switching to an avl.Tree transparently.

Potential usage example

type User struct {
    Username string
    // Other fields...
}
var db = avl.Tree
var id = seqid.ID
db, usernameIdx = index.String(func(entry interface{}) string {
    user := entry.(*User)
    return user.Username
})
db.Set(id.Next(), &User{Username: "manfred"}) 
usernameIdx.Get("manfred")
usernameIdx.Iterate(/*...*/) // sorted by username

Or

var db = avl.tree
db = index.String(db, "username", func(entry interface{}) string {})
db = index.Int(db, "age", func(entry interface{}) int {})
// then we can use db as usual + we have new helpers
db.Get(/*...*/) // standard avl
db.GetByIndex("username", "manfred")
db.IterateByIndex("username")
@linhpn99
Copy link
Contributor

linhpn99 commented May 26, 2024

@moul I see the second example appears to be cleaner, however it seems that we would need to define an object implementing the tree and an interface for methods related to indexing. If you're confident, please provide a specific plan, and I will create a PR to address it

@moul
Copy link
Member Author

moul commented May 26, 2024

I prefer the first approach for its versatility, as it can work with any datastore implementation, not just avl.Tree.

I prefer the second approach specifically for avl.Tree.

The underlying logic will likely be similar, and we could expose the two interfaces as independent packages to allow A/B testing and see which approach users prefer.

The p/index feature is the first step in a longer-term plan to provide a userland SQL-like interface.

@notJoon
Copy link
Member

notJoon commented May 28, 2024

It seems to be somewhat related to the previously proposed avlorm(#1467) package. Would it be better to create it as a separate package or add query functionality to the ORM package?

If it's to be created as a separate package, I will work on the ORM. I have something that still in WIP.

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

No branches or pull requests

4 participants