Skip to content

Commit

Permalink
Merge pull request #189 from asdine/v2
Browse files Browse the repository at this point in the history
Storm v2
  • Loading branch information
asdine committed Dec 3, 2017
2 parents f0fe866 + b6e4c6b commit dbd3772
Show file tree
Hide file tree
Showing 32 changed files with 184 additions and 414 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -6,6 +6,7 @@ before_install:
go:
- 1.7
- 1.8
- 1.9
- tip

script:
Expand Down
53 changes: 27 additions & 26 deletions README.md
@@ -1,11 +1,10 @@
# Storm

[![Join the chat at https://gitter.im/asdine/storm](https://badges.gitter.im/asdine/storm.svg)](https://gitter.im/asdine/storm?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Build Status](https://travis-ci.org/asdine/storm.svg)](https://travis-ci.org/asdine/storm)
[![GoDoc](https://godoc.org/github.com/asdine/storm?status.svg)](https://godoc.org/github.com/asdine/storm)
[![Go Report Card](https://goreportcard.com/badge/github.com/asdine/storm)](https://goreportcard.com/report/github.com/asdine/storm)

Storm is a simple and powerful toolkit for [BoltDB](https://github.com/boltdb/bolt). Basically, Storm provides indexes, a wide range of methods to store and fetch data, an advanced query system, and much more.
Storm is a simple and powerful toolkit for [BoltDB](https://github.com/coreos/bbolt). Basically, Storm provides indexes, a wide range of methods to store and fetch data, an advanced query system, and much more.

In addition to the examples below, see also the [examples in the GoDoc](https://godoc.org/github.com/asdine/storm#pkg-examples).

Expand Down Expand Up @@ -43,7 +42,6 @@ In addition to the examples below, see also the [examples in the GoDoc](https://
- [Node options](#node-options)
- [Simple Key/Value store](#simple-keyvalue-store)
- [BoltDB](#boltdb)
- [Migrations](#migrations)
- [License](#license)
- [Credits](#credits)

Expand All @@ -62,6 +60,7 @@ import "github.com/asdine/storm"
## Open a database

Quick way of opening a database

```go
db, err := storm.Open("my.db")

Expand Down Expand Up @@ -103,11 +102,11 @@ type Base struct {
}

type User struct {
Base `storm:"inline"`
Group string `storm:"index"`
Email string `storm:"unique"`
Name string
CreatedAt time.Time `storm:"index"`
Base `storm:"inline"`
Group string `storm:"index"`
Email string `storm:"unique"`
Name string
CreatedAt time.Time `storm:"index"`
}
```

Expand Down Expand Up @@ -142,11 +141,11 @@ Storm can auto increment integer values so you don't have to worry about that wh
```go

type Product struct {
Pk int `storm:"id,increment"` // primary key with auto increment
Name string
IntegerField uint64 `storm:"increment"`
IndexedIntegerField uint32 `storm:"index,increment"`
UniqueIntegerField int16 `storm:"unique,increment=100"` // the starting value can be set
Pk int `storm:"id,increment"` // primary key with auto increment
Name string
IntegerField uint64 `storm:"increment"`
IndexedIntegerField uint32 `storm:"index,increment"`
UniqueIntegerField int16 `storm:"unique,increment=100"` // the starting value can be set
}

p := Product{Name: "Vaccum Cleaner"}
Expand Down Expand Up @@ -175,7 +174,7 @@ fmt.Println(p.UniqueIntegerField)

### Simple queries

Any object can be fetched, indexed or not. Storm uses indexes when available, otherwhise it uses the [query system](#advanced-queries).
Any object can be fetched, indexed or not. Storm uses indexes when available, otherwise it uses the [query system](#advanced-queries).

#### Fetch one object

Expand Down Expand Up @@ -433,6 +432,7 @@ if err != nil {

return tx.Commit()
```

### Options

Storm options are functions that can be passed when constructing you Storm instance. You can pass it any number of options.
Expand Down Expand Up @@ -462,12 +462,12 @@ These can be used by importing the relevant package and use that codec to config

```go
import (
"github.com/asdine/storm"
"github.com/asdine/storm/codec/gob"
"github.com/asdine/storm/codec/json"
"github.com/asdine/storm/codec/sereal"
"github.com/asdine/storm/codec/protobuf"
"github.com/asdine/storm/codec/msgpack"
"github.com/asdine/storm"
"github.com/asdine/storm/codec/gob"
"github.com/asdine/storm/codec/json"
"github.com/asdine/storm/codec/sereal"
"github.com/asdine/storm/codec/protobuf"
"github.com/asdine/storm/codec/msgpack"
)

var gobDb, _ = storm.Open("gob.db", storm.Codec(gob.Codec))
Expand All @@ -490,7 +490,7 @@ db := storm.Open("my.db", storm.UseDB(bDB))

#### Batch mode

Batch mode can be enabled to speed up concurrent writes (see [Batch read-write transactions](https://github.com/boltdb/bolt#batch-read-write-transactions))
Batch mode can be enabled to speed up concurrent writes (see [Batch read-write transactions](https://github.com/coreos/bbolt#batch-read-write-transactions))

```go
db := storm.Open("my.db", storm.Batch())
Expand Down Expand Up @@ -546,16 +546,19 @@ n := db.From("my-node")
```

Give a bolt.Tx transaction to the Node

```go
n = n.WithTransaction(tx)
```

Enable batch mode

```go
n = n.WithBatch(true)
```

Use a Codec

```go
n = n.WithCodec(gob.Codec)
```
Expand All @@ -566,6 +569,7 @@ Storm can be used as a simple, robust, key/value store that can store anything.
The key and the value can be of any type as long as the key is not a zero value.

Saving data :

```go
db.Set("logs", time.Now(), "I'm eating my breakfast man")
db.Set("sessions", bson.NewObjectId(), &someUser)
Expand All @@ -576,6 +580,7 @@ db.Set("weird storage", "754-3010", map[string]interface{}{
```

Fetching data :

```go
user := User{}
db.Get("sessions", someObjectId, &user)
Expand All @@ -587,6 +592,7 @@ db.Get("sessions", someObjectId, &details)
```

Deleting data :

```go
db.Delete("sessions", someObjectId)
db.Delete("weird storage", "754-3010")
Expand Down Expand Up @@ -617,11 +623,6 @@ db.Bolt.Update(func(tx *bolt.Tx) error {
})
```

## Migrations

You can use the migration tool to migrate databases that use older version of Storm.
See this [README](https://github.com/asdine/storm-migrator) for more informations.

## License

MIT
Expand Down
12 changes: 6 additions & 6 deletions bench_test.go
Expand Up @@ -7,7 +7,7 @@ import (
)

func BenchmarkFindWithIndex(b *testing.B) {
db, cleanup := createDB(b, AutoIncrement())
db, cleanup := createDB(b)
defer cleanup()

var users []User
Expand Down Expand Up @@ -37,7 +37,7 @@ func BenchmarkFindWithIndex(b *testing.B) {
}

func BenchmarkFindWithoutIndex(b *testing.B) {
db, cleanup := createDB(b, AutoIncrement())
db, cleanup := createDB(b)
defer cleanup()

var users []User
Expand Down Expand Up @@ -67,7 +67,7 @@ func BenchmarkFindWithoutIndex(b *testing.B) {
}

func BenchmarkOneWithIndex(b *testing.B) {
db, cleanup := createDB(b, AutoIncrement())
db, cleanup := createDB(b)
defer cleanup()

var u User
Expand All @@ -89,7 +89,7 @@ func BenchmarkOneWithIndex(b *testing.B) {
}

func BenchmarkOneByID(b *testing.B) {
db, cleanup := createDB(b, AutoIncrement())
db, cleanup := createDB(b)
defer cleanup()

type User struct {
Expand Down Expand Up @@ -120,7 +120,7 @@ func BenchmarkOneByID(b *testing.B) {
}

func BenchmarkOneWithoutIndex(b *testing.B) {
db, cleanup := createDB(b, AutoIncrement())
db, cleanup := createDB(b)
defer cleanup()

var u User
Expand All @@ -142,7 +142,7 @@ func BenchmarkOneWithoutIndex(b *testing.B) {
}

func BenchmarkSave(b *testing.B) {
db, cleanup := createDB(b, AutoIncrement())
db, cleanup := createDB(b)
defer cleanup()

w := User{Name: "John"}
Expand Down
2 changes: 1 addition & 1 deletion bucket.go
@@ -1,6 +1,6 @@
package storm

import "github.com/boltdb/bolt"
import "github.com/coreos/bbolt"

// CreateBucketIfNotExists creates the bucket below the current node if it doesn't
// already exist.
Expand Down
12 changes: 6 additions & 6 deletions bucket_test.go
Expand Up @@ -16,9 +16,9 @@ func TestBucket(t *testing.T) {
t.Fatal(err)
}

require.Nil(t, db.root.GetBucket(readTx, "none"))
require.Nil(t, db.GetBucket(readTx, "none"))

b, err := db.root.CreateBucketIfNotExists(readTx, "new")
b, err := db.CreateBucketIfNotExists(readTx, "new")

// Cannot create buckets in a read transaction
require.Error(t, err)
Expand All @@ -36,9 +36,9 @@ func TestBucket(t *testing.T) {
t.Fatal(err)
}

require.Nil(t, db.root.GetBucket(writeTx, "none"))
require.Nil(t, db.GetBucket(writeTx, "none"))

b, err = db.root.CreateBucketIfNotExists(writeTx, "new")
b, err = db.CreateBucketIfNotExists(writeTx, "new")

require.NoError(t, err)
require.NotNil(t, b)
Expand All @@ -59,8 +59,8 @@ func TestBucket(t *testing.T) {
t.Fatal(err)
}

require.NotNil(t, db.root.GetBucket(readTx, "new"))
require.Nil(t, db.root.GetBucket(readTx, "c"))
require.NotNil(t, db.GetBucket(readTx, "new"))
require.Nil(t, db.GetBucket(readTx, "c"))
require.NotNil(t, n2.GetBucket(readTx, "c"))

readTx.Rollback()
Expand Down
3 changes: 0 additions & 3 deletions errors.go
Expand Up @@ -43,9 +43,6 @@ var (
// ErrNotInTransaction is returned when trying to rollback or commit when not in transaction.
ErrNotInTransaction = errors.New("not in transaction")

// ErrUnAddressable is returned when a struct or an exported field of a struct is unaddressable
ErrUnAddressable = errors.New("unaddressable value")

// ErrIncompatibleValue is returned when trying to set a value with a different type than the chosen field
ErrIncompatibleValue = errors.New("incompatible value")

Expand Down
14 changes: 7 additions & 7 deletions examples_test.go
Expand Up @@ -10,15 +10,16 @@ import (
"time"

"github.com/asdine/storm"
"github.com/boltdb/bolt"
"github.com/asdine/storm/codec/gob"
"github.com/coreos/bbolt"
)

func ExampleDB_Save() {
dir, _ := ioutil.TempDir(os.TempDir(), "storm")
defer os.RemoveAll(dir)

type User struct {
ID int `storm:"id"`
ID int `storm:"id,increment"` // the increment tag will auto-increment integer IDs without existing values.
Group string `storm:"index"`
Email string `storm:"unique"`
Name string
Expand All @@ -27,8 +28,7 @@ func ExampleDB_Save() {
}

// Open takes an optional list of options as the last argument.
// AutoIncrement will auto-increment integer IDs without existing values.
db, _ := storm.Open(filepath.Join(dir, "storm.db"), storm.AutoIncrement())
db, _ := storm.Open(filepath.Join(dir, "storm.db"), storm.Codec(gob.Codec))
defer db.Close()

user := User{
Expand Down Expand Up @@ -497,7 +497,7 @@ func ExampleNode_RangeScan() {
}

type User struct {
ID int `storm:"id"`
ID int `storm:"id,increment"`
Group string `storm:"index"`
Email string `storm:"unique"`
Name string
Expand All @@ -506,7 +506,7 @@ type User struct {
}

type Account struct {
ID int `storm:"id"`
ID int `storm:"id,increment"`
Amount int64 // amount in cents
}

Expand All @@ -517,7 +517,7 @@ type Note struct {

func prepareDB() (string, *storm.DB) {
dir, _ := ioutil.TempDir(os.TempDir(), "storm")
db, _ := storm.Open(filepath.Join(dir, "storm.db"), storm.AutoIncrement())
db, _ := storm.Open(filepath.Join(dir, "storm.db"))

for i, name := range []string{"John", "Eric", "Dilbert"} {
email := strings.ToLower(name + "@provider.com")
Expand Down
2 changes: 1 addition & 1 deletion extract.go
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/asdine/storm/index"
"github.com/boltdb/bolt"
"github.com/coreos/bbolt"
)

// Storm tags
Expand Down
6 changes: 3 additions & 3 deletions finder.go
Expand Up @@ -6,11 +6,11 @@ import (

"github.com/asdine/storm/index"
"github.com/asdine/storm/q"
"github.com/boltdb/bolt"
"github.com/coreos/bbolt"
)

// A Finder can fetch types from BoltDB
type Finder interface {
// A finder can fetch types from BoltDB
type finder interface {
// One returns one record by the specified index
One(fieldName string, value interface{}, to interface{}) error

Expand Down
8 changes: 4 additions & 4 deletions finder_test.go
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
"time"

"github.com/boltdb/bolt"
"github.com/coreos/bbolt"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -110,7 +110,7 @@ func TestFind(t *testing.T) {
}

func TestFindNil(t *testing.T) {
db, cleanup := createDB(t, AutoIncrement())
db, cleanup := createDB(t)
defer cleanup()

type User struct {
Expand Down Expand Up @@ -150,11 +150,11 @@ func TestFindNil(t *testing.T) {
}

func TestFindIntIndex(t *testing.T) {
db, cleanup := createDB(t, AutoIncrement())
db, cleanup := createDB(t)
defer cleanup()

type Score struct {
ID int
ID int `storm:"increment"`
Score uint64 `storm:"index"`
}

Expand Down
2 changes: 1 addition & 1 deletion index/list.go
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"

"github.com/asdine/storm/internal"
"github.com/boltdb/bolt"
"github.com/coreos/bbolt"
)

// NewListIndex loads a ListIndex
Expand Down

0 comments on commit dbd3772

Please sign in to comment.