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

Fixed duplicate primary keys #68

Merged
merged 2 commits into from Jun 12, 2022
Merged

Fixed duplicate primary keys #68

merged 2 commits into from Jun 12, 2022

Conversation

kelindar
Copy link
Owner

This PR fixes #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
})

@kelindar kelindar merged commit 7ce54ab into main Jun 12, 2022
@kelindar kelindar deleted the pkdup branch June 12, 2022 20:34
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.

Duplicate key records getting inserted
1 participant