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


Golang Bucket.Put方法代码示例

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

示例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
}
开发者ID:vampirewalk,项目名称:rehook,代码行数:30,代码来源:rate_limit_filter.go

示例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

}
开发者ID:ziahamza,项目名称:het,代码行数:60,代码来源:crawl.go

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

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

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

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

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

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

示例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
}
开发者ID:voidException,项目名称:bazil,代码行数:31,代码来源:inodes.go

示例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
}
开发者ID:tylerchr,项目名称:parallel-database,代码行数:33,代码来源:db-generator.go

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

示例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
}
开发者ID:lebedev-yury,项目名称:cities-api,代码行数:26,代码来源:utils.go

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

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


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