kelindar / column

High-performance, columnar, in-memory store with bitmap indexing in Go
MIT License
1.44k stars 57 forks source link

Fixed duplicate primary keys #68

Closed kelindar closed 2 years ago

kelindar commented 2 years ago

This PR fixes https://github.com/kelindar/column/issues/57 by checking the Insert() method and providing InsertKey(), UpsertKey(), QueryKey() and DeleteKey() methods that are specifically designed to deal with primary keys.

In addition, I've also improved the API for TTL management which is now a bit cleaner.

Here's an exerpt from the documentation on the primary keys:

Using Primary Keys

In certain cases it is useful to access a specific row by its primary key instead of an index which is generated internally by the collection. For such use-cases, the library provides Key column type that enables a seamless lookup by a user-defined primary key. In the example below we create a collection with a primary key name using CreateColumn() method with a ForKey() column type. Then, we use InsertKey() method to insert a value.

players := column.NewCollection()
players.CreateColumn("name", column.ForKey())     // Create a "name" as a primary-key
players.CreateColumn("class", column.ForString()) // .. and some other columns

// Insert a player with "merlin" as its primary key
c.InsertKey("merlin", func(r column.Row) error {
    r.SetString("class", "mage")
    return nil
})

Similarly, you can use primary key to query that data directly, without knowing the exact offset. Do note that using primary keys will have an overhead, as it requires an additional step of looking up the offset using a hash table managed internally.

// Query merlin's class
c.QueryKey("merlin", func(r column.Row) error {
    class, _ := r.String("class")
    return nil
})