Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/KenanBek/memdb
In-memory database with O(1) insert, delete and search.
https://github.com/KenanBek/memdb
database go golang in-memory-database
Last synced: 3 months ago
JSON representation
In-memory database with O(1) insert, delete and search.
- Host: GitHub
- URL: https://github.com/KenanBek/memdb
- Owner: KenanBek
- License: apache-2.0
- Created: 2020-03-20T01:52:19.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-03-20T05:12:14.000Z (over 4 years ago)
- Last Synced: 2024-06-20T08:17:05.845Z (5 months ago)
- Topics: database, go, golang, in-memory-database
- Language: Go
- Homepage:
- Size: 9.77 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-golang-repositories - memdb - memory database with O(1) insert, delete and search. (Repositories)
README
# MemDB
In-memory database with O(1) insert, delete and search.
**Note** for now it's not safe for concurrent use
### Usage
First, we define model to be used. It should implement `memdb.Entry` interface:
```Go
// User model.
type User struct {
Username string
FirstName string
LastName string
}// Key returns unique username of the user.
func (u *User) Key() string {
return u.Username
}
```Key function required to implement Entry interface.
Initialize database and insert an entry:
```Go
user := User{
Username: "user1",
FirstName: "user1 first name",
LastName: "user1 last name",
}db := NewMemDb()
db.AddEntry(&user)
```Search and delete:
```Go
// search for entry by key
entry1 := db.GetEntry("user1")
// cast entry to User type
entry1user := entry1.(*User)
// delete an entry
db.DeleteEntry("user1")
```#### Groups
It's also possible to group entries. Let's say we have another model called Post:
```go
// Post model.
type Post struct {
Slug string
Author string
Title string
Content string
}// Key returns unique slug of the post.
func (p *Post) Key() string {
return p.Slug
}
```Using `AddGroupEntry`, `GetGroupEntry`, `DeleteGroupEntry` and `ListGroupEntry` we can manipulate items in a group.
```go
user1 := User{
Username: "user1",
FirstName: "user1 first name",
LastName: "user1 last name",
}
post1 := Post{
Slug: "post1",
Author: "user1",
Title: "post1 title",
Content: "post1 content",
}
post2 := Post{
Slug: "post2",
Author: "user2",
Title: "post2 title",
Content: "post2 content",
}db := NewMemDb()
db.AddGroupEntry("users", &user1)
db.AddGroupEntry("posts", &post1)
db.AddGroupEntry("posts", &post2)
````GetGroupEntry` returns an entry in a group:
```go
db.GetGroupEntry("users", "user1")
db.GetGroupEntry("posts", "post1")
```Returned interface can be casted to the origin type:
```go
entry := db.GetGroupEntry("users", "user1")
user := entry.(*User)
```It's also possible to add different types to database without specifying any group:
```go
user1 := User{
Username: "user1",
FirstName: "user1 first name",
LastName: "user1 last name",
}
post1 := Post{
Slug: "post1",
Author: "user1",
Title: "post1 title",
Content: "post1 content",
}db := NewMemDb()
db.AddEntry(&user1)
db.AddEntry(&post1)
```