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


Golang bolt.DB类代码示例

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


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

示例1: benchReadSequentialNested

func benchReadSequentialNested(db *bolt.DB, options *BenchOptions, results *BenchResults) error {
	return db.View(func(tx *bolt.Tx) error {
		var t = time.Now()

		for {
			var count int
			var top = tx.Bucket(benchBucketName)
			top.ForEach(func(name, _ []byte) error {
				c := top.Bucket(name).Cursor()
				for k, v := c.First(); k != nil; k, v = c.Next() {
					if v == nil {
						return errors.New("invalid value")
					}
					count++
				}
				return nil
			})

			if options.WriteMode == "seq-nest" && count != options.Iterations {
				return fmt.Errorf("read seq-nest: iter mismatch: expected %d, got %d", options.Iterations, count)
			}

			results.ReadOps += count

			// Make sure we do this for at least a second.
			if time.Since(t) >= time.Second {
				break
			}
		}

		return nil
	})
}
开发者ID:Castcloud,项目名称:castcloud-go-server,代码行数:33,代码来源:bench.go

示例2: benchWriteNestedWithSource

func benchWriteNestedWithSource(db *bolt.DB, options *BenchOptions, results *BenchResults, keySource func() uint32) error {
	results.WriteOps = options.Iterations

	for i := 0; i < options.Iterations; i += options.BatchSize {
		err := db.Update(func(tx *bolt.Tx) error {
			top, _ := tx.CreateBucketIfNotExists(benchBucketName)
			top.FillPercent = options.FillPercent

			var name = make([]byte, options.KeySize)
			binary.BigEndian.PutUint32(name, keySource())
			b, _ := top.CreateBucketIfNotExists(name)
			b.FillPercent = options.FillPercent

			for j := 0; j < options.BatchSize; j++ {
				var key = make([]byte, options.KeySize)
				var value = make([]byte, options.ValueSize)
				binary.BigEndian.PutUint32(key, keySource())
				if err := b.Put(key, value); err != nil {
					return err
				}
			}

			return nil
		})
		if err != nil {
			return err
		}
	}
	return nil
}
开发者ID:Castcloud,项目名称:castcloud-go-server,代码行数:30,代码来源:bench.go

示例3: TestDB_Update_Closed

// Ensure a closed database returns an error while running a transaction block
func TestDB_Update_Closed(t *testing.T) {
	var db bolt.DB
	err := db.Update(func(tx *bolt.Tx) error {
		tx.CreateBucket([]byte("widgets"))
		return nil
	})
	equals(t, err, bolt.ErrDatabaseNotOpen)
}
开发者ID:Castcloud,项目名称:castcloud-go-server,代码行数:9,代码来源:db_test.go

示例4: printStats

// Continuously prints stats on the database at given intervals.
func printStats(db *bolt.DB, interval time.Duration) {
	var prevStats = db.Stats()
	var encoder = json.NewEncoder(os.Stdout)

	for {
		// Wait for the stats interval.
		time.Sleep(interval)

		// Retrieve new stats and find difference from previous iteration.
		var stats = db.Stats()
		var diff = stats.Sub(&prevStats)

		// Print as JSON to STDOUT.
		if err := encoder.Encode(diff); err != nil {
			fatal(err)
		}

		// Save stats for next iteration.
		prevStats = stats
	}
}
开发者ID:Castcloud,项目名称:castcloud-go-server,代码行数:22,代码来源:bench.go

示例5: TestDB_BeginRW_Closed

// Ensure that opening a transaction while the DB is closed returns an error.
func TestDB_BeginRW_Closed(t *testing.T) {
	var db bolt.DB
	tx, err := db.Begin(true)
	equals(t, err, bolt.ErrDatabaseNotOpen)
	assert(t, tx == nil, "")
}
开发者ID:Castcloud,项目名称:castcloud-go-server,代码行数:7,代码来源:db_test.go

示例6: TestDB_Begin_DatabaseNotOpen

// Ensure that a database cannot open a transaction when it's not open.
func TestDB_Begin_DatabaseNotOpen(t *testing.T) {
	var db bolt.DB
	tx, err := db.Begin(false)
	assert(t, tx == nil, "")
	equals(t, err, bolt.ErrDatabaseNotOpen)
}
开发者ID:Castcloud,项目名称:castcloud-go-server,代码行数:7,代码来源:db_test.go


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