當前位置: 首頁>>代碼示例>>Golang>>正文


Golang util.Uint32toBytes函數代碼示例

本文整理匯總了Golang中code/google/com/p/weed-fs/go/util.Uint32toBytes函數的典型用法代碼示例。如果您正苦於以下問題:Golang Uint32toBytes函數的具體用法?Golang Uint32toBytes怎麽用?Golang Uint32toBytes使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Uint32toBytes函數的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: DumpNeedleMapToCdb

// dumps a NeedleMap into a cdb
func DumpNeedleMapToCdb(cdbName string, nm *NeedleMap) error {
	tempnam := cdbName + "t"
	fnames := make([]string, 1, 2)
	adder, closer, err := openTempCdb(tempnam)
	if err != nil {
		return fmt.Errorf("error creating factory: %s", err)
	}
	fnames[0] = tempnam

	elt := cdb.Element{Key: make([]byte, 8), Data: make([]byte, 8)}

	fcount := uint64(0)
	walk := func(key uint64, offset, size uint32) error {
		if fcount >= maxCdbRecCount {
			if err = closer(); err != nil {
				return err
			}
			tempnam = cdbName + "t2"
			if adder, closer, err = openTempCdb(tempnam); err != nil {
				return fmt.Errorf("error creating second factory: %s", err)
			}
			fnames = append(fnames, tempnam)
			fcount = 0
		}
		util.Uint64toBytes(elt.Key, key)
		util.Uint32toBytes(elt.Data[:4], offset)
		util.Uint32toBytes(elt.Data[4:], size)
		fcount++
		return adder(elt)
	}
	// and write out the cdb from there
	err = nm.Visit(func(nv NeedleValue) error {
		return walk(uint64(nv.Key), nv.Offset, nv.Size)
	})
	if err != nil {
		closer()
		return fmt.Errorf("error walking index %s: %s", nm, err)
	}
	// store fileBytes
	data, e := json.Marshal(nm.mapMetric)
	if e != nil {
		return fmt.Errorf("error marshaling metric %s: %s", nm.mapMetric, e)
	}
	if err = adder(cdb.Element{Key: []byte{'M'}, Data: data}); err != nil {
		return err
	}
	if err = closer(); err != nil {
		return err
	}

	os.Remove(cdbName)
	if len(fnames) == 1 {
		return os.Rename(fnames[0], cdbName)
	}
	bn, ext := nakeFilename(cdbName)
	if err = os.Rename(fnames[0], bn+".1"+ext); err != nil {
		return err
	}
	return os.Rename(fnames[1], bn+".2"+ext)
}
開發者ID:screscent,項目名稱:weedfs,代碼行數:61,代碼來源:cdb_map.go

示例2: Put

func (nm *NeedleMap) Put(key uint64, offset uint32, size uint32) (int, error) {
	oldSize := nm.m.Set(Key(key), offset, size)
	util.Uint64toBytes(nm.bytes[0:8], key)
	util.Uint32toBytes(nm.bytes[8:12], offset)
	util.Uint32toBytes(nm.bytes[12:16], size)
	nm.fileCounter++
	nm.fileByteCounter = nm.fileByteCounter + uint64(size)
	if oldSize > 0 {
		nm.deletionCounter++
		nm.deletionByteCounter = nm.deletionByteCounter + uint64(oldSize)
	}
	return nm.indexFile.Write(nm.bytes)
}
開發者ID:abasse,項目名稱:weed-fs,代碼行數:13,代碼來源:needle_map.go

示例3: Delete

func (nm *NeedleMap) Delete(key uint64) error {
	nm.DeletionByteCounter = nm.DeletionByteCounter + uint64(nm.m.Delete(Key(key)))
	bytes := make([]byte, 16)
	util.Uint64toBytes(bytes[0:8], key)
	util.Uint32toBytes(bytes[8:12], 0)
	util.Uint32toBytes(bytes[12:16], 0)
	if _, err := nm.indexFile.Seek(0, 2); err != nil {
		return fmt.Errorf("cannot go to the end of indexfile %s: %s", nm.indexFile.Name(), err.Error())
	}
	if _, err := nm.indexFile.Write(bytes); err != nil {
		return fmt.Errorf("error writing to indexfile %s: %s", nm.indexFile.Name(), err.Error())
	}
	nm.DeletionCounter++
	return nil
}
開發者ID:hanlz,項目名稱:weed-fs,代碼行數:15,代碼來源:needle_map.go

示例4: Delete

func (nm *NeedleMap) Delete(key uint64) error {
	nm.deletionByteCounter = nm.deletionByteCounter + uint64(nm.m.Delete(Key(key)))
	offset, err := nm.indexFile.Seek(0, 1)
	if err != nil {
		return fmt.Errorf("cannot get position of indexfile: %s", err)
	}
	util.Uint64toBytes(nm.bytes[0:8], key)
	util.Uint32toBytes(nm.bytes[8:12], 0)
	util.Uint32toBytes(nm.bytes[12:16], 0)
	if _, err = nm.indexFile.Write(nm.bytes); err != nil {
		nm.indexFile.Truncate(offset)
		return fmt.Errorf("error writing to indexfile %s: %s", nm.indexFile, err)
	}
	nm.deletionCounter++
	return nil
}
開發者ID:abasse,項目名稱:weed-fs,代碼行數:16,代碼來源:needle_map.go

示例5: Put

func (nm *NeedleMap) Put(key uint64, offset uint32, size uint32) (int, error) {
	if key > nm.MaximumFileKey {
		nm.MaximumFileKey = key
	}
	oldSize := nm.m.Set(Key(key), offset, size)
	bytes := make([]byte, 16)
	util.Uint64toBytes(bytes[0:8], key)
	util.Uint32toBytes(bytes[8:12], offset)
	util.Uint32toBytes(bytes[12:16], size)
	nm.FileCounter++
	nm.FileByteCounter = nm.FileByteCounter + uint64(size)
	if oldSize > 0 {
		nm.DeletionCounter++
		nm.DeletionByteCounter = nm.DeletionByteCounter + uint64(oldSize)
	}
	return nm.indexFile.Write(bytes)
}
開發者ID:yonglehou,項目名稱:weedfs,代碼行數:17,代碼來源:needle_map.go

示例6: String

func (n *FileId) String() string {
	bytes := make([]byte, 12)
	util.Uint64toBytes(bytes[0:8], n.Key)
	util.Uint32toBytes(bytes[8:12], n.Hashcode)
	nonzero_index := 0
	for ; bytes[nonzero_index] == 0; nonzero_index++ {
	}
	return n.VolumeId.String() + "," + hex.EncodeToString(bytes[nonzero_index:])
}
開發者ID:screscent,項目名稱:weedfs,代碼行數:9,代碼來源:file_id.go

示例7: Put

func (nm *NeedleMap) Put(key uint64, offset uint32, size uint32) (int, error) {
	if key > nm.MaximumFileKey {
		nm.MaximumFileKey = key
	}
	oldSize := nm.m.Set(Key(key), offset, size)
	bytes := make([]byte, 16)
	util.Uint64toBytes(bytes[0:8], key)
	util.Uint32toBytes(bytes[8:12], offset)
	util.Uint32toBytes(bytes[12:16], size)
	nm.FileCounter++
	nm.FileByteCounter = nm.FileByteCounter + uint64(size)
	if oldSize > 0 {
		nm.DeletionCounter++
		nm.DeletionByteCounter = nm.DeletionByteCounter + uint64(oldSize)
	}
	if _, err := nm.indexFile.Seek(0, 2); err != nil {
		return 0, fmt.Errorf("cannot go to the end of indexfile %s: %s", nm.indexFile.Name(), err.Error())
	}
	return nm.indexFile.Write(bytes)
}
開發者ID:hanlz,項目名稱:weed-fs,代碼行數:20,代碼來源:needle_map.go

示例8: Delete

func (nm *NeedleMap) Delete(key uint64) error {
	nm.DeletionByteCounter = nm.DeletionByteCounter + uint64(nm.m.Delete(Key(key)))
	offset, err := nm.indexFile.Seek(0, 1)
	if err != nil {
		return fmt.Errorf("cannot get position of indexfile: %s", err)
	}
	bytes := make([]byte, 16)
	util.Uint64toBytes(bytes[0:8], key)
	util.Uint32toBytes(bytes[8:12], 0)
	util.Uint32toBytes(bytes[12:16], 0)
	if _, err = nm.indexFile.Write(bytes); err != nil {
		plus := ""
		if e := nm.indexFile.Truncate(offset); e != nil {
			plus = "\ncouldn't truncate index file: " + e.Error()
		}
		return fmt.Errorf("error writing to indexfile %s: %s%s", nm.indexFile.Name(), err, plus)
	}
	nm.DeletionCounter++
	return nil
}
開發者ID:yonglehou,項目名稱:weedfs,代碼行數:20,代碼來源:needle_map.go

示例9: Append

func (n *Needle) Append(w io.Writer, version Version) (size uint32, err error) {
	if s, ok := w.(io.Seeker); ok {
		if end, e := s.Seek(0, 1); e == nil {
			defer func(s io.Seeker, off int64) {
				if err != nil {
					if _, e = s.Seek(off, 0); e != nil {
						log.Printf("Failed to seek %s back to %d with error: %s\n", w, off, e)
					}
				}
			}(s, end)
		} else {
			err = fmt.Errorf("Cnnot Read Current Volume Position: %s", e)
			return
		}
	}
	switch version {
	case Version1:
		header := make([]byte, NeedleHeaderSize)
		util.Uint32toBytes(header[0:4], n.Cookie)
		util.Uint64toBytes(header[4:12], n.Id)
		n.Size = uint32(len(n.Data))
		size = n.Size
		util.Uint32toBytes(header[12:16], n.Size)
		if _, err = w.Write(header); err != nil {
			return
		}
		if _, err = w.Write(n.Data); err != nil {
			return
		}
		padding := NeedlePaddingSize - ((NeedleHeaderSize + n.Size + NeedleChecksumSize) % NeedlePaddingSize)
		util.Uint32toBytes(header[0:NeedleChecksumSize], n.Checksum.Value())
		_, err = w.Write(header[0 : NeedleChecksumSize+padding])
		return
	case Version2:
		header := make([]byte, NeedleHeaderSize)
		util.Uint32toBytes(header[0:4], n.Cookie)
		util.Uint64toBytes(header[4:12], n.Id)
		n.DataSize, n.NameSize, n.MimeSize = uint32(len(n.Data)), uint8(len(n.Name)), uint8(len(n.Mime))
		if n.DataSize > 0 {
			n.Size = 4 + n.DataSize + 1
			if n.HasName() {
				n.Size = n.Size + 1 + uint32(n.NameSize)
			}
			if n.HasMime() {
				n.Size = n.Size + 1 + uint32(n.MimeSize)
			}
			if n.HasLastModifiedDate() {
				n.Size = n.Size + LastModifiedBytesLength
			}
		}
		size = n.DataSize
		util.Uint32toBytes(header[12:16], n.Size)
		if _, err = w.Write(header); err != nil {
			return
		}
		if n.DataSize > 0 {
			util.Uint32toBytes(header[0:4], n.DataSize)
			if _, err = w.Write(header[0:4]); err != nil {
				return
			}
			if _, err = w.Write(n.Data); err != nil {
				return
			}
			util.Uint8toBytes(header[0:1], n.Flags)
			if _, err = w.Write(header[0:1]); err != nil {
				return
			}
			if n.HasName() {
				util.Uint8toBytes(header[0:1], n.NameSize)
				if _, err = w.Write(header[0:1]); err != nil {
					return
				}
				if _, err = w.Write(n.Name); err != nil {
					return
				}
			}
			if n.HasMime() {
				util.Uint8toBytes(header[0:1], n.MimeSize)
				if _, err = w.Write(header[0:1]); err != nil {
					return
				}
				if _, err = w.Write(n.Mime); err != nil {
					return
				}
			}
			if n.HasLastModifiedDate() {
				util.Uint64toBytes(header[0:8], n.LastModified)
				if _, err = w.Write(header[8-LastModifiedBytesLength : 8]); err != nil {
					return
				}
			}
		}
		padding := NeedlePaddingSize - ((NeedleHeaderSize + n.Size + NeedleChecksumSize) % NeedlePaddingSize)
		util.Uint32toBytes(header[0:NeedleChecksumSize], n.Checksum.Value())
		_, err = w.Write(header[0 : NeedleChecksumSize+padding])
		return n.DataSize, err
	}
	return 0, fmt.Errorf("Unsupported Version! (%d)", version)
}
開發者ID:pipul,項目名稱:weed-fs,代碼行數:99,代碼來源:needle_read_write.go

示例10: Etag

func (n *Needle) Etag() string {
	bits := make([]byte, 4)
	util.Uint32toBytes(bits, uint32(n.Checksum))
	return fmt.Sprintf("%x", bits)
}
開發者ID:hanlz,項目名稱:weed-fs,代碼行數:5,代碼來源:crc.go


注:本文中的code/google/com/p/weed-fs/go/util.Uint32toBytes函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。