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


Golang DB.Update方法代码示例

本文整理汇总了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
}
开发者ID:sandeepone,项目名称:chillax,代码行数:29,代码来源:cookie.go

示例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
	}
}
开发者ID:GetGoBlog,项目名称:GoBlog,代码行数:25,代码来源:main.go

示例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)
	})
}
开发者ID:rowhit,项目名称:otto,代码行数:25,代码来源:layered.go

示例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
}
开发者ID:rgeronimi,项目名称:bolt,代码行数:28,代码来源:main.go

示例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
}
开发者ID:ronggenliu,项目名称:bolt,代码行数:28,代码来源:bench.go

示例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
		})
	}
}
开发者ID:anders007,项目名称:rank,代码行数:26,代码来源:service.go

示例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
}
开发者ID:vdemeester,项目名称:libnetwork,代码行数:35,代码来源:boltdb.go

示例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
	})
}
开发者ID:aaronburrell,项目名称:golang,代码行数:7,代码来源:db.go

示例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
	})
}
开发者ID:nubunto,项目名称:vise,代码行数:32,代码来源:types.go

示例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
	})
}
开发者ID:KiiPlatform,项目名称:gateway-agent,代码行数:34,代码来源:boltdb.go

示例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
	})
}
开发者ID:dexter-cn,项目名称:chat,代码行数:25,代码来源:service.go

示例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(&current)
			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
	})

}
开发者ID:imdario,项目名称:tagopher,代码行数:27,代码来源:tagopher.go

示例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
}
开发者ID:fanyang01,项目名称:crawler,代码行数:29,代码来源:diskheap.go

示例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)
}
开发者ID:KanwarGill,项目名称:trifles,代码行数:29,代码来源:main.go

示例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
	})
}
开发者ID:zeroed,项目名称:alphazerobot,代码行数:25,代码来源:main.go


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