-
Notifications
You must be signed in to change notification settings - Fork 182
Basic Operations
roseduan edited this page Mar 19, 2022
·
1 revision
Basic operations for LotusDB.
import (
"github.com/flower-corp/lotusdb"
"io/ioutil"
"os"
)
func main() {
// open a db with default options.
path, _ := ioutil.TempDir("", "lotusdb")
// you must specify a db path.
opts := lotusdb.DefaultOptions(path)
db, err := lotusdb.Open(opts)
defer func() {
_ = db.Close()
}()
if err != nil {
panic(err)
}
}
Opening a database is open the default Column Family, and you can set other options by opts.CfOpts.XXX
.
import (
"github.com/flower-corp/lotusdb"
"io/ioutil"
"time"
)
func main() {
path, _ := ioutil.TempDir("", "lotusdb")
opts := lotusdb.DefaultOptions(path)
db, err := lotusdb.Open(opts)
if err != nil {
panic(err)
}
defer db.Close()
// 1.----put----
key1 := []byte("name")
err = db.Put(key1, []byte("lotusdb"))
if err != nil {
// ...
}
key2 := []byte("feature")
// 2.----put with options----
writeOpts := &lotusdb.WriteOptions{
Sync: true,
ExpiredAt: time.Now().Add(time.Second * 100).Unix(),
}
err = db.PutWithOptions(key2, []byte("store data"), writeOpts)
if err != nil {
// ...
}
}
import (
"github.com/flower-corp/lotusdb"
"io/ioutil"
"time"
)
func main() {
path, _ := ioutil.TempDir("", "lotusdb")
opts := lotusdb.DefaultOptions(path)
db, err := lotusdb.Open(opts)
if err != nil {
panic(err)
}
defer db.Close()
key1 := []byte("name")
// 3.----get----
val, err := db.Get(key1)
if err != nil {
// ...
}
if len(val) > 0 {
// ...
}
}
import (
"github.com/flower-corp/lotusdb"
"io/ioutil"
"time"
)
func main() {
path, _ := ioutil.TempDir("", "lotusdb")
opts := lotusdb.DefaultOptions(path)
db, err := lotusdb.Open(opts)
if err != nil {
panic(err)
}
defer db.Close()
key1 := []byte("name")
// 4.----delete----
err = db.Delete(key1)
if err != nil {
// ...
}
// 5.----delete with options----
deleteOpts := &lotusdb.WriteOptions{
Sync: false,
DisableWal: true,
}
err = db.DeleteWithOptions([]byte("dummy key"), deleteOpts)
if err != nil {
// ...
}
}
ColumnFamily is a namespace of keys and values, each key-value pair in LotusDB is associated with exactly one Column Family.
If no Column Family is specified, key/value pair is associated with Column Family "cf_default".
Column Families provide a way to logically partition the database.
import (
"github.com/flower-corp/lotusdb"
"io/ioutil"
)
func main() {
// open a db with default options.
path, _ := ioutil.TempDir("", "lotusdb")
// you must specify a db path.
opts := lotusdb.DefaultOptions(path)
db, err := lotusdb.Open(opts)
defer func() {
_ = db.Close()
}()
if err != nil {
panic(err)
}
cfOpts := lotusdb.DefaultColumnFamilyOptions("a-new-cf")
cfOpts.DirPath = "/tmp"
cf, err := db.OpenColumnFamily(cfOpts)
if err != nil {
panic(err)
}
err = cf.Put([]byte("name"), []byte("LotusDB"))
if err != nil {
// ...
}
}