You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Star5 (5)You must be signed in to star a repository
About
k-v distributed database inspired by redis, includes optional TTLs, lock-free read/writes, and a leader-node election mechanism paired with a heartbeat-based leader failover detection.
utilises an inter-node based communication system with a leader election mechanism upon leader failure.
centralised serverside error handling.
interactive cli mode, which allows storing data as well as data retrieval.
encodes integer type values to bytes before storing to the in-memory db for optimised performance during node-to-node communication.
pre-requisite:
go version 1.27+
instillation
go get github.com/72sevenzy2/in-memory-database
getting started:
package main
import (
"fmt"
db "github.com/72sevenzy2/in-memory-database"
)
func main() {
v := db.NewDB()
// db.Integer{} specifies that its an int-type value.
err := v.Set("test1", 200, db. db.Integer{}) // setting ints
if err != nil {
panic(err)
}
// db.String{} specifies that its an string-type value.
err2 := v.Set("test2", "hello", db.String{}) // setting strings
if err2 != nil {
panic(err2)
}
}
setting data with TTLs:
package main
import (
"fmt"
db "github.com/72sevenzy2/in-memory-database"
)
func main() {
v := db.NewDB()
err := v.Set("test1", 200, time.Second*5, db.Integer{})
if err != nil {
panic(err)
}
err2 := v.Set("test2", "hello", time.Second*10, db.String{})
if err2 != nil {
panic(err2)
}
}
afterwards, you may retrieve them like so:
package main
import (
"fmt"
db "github.com/72sevenzy2/in-memory-database"
)
func main() {
v := db.NewDB()
// here, db.Integer{} specifies that your retrieving an int-typed value of the given key.
val, ok := v.Get("test1", db.Integer{})
if ok {
fmt.Println(val) // 200
}
// and here, db.String{} specifies that your retrieving an string-typed value of the given key.
val2, ok2 := v.Get("test2", db.String{})
if ok2 {
fmt.Println(val2) // "hello"
}
}
to retrieve all values of type string/int:
package main
import (
"fmt"
db "github.com/72sevenzy2/in-memory-database"
)
func main() {
v := db.NewDB()
vals, ok := v.GetAll(db.String{}) // vals is of type map[string]string
if ok {
for k, v := range vals {
fmt.Println(k, v)
}
}
vals2, ok2 := v.GetAll(db.Integer{}) // vals2 is of type map[string]uint32
if ok2 {
for k, v := range vals2 {
fmt.Println(k, v)
}
}
}
you can also edit existing keys:
package main
import (
"fmt"
db "github.com/72sevenzy2/in-memory-database"
)
func main() {
v := db.NewDB()
err := v.Set("test1", 200, db. db.Integer{}) // setting ints
if err != nil {
panic(err)
}
err := v.Edit("test1", "new value", db.String{}) // "test1" is now set to the string value "new value"
if err != nil {
panic(err)
}
}
lastly, to delete keys:
package main
import (
"fmt"
db "github.com/72sevenzy2/in-memory-database"
)
func main() {
v := db.NewDB()
v.Del("keyName")
}
interactive cli tutorial:
firstly, switch to the cmd/server directory:
cd cmd/server
and begin with spinning up a db node:
go run . -id [node-id] -addr [port] -replicas [port1,port2,port3]
where -id represents an unique id for the leader node, eg "node-#1", -addr is the port in which it will listen on, and -replicas is for the ports in which the replica nodes will run on (comma-seperated).
and afterwards, switch to the cmd/cli directory and start the cli (in another terminal):
cd cmd/cli
go run .
follow up by declaring a variable:
SET [KeyName] [Value]
with TTL expiration:
SET [KeyName] [Value] [TTL, eg: 30s / 1m]
and retrieve it like so:
GET [KeyName]
you can also edit variables:
EDIT [KeyName] [NewValue]
and to delete a variable/key:
DEL [KeyName]
lastly, to exit:
EXIT
About
k-v distributed database inspired by redis, includes optional TTLs, lock-free read/writes, and a leader-node election mechanism paired with a heartbeat-based leader failover detection.