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


Golang binary.PutVarint函数代码示例

本文整理汇总了Golang中encoding/binary.PutVarint函数的典型用法代码示例。如果您正苦于以下问题:Golang PutVarint函数的具体用法?Golang PutVarint怎么用?Golang PutVarint使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了PutVarint函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Marshal

func (t *LeaseMetadata) Marshal(wire io.Writer) {
	var b [10]byte
	var bs []byte
	bs = b[:]
	alen1 := int64(len(t.Quorum))
	if wlen := binary.PutVarint(bs, alen1); wlen >= 0 {
		wire.Write(b[0:wlen])
	}
	for i := int64(0); i < alen1; i++ {
		bs = b[:4]
		tmp32 := t.Quorum[i]
		bs[0] = byte(tmp32)
		bs[1] = byte(tmp32 >> 8)
		bs[2] = byte(tmp32 >> 16)
		bs[3] = byte(tmp32 >> 24)
		wire.Write(bs)
	}
	bs = b[:]
	alen2 := int64(len(t.ObjectKeys))
	if wlen := binary.PutVarint(bs, alen2); wlen >= 0 {
		wire.Write(b[0:wlen])
	}
	for i := int64(0); i < alen2; i++ {
		t.ObjectKeys[i].Marshal(wire)
	}
	bs = b[:2]
	bs[0] = byte(t.IgnoreReplicas)
	bs[1] = byte(t.ReinstateReplicas)
	wire.Write(bs)
}
开发者ID:pombredanne,项目名称:qlease,代码行数:30,代码来源:qlprotomarsh.go

示例2: Increment

func (b *BoltDB) Increment() int64 {
	buf := make([]byte, 8)
	var retVal int64
	err := b.db.Update(func(tx *bolt.Tx) error {
		b, err := tx.CreateBucketIfNotExists(demoBucket)
		if err != nil {
			log.Fatal(err)
		}
		v := b.Get(demoKey)
		if v == nil {
			binary.PutVarint(buf, 0)
			retVal = 0
		} else {
			i, err := binary.ReadVarint(bytes.NewBuffer(v))
			if err != nil {
				log.Fatal(err)
			}
			i++
			retVal = i
			binary.PutVarint(buf, i)
		}
		err = b.Put(demoKey, buf)
		return err
	})
	if err != nil {
		log.Fatal(err)
	}

	return retVal
}
开发者ID:TrevorSStone,项目名称:boltmobiledemo,代码行数:30,代码来源:boltmobiledemo.go

示例3: writeNumber

func (b *binaryNomsWriter) writeNumber(v Number) {
	b.ensureCapacity(binary.MaxVarintLen64 * 2)
	i, exp := float64ToIntExp(float64(v))
	count := binary.PutVarint(b.buff[b.offset:], i)
	b.offset += uint32(count)
	count = binary.PutVarint(b.buff[b.offset:], int64(exp))
	b.offset += uint32(count)
}
开发者ID:Richardphp,项目名称:noms,代码行数:8,代码来源:codec.go

示例4: zoneFilename

func zoneFilename(x, y int64, z int8) string {
	var buf [binary.MaxVarintLen64*2 + 1]byte
	i := binary.PutVarint(buf[:], x)
	i += binary.PutVarint(buf[i:], y)
	buf[i] = uint8(z)
	i++
	encoded := base32.StdEncoding.EncodeToString(buf[:i])
	return filepath.Join("rnoadm-AA", "zone"+encoded+".gz")
}
开发者ID:BenLubar,项目名称:Rnoadm,代码行数:9,代码来源:storage.go

示例5: putFunc

func putFunc(cmd *cobra.Command, args []string) {
	if keySpaceSize <= 0 {
		fmt.Fprintf(os.Stderr, "expected positive --key-space-size, got (%v)", keySpaceSize)
		os.Exit(1)
	}

	requests := make(chan v3.Op, totalClients)
	clients := mustCreateClients(totalClients, totalConns)
	k, v := make([]byte, keySize), string(mustRandBytes(valSize))

	bar = pb.New(putTotal)
	bar.Format("Bom !")
	bar.Start()

	r := newReport()
	for i := range clients {
		wg.Add(1)
		go func(c *v3.Client) {
			defer wg.Done()
			for op := range requests {
				st := time.Now()
				_, err := c.Do(context.Background(), op)
				r.Results() <- report.Result{Err: err, Start: st, End: time.Now()}
				bar.Increment()
			}
		}(clients[i])
	}

	go func() {
		for i := 0; i < putTotal; i++ {
			if seqKeys {
				binary.PutVarint(k, int64(i%keySpaceSize))
			} else {
				binary.PutVarint(k, int64(rand.Intn(keySpaceSize)))
			}
			requests <- v3.OpPut(string(k), v)
		}
		close(requests)
	}()

	if compactInterval > 0 {
		go func() {
			for {
				time.Sleep(compactInterval)
				compactKV(clients)
			}
		}()
	}

	rc := r.Run()
	wg.Wait()
	close(r.Results())
	bar.Finish()
	fmt.Println(<-rc)
}
开发者ID:pulcy,项目名称:vault-monkey,代码行数:55,代码来源:put.go

示例6: putFunc

func putFunc(cmd *cobra.Command, args []string) {
	if keySpaceSize <= 0 {
		fmt.Fprintf(os.Stderr, "expected positive --key-space-size, got (%v)", keySpaceSize)
		os.Exit(1)
	}

	results = make(chan result)
	requests := make(chan v3.Op, totalClients)
	bar = pb.New(putTotal)

	k, v := make([]byte, keySize), string(mustRandBytes(valSize))

	clients := mustCreateClients(totalClients, totalConns)

	bar.Format("Bom !")
	bar.Start()

	for i := range clients {
		wg.Add(1)
		go doPut(context.Background(), clients[i], requests)
	}

	pdoneC := printReport(results)

	go func() {
		for i := 0; i < putTotal; i++ {
			if seqKeys {
				binary.PutVarint(k, int64(i%keySpaceSize))
			} else {
				binary.PutVarint(k, int64(rand.Intn(keySpaceSize)))
			}
			requests <- v3.OpPut(string(k), v)
		}
		close(requests)
	}()

	if compactInterval > 0 {
		go func() {
			for {
				time.Sleep(compactInterval)
				compactKV(clients)
			}
		}()
	}

	wg.Wait()

	bar.Finish()

	close(results)
	<-pdoneC
}
开发者ID:CliffYuan,项目名称:etcd,代码行数:52,代码来源:put.go

示例7: zoneFilenameV1

func zoneFilenameV1(x, y int64) string {
	var buf [binary.MaxVarintLen64 * 2]byte
	i := binary.PutVarint(buf[:], x)
	i += binary.PutVarint(buf[i:], y)
	encoded := base32.StdEncoding.EncodeToString(buf[:i])
	for i := range encoded {
		if encoded[i] == '=' {
			encoded = encoded[:i]
			break
		}
	}
	return filepath.Join("rnoadm-AA", "zone"+encoded+".gz")
}
开发者ID:BenLubar,项目名称:Rnoadm,代码行数:13,代码来源:storage.go

示例8: putFunc

func putFunc(cmd *cobra.Command, args []string) {
	if keySpaceSize <= 0 {
		fmt.Fprintf(os.Stderr, "expected positive --key-space-size, got (%v)", keySpaceSize)
		os.Exit(1)
	}

	results = make(chan result)
	requests := make(chan etcdserverpb.PutRequest, totalClients)
	bar = pb.New(putTotal)

	k, v := make([]byte, keySize), mustRandBytes(valSize)

	conns := make([]*grpc.ClientConn, totalConns)
	for i := range conns {
		conns[i] = mustCreateConn()
	}

	clients := make([]etcdserverpb.KVClient, totalClients)
	for i := range clients {
		clients[i] = etcdserverpb.NewKVClient(conns[i%int(totalConns)])
	}

	bar.Format("Bom !")
	bar.Start()

	for i := range clients {
		wg.Add(1)
		go doPut(context.Background(), clients[i], requests)
	}

	pdoneC := printReport(results)

	go func() {
		for i := 0; i < putTotal; i++ {
			if seqKeys {
				binary.PutVarint(k, int64(i%keySpaceSize))
			} else {
				binary.PutVarint(k, int64(rand.Intn(keySpaceSize)))
			}
			requests <- etcdserverpb.PutRequest{Key: k, Value: v}
		}
		close(requests)
	}()

	wg.Wait()

	bar.Finish()

	close(results)
	<-pdoneC
}
开发者ID:ikatson,项目名称:etcd,代码行数:51,代码来源:put.go

示例9: Encode

func (e *CacheEntry) Encode() []byte {
	hl := 0
	for key, vals := range e.Header {
		hl = hl + binary.MaxVarintLen32*2 + len(key)
		for _, v := range vals {
			hl += binary.MaxVarintLen32 + len(v)
		}
	}

	size := 5*binary.MaxVarintLen32 + len(e.Body) + hl
	buffer := make([]byte, size)

	n := 0

	n += binary.PutVarint(buffer[n:], int64(e.Status))
	n += binary.PutVarint(buffer[n:], int64(e.AddTime))
	n += binary.PutVarint(buffer[n:], int64(e.Expire))
	n += binary.PutVarint(buffer[n:], int64(len(e.Body)))
	n += copy(buffer[n:], e.Body)
	n += binary.PutVarint(buffer[n:], int64(len(e.Header)))

	for key, vals := range e.Header {
		n += binary.PutVarint(buffer[n:], int64(len(key)))
		n += copy(buffer[n:], key)
		n += binary.PutVarint(buffer[n:], int64(len(vals)))
		for _, v := range vals {
			n += binary.PutVarint(buffer[n:], int64(len(v)))
			n += copy(buffer[n:], v)
		}
	}

	return buffer[:n]
}
开发者ID:shenfeng,项目名称:proxies,代码行数:33,代码来源:cache.go

示例10: MarshalDeltaNodes

func MarshalDeltaNodes(nodes []element.Node, buf []byte) []byte {
	estimatedLength := len(nodes)*4*3 + binary.MaxVarintLen64

	if cap(buf) < estimatedLength {
		buf = make([]byte, estimatedLength)
	} else {
		// resize slice to full capacity
		buf = buf[:cap(buf)-1]
	}

	lastId := int64(0)
	nextPos := binary.PutUvarint(buf, uint64(len(nodes)))

	for i := range nodes {
		if len(buf)-nextPos < binary.MaxVarintLen64 {
			tmp := make([]byte, len(buf)*3/2)
			copy(tmp, buf)
			buf = tmp
		}
		nextPos += binary.PutVarint(buf[nextPos:], nodes[i].Id-lastId)
		lastId = nodes[i].Id
	}

	lastLong := int64(0)
	for i := range nodes {
		if len(buf)-nextPos < binary.MaxVarintLen64 {
			tmp := make([]byte, len(buf)*3/2)
			copy(tmp, buf)
			buf = tmp
		}
		long := int64(CoordToInt(nodes[i].Long))
		nextPos += binary.PutVarint(buf[nextPos:], long-lastLong)
		lastLong = long
	}

	lastLat := int64(0)
	for i := range nodes {
		if len(buf)-nextPos < binary.MaxVarintLen64 {
			tmp := make([]byte, len(buf)*3/2)
			copy(tmp, buf)
			buf = tmp
		}
		lat := int64(CoordToInt(nodes[i].Lat))
		nextPos += binary.PutVarint(buf[nextPos:], lat-lastLat)
		lastLat = lat
	}

	return buf[:nextPos]
}
开发者ID:Kotaimen,项目名称:imposm3,代码行数:49,代码来源:deltacoords.go

示例11: TestIntSliceEncode

func TestIntSliceEncode(t *testing.T) {
	var result []byte
	result = strconv.AppendInt(result, 124, 10)
	if string(result) != "124" {
		t.Errorf("%v\n", result)
	}
	result = strconv.AppendInt(result, 12, 10)

	another := make([]byte, 14)
	nul := binary.PutVarint(another, 2)
	nul = binary.PutVarint(another[4:], 111111111)
	if nul < 0 {
		t.Error()
	}
}
开发者ID:compasses,项目名称:GOProjects,代码行数:15,代码来源:repo_test.go

示例12: growBucket

// Create and chain a new bucket.
func (ht *HashTable) growBucket(bucket int) {
	ht.EnsureSize(BUCKET_SIZE)
	lastBucketAddr := ht.lastBucket(bucket) * BUCKET_SIZE
	binary.PutVarint(ht.Buf[lastBucketAddr:lastBucketAddr+10], int64(ht.numBuckets))
	ht.Used += BUCKET_SIZE
	ht.numBuckets++
}
开发者ID:Lanzafame,项目名称:tiedot,代码行数:8,代码来源:hashtable.go

示例13: handleWrite

func handleWrite(c net.Conn) {
	for angle := range out {
		m := Message{angle}
		if buf, err := json.Marshal(m); err != nil {
			log.Printf("Could not json encode angle: %+v\n", m)
		} else {
			var frame []byte
			mode := byte(0x81)
			mask := byte(0x80)
			if len(buf) <= 125 {
				// can fit in the mask
				frame = []byte{mode, mask | byte(len(buf))}
			} else if len(buf) > 0xffff {
				// needs 8 extra bytes
				frame = make([]byte, 10)
				frame[0] = mode
				frame[1] = mask | 127
				// for now, len is 32-bit
				// TODO: update when 64-bit is supported
				// this should never happen anyway
				binary.PutVarint(frame[2:], int64(len(buf)))
			} else {
				// needs 2 extra bytes
				frame = []byte{
					mode,
					mask | 128,
					byte(len(buf)&0xff00) >> 8,
					byte(len(buf) & 0xff)}
			}

			c.Write(append(frame, buf...))
		}
	}
}
开发者ID:AungWinnHtut,项目名称:arduino-tcp-server,代码行数:34,代码来源:client.go

示例14: hashVarint

func (rv *rollingValueHasher) hashVarint(n int64) {
	buff := [binary.MaxVarintLen64]byte{}
	count := binary.PutVarint(buff[:], n)
	for i := 0; i < count; i++ {
		rv.HashByte(buff[i])
	}
}
开发者ID:kalman,项目名称:noms-pre-release,代码行数:7,代码来源:rolling_value_hasher.go

示例15: VerifySpace

func (v *Verifier) VerifySpace(challenges []int64, hashes [][]byte, parents [][][]byte, proofs [][][]byte, pProofs [][][][]byte) bool {
	for i := range challenges {
		buf := make([]byte, hashSize)
		binary.PutVarint(buf, challenges[i]+v.pow2)
		val := append(v.pk, buf...)
		for _, ph := range parents[i] {
			val = append(val, ph...)
		}
		exp := sha3.Sum256(val)
		for j := range exp {
			if exp[j] != hashes[i][j] {
				return false
			}
		}
		if !v.Verify(challenges[i], hashes[i], proofs[i]) {
			return false
		}

		ps := v.graph.GetParents(challenges[i], v.index)
		for j := range ps {
			if !v.Verify(ps[j], parents[i][j], pProofs[i][j]) {
				return false
			}
		}
	}
	return true
}
开发者ID:kwonalbert,项目名称:spacemint,代码行数:27,代码来源:verifier.go


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