當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。