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

reindex error: bucket not found #264

Open
wshelly opened this issue Feb 21, 2020 · 1 comment
Open

reindex error: bucket not found #264

wshelly opened this issue Feb 21, 2020 · 1 comment

Comments

@wshelly
Copy link

wshelly commented Feb 21, 2020

import "github.com/asdine/storm/v3"

when adding a new index to an existing struct, if data store with db.Save(xxx), the db.Reindex() work well, but when my data store with db.from("xxxx","1", "1").Save(), the db.from("xxxx","1", "1").Reindex() will got error: bucket not found

@wshelly
Copy link
Author

wshelly commented Feb 22, 2020

This is an example:
One program save data

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/asdine/storm/v3"
	bolt "go.etcd.io/bbolt"
)

type Record struct {
	ID    int    `storm:"id"`
	Name  string `storm:"index"`
	Value string
	Seq   int64
}

func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	db, err := storm.Open("1.db", storm.BoltOptions(0600, &bolt.Options{Timeout: 1 * time.Second}))
	if err != nil {
		log.Panicln(err)
	}
	defer db.Close()
	count := 10
	tx, _ := db.From("a", "b").Begin(true)
	for n := 0; n < count; n++ {
		tx.Save(&Record{
			ID:    n + 1,
			Name:  fmt.Sprintf("name_%d", n+1),
			Value: fmt.Sprintf("value_%d", n+1),
			Seq:   int64(n + 101),
		})
	}
	tx.Commit()
	var results []Record
	db.From("a", "b").All(&results)
	for _, v := range results {
		log.Println(v)
	}

	var result Record
	err = db.From("a", "b").One("Seq", 102, &result)
	if err == nil {
		log.Printf("✔️ find record: %+v", result)
	} else {
		log.Printf("❌ error: %s", err.Error())
	}
}

another program reindex:

package main

import (
	"log"
	"time"

	"github.com/asdine/storm/v3"
	bolt "go.etcd.io/bbolt"
)

type Record struct {
	ID    int    `storm:"id"`
	Name  string `storm:"index"`
	Value string
	Seq   int64 `storm:"index"`
}

func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	db, err := storm.Open("1.db", storm.BoltOptions(0600, &bolt.Options{Timeout: 1 * time.Second}))
	if err != nil {
		log.Panicln(err)
	}
	defer db.Close()

	//ReIndex return error  bucket not found
	log.Println(db.From("a", "b").ReIndex(&Record{}))

	var results []Record
	db.From("a", "b").All(&results)
	for _, v := range results {
		log.Println(v)
	}
	var result Record
	err = db.From("a", "b").One("Seq", 102, &result)
	if err == nil {
		log.Printf("✔️ find record: %+v", result)
	} else {
		log.Printf("❌ error: %s", err.Error())
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant