本文整理汇总了Golang中github.com/boltdb/bolt.Bucket.Put方法的典型用法代码示例。如果您正苦于以下问题:Golang Bucket.Put方法的具体用法?Golang Bucket.Put怎么用?Golang Bucket.Put使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/boltdb/bolt.Bucket
的用法示例。
在下文中一共展示了Bucket.Put方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: PersistOneBoltTx
func PersistOneBoltTx(bucket *bolt.Bucket, key string, obj proto.Marshaler) error {
exBytes, err := obj.Marshal()
if err != nil { // pragma: nocover -- no idea how to produce this error
return fmt.Errorf("Could not marshal object")
}
return bucket.Put([]byte(key), exBytes)
}
示例2: Init
// Init initializes this component. It requires an amount and an interval in
// seconds to be present.
func (RateLimitFilter) Init(h Hook, params map[string]string, b *bolt.Bucket) error {
amount, ok := params["amount"]
if !ok {
return errors.New("amount is required")
}
if i, err := strconv.Atoi(amount); err != nil || i <= 0 {
return fmt.Errorf("amount must be a positive number > 0: %s", err)
}
interval, ok := params["interval"]
if !ok {
return errors.New("interval is required")
}
if i, err := strconv.Atoi(interval); err != nil || i <= 0 {
return fmt.Errorf("interval must be a positive number: %s", err)
}
if err := b.Put([]byte(fmt.Sprintf("%s-amount", h.ID)), []byte(amount)); err != nil {
return err
}
if err := b.Put([]byte(fmt.Sprintf("%s-interval", h.ID)), []byte(interval)); err != nil {
return err
}
_, err := b.CreateBucketIfNotExists([]byte("requests"))
return err
}
示例3: GetLink
func GetLink(links *bolt.Bucket, stats *het.CountStats, url *url.URL) (het.Link, error) {
url.Fragment = ""
lbytes := links.Get([]byte(url.String()))
link := het.Link{}
if lbytes != nil {
// link already exists, return early
json.Unmarshal(lbytes, &link)
// follow redirects in the links bucket
if link.Redirect {
return GetLink(links, stats, &link.URL)
}
return link, nil
}
resp, err := http.Get(url.String())
if err != nil {
return link, err
}
defer resp.Body.Close()
finalURL := resp.Request.URL
finalURL.Fragment = ""
link = het.Link{
URL: *finalURL,
StatusCode: resp.StatusCode,
ContentType: resp.Header.Get("Content-Type"),
LastModified: strings.Trim(resp.Header.Get("Last-Modified"), " \t\n"),
}
lbytes, err = json.Marshal(&link)
if err != nil {
log.Fatal(err)
}
links.Put([]byte(finalURL.String()), lbytes)
stats.LinkCount++
// redirect link
if finalURL.String() != url.String() {
lrbytes, err := json.Marshal(&het.Link{
URL: *finalURL,
Redirect: true,
})
if err != nil {
log.Fatal(err)
}
links.Put([]byte(url.String()), lrbytes)
stats.LinkCount++
}
return link, nil
}
示例4: AddOutgoingLink
func AddOutgoingLink(links *bolt.Bucket, parentLink, childLink *het.Link) {
if parentLink.Outgoing == nil {
parentLink.Outgoing = make(map[string]bool)
}
if childLink.Incomming == nil {
childLink.Incomming = make(map[string]bool)
}
parentLink.Outgoing[childLink.URL.String()] = true
childLink.Incomming[parentLink.URL.String()] = true
fmt.Printf("Incomming for %s %d\n", childLink.URL.String(), len(childLink.Incomming))
fmt.Printf("Outgoing for %s %d\n", parentLink.URL.String(), len(parentLink.Outgoing))
pbytes, err := json.Marshal(&parentLink)
if err != nil {
log.Fatal(err)
}
cbytes, err := json.Marshal(&childLink)
if err != nil {
log.Fatal(err)
}
links.Put([]byte(childLink.URL.String()), cbytes)
links.Put([]byte(parentLink.URL.String()), pbytes)
}
示例5: FlushStats
// regularly flush statistics
func FlushStats(stats *bolt.Bucket, countStats *het.CountStats) {
sbytes, err := json.Marshal(countStats)
if err != nil {
log.Fatal(err)
}
stats.Put([]byte("count"), sbytes)
}
示例6: SaveEntry
func (db *DB) SaveEntry(entry omron.Entry, b *bolt.Bucket) error {
encoded, err := json.Marshal(entry)
if err != nil {
return err
}
return b.Put([]byte(entryKey(entry)), encoded)
}
示例7: putChanCommitTxns
func putChanCommitTxns(nodeChanBucket *bolt.Bucket, channel *OpenChannel) error {
var bc bytes.Buffer
if err := writeOutpoint(&bc, channel.ChanID); err != nil {
return err
}
txnsKey := make([]byte, len(commitTxnsKey)+bc.Len())
copy(txnsKey[:3], commitTxnsKey)
copy(txnsKey[3:], bc.Bytes())
var b bytes.Buffer
if err := channel.OurCommitTx.Serialize(&b); err != nil {
return err
}
if err := wire.WriteVarBytes(&b, 0, channel.OurCommitSig); err != nil {
return err
}
// TODO(roasbeef): should move this into putChanFundingInfo
scratch := make([]byte, 4)
byteOrder.PutUint32(scratch, channel.LocalCsvDelay)
if _, err := b.Write(scratch); err != nil {
return err
}
byteOrder.PutUint32(scratch, channel.RemoteCsvDelay)
if _, err := b.Write(scratch); err != nil {
return err
}
return nodeChanBucket.Put(txnsKey, b.Bytes())
}
示例8: storeWords
// Store a word,word -> word sequence
func storeWords(bucket *bolt.Bucket, word1, word2, word3 string) error {
key := []byte(word1 + " " + word2)
// Get value from bucket and decode it
rawValue := bucket.Get(key)
var value []string
if rawValue == nil {
value = make([]string, 0, 1)
} else {
if err := json.Unmarshal(rawValue, &value); err != nil {
log.Printf("Cannot decode raw value for key '%v': %v; starting new empty key; old value is: %v", string(key), string(rawValue))
value = make([]string, 0, 1)
}
}
// Add new word to value
value = append(value, word3)
// Encode value and store it in bucket
rawValue, err := json.Marshal(value)
if err != nil {
return err
}
if err := bucket.Put(key, rawValue); err != nil {
return err
}
// All done
return nil
}
示例9: Save
// Save saves an Entry in the database.
func (e *Entry) Save() error {
if !database.Main.Opened {
return fmt.Errorf("db must be opened before saving")
}
return database.Main.DB.Update(func(tx *bolt.Tx) error {
var err error
var b *bolt.Bucket
var enc []byte
var id uint64
if b, err = tx.CreateBucketIfNotExists([]byte(Bucket)); err != nil {
return fmt.Errorf("Error creating bucket : %s", err)
}
if enc, err = e.Encode(); err != nil {
return fmt.Errorf("Could not encode : %s", err)
}
if e.ID == 0 {
if id, err = b.NextSequence(); err != nil {
return fmt.Errorf("Could not generate ID : %s", err)
}
e.ID = int(id)
}
return b.Put([]byte(strconv.Itoa(e.ID)), enc)
})
}
示例10: Allocate
// Allocate returns the next available inode number, and marks it
// used.
//
// Returns OutOfInodes if there are no free inodes.
func Allocate(bucket *bolt.Bucket) (uint64, error) {
c := bucket.Cursor()
var i uint64
k, _ := c.Last()
if k != nil {
i = bytesToInode(k)
}
// reserve a few inodes for internal use
if i < tokens.MaxReservedInode {
i = tokens.MaxReservedInode
}
i++
if i&tokens.InodeKindMask != tokens.InodeKindNormal {
return 0, OutOfInodes
}
var buf [8]byte
inodeToBytes(i, buf[:])
err := bucket.Put(buf[:], nil)
if err != nil {
return 0, err
}
return i, nil
}
示例11: putTypedValue
func putTypedValue(bucket *bolt.Bucket, key string, value string, dataType string) error {
buf := new(bytes.Buffer)
shouldWrite := true
switch dataType {
case "int":
i64, err := strconv.ParseInt(value, 10, 64)
checkError(err)
binary.Write(buf, binary.BigEndian, i64)
case "float":
f64, err := strconv.ParseFloat(value, 64)
checkError(err)
binary.Write(buf, binary.BigEndian, f64)
if math.IsNaN(f64) {
shouldWrite = false
}
default:
err := bucket.Put([]byte(key), []byte(value))
return err
}
if !shouldWrite {
return nil
}
err := bucket.Put([]byte(key), buf.Bytes())
return err
}
示例12: executeUpdate
func executeUpdate(stmt *boltq.UpdateStatement, db *bolt.DB) error {
return db.Update(func(tx *bolt.Tx) error {
var bucket *bolt.Bucket
var err error
for _, name := range stmt.BucketPath {
log.Debugln("navigating to bucket", name)
bucket, err = tx.CreateBucketIfNotExists([]byte(name))
if err != nil {
return err
}
if bucket == nil {
return fmt.Errorf("cannot find bucket %s", name)
}
}
for k, v := range stmt.Fields {
log.Debugf("putting %s -> %v", k, v)
b, err := encode(v)
if err != nil {
return err
}
if err = bucket.Put([]byte(k), b); err != nil {
return err
}
}
return nil
})
}
示例13: addCityToIndex
func addCityToIndex(
bucket *bolt.Bucket, id string, name string, locale string, population uint32,
) error {
var err error
var cityName *ds.CityName
if locale == "" {
locale = "en"
}
cityNameKey := []byte(ds.PrepareCityNameKey(name))
if conflict := bucket.Get(cityNameKey); conflict != nil {
cityName, err = ds.CityNameFromString(string(cityNameKey), string(conflict))
if strconv.Itoa(cityName.CityId) != id {
cityNameKey = []byte(string(cityNameKey) + "|" + id)
}
}
err = bucket.Put(
cityNameKey, []byte(
name+"\t"+id+"\t"+locale+"\t"+strconv.Itoa(int(population)),
),
)
return err
}
示例14: putEncodedObject
func putEncodedObject(bucket *bolt.Bucket, key string, value interface{}) error {
data, err := json.Marshal(value)
if err != nil {
return err
}
return bucket.Put([]byte(key), data)
}
示例15: instrumentsToDB
func instrumentsToDB(b *bolt.Bucket) error {
d, err := ioutil.ReadFile("instruments")
if err != nil {
return err
}
lines := strings.Split(string(d), "\n")
lines = lines[:len(lines)-1]
instruments = make([]Instrument, len(lines))
for i, line := range lines {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
s := strings.Split(line, ",")
inst := Instrument{ID: i, Name: s[0], Tuned: s[1] == "1"}
instruments[i] = inst
err = enc.Encode(inst)
if err != nil {
return err
}
err = b.Put([]byte(s[0]), buf.Bytes())
if err != nil {
return err
}
}
return nil
}