本文整理汇总了Golang中github.com/boltdb/bolt.DB.Update方法的典型用法代码示例。如果您正苦于以下问题:Golang DB.Update方法的具体用法?Golang DB.Update怎么用?Golang DB.Update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/boltdb/bolt.DB
的用法示例。
在下文中一共展示了DB.Update方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewCookie
func NewCookie(kvdb *bolt.DB) (*sessions.CookieStore, error) {
var secret []byte
// Create cookie secret if one does not exist.
err := kvdb.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists([]byte("cookie"))
if err != nil {
return err
}
secret = bucket.Get([]byte("secret"))
if len(secret) == 0 {
secret = []byte(uuid.New())
err = bucket.Put([]byte("secret"), secret)
}
return err
})
if err != nil {
return nil, err
}
if len(secret) == 0 {
return nil, errors.New("Cookie secret cannot be empty.")
}
return sessions.NewCookieStore(secret), nil
}
示例2: addUser
func addUser(db *bolt.DB, email string, password string) bool {
check := []byte("")
hashedPass, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
fmt.Println(err)
}
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("UsersBucket"))
check = b.Get([]byte(email))
return nil
})
if check == nil {
db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("UsersBucket"))
err := b.Put([]byte(email), []byte(hashedPass))
return err
})
if err != nil {
fmt.Println(err)
}
return true
} else {
return false
}
}
示例3: updateLayer
func (l *Layered) updateLayer(db *bolt.DB, layer *Layer, f func(*layerVertex)) error {
return db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(boltLayersBucket)
key := []byte(layer.ID)
data := bucket.Get(key)
if len(data) == 0 {
// This should never happen through this struct
panic(fmt.Errorf("layer %s not found", layer.ID))
}
// Read the vertex, call the function to modify it
var v layerVertex
if err := l.structRead(&v, data); err != nil {
return err
}
f(&v)
// Save the resulting layer data
data, err := l.structData(&v)
if err != nil {
return err
}
return bucket.Put(key, data)
})
}
示例4: runWritesWithSource
func (cmd *BenchCommand) runWritesWithSource(db *bolt.DB, options *BenchOptions, results *BenchResults, keySource func() uint32) error {
results.WriteOps = options.Iterations
for i := 0; i < options.Iterations; i += options.BatchSize {
if err := db.Update(func(tx *bolt.Tx) error {
b, _ := tx.CreateBucketIfNotExists(benchBucketName)
b.FillPercent = options.FillPercent
for j := 0; j < options.BatchSize; j++ {
key := make([]byte, options.KeySize)
value := make([]byte, options.ValueSize)
// Write key as uint32.
binary.BigEndian.PutUint32(key, keySource())
// Insert key/value.
if err := b.Put(key, value); err != nil {
return err
}
}
return nil
}); err != nil {
return err
}
}
return nil
}
示例5: 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)
var name = make([]byte, options.KeySize)
binary.BigEndian.PutUint32(name, keySource())
b, _ := top.CreateBucketIfNotExists(name)
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
}
示例6: dump
func (s *server) dump(db *bolt.DB, changes map[uint64]bool) {
for k := range changes {
// marshal
var rs *RankSet
s.lock_read(func() {
rs = s.ranks[k]
})
if rs == nil {
log.Warning("empty rankset:", k)
continue
}
// serialization and save
bin, err := rs.Marshal()
if err != nil {
log.Critical("cannot marshal:", err)
os.Exit(-1)
}
db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(BOLTDB_BUCKET))
err := b.Put([]byte(fmt.Sprint(k)), bin)
return err
})
}
}
示例7: Put
//Put the key, value pair. index number metadata is prepended to the value
func (b *BoltDB) Put(key string, value []byte, opts *store.WriteOptions) error {
var (
dbIndex uint64
db *bolt.DB
err error
)
b.Lock()
defer b.Unlock()
dbval := make([]byte, libkvmetadatalen)
if db, err = b.getDBhandle(); err != nil {
return err
}
defer b.releaseDBhandle()
err = db.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists(b.boltBucket)
if err != nil {
return err
}
dbIndex = atomic.AddUint64(&b.dbIndex, 1)
binary.LittleEndian.PutUint64(dbval, dbIndex)
dbval = append(dbval, value...)
err = bucket.Put([]byte(key), dbval)
if err != nil {
return err
}
return nil
})
return err
}
示例8: SaveBucket
func SaveBucket(db *bolt.DB, bucketName string, key string, value string) {
db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucketName))
err := b.Put([]byte(key), []byte(value))
return err
})
}
示例9: Save
func (file File) Save(db *bolt.DB) error {
return db.Update(func(tx *bolt.Tx) error {
filesBucket := tx.Bucket([]byte("files"))
fileBucket, err := filesBucket.CreateBucketIfNotExists([]byte(file.FileToken))
if err != nil {
return err
}
now := time.Now()
marshaledCreationTime, err := now.MarshalBinary()
if err != nil {
return err
}
marshaledDeleteDate, err := now.AddDate(0, 0, file.DaysAvailable).MarshalBinary()
if err != nil {
return err
}
err = fileBucket.Put([]byte("user-token"), []byte(file.UserToken))
err = fileBucket.Put([]byte("filename"), []byte(file.Filename))
err = fileBucket.Put([]byte("expires-in"), []byte(strconv.Itoa(file.DaysAvailable)))
err = fileBucket.Put([]byte("creation-time"), marshaledCreationTime)
err = fileBucket.Put([]byte("delete-date"), marshaledDeleteDate)
if err != nil {
return err
}
return nil
})
}
示例10: boltPrepareBuckets
func boltPrepareBuckets(db *bolt.DB) error {
return db.Update(func(tx *bolt.Tx) error {
var err error
_, err = tx.CreateBucketIfNotExists([]byte(boltTokenBucket))
if err != nil {
return err
}
_, err = tx.CreateBucketIfNotExists([]byte(boltItemBucket))
if err != nil {
return err
}
_, err = tx.CreateBucketIfNotExists([]byte(boltPendingBucket))
if err != nil {
return err
}
_, err = tx.CreateBucketIfNotExists([]byte(boltCloudBucket))
if err != nil {
return err
}
_, err = tx.CreateBucketIfNotExists([]byte(boltGatewayBucket))
if err != nil {
return err
}
_, err = tx.CreateBucketIfNotExists([]byte(boltLocalTokenBucket))
if err != nil {
return err
}
_, err = tx.CreateBucketIfNotExists([]byte(boltOnboardedAppBucket))
if err != nil {
return err
}
return nil
})
}
示例11: dump
func (s *server) dump(db *bolt.DB, changes map[uint64]bool) {
db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(BOLTDB_BUCKET))
for k := range changes {
ep := s.read_ep(k)
if ep == nil {
log.Errorf("cannot find endpoint %v", k)
continue
}
// serialization and save
bin, err := msgpack.Marshal(ep.Read())
if err != nil {
log.Critical("cannot marshal:", err)
continue
}
err = b.Put([]byte(fmt.Sprint(k)), bin)
if err != nil {
log.Critical(err)
continue
}
}
return nil
})
}
示例12: updateTags
func updateTags(db *bolt.DB, ref string, tags []string, remove bool) error {
return db.Update(func(tx *bolt.Tx) (err error) {
bucket := tx.Bucket([]byte(TAG))
if err != nil {
return
}
current := make([]string, 0)
value := bucket.Get([]byte(ref))
if value != nil {
dec := gob.NewDecoder(bytes.NewBuffer(value))
err = dec.Decode(¤t)
if err != nil {
return
}
}
current = applyTags(current, tags, remove)
var buffer bytes.Buffer
enc := gob.NewEncoder(&buffer)
err = enc.Encode(current)
if err != nil {
return
}
bucket.Put([]byte(ref), buffer.Bytes())
return
})
}
示例13: NewHeap
// New creates a wait queue.
// The bucket will be created if it does not exist. Otherwise,
// the bucket will be deleted and created again.
func NewHeap(db *bolt.DB, bucket []byte, bufsize int) (h *Heap, err error) {
if err = db.Update(func(tx *bolt.Tx) error {
if tx.Bucket(bucket) != nil {
if err := tx.DeleteBucket(bucket); err != nil {
return err
}
}
if _, err := tx.CreateBucketIfNotExists(bucket); err != nil {
return err
}
return nil
}); err != nil {
db.Close()
return
}
h = &Heap{
db: db,
count: 0,
min: nil,
writebuf: list.New(),
bufsize: bufsize,
bucket: bucket,
}
return
}
示例14: updateList
func updateList(db *bolt.DB, params martini.Params, req *http.Request, r render.Render) {
var l DistributionList
body, _ := ioutil.ReadAll(req.Body)
req.Body.Close()
err := json.Unmarshal(body, &l)
if err != nil {
r.Error(http.StatusBadRequest)
return
}
if params["id"] != l.Id {
r.Error(http.StatusBadRequest)
return
}
// marshal back out to json to normalize our data
j, err := json.Marshal(l)
db.Update(
func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucketDistributionLists))
return b.Put([]byte(l.Id), j)
})
r.Status(http.StatusOK)
}
示例15: logAction
func logAction(db *bolt.DB, action string) {
dbBucketName := "alphazero"
dbBucketLogs := fmt.Sprintf("%s.logs", dbBucketName)
db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(dbBucketLogs))
id, _ := b.NextSequence()
l := LogEntry{
ID: id,
When: strconv.FormatInt(time.Now().Unix(), 10),
Action: action,
UUID: uuid.NewV4().String(),
}
buf, err := json.Marshal(l)
if err != nil {
return err
}
err = b.Put(itob(l.ID), buf)
if err != nil {
return err
}
return nil
})
}