func ExampleTx_CopyFile() { // Open the database. db, _ := bolt.Open(tempfile(), 0666, nil) defer os.Remove(db.Path()) defer db.Close() // Create a bucket and a key. db.Update(func(tx *bolt.Tx) error { tx.CreateBucket([]byte("widgets")) tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar")) return nil }) // Copy the database to another file. toFile := tempfile() db.View(func(tx *bolt.Tx) error { return tx.CopyFile(toFile, 0666) }) defer os.Remove(toFile) // Open the cloned database. db2, _ := bolt.Open(toFile, 0666, nil) defer db2.Close() // Ensure that the key exists in the copy. db2.View(func(tx *bolt.Tx) error { value := tx.Bucket([]byte("widgets")).Get([]byte("foo")) fmt.Printf("The value for 'foo' in the clone is: %s\n", value) return nil }) // Output: // The value for 'foo' in the clone is: bar }
// Ensure that opening an already open database file will wait until its closed. func TestOpen_Wait(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("timeout not supported on windows") } if runtime.GOOS == "solaris" { t.Skip("solaris fcntl locks don't support intra-process locking") } path := tempfile() defer os.Remove(path) // Open a data file. db0, err := bolt.Open(path, 0666, nil) assert(t, db0 != nil, "") ok(t, err) // Close it in just a bit. time.AfterFunc(100*time.Millisecond, func() { db0.Close() }) // Attempt to open the database again. start := time.Now() db1, err := bolt.Open(path, 0666, &bolt.Options{Timeout: 200 * time.Millisecond}) assert(t, db1 != nil, "") ok(t, err) assert(t, time.Since(start) > 100*time.Millisecond, "") }
// Ensure that a re-opened database is consistent. func TestOpen_Check(t *testing.T) { path := tempfile() defer os.Remove(path) db, err := bolt.Open(path, 0666, nil) ok(t, err) ok(t, db.View(func(tx *bolt.Tx) error { return <-tx.Check() })) db.Close() db, err = bolt.Open(path, 0666, nil) ok(t, err) ok(t, db.View(func(tx *bolt.Tx) error { return <-tx.Check() })) db.Close() }
// Ensure that a database that is too small returns an error. func TestDB_Open_FileTooSmall(t *testing.T) { path := tempfile() defer os.Remove(path) db, err := bolt.Open(path, 0666, nil) ok(t, err) db.Close() // corrupt the database ok(t, os.Truncate(path, int64(os.Getpagesize()))) db, err = bolt.Open(path, 0666, nil) equals(t, errors.New("file size too small"), err) }
func (s Store) Open(dbPath string, cfg *config.Config) (driver.IDB, error) { os.MkdirAll(dbPath, 0755) name := path.Join(dbPath, "ledis_bolt.db") db := new(DB) var err error db.path = name db.cfg = cfg db.db, err = bolt.Open(name, 0600, nil) if err != nil { return nil, err } var tx *bolt.Tx tx, err = db.db.Begin(true) if err != nil { return nil, err } _, err = tx.CreateBucketIfNotExists(bucketName) if err != nil { tx.Rollback() return nil, err } if err = tx.Commit(); err != nil { return nil, err } return db, nil }
func ExampleBucket_ForEach() { // Open the database. db, _ := bolt.Open(tempfile(), 0666, nil) defer os.Remove(db.Path()) defer db.Close() // Insert data into a bucket. db.Update(func(tx *bolt.Tx) error { tx.CreateBucket([]byte("animals")) b := tx.Bucket([]byte("animals")) b.Put([]byte("dog"), []byte("fun")) b.Put([]byte("cat"), []byte("lame")) b.Put([]byte("liger"), []byte("awesome")) // Iterate over items in sorted key order. b.ForEach(func(k, v []byte) error { fmt.Printf("A %s is %s.\n", k, v) return nil }) return nil }) // Output: // A cat is lame. // A dog is fun. // A liger is awesome. }
func ExampleDB_View() { // Open the database. db, _ := bolt.Open(tempfile(), 0666, nil) defer os.Remove(db.Path()) defer db.Close() // Insert data into a bucket. db.Update(func(tx *bolt.Tx) error { tx.CreateBucket([]byte("people")) b := tx.Bucket([]byte("people")) b.Put([]byte("john"), []byte("doe")) b.Put([]byte("susy"), []byte("que")) return nil }) // Access data from within a read-only transactional block. db.View(func(tx *bolt.Tx) error { v := tx.Bucket([]byte("people")).Get([]byte("john")) fmt.Printf("John's last name is %s.\n", v) return nil }) // Output: // John's last name is doe. }
func ExampleBucket_Put() { // Open the database. db, _ := bolt.Open(tempfile(), 0666, nil) defer os.Remove(db.Path()) defer db.Close() // Start a write transaction. db.Update(func(tx *bolt.Tx) error { // Create a bucket. tx.CreateBucket([]byte("widgets")) // Set the value "bar" for the key "foo". tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar")) return nil }) // Read value back in a different read-only transaction. db.View(func(tx *bolt.Tx) error { value := tx.Bucket([]byte("widgets")).Get([]byte("foo")) fmt.Printf("The value of 'foo' is: %s\n", value) return nil }) // Output: // The value of 'foo' is: bar }
func New() *Service { config.Init("f", "tester", "collect.conf") var conf TesterConfig if err := config.Load(&conf); err != nil { log.Fatal("config.Load failed:", err) return nil } //init mongo var colls collection _, err := mgoutil.Open(&colls, &mgoutil.Config{Host: "127.0.0.1", DB: "pandora_test"}) if err != nil { log.Fatal("open mongo fail:", err) } colls.Alert.RemoveAll(M{}) colls.Repo.RemoveAll(M{}) colls.RepoConfig.RemoveAll(M{}) c := NewInfluxClient() db, err := bolt.Open("RepoConfigDB", 0600, nil) if err != nil { log.Fatal("RepoConfigDB open fail", err) } //init service srv := &Service{colls: colls, influxClient: c, done: make(chan bool), db: db} return srv }
func ExampleDB_Begin_ReadOnly() { // Open the database. db, _ := bolt.Open(tempfile(), 0666, nil) defer os.Remove(db.Path()) defer db.Close() // Create a bucket. db.Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucket([]byte("widgets")) return err }) // Create several keys in a transaction. tx, _ := db.Begin(true) b := tx.Bucket([]byte("widgets")) b.Put([]byte("john"), []byte("blue")) b.Put([]byte("abby"), []byte("red")) b.Put([]byte("zephyr"), []byte("purple")) tx.Commit() // Iterate over the values in sorted key order. tx, _ = db.Begin(false) c := tx.Bucket([]byte("widgets")).Cursor() for k, v := c.First(); k != nil; k, v = c.Next() { fmt.Printf("%s likes %s\n", k, v) } tx.Rollback() // Output: // abby likes red // john likes blue // zephyr likes purple }
func ExampleTx_Rollback() { // Open the database. db, _ := bolt.Open(tempfile(), 0666, nil) defer os.Remove(db.Path()) defer db.Close() // Create a bucket. db.Update(func(tx *bolt.Tx) error { _, err := tx.CreateBucket([]byte("widgets")) return err }) // Set a value for a key. db.Update(func(tx *bolt.Tx) error { return tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar")) }) // Update the key but rollback the transaction so it never saves. tx, _ := db.Begin(true) b := tx.Bucket([]byte("widgets")) b.Put([]byte("foo"), []byte("baz")) tx.Rollback() // Ensure that our original value is still set. db.View(func(tx *bolt.Tx) error { value := tx.Bucket([]byte("widgets")).Get([]byte("foo")) fmt.Printf("The value for 'foo' is still: %s\n", value) return nil }) // Output: // The value for 'foo' is still: bar }
// NewTestDB returns a new instance of TestDB. func NewTestDB() *TestDB { db, err := bolt.Open(tempfile(), 0666, nil) if err != nil { panic("cannot open db: " + err.Error()) } return &TestDB{db} }
func ExampleDB_Update() { // Open the database. db, _ := bolt.Open(tempfile(), 0666, nil) defer os.Remove(db.Path()) defer db.Close() // Execute several commands within a write transaction. err := db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket([]byte("widgets")) if err != nil { return err } if err := b.Put([]byte("foo"), []byte("bar")); err != nil { return err } return nil }) // If our transactional block didn't return an error then our data is saved. if err == nil { db.View(func(tx *bolt.Tx) error { value := tx.Bucket([]byte("widgets")).Get([]byte("foo")) fmt.Printf("The value of 'foo' is: %s\n", value) return nil }) } // Output: // The value of 'foo' is: bar }
func (s *Schedule) Init(c *conf.Conf) error { var err error s.Conf = c s.AlertStatuses = make(map[string]*AlertStatus) s.Silence = make(map[string]*Silence) s.Group = make(map[time.Time]expr.AlertKeys) s.Incidents = make(map[uint64]*Incident) s.pendingUnknowns = make(map[*conf.Notification][]*State) s.status = make(States) s.Search = search.NewSearch() s.LastCheck = time.Now() s.ctx = &checkContext{time.Now(), cache.New(0)} if s.DataAccess == nil { if c.RedisHost != "" { s.DataAccess = database.NewDataAccess(c.RedisHost, true) } else { bind := "127.0.0.1:9565" _, err := database.StartLedis(c.LedisDir, bind) if err != nil { return err } s.DataAccess = database.NewDataAccess(bind, false) } } if c.StateFile != "" { s.db, err = bolt.Open(c.StateFile, 0600, nil) if err != nil { return err } } return nil }
func (s *Schedule) Init(c *conf.Conf) error { //initialize all variables and collections so they are ready to use. //this will be called once at app start, and also every time the rule //page runs, so be careful not to spawn long running processes that can't //be avoided. var err error s.Conf = c s.Group = make(map[time.Time]models.AlertKeys) s.pendingUnknowns = make(map[*conf.Notification][]*models.IncidentState) s.lastLogTimes = make(map[models.AlertKey]time.Time) s.LastCheck = time.Now() s.ctx = &checkContext{time.Now(), cache.New(0)} if s.DataAccess == nil { if c.RedisHost != "" { s.DataAccess = database.NewDataAccess(c.RedisHost, true, c.RedisDb, c.RedisPassword) } else { bind := "127.0.0.1:9565" _, err := database.StartLedis(c.LedisDir, bind) if err != nil { return err } s.DataAccess = database.NewDataAccess(bind, false, 0, "") } } if s.Search == nil { s.Search = search.NewSearch(s.DataAccess) } if c.StateFile != "" { s.db, err = bolt.Open(c.StateFile, 0600, nil) if err != nil { return err } } return nil }
// Ensure that the database returns an error if the file handle cannot be open. func TestDB_Open_FileError(t *testing.T) { path := tempfile() defer os.Remove(path) _, err := bolt.Open(path+"/youre-not-my-real-parent", 0666, nil) assert(t, err.(*os.PathError) != nil, "") equals(t, path+"/youre-not-my-real-parent", err.(*os.PathError).Path) equals(t, "open", err.(*os.PathError).Op) }
// Ensure that a database can be opened without error. func TestOpen(t *testing.T) { path := tempfile() defer os.Remove(path) db, err := bolt.Open(path, 0666, nil) assert(t, db != nil, "") ok(t, err) equals(t, db.Path(), path) ok(t, db.Close()) }
func ExampleDB_Batch() { // Open the database. db, _ := bolt.Open(tempfile(), 0666, nil) defer os.Remove(db.Path()) defer db.Close() // Start our web server count := counter{db} srv := httptest.NewServer(count) defer srv.Close() // Decrease the batch size to make things more interesting. db.MaxBatchSize = 3 // Get every path multiple times concurrently. const clients = 10 paths := []string{ "/foo", "/bar", "/baz", "/quux", "/thud", "/xyzzy", } errors := make(chan error, clients) for i := 0; i < clients; i++ { go func(id int) { errors <- client(id, srv.URL, paths) }(i) } // Check all responses to make sure there's no error. for i := 0; i < clients; i++ { if err := <-errors; err != nil { fmt.Printf("client error: %v", err) return } } // Check the final result db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("hits")) c := b.Cursor() for k, v := c.First(); k != nil; k, v = c.Next() { fmt.Printf("hits to %s: %d\n", k, decode(v)) } return nil }) // Output: // hits to /bar: 10 // hits to /baz: 10 // hits to /foo: 10 // hits to /quux: 10 // hits to /thud: 10 // hits to /xyzzy: 10 }
// Ensure that opening an already open database file will timeout. func TestOpen_Timeout(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("timeout not supported on windows") } path := tempfile() defer os.Remove(path) // Open a data file. db0, err := bolt.Open(path, 0666, nil) assert(t, db0 != nil, "") ok(t, err) // Attempt to open the database again. start := time.Now() db1, err := bolt.Open(path, 0666, &bolt.Options{Timeout: 100 * time.Millisecond}) assert(t, db1 == nil, "") equals(t, bolt.ErrTimeout, err) assert(t, time.Since(start) > 100*time.Millisecond, "") db0.Close() }
// Ensure that opening a database beyond the max step size does not increase its size. // https://github.com/boltdb/bolt/issues/303 func TestOpen_Size_Large(t *testing.T) { if testing.Short() { t.Skip("short mode") } // Open a data file. db := NewTestDB() path := db.Path() defer db.Close() // Insert until we get above the minimum 4MB size. var index uint64 for i := 0; i < 10000; i++ { ok(t, db.Update(func(tx *bolt.Tx) error { b, _ := tx.CreateBucketIfNotExists([]byte("data")) for j := 0; j < 1000; j++ { ok(t, b.Put(u64tob(index), make([]byte, 50))) index++ } return nil })) } // Close database and grab the size. db.DB.Close() sz := fileSize(path) if sz == 0 { t.Fatalf("unexpected new file size: %d", sz) } else if sz < (1 << 30) { t.Fatalf("expected larger initial size: %d", sz) } // Reopen database, update, and check size again. db0, err := bolt.Open(path, 0666, nil) ok(t, err) ok(t, db0.Update(func(tx *bolt.Tx) error { return tx.Bucket([]byte("data")).Put([]byte{0}, []byte{0}) })) ok(t, db0.Close()) newSz := fileSize(path) if newSz == 0 { t.Fatalf("unexpected new file size: %d", newSz) } // Compare the original size with the new size. if sz != newSz { t.Fatalf("unexpected file growth: %d => %d", sz, newSz) } }
func (s *Schedule) Init(c *conf.Conf) error { var err error s.Conf = c s.Silence = make(map[string]*Silence) s.Group = make(map[time.Time]expr.AlertKeys) s.Metadata = make(map[metadata.Metakey]*Metavalue) s.Incidents = make(map[uint64]*Incident) s.status = make(States) s.Search = search.NewSearch() if c.StateFile != "" { s.db, err = bolt.Open(c.StateFile, 0600, nil) if err != nil { return err } } return nil }
// NewEngine returns an instance of an engine based on its format. // If the path does not exist then the DefaultFormat is used. func NewEngine(path string, walPath string, options EngineOptions) (Engine, error) { // Create a new engine if _, err := os.Stat(path); os.IsNotExist(err) { return newEngineFuncs[options.EngineVersion](path, walPath, options), nil } // Only bolt-based backends are currently supported so open it and check the format. var format string if err := func() error { db, err := bolt.Open(path, 0666, &bolt.Options{Timeout: 1 * time.Second}) if err != nil { return err } defer db.Close() return db.View(func(tx *bolt.Tx) error { // Retrieve the meta bucket. b := tx.Bucket([]byte("meta")) // If no format is specified then it must be an original b1 database. if b == nil { format = "b1" return nil } // Save the format. format = string(b.Get([]byte("format"))) if format == "v1" { format = "b1" } return nil }) }(); err != nil { return nil, err } // Lookup engine by format. fn := newEngineFuncs[format] if fn == nil { return nil, fmt.Errorf("invalid engine format: %q", format) } return fn(path, walPath, options), nil }
func TestNewBoltStore(t *testing.T) { fh, err := ioutil.TempFile("", "bolt") if err != nil { t.Fatalf("err: %s", err) } os.Remove(fh.Name()) defer os.Remove(fh.Name()) // Successfully creates and returns a store store, err := NewBoltStore(fh.Name()) if err != nil { t.Fatalf("err: %s", err) } // Ensure the file was created if store.path != fh.Name() { t.Fatalf("unexpected file path %q", store.path) } if _, err := os.Stat(fh.Name()); err != nil { t.Fatalf("err: %s", err) } // Close the store so we can open again if err := store.Close(); err != nil { t.Fatalf("err: %s", err) } // Ensure our tables were created db, err := bolt.Open(fh.Name(), dbFileMode, nil) if err != nil { t.Fatalf("err: %s", err) } tx, err := db.Begin(true) if err != nil { t.Fatalf("err: %s", err) } if _, err := tx.CreateBucket([]byte(dbLogs)); err != bolt.ErrBucketExists { t.Fatalf("bad: %v", err) } if _, err := tx.CreateBucket([]byte(dbConf)); err != bolt.ErrBucketExists { t.Fatalf("bad: %v", err) } }
func (s *Schedule) Init(c *conf.Conf) error { var err error s.Conf = c s.Silence = make(map[string]*Silence) s.Group = make(map[time.Time]expr.AlertKeys) s.Metadata = make(map[metadata.Metakey]*Metavalue) s.Incidents = make(map[uint64]*Incident) s.pendingUnknowns = make(map[*conf.Notification][]*State) s.status = make(States) s.Search = search.NewSearch() s.LastCheck = time.Now() s.ctx = &checkContext{time.Now(), cache.New(0)} if c.StateFile != "" { s.db, err = bolt.Open(c.StateFile, 0600, nil) if err != nil { return err } } return nil }
// NewBoltStore takes a file path and returns a connected Raft backend. func NewBoltStore(path string) (*BoltStore, error) { // Try to connect handle, err := bolt.Open(path, dbFileMode, nil) if err != nil { return nil, err } // Create the new store store := &BoltStore{ conn: handle, path: path, } // Set up our buckets if err := store.initialize(); err != nil { store.Close() return nil, err } return store, nil }
func ExampleBucket_Delete() { // Open the database. db, _ := bolt.Open(tempfile(), 0666, nil) defer os.Remove(db.Path()) defer db.Close() // Start a write transaction. db.Update(func(tx *bolt.Tx) error { // Create a bucket. tx.CreateBucket([]byte("widgets")) b := tx.Bucket([]byte("widgets")) // Set the value "bar" for the key "foo". b.Put([]byte("foo"), []byte("bar")) // Retrieve the key back from the database and verify it. value := b.Get([]byte("foo")) fmt.Printf("The value of 'foo' was: %s\n", value) return nil }) // Delete the key in a different write transaction. db.Update(func(tx *bolt.Tx) error { return tx.Bucket([]byte("widgets")).Delete([]byte("foo")) }) // Retrieve the key again. db.View(func(tx *bolt.Tx) error { value := tx.Bucket([]byte("widgets")).Get([]byte("foo")) if value == nil { fmt.Printf("The value of 'foo' is now: nil\n") } return nil }) // Output: // The value of 'foo' was: bar // The value of 'foo' is now: nil }
func ExampleCursor_reverse() { // Open the database. db, _ := bolt.Open(tempfile(), 0666, nil) defer os.Remove(db.Path()) defer db.Close() // Start a read-write transaction. db.Update(func(tx *bolt.Tx) error { // Create a new bucket. tx.CreateBucket([]byte("animals")) // Insert data into a bucket. b := tx.Bucket([]byte("animals")) b.Put([]byte("dog"), []byte("fun")) b.Put([]byte("cat"), []byte("lame")) b.Put([]byte("liger"), []byte("awesome")) // Create a cursor for iteration. c := b.Cursor() // Iterate over items in reverse sorted key order. This starts // from the last key/value pair and updates the k/v variables to // the previous key/value on each iteration. // // The loop finishes at the beginning of the cursor when a nil key // is returned. for k, v := c.Last(); k != nil; k, v = c.Prev() { fmt.Printf("A %s is %s.\n", k, v) } return nil }) // Output: // A liger is awesome. // A dog is fun. // A cat is lame. }
// Ensure that the database can be copied to a file path. func TestTx_CopyFile(t *testing.T) { db := NewTestDB() defer db.Close() var dest = tempfile() db.Update(func(tx *bolt.Tx) error { tx.CreateBucket([]byte("widgets")) tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte("bar")) tx.Bucket([]byte("widgets")).Put([]byte("baz"), []byte("bat")) return nil }) ok(t, db.View(func(tx *bolt.Tx) error { return tx.CopyFile(dest, 0600) })) db2, err := bolt.Open(dest, 0600, nil) ok(t, err) defer db2.Close() db2.View(func(tx *bolt.Tx) error { equals(t, []byte("bar"), tx.Bucket([]byte("widgets")).Get([]byte("foo"))) equals(t, []byte("bat"), tx.Bucket([]byte("widgets")).Get([]byte("baz"))) return nil }) }
// Ensure that opening a database does not increase its size. // https://github.com/boltdb/bolt/issues/291 func TestOpen_Size(t *testing.T) { // Open a data file. db := NewTestDB() path := db.Path() defer db.Close() // Insert until we get above the minimum 4MB size. ok(t, db.Update(func(tx *bolt.Tx) error { b, _ := tx.CreateBucketIfNotExists([]byte("data")) for i := 0; i < 10000; i++ { ok(t, b.Put([]byte(fmt.Sprintf("%04d", i)), make([]byte, 1000))) } return nil })) // Close database and grab the size. db.DB.Close() sz := fileSize(path) if sz == 0 { t.Fatalf("unexpected new file size: %d", sz) } // Reopen database, update, and check size again. db0, err := bolt.Open(path, 0666, nil) ok(t, err) ok(t, db0.Update(func(tx *bolt.Tx) error { return tx.Bucket([]byte("data")).Put([]byte{0}, []byte{0}) })) ok(t, db0.Close()) newSz := fileSize(path) if newSz == 0 { t.Fatalf("unexpected new file size: %d", newSz) } // Compare the original size with the new size. if sz != newSz { t.Fatalf("unexpected file growth: %d => %d", sz, newSz) } }
// Ensure that a database can be opened in read-only mode by multiple processes // and that a database can not be opened in read-write mode and in read-only // mode at the same time. func TestOpen_ReadOnly(t *testing.T) { if runtime.GOOS == "solaris" { t.Skip("solaris fcntl locks don't support intra-process locking") } bucket, key, value := []byte(`bucket`), []byte(`key`), []byte(`value`) path := tempfile() defer os.Remove(path) // Open in read-write mode. db, err := bolt.Open(path, 0666, nil) ok(t, db.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucket(bucket) if err != nil { return err } return b.Put(key, value) })) assert(t, db != nil, "") assert(t, !db.IsReadOnly(), "") ok(t, err) ok(t, db.Close()) // Open in read-only mode. db0, err := bolt.Open(path, 0666, &bolt.Options{ReadOnly: true}) ok(t, err) defer db0.Close() // Opening in read-write mode should return an error. _, err = bolt.Open(path, 0666, &bolt.Options{Timeout: time.Millisecond * 100}) assert(t, err != nil, "") // And again (in read-only mode). db1, err := bolt.Open(path, 0666, &bolt.Options{ReadOnly: true}) ok(t, err) defer db1.Close() // Verify both read-only databases are accessible. for _, db := range []*bolt.DB{db0, db1} { // Verify is is in read only mode indeed. assert(t, db.IsReadOnly(), "") // Read-only databases should not allow updates. assert(t, bolt.ErrDatabaseReadOnly == db.Update(func(*bolt.Tx) error { panic(`should never get here`) }), "") // Read-only databases should not allow beginning writable txns. _, err = db.Begin(true) assert(t, bolt.ErrDatabaseReadOnly == err, "") // Verify the data. ok(t, db.View(func(tx *bolt.Tx) error { b := tx.Bucket(bucket) if b == nil { return fmt.Errorf("expected bucket `%s`", string(bucket)) } got := string(b.Get(key)) expected := string(value) if got != expected { return fmt.Errorf("expected `%s`, got `%s`", expected, got) } return nil })) } }