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

badger: update storage to use badgerV3 #68

Merged
merged 6 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions badger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
A fast key-value DB using [dgraph-io/badger](https://github.com/dgraph-io/badger)

### Table of Contents

- [Signatures](#signatures)
- [Installation](#installation)
- [Examples](#examples)
- [Config](#config)
- [Default Config](#default-config)


### Signatures

```go
func New(config ...Config) Storage
func (s *Storage) Get(key string) ([]byte, error)
Expand All @@ -21,22 +22,29 @@ func (s *Storage) Close() error
```

### Installation

Badger is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:

```bash
go mod init github.com/<user>/<repo>
```

And then install the badger implementation:

```bash
go get github.com/gofiber/storage/badger
```

### Examples

Import the storage package.

```go
import "github.com/gofiber/storage/badger"
```

You can use the following possibilities to create a storage:

```go
// Initialize default config
store := badger.New()
Expand All @@ -50,6 +58,7 @@ store := badger.New(badger.Config{
```

### Config

```go
type Config struct {
// Database name
Expand All @@ -66,14 +75,33 @@ type Config struct {
//
// Optional. Default is 10 * time.Second
GCInterval time.Duration

// BadgerOptions is a way to set options in badger
//
// Optional. Default is badger.DefaultOptions("./fiber.badger")
BadgerOptions badger.Options

// Logger is the default logger used by badger
//
// Optional. Default is nil
Logger badger.Logger

// UseLogger define if any logger will be used
//
// Optional. Default is false
UseLogger bool
}
```

### Default Config

```go
var ConfigDefault = Config{
Database: "./fiber.badger",
Reset: false,
GCInterval: 10 * time.Second,
Database: "./fiber.badger",
Reset: false,
GCInterval: 10 * time.Second,
BadgerOptions: badger.DefaultOptions("./fiber.badger").WithLogger(nil),
Logger: nil,
UseLogger: false,
}
```
4 changes: 2 additions & 2 deletions badger/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package badger
import (
"time"

"github.com/dgraph-io/badger"
"github.com/dgraph-io/badger/v3"
"github.com/gofiber/utils"
)

Expand All @@ -20,7 +20,7 @@ func New(config ...Config) *Storage {
cfg := configDefault(config...)

// Set options
opt := badger.DefaultOptions(cfg.Database).WithTruncate(true).WithLogger(nil)
opt := cfg.BadgerOptions

// Open database
db, err := badger.Open(opt)
Expand Down
43 changes: 39 additions & 4 deletions badger/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package badger

import "time"
import (
"github.com/dgraph-io/badger/v3"
"time"
)

// Config defines the config for storage.
type Config struct {
Expand All @@ -18,13 +21,33 @@ type Config struct {
//
// Optional. Default is 10 * time.Second
GCInterval time.Duration

// BadgerOptions is a way to set options in badger
//
// Optional. Default is badger.DefaultOptions("./fiber.badger")
BadgerOptions badger.Options

// Logger is the default logger used by badger
//
// Optional. Default is nil
Logger badger.Logger

// UseLogger define if any logger will be used
//
// Optional. Default is false
UseLogger bool
}

const defaultDatabase = "./fiber.badger"

// ConfigDefault is the default config
var ConfigDefault = Config{
Database: "./fiber.badger",
Reset: false,
GCInterval: 10 * time.Second,
Database: defaultDatabase,
Reset: false,
GCInterval: 10 * time.Second,
BadgerOptions: badger.DefaultOptions(defaultDatabase).WithLogger(nil),
Logger: nil,
UseLogger: false,
}

// Helper function to set default values
Expand All @@ -44,5 +67,17 @@ func configDefault(config ...Config) Config {
if int(cfg.GCInterval.Seconds()) <= 0 {
cfg.GCInterval = ConfigDefault.GCInterval
}
overrideLogger := false
if cfg.BadgerOptions.Dir == defaultDatabase && cfg.BadgerOptions.Dir != cfg.Database {
cfg.BadgerOptions = badger.DefaultOptions(cfg.Database)
overrideLogger = true
}
if overrideLogger {
if cfg.UseLogger && cfg.Logger != nil {
cfg.BadgerOptions = cfg.BadgerOptions.WithLogger(cfg.Logger)
} else if !cfg.UseLogger {
cfg.BadgerOptions = cfg.BadgerOptions.WithLogger(nil)
}
}
return cfg
}
4 changes: 1 addition & 3 deletions badger/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ module github.com/gofiber/storage/badger
go 1.14

require (
github.com/dgraph-io/badger v1.6.2
github.com/dgraph-io/ristretto v0.0.3 // indirect
github.com/dgraph-io/badger/v3 v3.2011.1
github.com/gofiber/utils v0.1.2
github.com/golang/protobuf v1.4.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b // indirect
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
google.golang.org/protobuf v1.25.0 // indirect
Expand Down
Loading