当前位置: 首页>>代码示例>>Golang>>正文


Golang bolt.Open函数代码示例

本文整理汇总了Golang中github.com/boltdb/bolt.Open函数的典型用法代码示例。如果您正苦于以下问题:Golang Open函数的具体用法?Golang Open怎么用?Golang Open使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Open函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestOpen_Check

// Ensure that a re-opened database is consistent.
func TestOpen_Check(t *testing.T) {
	path := tempfile()

	db, err := bolt.Open(path, 0666, nil)
	if err != nil {
		t.Fatal(err)
	}
	if err := db.View(func(tx *bolt.Tx) error { return <-tx.Check() }); err != nil {
		t.Fatal(err)
	}
	if err := db.Close(); err != nil {
		t.Fatal(err)
	}

	db, err = bolt.Open(path, 0666, nil)
	if err != nil {
		t.Fatal(err)
	}
	if err := db.View(func(tx *bolt.Tx) error { return <-tx.Check() }); err != nil {
		t.Fatal(err)
	}
	if err := db.Close(); err != nil {
		t.Fatal(err)
	}
}
开发者ID:codedebug,项目名称:bolt,代码行数:26,代码来源:db_test.go

示例2: TestOpen_Wait

// 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, "")
}
开发者ID:resouer,项目名称:contrib,代码行数:27,代码来源:db_test.go

示例3: ExampleTx_CopyFile

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
}
开发者ID:creativeprogramming,项目名称:drive,代码行数:32,代码来源:tx_test.go

示例4: TestOpen_Wait

// Ensure that opening an already open database file will wait until its closed.
func TestOpen_Wait(t *testing.T) {
	if runtime.GOOS == "solaris" {
		t.Skip("solaris fcntl locks don't support intra-process locking")
	}

	path := tempfile()

	// Open a data file.
	db0, err := bolt.Open(path, 0666, nil)
	if err != nil {
		t.Fatal(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})
	if err != nil {
		t.Fatal(err)
	} else if time.Since(start) <= 100*time.Millisecond {
		t.Fatal("expected to wait at least timeout duration")
	}

	if err := db1.Close(); err != nil {
		t.Fatal(err)
	}
}
开发者ID:codedebug,项目名称:bolt,代码行数:30,代码来源:db_test.go

示例5: Run

// Run executes the command.
func (cmd *CompactCommand) Run(args ...string) (err error) {
	// Parse flags.
	fs := flag.NewFlagSet("", flag.ContinueOnError)
	fs.SetOutput(ioutil.Discard)
	fs.StringVar(&cmd.DstPath, "o", "", "")
	fs.Int64Var(&cmd.TxMaxSize, "tx-max-size", 65536, "")
	if err := fs.Parse(args); err == flag.ErrHelp {
		fmt.Fprintln(cmd.Stderr, cmd.Usage())
		return ErrUsage
	} else if err != nil {
		return err
	} else if cmd.DstPath == "" {
		return fmt.Errorf("output file required")
	}

	// Require database paths.
	cmd.SrcPath = fs.Arg(0)
	if cmd.SrcPath == "" {
		return ErrPathRequired
	}

	// Ensure source file exists.
	fi, err := os.Stat(cmd.SrcPath)
	if os.IsNotExist(err) {
		return ErrFileNotFound
	} else if err != nil {
		return err
	}
	initialSize := fi.Size()

	// Open source database.
	src, err := bolt.Open(cmd.SrcPath, 0444, nil)
	if err != nil {
		return err
	}
	defer src.Close()

	// Open destination database.
	dst, err := bolt.Open(cmd.DstPath, fi.Mode(), nil)
	if err != nil {
		return err
	}
	defer dst.Close()

	// Run compaction.
	if err := cmd.compact(dst, src); err != nil {
		return err
	}

	// Report stats on new size.
	fi, err = os.Stat(cmd.DstPath)
	if err != nil {
		return err
	} else if fi.Size() == 0 {
		return fmt.Errorf("zero db size")
	}
	fmt.Fprintf(cmd.Stdout, "%d -> %d bytes (gain=%.2fx)\n", initialSize, fi.Size(), float64(initialSize)/float64(fi.Size()))

	return nil
}
开发者ID:rgeronimi,项目名称:bolt,代码行数:61,代码来源:main.go

示例6: migrateDatabase

func migrateDatabase(c *gin.Context) {
	fromDB := strings.ToLower(c.DefaultQuery("from", "noneasdf"))
	toDB := strings.ToLower(c.DefaultQuery("to", "noneasdf"))
	Debug.Printf("Migrating %s to %s.\n", fromDB, toDB)
	if !exists(path.Join(RuntimeArgs.SourcePath, fromDB+".db")) {
		c.JSON(http.StatusOK, gin.H{"success": false, "message": "Can't migrate from " + fromDB + ", it does not exist."})
		return
	}
	if !exists(path.Join(RuntimeArgs.SourcePath, toDB)) {
		CopyFile(path.Join(RuntimeArgs.SourcePath, fromDB+".db"), path.Join(RuntimeArgs.SourcePath, toDB+".db"))
	} else {
		db, err := bolt.Open(path.Join(RuntimeArgs.SourcePath, fromDB+".db"), 0664, nil)
		if err != nil {
			log.Fatal(err)
		}
		defer db.Close()

		db2, err := bolt.Open(path.Join(RuntimeArgs.SourcePath, toDB+".db"), 0664, nil)
		if err != nil {
			log.Fatal(err)
		}
		defer db2.Close()

		db2.Update(func(tx *bolt.Tx) error {
			bucket, err := tx.CreateBucketIfNotExists([]byte("fingerprints"))
			if err != nil {
				return fmt.Errorf("create bucket: %s", err)
			}

			db.View(func(tx *bolt.Tx) error {
				b := tx.Bucket([]byte("fingerprints"))
				c := b.Cursor()
				for k, v := c.First(); k != nil; k, v = c.Next() {
					bucket.Put(k, v)
				}
				return nil
			})
			return nil
		})

		db2.Update(func(tx *bolt.Tx) error {
			bucket, err := tx.CreateBucketIfNotExists([]byte("fingerprints-track"))
			if err != nil {
				return fmt.Errorf("create bucket: %s", err)
			}

			db.View(func(tx *bolt.Tx) error {
				b := tx.Bucket([]byte("fingerprints-track"))
				c := b.Cursor()
				for k, v := c.First(); k != nil; k, v = c.Next() {
					bucket.Put(k, v)
				}
				return nil
			})
			return nil
		})
	}
	c.JSON(http.StatusOK, gin.H{"success": true, "message": "Successfully migrated " + fromDB + " to " + toDB})
}
开发者ID:schollz,项目名称:find,代码行数:59,代码来源:api.go

示例7: ExampleTx_CopyFile

func ExampleTx_CopyFile() {
	// Open the database.
	db, err := bolt.Open(tempfile(), 0666, nil)
	if err != nil {
		log.Fatal(err)
	}
	defer os.Remove(db.Path())

	// Create a bucket and a key.
	if 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
	}); err != nil {
		log.Fatal(err)
	}

	// Copy the database to another file.
	toFile := tempfile()
	if err := db.View(func(tx *bolt.Tx) error {
		return tx.CopyFile(toFile, 0666)
	}); err != nil {
		log.Fatal(err)
	}
	defer os.Remove(toFile)

	// Open the cloned database.
	db2, err := bolt.Open(toFile, 0666, nil)
	if err != nil {
		log.Fatal(err)
	}

	// Ensure that the key exists in the copy.
	if err := 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
	}); err != nil {
		log.Fatal(err)
	}

	// Close database to release file lock.
	if err := db.Close(); err != nil {
		log.Fatal(err)
	}

	if err := db2.Close(); err != nil {
		log.Fatal(err)
	}

	// Output:
	// The value for 'foo' in the clone is: bar
}
开发者ID:RomainVabre,项目名称:origin,代码行数:58,代码来源:tx_test.go

示例8: Defrag

func (b *backend) Defrag() error {
	// TODO: make this non-blocking?
	// lock batchTx to ensure nobody is using previous tx, and then
	// close previous ongoing tx.
	b.batchTx.Lock()
	defer b.batchTx.Unlock()

	// lock database after lock tx to avoid deadlock.
	b.mu.Lock()
	defer b.mu.Unlock()

	b.batchTx.commit(true)
	b.batchTx.tx = nil

	tmpdb, err := bolt.Open(b.db.Path()+".tmp", 0600, boltOpenOptions)
	if err != nil {
		return err
	}

	err = defragdb(b.db, tmpdb, defragLimit)

	if err != nil {
		tmpdb.Close()
		os.RemoveAll(tmpdb.Path())
		return err
	}

	dbp := b.db.Path()
	tdbp := tmpdb.Path()

	err = b.db.Close()
	if err != nil {
		log.Fatalf("backend: cannot close database (%s)", err)
	}
	err = tmpdb.Close()
	if err != nil {
		log.Fatalf("backend: cannot close database (%s)", err)
	}
	err = os.Rename(tdbp, dbp)
	if err != nil {
		log.Fatalf("backend: cannot rename database (%s)", err)
	}

	b.db, err = bolt.Open(dbp, 0600, boltOpenOptions)
	if err != nil {
		log.Panicf("backend: cannot open database at %s (%v)", dbp, err)
	}
	b.batchTx.tx, err = b.db.Begin(true)
	if err != nil {
		log.Fatalf("backend: cannot begin tx (%s)", err)
	}

	return nil
}
开发者ID:xingfeng2510,项目名称:etcd,代码行数:54,代码来源:backend.go

示例9: TestOpen_FileTooSmall

// Ensure that a database that is too small returns an error.
func TestOpen_FileTooSmall(t *testing.T) {
	path := tempfile()

	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)
}
开发者ID:fcavani,项目名称:monlite,代码行数:14,代码来源:db_test.go

示例10: TestOpen_Check

// Ensure that a re-opened database is consistent.
func TestOpen_Check(t *testing.T) {
	path := tempfile()

	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()
}
开发者ID:fcavani,项目名称:monlite,代码行数:14,代码来源:db_test.go

示例11: GetBoltPageSaver

func GetBoltPageSaver(indexFilename, redirFilename string) (PageSaver, error) {
	index, err := bolt.Open(indexFilename, 0600, nil)
	if err != nil {
		return nil, err
	}

	redir, err := bolt.Open(redirFilename, 0600, nil)
	if err != nil {
		return nil, err
	}

	pageLoader := boltLoader{index, redir, sync.WaitGroup{}, false, sync.Mutex{}}
	return &pageLoader, nil
}
开发者ID:kbuzsaki,项目名称:wikidegree,代码行数:14,代码来源:apibolt.go

示例12: GetBoltPageLoader

func GetBoltPageLoader() (PageLoader, error) {
	index, err := bolt.Open(DefaultIndexName, 0600, &bolt.Options{ReadOnly: true})
	if err != nil {
		return nil, err
	}

	redir, err := bolt.Open(DefaultRedirName, 0600, &bolt.Options{ReadOnly: true})
	if err != nil {
		return nil, err
	}

	pageLoader := boltLoader{index, redir, sync.WaitGroup{}, false, sync.Mutex{}}
	return &pageLoader, nil
}
开发者ID:kbuzsaki,项目名称:wikidegree,代码行数:14,代码来源:apibolt.go

示例13: CreatePlaylist

// CreatePlaylist function creates a playlist item in the database and
// also creates it in the filesystem.
// It receives the playlist name and returns the modified name and an
// error if something went wrong.
func CreatePlaylist(name, mPoint string) (string, error) {
	glog.Infof("Creating Playlist with name: %s\n", name)
	name = GetCompatibleString(name)
	db, err := bolt.Open(config.DbPath, 0600, nil)
	if err != nil {
		return "", err
	}
	defer db.Close()

	err = db.Update(func(tx *bolt.Tx) error {
		root, err := tx.CreateBucketIfNotExists([]byte("Playlists"))
		if err != nil {
			glog.Errorf("Error creating Playlists bucket: %s\n", err)
			return err
		}

		_, err = root.CreateBucketIfNotExists([]byte(name))
		if err != nil {
			glog.Errorf("Error creating %s bucket: %s\n", name, err)
			return err
		}

		return nil
	})
	if err != nil {
		return "", err
	}

	return name, err
}
开发者ID:dankomiocevic,项目名称:mulifs,代码行数:34,代码来源:playlistManager.go

示例14: read

func read(dbPath string, mapPop bool) {
	opt := &bolt.Options{Timeout: 5 * time.Minute, ReadOnly: true}
	if mapPop {
		fmt.Println("read with MAP_POPULATE flag...")
		opt = &bolt.Options{Timeout: 5 * time.Minute, ReadOnly: true, MmapFlags: syscall.MAP_POPULATE}
	} else {
		fmt.Println("read without MAP_POPULATE flag...")
	}

	to := time.Now()
	db, err := bolt.Open(dbPath, 0600, opt)
	if err != nil {
		panic(err)
	}
	defer db.Close()
	fmt.Println("bolt.Open took", time.Since(to))

	tr := time.Now()
	tx, err := db.Begin(writable)
	if err != nil {
		panic(err)
	}
	defer tx.Rollback()

	bk := tx.Bucket([]byte(bucketName))
	c := bk.Cursor()

	for k, v := c.First(); k != nil; k, v = c.Next() {
		// fmt.Printf("%s ---> %s.\n", k, v)
		_ = k
		_ = v
	}
	fmt.Println("bolt read took:", time.Since(tr))
}
开发者ID:xqbumu,项目名称:learn,代码行数:34,代码来源:01_read_all_data.go

示例15: main

func main() {
	items := []TestMessage_MsgItem{
		TestMessage_MsgItem{
			Id:        1,
			ItemName:  "aaaaa",
			ItemValue: 333,
			ItemType:  111,
		},
	}
	msg := TestMessage{
		ClientName:   "upccup",
		Description:  "test proto",
		ClientId:     1,
		Messageitems: items,
	}

	db, err := bolt.Open("bolt.db", 0644, nil)
	if err != nil {
		log.Fatal(err)
	}

	if err := PutMessages(db, &msg); err != nil {
		log.Fatal(err)
	}

	queryMsg := GetMessage(db)
	log.Printf("query msg: %+v", queryMsg)
	log.Printf("put msg: %+v", msg)
}
开发者ID:upccup,项目名称:cuplearn,代码行数:29,代码来源:main.go


注:本文中的github.com/boltdb/bolt.Open函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。