blob: 7f6a12bc2524bbfd0aa800d3e2a2a510e4799862 [file] [log] [blame]
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package database
import (
"crypto/sha1"
"hash"
"sync"
"android.googlesource.com/platform/tools/gpu/framework/binary"
"android.googlesource.com/platform/tools/gpu/framework/binary/cyclic"
"android.googlesource.com/platform/tools/gpu/framework/binary/vle"
"android.googlesource.com/platform/tools/gpu/framework/id"
)
var sha1Pool = sync.Pool{New: func() interface{} { return sha1.New() }}
// Hash returns a unique id.ID based on the contents of the object.
// Two objects of identical content will return the same ID, and the
// probability of two objects with different content generating the same ID
// will be ignorable.
// Objects with a graph structure are allowed.
// Only members that would be encoded using a binary.Encoder are considered.
func Hash(v interface{}) (id.ID, error) {
id := id.ID{}
o, err := binary.Box(v)
if err != nil {
return id, err
}
h := sha1Pool.Get().(hash.Hash)
h.Reset()
e := cyclic.Encoder(vle.Writer(h))
e.Object(o)
copy(id[:], h.Sum(nil))
sha1Pool.Put(h)
return id, e.Error()
}