mirror of
https://github.com/asdine/storm.git
synced 2025-11-03 11:01:51 +08:00
Removing AutoIncrement option
This commit is contained in:
@@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func BenchmarkFindWithIndex(b *testing.B) {
|
func BenchmarkFindWithIndex(b *testing.B) {
|
||||||
db, cleanup := createDB(b, AutoIncrement())
|
db, cleanup := createDB(b)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
var users []User
|
var users []User
|
||||||
@@ -37,7 +37,7 @@ func BenchmarkFindWithIndex(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkFindWithoutIndex(b *testing.B) {
|
func BenchmarkFindWithoutIndex(b *testing.B) {
|
||||||
db, cleanup := createDB(b, AutoIncrement())
|
db, cleanup := createDB(b)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
var users []User
|
var users []User
|
||||||
@@ -67,7 +67,7 @@ func BenchmarkFindWithoutIndex(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkOneWithIndex(b *testing.B) {
|
func BenchmarkOneWithIndex(b *testing.B) {
|
||||||
db, cleanup := createDB(b, AutoIncrement())
|
db, cleanup := createDB(b)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
var u User
|
var u User
|
||||||
@@ -89,7 +89,7 @@ func BenchmarkOneWithIndex(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkOneByID(b *testing.B) {
|
func BenchmarkOneByID(b *testing.B) {
|
||||||
db, cleanup := createDB(b, AutoIncrement())
|
db, cleanup := createDB(b)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
@@ -120,7 +120,7 @@ func BenchmarkOneByID(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkOneWithoutIndex(b *testing.B) {
|
func BenchmarkOneWithoutIndex(b *testing.B) {
|
||||||
db, cleanup := createDB(b, AutoIncrement())
|
db, cleanup := createDB(b)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
var u User
|
var u User
|
||||||
@@ -142,7 +142,7 @@ func BenchmarkOneWithoutIndex(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkSave(b *testing.B) {
|
func BenchmarkSave(b *testing.B) {
|
||||||
db, cleanup := createDB(b, AutoIncrement())
|
db, cleanup := createDB(b)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
w := User{Name: "John"}
|
w := User{Name: "John"}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/asdine/storm"
|
"github.com/asdine/storm"
|
||||||
|
"github.com/asdine/storm/codec/gob"
|
||||||
"github.com/boltdb/bolt"
|
"github.com/boltdb/bolt"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -18,7 +19,7 @@ func ExampleDB_Save() {
|
|||||||
defer os.RemoveAll(dir)
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
ID int `storm:"id"`
|
ID int `storm:"id,increment"`
|
||||||
Group string `storm:"index"`
|
Group string `storm:"index"`
|
||||||
Email string `storm:"unique"`
|
Email string `storm:"unique"`
|
||||||
Name string
|
Name string
|
||||||
@@ -28,7 +29,7 @@ func ExampleDB_Save() {
|
|||||||
|
|
||||||
// Open takes an optional list of options as the last argument.
|
// Open takes an optional list of options as the last argument.
|
||||||
// AutoIncrement will auto-increment integer IDs without existing values.
|
// 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()
|
defer db.Close()
|
||||||
|
|
||||||
user := User{
|
user := User{
|
||||||
@@ -497,7 +498,7 @@ func ExampleNode_RangeScan() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
ID int `storm:"id"`
|
ID int `storm:"id,increment"`
|
||||||
Group string `storm:"index"`
|
Group string `storm:"index"`
|
||||||
Email string `storm:"unique"`
|
Email string `storm:"unique"`
|
||||||
Name string
|
Name string
|
||||||
@@ -506,7 +507,7 @@ type User struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Account struct {
|
type Account struct {
|
||||||
ID int `storm:"id"`
|
ID int `storm:"id,increment"`
|
||||||
Amount int64 // amount in cents
|
Amount int64 // amount in cents
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -517,7 +518,7 @@ type Note struct {
|
|||||||
|
|
||||||
func prepareDB() (string, *storm.DB) {
|
func prepareDB() (string, *storm.DB) {
|
||||||
dir, _ := ioutil.TempDir(os.TempDir(), "storm")
|
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"} {
|
for i, name := range []string{"John", "Eric", "Dilbert"} {
|
||||||
email := strings.ToLower(name + "@provider.com")
|
email := strings.ToLower(name + "@provider.com")
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ func TestFind(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFindNil(t *testing.T) {
|
func TestFindNil(t *testing.T) {
|
||||||
db, cleanup := createDB(t, AutoIncrement())
|
db, cleanup := createDB(t)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
@@ -150,11 +150,11 @@ func TestFindNil(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFindIntIndex(t *testing.T) {
|
func TestFindIntIndex(t *testing.T) {
|
||||||
db, cleanup := createDB(t, AutoIncrement())
|
db, cleanup := createDB(t)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
type Score struct {
|
type Score struct {
|
||||||
ID int
|
ID int `storm:"increment"`
|
||||||
Score uint64 `storm:"index"`
|
Score uint64 `storm:"index"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,15 +33,6 @@ func Batch() func(*DB) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// AutoIncrement used to enable bolt.NextSequence on empty integer ids.
|
|
||||||
// Deprecated: Set the increment tag to the id field instead.
|
|
||||||
func AutoIncrement() func(*DB) error {
|
|
||||||
return func(d *DB) error {
|
|
||||||
d.autoIncrement = true
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Root used to set the root bucket. See also the From method.
|
// Root used to set the root bucket. See also the From method.
|
||||||
func Root(root ...string) func(*DB) error {
|
func Root(root ...string) func(*DB) error {
|
||||||
return func(d *DB) error {
|
return func(d *DB) error {
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ func ExampleRe() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
ID int `storm:"id"`
|
ID int `storm:"id,increment"`
|
||||||
Group string `storm:"index"`
|
Group string `storm:"index"`
|
||||||
Email string `storm:"unique"`
|
Email string `storm:"unique"`
|
||||||
Name string
|
Name string
|
||||||
@@ -47,7 +47,7 @@ type User struct {
|
|||||||
|
|
||||||
func prepareDB() (string, *storm.DB) {
|
func prepareDB() (string, *storm.DB) {
|
||||||
dir, _ := ioutil.TempDir(os.TempDir(), "storm")
|
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", "Norm", "Donald", "Eric", "Dilbert"} {
|
for i, name := range []string{"John", "Norm", "Donald", "Eric", "Dilbert"} {
|
||||||
email := strings.ToLower(name + "@provider.com")
|
email := strings.ToLower(name + "@provider.com")
|
||||||
|
|||||||
@@ -10,12 +10,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Score struct {
|
type Score struct {
|
||||||
ID int
|
ID int `storm:"increment"`
|
||||||
Value int
|
Value int
|
||||||
}
|
}
|
||||||
|
|
||||||
func prepareScoreDB(t *testing.T) (*DB, func()) {
|
func prepareScoreDB(t *testing.T) (*DB, func()) {
|
||||||
db, cleanup := createDB(t, AutoIncrement())
|
db, cleanup := createDB(t)
|
||||||
|
|
||||||
for i := 0; i < 20; i++ {
|
for i := 0; i < 20; i++ {
|
||||||
err := db.Save(&Score{
|
err := db.Save(&Score{
|
||||||
@@ -492,7 +492,7 @@ func TestSelectCount(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSelectRaw(t *testing.T) {
|
func TestSelectRaw(t *testing.T) {
|
||||||
db, cleanup := createDB(t, AutoIncrement(), Codec(json.Codec))
|
db, cleanup := createDB(t, Codec(json.Codec))
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
for i := 0; i < 20; i++ {
|
for i := 0; i < 20; i++ {
|
||||||
@@ -520,7 +520,7 @@ func TestSelectRaw(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSelectEach(t *testing.T) {
|
func TestSelectEach(t *testing.T) {
|
||||||
db, cleanup := createDB(t, AutoIncrement(), Codec(json.Codec))
|
db, cleanup := createDB(t, Codec(json.Codec))
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
for i := 0; i < 20; i++ {
|
for i := 0; i < 20; i++ {
|
||||||
|
|||||||
2
store.go
2
store.go
@@ -152,7 +152,7 @@ func (n *node) Save(data interface{}) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if cfg.ID.IsZero {
|
if cfg.ID.IsZero {
|
||||||
if !cfg.ID.IsInteger || (!n.s.autoIncrement && !cfg.ID.Increment) {
|
if !cfg.ID.IsInteger || !cfg.ID.Increment {
|
||||||
return ErrZeroID
|
return ErrZeroID
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -312,43 +312,8 @@ func TestSaveEmptyValues(t *testing.T) {
|
|||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSaveAutoIncrement(t *testing.T) {
|
|
||||||
db, cleanup := createDB(t, AutoIncrement())
|
|
||||||
defer cleanup()
|
|
||||||
|
|
||||||
for i := 1; i < 10; i++ {
|
|
||||||
s := SimpleUser{Name: "John"}
|
|
||||||
err := db.Save(&s)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, i, s.ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
u := UserWithUint64IDField{Name: "John"}
|
|
||||||
err := db.Save(&u)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, uint64(1), u.ID)
|
|
||||||
v := UserWithUint64IDField{}
|
|
||||||
err = db.One("ID", uint64(1), &v)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, u, v)
|
|
||||||
|
|
||||||
ui := UserWithIDField{Name: "John"}
|
|
||||||
err = db.Save(&ui)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, 1, ui.ID)
|
|
||||||
vi := UserWithIDField{}
|
|
||||||
err = db.One("ID", 1, &vi)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, ui, vi)
|
|
||||||
|
|
||||||
us := UserWithStringIDField{Name: "John"}
|
|
||||||
err = db.Save(&us)
|
|
||||||
require.Error(t, err)
|
|
||||||
require.Equal(t, ErrZeroID, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSaveIncrement(t *testing.T) {
|
func TestSaveIncrement(t *testing.T) {
|
||||||
db, cleanup := createDB(t, AutoIncrement())
|
db, cleanup := createDB(t)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
@@ -413,10 +378,9 @@ func TestSaveDifferentBucketRoot(t *testing.T) {
|
|||||||
func TestSaveEmbedded(t *testing.T) {
|
func TestSaveEmbedded(t *testing.T) {
|
||||||
db, cleanup := createDB(t)
|
db, cleanup := createDB(t)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
AutoIncrement()(db)
|
|
||||||
|
|
||||||
type Base struct {
|
type Base struct {
|
||||||
ID int `storm:"id"`
|
ID int `storm:"id,increment"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
|
|||||||
3
storm.go
3
storm.go
@@ -80,9 +80,6 @@ type DB struct {
|
|||||||
// Bolt options
|
// Bolt options
|
||||||
boltOptions *bolt.Options
|
boltOptions *bolt.Options
|
||||||
|
|
||||||
// Enable auto increment on empty integer fields
|
|
||||||
autoIncrement bool
|
|
||||||
|
|
||||||
// The root node that points to the root bucket.
|
// The root node that points to the root bucket.
|
||||||
root *node
|
root *node
|
||||||
|
|
||||||
|
|||||||
@@ -48,9 +48,8 @@ func TestNewStormWithStormOptions(t *testing.T) {
|
|||||||
defer os.RemoveAll(dir)
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
dc := new(dummyCodec)
|
dc := new(dummyCodec)
|
||||||
db1, _ := Open(filepath.Join(dir, "storm1.db"), BoltOptions(0660, &bolt.Options{Timeout: 10 * time.Second}), Codec(dc), AutoIncrement(), Root("a", "b"))
|
db1, _ := Open(filepath.Join(dir, "storm1.db"), BoltOptions(0660, &bolt.Options{Timeout: 10 * time.Second}), Codec(dc), Root("a", "b"))
|
||||||
require.Equal(t, dc, db1.Codec())
|
require.Equal(t, dc, db1.Codec())
|
||||||
require.True(t, db1.autoIncrement)
|
|
||||||
require.Equal(t, os.FileMode(0660), db1.boltMode)
|
require.Equal(t, os.FileMode(0660), db1.boltMode)
|
||||||
require.Equal(t, 10*time.Second, db1.boltOptions.Timeout)
|
require.Equal(t, 10*time.Second, db1.boltOptions.Timeout)
|
||||||
require.Equal(t, []string{"a", "b"}, db1.rootBucket)
|
require.Equal(t, []string{"a", "b"}, db1.rootBucket)
|
||||||
|
|||||||
Reference in New Issue
Block a user