本文整理汇总了Golang中hash/adler32.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestDeepObjectPointer
func TestDeepObjectPointer(t *testing.T) {
// Arrange
wheel1 := wheel{radius: 17}
wheel2 := wheel{radius: 22}
wheel3 := wheel{radius: 17}
myUni1 := unicycle{licencePlateID: "blah", primaryWheel: &wheel1, tags: map[string]string{"color": "blue", "name": "john"}}
myUni2 := unicycle{licencePlateID: "blah", primaryWheel: &wheel2, tags: map[string]string{"color": "blue", "name": "john"}}
myUni3 := unicycle{licencePlateID: "blah", primaryWheel: &wheel3, tags: map[string]string{"color": "blue", "name": "john"}}
// Run it more than once to verify determinism of hasher.
for i := 0; i < 100; i++ {
hasher1 := adler32.New()
hasher2 := adler32.New()
hasher3 := adler32.New()
// Act
DeepHashObject(hasher1, myUni1)
hash1 := hasher1.Sum32()
DeepHashObject(hasher2, myUni2)
hash2 := hasher2.Sum32()
DeepHashObject(hasher3, myUni3)
hash3 := hasher3.Sum32()
// Assert
if hash1 == hash2 {
t.Errorf("hash1 (%d) and hash2(%d) must be different because they have different values for wheel size", hash1, hash2)
}
if hash1 != hash3 {
t.Errorf("hash1 (%d) and hash3(%d) must be the same because although they point to different objects, they have the same values for wheel size", hash1, hash3)
}
}
}
示例2: TestDeepObjectPointer
func TestDeepObjectPointer(t *testing.T) {
// Arrange
wheel1 := wheel{radius: 17}
wheel2 := wheel{radius: 22}
wheel3 := wheel{radius: 17}
myUni1 := unicycle{licencePlateID: "blah", primaryWheel: &wheel1}
myUni2 := unicycle{licencePlateID: "blah", primaryWheel: &wheel2}
myUni3 := unicycle{licencePlateID: "blah", primaryWheel: &wheel3}
hasher1 := adler32.New()
hasher2 := adler32.New()
hasher3 := adler32.New()
// Act
DeepHashObject(hasher1, myUni1)
hash1 := hasher1.Sum32()
DeepHashObject(hasher2, myUni2)
hash2 := hasher2.Sum32()
DeepHashObject(hasher3, myUni3)
hash3 := hasher3.Sum32()
// Assert
if hash1 == hash2 {
t.Errorf("hash1 (%d) and hash2(%d) must be different because they have different values for wheel size", hash1, hash2)
}
if hash1 != hash3 {
t.Errorf("hash1 (%d) and hash3(%d) must be the same because although they point to different objects, they have the same values for wheel size", hash1, hash3)
}
}
示例3: syncPrinters
func (pm *PrinterManager) syncPrinters(ignorePrivet bool) error {
glog.Info("Synchronizing printers, stand by")
// Get current snapshot of CUPS printers.
cupsPrinters, err := pm.cups.GetPrinters()
if err != nil {
return fmt.Errorf("Sync failed while calling GetPrinters(): %s", err)
}
if pm.ignoreRawPrinters {
cupsPrinters, _ = lib.FilterRawPrinters(cupsPrinters)
}
// Augment CUPS printers with extra information from SNMP.
if pm.snmp != nil {
err = pm.snmp.AugmentPrinters(cupsPrinters)
if err != nil {
glog.Warningf("Failed to augment printers with SNMP data: %s", err)
}
}
// Set CapsHash on all printers.
for i := range cupsPrinters {
h := adler32.New()
lib.DeepHash(cupsPrinters[i].Tags, h)
cupsPrinters[i].Tags["tagshash"] = fmt.Sprintf("%x", h.Sum(nil))
h = adler32.New()
lib.DeepHash(cupsPrinters[i].Description, h)
cupsPrinters[i].CapsHash = fmt.Sprintf("%x", h.Sum(nil))
}
// Compare the snapshot to what we know currently.
diffs := lib.DiffPrinters(cupsPrinters, pm.printers.GetAll())
if diffs == nil {
glog.Infof("Printers are already in sync; there are %d", len(cupsPrinters))
return nil
}
// Update GCP.
ch := make(chan lib.Printer, len(diffs))
for i := range diffs {
go pm.applyDiff(&diffs[i], ch, ignorePrivet)
}
currentPrinters := make([]lib.Printer, 0, len(diffs))
for _ = range diffs {
p := <-ch
if p.Name != "" {
currentPrinters = append(currentPrinters, p)
}
}
// Update what we know.
pm.printers.Refresh(currentPrinters)
glog.Infof("Finished synchronizing %d printers", len(currentPrinters))
return nil
}
示例4: TestDeepHashObject
func TestDeepHashObject(t *testing.T) {
successCases := []func() interface{}{
func() interface{} { return 8675309 },
func() interface{} { return "Jenny, I got your number" },
func() interface{} { return []string{"eight", "six", "seven"} },
func() interface{} { return [...]int{5, 3, 0, 9} },
func() interface{} { return map[int]string{8: "8", 6: "6", 7: "7"} },
func() interface{} { return map[string]int{"5": 5, "3": 3, "0": 0, "9": 9} },
func() interface{} { return A{867, "5309"} },
func() interface{} { return &A{867, "5309"} },
func() interface{} {
return B{[]int{8, 6, 7}, map[string]bool{"5": true, "3": true, "0": true, "9": true}}
},
func() interface{} { return map[A]bool{A{8675309, "Jenny"}: true, A{9765683, "!Jenny"}: false} },
func() interface{} { return map[C]bool{C{8675309, "Jenny"}: true, C{9765683, "!Jenny"}: false} },
func() interface{} { return map[*A]bool{&A{8675309, "Jenny"}: true, &A{9765683, "!Jenny"}: false} },
func() interface{} { return map[*C]bool{&C{8675309, "Jenny"}: true, &C{9765683, "!Jenny"}: false} },
}
for _, tc := range successCases {
hasher1 := adler32.New()
DeepHashObject(hasher1, tc())
hash1 := hasher1.Sum32()
DeepHashObject(hasher1, tc())
hash2 := hasher1.Sum32()
if hash1 != hash2 {
t.Fatalf("hash of the same object (%q) produced different results: %d vs %d", toString(tc()), hash1, hash2)
}
for i := 0; i < 100; i++ {
hasher2 := adler32.New()
DeepHashObject(hasher1, tc())
hash1a := hasher1.Sum32()
DeepHashObject(hasher2, tc())
hash2a := hasher2.Sum32()
if hash1a != hash1 {
t.Errorf("repeated hash of the same object (%q) produced different results: %d vs %d", toString(tc()), hash1, hash1a)
}
if hash2a != hash2 {
t.Errorf("repeated hash of the same object (%q) produced different results: %d vs %d", toString(tc()), hash2, hash2a)
}
if hash1a != hash2a {
t.Errorf("hash of the same object produced (%q) different results: %d vs %d", toString(tc()), hash1a, hash2a)
}
}
}
}
示例5: picHash
func picHash(picf io.Reader) string {
hash := adler32.New()
if _, err := io.Copy(hash, picf); err != nil {
log.Panic(err)
}
return fmt.Sprintf("%x", hash.Sum32())
}
示例6: computeChecksum
func (c *controller) computeChecksum(fn string) (string, error) {
checksumType := strings.ToLower(c.conf.GetDirectives().Data.Simple.Checksum)
var hash hash.Hash
switch checksumType {
case "md5":
hash = md5.New()
case "adler32":
hash = adler32.New()
case "sha1":
hash = sha1.New()
case "sha256":
hash = sha256.New()
default:
return "", errors.New("provided checksum not implemented")
}
fd, err := os.Open(fn)
defer fd.Close()
if err != nil {
return "", err
}
if _, err := io.Copy(hash, fd); err != nil {
return "", err
}
checksum := fmt.Sprintf("%x", hash.Sum([]byte{}))
return checksumType + ":" + checksum, nil
}
示例7: NewDeflaterLevel
// NewDeflater creates a new io.WriteCloser that satisfies writes by compressing data written to w.
// It is the caller's responsibility to call Close on the WriteCloser when done.
// level is the compression level, which can be DefaultCompression, NoCompression,
// or any integer value between BestSpeed and BestCompression (inclusive).
func NewDeflaterLevel(w io.Writer, level int) (io.WriteCloser, os.Error) {
z := new(writer)
// ZLIB has a two-byte header (as documented in RFC 1950).
// The first four bits is the CINFO (compression info), which is 7 for the default deflate window size.
// The next four bits is the CM (compression method), which is 8 for deflate.
z.scratch[0] = 0x78
// The next two bits is the FLEVEL (compression level). The four values are:
// 0=fastest, 1=fast, 2=default, 3=best.
// The next bit, FDICT, is unused, in this implementation.
// The final five FCHECK bits form a mod-31 checksum.
switch level {
case 0, 1:
z.scratch[1] = 0x01
case 2, 3, 4, 5:
z.scratch[1] = 0x5e
case 6, -1:
z.scratch[1] = 0x9c
case 7, 8, 9:
z.scratch[1] = 0xda
default:
return nil, os.NewError("level out of range")
}
_, err := w.Write(z.scratch[0:2])
if err != nil {
return nil, err
}
z.w = w
z.deflater = flate.NewDeflater(w, level)
z.digest = adler32.New()
return z, nil
}
示例8: getIndex
// Helper function to take the key string and determine which bucket
// the item should be placed in. This is the simplest hash function
// I found in the core libraries. Not really sure how appropriate it is.
func getIndex(key string, max int) int {
hash := adler32.New()
hash.Write([]byte(key))
digest := hash.Sum32()
return int(digest) % max
}
示例9: adler32
func (e *Engine) adler32() error {
data, err := computeHash(adler32.New(), e.stack.Pop())
if err == nil {
e.stack.Push(data)
}
return err
}
示例10: AddFile
func (self *FileService) AddFile(file File) error {
writer := adler32.New()
job := NewJob(file, writer, 2048, func(job *Job) error {
var data []byte
var err error
switch {
case file.Type() == FILE_DEFAULT:
// Unchecked conversion. May cause problems
defaultFile, _ := file.(DefaultFile)
defaultFile.SetId(fmt.Sprintf("%x", writer.Sum32()))
log.Printf("Adding file %s", defaultFile.Id())
data, err = json.Marshal(defaultFile.Data())
if err != nil {
return err
}
}
err = self.ctx.Database.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(filesBucketName)
return b.Put([]byte(file.Id()), data)
})
return nil
})
self.ctx.JobService.Channel() <- job
return nil
}
示例11: extractFromFile
func extractFromFile(filename string) (api.BoundPod, error) {
var pod api.BoundPod
glog.V(3).Infof("Reading config file %q", filename)
file, err := os.Open(filename)
if err != nil {
return pod, err
}
defer file.Close()
data, err := ioutil.ReadAll(file)
if err != nil {
return pod, err
}
manifest := &api.ContainerManifest{}
// TODO: use api.Scheme.DecodeInto
if err := yaml.Unmarshal(data, manifest); err != nil {
return pod, fmt.Errorf("can't unmarshal file %q: %v", filename, err)
}
if err := api.Scheme.Convert(manifest, &pod); err != nil {
return pod, fmt.Errorf("can't convert pod from file %q: %v", filename, err)
}
hostname, err := os.Hostname() //TODO: kubelet name would be better
if err != nil {
return pod, err
}
if len(pod.UID) == 0 {
hasher := md5.New()
fmt.Fprintf(hasher, "host:%s", hostname)
fmt.Fprintf(hasher, "file:%s", filename)
util.DeepHashObject(hasher, pod)
pod.UID = hex.EncodeToString(hasher.Sum(nil)[0:])
glog.V(5).Infof("Generated UID %q for pod %q from file %s", pod.UID, pod.Name, filename)
}
if len(pod.Namespace) == 0 {
hasher := adler32.New()
fmt.Fprint(hasher, filename)
// TODO: file-<sum>.hostname would be better, if DNS subdomains
// are allowed for namespace (some places only allow DNS
// labels).
pod.Namespace = fmt.Sprintf("file-%08x-%s", hasher.Sum32(), hostname)
glog.V(5).Infof("Generated namespace %q for pod %q from file %s", pod.Namespace, pod.Name, filename)
}
// TODO(dchen1107): BoundPod is not type of runtime.Object. Once we allow kubelet talks
// about Pod directly, we can use SelfLinker defined in package: latest
// Currently just simply follow the same format in resthandler.go
pod.ObjectMeta.SelfLink = fmt.Sprintf("/api/v1beta2/pods/%s?namespace=%s",
pod.Name, pod.Namespace)
if glog.V(4) {
glog.Infof("Got pod from file %q: %#v", filename, pod)
} else {
glog.V(1).Infof("Got pod from file %q: %s.%s (%s)", filename, pod.Namespace, pod.Name, pod.UID)
}
return pod, nil
}
示例12: getDesiredRC
// Returns an RC that matches the intent of the given deployment.
// It creates a new RC if required.
func (d *DeploymentController) getDesiredRC(deployment *experimental.Deployment) (*api.ReplicationController, error) {
namespace := deployment.ObjectMeta.Namespace
// Find if the required RC exists already.
rcList, err := d.client.ReplicationControllers(namespace).List(labels.Everything())
if err != nil {
return nil, fmt.Errorf("error listing replication controllers: %v", err)
}
for _, rc := range rcList.Items {
if api.Semantic.DeepEqual(rc.Spec.Template, deployment.Spec.Template) {
// This is the desired RC.
return &rc, nil
}
}
// desired RC does not exist, create a new one.
podTemplateSpecHasher := adler32.New()
util.DeepHashObject(podTemplateSpecHasher, deployment.Spec.Template)
podTemplateSpecHash := podTemplateSpecHasher.Sum32()
rcName := fmt.Sprintf("deploymentrc-%d", podTemplateSpecHash)
desiredRC := api.ReplicationController{
ObjectMeta: api.ObjectMeta{
Name: rcName,
Namespace: namespace,
},
Spec: api.ReplicationControllerSpec{
Replicas: 0,
Template: deployment.Spec.Template,
},
}
createdRC, err := d.client.ReplicationControllers(namespace).Create(&desiredRC)
if err != nil {
return nil, fmt.Errorf("error creating replication controller: %v", err)
}
return createdRC, nil
}
示例13: NewReaderDict
// NewReaderDict is like NewReader but uses a preset dictionary.
// NewReaderDict ignores the dictionary if the compressed data does not refer to it.
func NewReaderDict(r io.Reader, dict []byte) (io.ReadCloser, os.Error) {
z := new(reader)
if fr, ok := r.(flate.Reader); ok {
z.r = fr
} else {
z.r = bufio.NewReader(r)
}
_, err := io.ReadFull(z.r, z.scratch[0:2])
if err != nil {
return nil, err
}
h := uint(z.scratch[0])<<8 | uint(z.scratch[1])
if (z.scratch[0]&0x0f != zlibDeflate) || (h%31 != 0) {
return nil, HeaderError
}
if z.scratch[1]&0x20 != 0 {
_, err = io.ReadFull(z.r, z.scratch[0:4])
if err != nil {
return nil, err
}
checksum := uint32(z.scratch[0])<<24 | uint32(z.scratch[1])<<16 | uint32(z.scratch[2])<<8 | uint32(z.scratch[3])
if checksum != adler32.Checksum(dict) {
return nil, DictionaryError
}
z.decompressor = flate.NewReaderDict(z.r, dict)
} else {
z.decompressor = flate.NewReader(z.r)
}
z.digest = adler32.New()
return z, nil
}
示例14: TestGolden
func TestGolden(t *testing.T) {
for _, g := range golden {
in := g.in
// We test the vanilla implementation
p := []byte(g.in)
vanilla := adler32.New()
vanilla.Write(p)
if got := vanilla.Sum32(); got != g.out {
t.Errorf("vanilla implentation: for %q, expected 0x%x, got 0x%x", in, g.out, got)
continue
}
// We test the rolling implementation by prefixing the slice by a
// space, writing it to our rolling hash, and then rolling once
q := []byte(" ")
q = append(q, p...)
rolling := rollsum.New()
rolling.Write(q[:len(q)-1])
rolling.Roll(q[len(q)-1])
if got := rolling.Sum32(); got != g.out {
t.Errorf("rolling implentation: for %q, expected 0x%x, got 0x%x", in, g.out, got)
continue
}
}
}
示例15: init
func (z *Writer) init(w io.Writer, level int) {
z.compressor = nil
z.ModTime = time.Now()
z.level = level
z.adler32 = adler32.New()
z.crc32 = crc32.NewIEEE()
z.w = io.MultiWriter(w, z.adler32, z.crc32)
}