本文整理汇总了Golang中github.com/boltdb/bolt.DB.View方法的典型用法代码示例。如果您正苦于以下问题:Golang DB.View方法的具体用法?Golang DB.View怎么用?Golang DB.View使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/boltdb/bolt.DB
的用法示例。
在下文中一共展示了DB.View方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: findList
func findList(db *bolt.DB, params martini.Params, req *http.Request, r render.Render) {
id := params["id"]
var dst []byte
err := db.View(
func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucketDistributionLists))
dst = b.Get([]byte(id))
return nil
})
if err != nil {
r.Error(http.StatusInternalServerError)
return
}
if dst == nil {
r.Error(http.StatusNotFound)
return
}
var l DistributionList
err = json.Unmarshal(dst, &l)
if err != nil {
r.Error(http.StatusInternalServerError)
return
}
r.JSON(http.StatusOK, l)
}
示例2: ShowStatus
func ShowStatus(db *bolt.DB, u string) error {
ur, err := url.Parse(u)
if err != nil {
return err
}
fmt.Printf("Host %s status:\n", ur.Host)
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(ur.Host))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
var sr StatRecord
err := json.Unmarshal(v, &sr)
if err != nil {
fmt.Println(err)
c.Next()
}
fmt.Printf("%s => %s\n", k, sr)
}
fmt.Printf("\n\n")
return nil
})
return nil
}
示例3: benchReadSequential
func benchReadSequential(db *bolt.DB, options *BenchOptions, results *BenchResults) error {
return db.View(func(tx *bolt.Tx) error {
var t = time.Now()
for {
c := tx.Bucket(benchBucketName).Cursor()
var count int
for k, v := c.First(); k != nil; k, v = c.Next() {
if v == nil {
return errors.New("invalid value")
}
count++
}
if options.WriteMode == "seq" && count != options.Iterations {
return fmt.Errorf("read seq: 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
})
}
示例4: NewMockAllocator
func NewMockAllocator(db *bolt.DB) *MockAllocator {
d := &MockAllocator{}
d.clustermap = make(map[string]sort.StringSlice)
var clusters []string
err := db.View(func(tx *bolt.Tx) error {
var err error
clusters, err = ClusterList(tx)
if err != nil {
return err
}
for _, cluster := range clusters {
err := d.addDevicesFromDb(tx, cluster)
if err != nil {
return err
}
}
return nil
})
if err != nil {
return nil
}
return d
}
示例5: DoRead
func DoRead(db *bolt.DB) error {
err := db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("MyBucket"))
log.Println("b is %s", b)
//if err != nil {
// return err
//}
v := b.Get([]byte("answer"))
//log.Printf("v=%s", v)
//if err != nil {
// return err
//}
//ctrl, err := JobControlFromBytes(v)
ctrl := &JobControl{}
err := ctrl.FromBytes(v)
if err != nil {
log.Println("error", err)
return err
}
log.Printf("the value is %+v", ctrl)
return nil
})
return err
}
示例6: QueryNodeByTaxID
// QueryNodeByTaxID querys Node by taxid
func QueryNodeByTaxID(db *bolt.DB, bucket string, taxids []string) ([]nodes.Node, error) {
for _, taxid := range taxids {
if !reDigitals.MatchString(taxid) {
return []nodes.Node{}, fmt.Errorf("non-digital taxid given: %s", taxid)
}
}
nods := make([]nodes.Node, len(taxids))
if len(taxids) == 0 {
return nods, nil
}
err := db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucket))
if b == nil {
return fmt.Errorf("database not exists: %s", bucket)
}
for i, taxid := range taxids {
s := string(b.Get([]byte(taxid)))
if s == "" {
nods[i] = nodes.Node{}
continue
}
node, err := nodes.NodeFromJSON(s)
if err != nil {
return errors.New("failed to parse node record from database")
}
nods[i] = node
}
return nil
})
return nods, err
}
示例7: runReadsSequentialNested
func (cmd *BenchCommand) runReadsSequentialNested(db *bolt.DB, options *BenchOptions, results *BenchResults) error {
return db.View(func(tx *bolt.Tx) error {
t := time.Now()
for {
var count int
var top = tx.Bucket(benchBucketName)
if err := 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 ErrInvalidValue
}
count++
}
return nil
}); err != nil {
return err
}
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
})
}
示例8: DestroyCheck
func (b *BrickEntry) DestroyCheck(db *bolt.DB, executor executors.Executor) error {
godbc.Require(db != nil)
godbc.Require(b.TpSize > 0)
godbc.Require(b.Info.Size > 0)
// Get node hostname
var host string
err := db.View(func(tx *bolt.Tx) error {
node, err := NewNodeEntryFromId(tx, b.Info.NodeId)
if err != nil {
return err
}
host = node.ManageHostName()
godbc.Check(host != "")
return nil
})
if err != nil {
return err
}
// Create request
req := &executors.BrickRequest{}
req.Name = b.Info.Id
req.Size = b.Info.Size
req.TpSize = b.TpSize
req.VgId = b.Info.DeviceId
// Check brick on node
return executor.BrickDestroyCheck(host, req)
}
示例9: findReadAndWritePointers
func findReadAndWritePointers(db *bolt.DB) (int64, int64, error) {
writePointer := int64(0)
readPointer := int64(math.MaxInt64)
err := db.View(func(tx *bolt.Tx) error {
events := tx.Bucket(EVENTS_BUCKET)
err := events.ForEach(func(k, v []byte) error {
seq, _ := readSequence(k)
if seq > writePointer {
writePointer = seq
}
if seq < readPointer {
readPointer = seq
}
return nil
})
return err
})
if err != nil {
return -1, -1, err
}
if readPointer == int64(math.MaxInt64) {
readPointer = int64(1)
}
return writePointer, readPointer, nil
}
示例10: ValidateLogin
func ValidateLogin(user, password string, db *bolt.DB) bool {
err := db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte("users"))
if bucket == nil {
return fmt.Errorf("Bucket pastes not found!")
}
var uByte []byte
if uByte = bucket.Get([]byte(user)); uByte == nil {
return fmt.Errorf("User not found!")
}
var u User
err := json.Unmarshal(uByte, &u)
if err != nil {
return err
}
err = bcrypt.CompareHashAndPassword(u.Password, []byte(password))
return err
})
if err != nil {
return false
}
return true
}
示例11: getGroup
func getGroup(db *bolt.DB, id string) (g *Group, err error) {
err = db.View(func(tx *bolt.Tx) error {
g, err = getGroupWithTx(tx, id)
return err
})
return
}
示例12: searchCityNames
func searchCityNames(
db *bolt.DB, locales []string, query string, limit int,
) (*CityNames, error) {
var cityNames CityNames
err := db.View(func(tx *bolt.Tx) error {
var err error
c := tx.Bucket(CityNamesBucketName).Cursor()
prefix := []byte(PrepareCityNameKey(query))
for k, v := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, v = c.Next() {
var cityName *CityName
cityName, err = CityNameFromString(string(k), string(v))
cityNames = append(cityNames, cityName)
}
return err
})
sort.Sort(CityNamesComparator{cityNames, locales})
cityNames.Uniq()
cityNames.Limit(limit)
return &cityNames, err
}
示例13: iterate
// iterate continually loops over a subsection of the database and reads key/values.
func iterate(db *bolt.DB, c chan bool) {
max := make([]byte, keySize)
binary.BigEndian.PutUint64(max, uint64(itemCount*iteratePct))
var d time.Duration
var n int
loop:
for {
t := time.Now()
// Loop over a subset of the data.
var count int
db.View(func(tx *bolt.Tx) error {
c := tx.Bucket([]byte("root")).Cursor()
for k, _ := c.First(); k != nil && bytes.Compare(k, max) == -1; k, _ = c.Next() {
count++
}
return nil
})
log.Printf(" iterate: %v (n=%d)", time.Since(t), count)
d += time.Since(t)
n++
// Check for completion.
select {
case <-c:
break loop
default:
}
}
fmt.Printf("iterate: avg: %v (n=%d)\n", (d / time.Duration(n)), n)
}
示例14: stat
// stat prints out stats about the db.
func stat(db *bolt.DB) error {
return db.View(func(tx *bolt.Tx) error {
fmt.Printf("size: %d bytes\n", tx.Size())
fmt.Println("")
return nil
})
}
示例15: printDBDebug
func printDBDebug(db *bolt.DB) {
dbBucketName := "alphazero"
dbBucketLogs := fmt.Sprintf("%s.logs", dbBucketName)
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(dbBucketLogs))
c := b.Cursor()
i := 10
for k, v := c.Last(); k != nil; k, v = c.Prev() {
log := LogEntry{}
json.Unmarshal(v, &log)
fmt.Printf("- %s\n", log.String())
i--
if i == 0 {
break
}
}
return nil
})
dbBucketStats := fmt.Sprintf("%s.stats", dbBucketName)
db.View(func(tx *bolt.Tx) error {
c := tx.Bucket([]byte(dbBucketStats)).Cursor()
prefix := []byte("stats.")
for k, v := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, v = c.Next() {
fmt.Printf("key=%s, value=%s\n", k, v)
}
return nil
})
}