GetHash returns a hash object, create new one if nonexists
(txn *Transaction, key []byte)
| 37 | |
| 38 | // GetHash returns a hash object, create new one if nonexists |
| 39 | func GetHash(txn *Transaction, key []byte) (*Hash, error) { |
| 40 | hash := newHash(txn, key) |
| 41 | mkey := MetaKey(txn.db, key) |
| 42 | meta, err := txn.t.Get(mkey) |
| 43 | if err != nil { |
| 44 | if IsErrNotFound(err) { |
| 45 | return hash, nil |
| 46 | } |
| 47 | return nil, err |
| 48 | } |
| 49 | hmeta, err := DecodeHashMeta(meta) |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | if IsExpired(&hmeta.Object, Now()) { |
| 54 | return hash, nil |
| 55 | } |
| 56 | if hmeta.Type != ObjectHash { |
| 57 | return nil, ErrTypeMismatch |
| 58 | } |
| 59 | hash.meta = hmeta |
| 60 | hash.exists = true |
| 61 | return hash, nil |
| 62 | } |
| 63 | |
| 64 | //newHash creates a hash object |
| 65 | func newHash(txn *Transaction, key []byte) *Hash { |