MCPcopy Create free account
hub / github.com/encoder-run/operator / EncodedObject

Method EncodedObject

pkg/cache/postgres/object.go:56–88  ·  view source on GitHub ↗

EncodedObject retrieves an encoded object from the database by its hash, handling any type.

(t plumbing.ObjectType, h plumbing.Hash)

Source from the content-addressed store, hash-verified

54
55// EncodedObject retrieves an encoded object from the database by its hash, handling any type.
56func (s *ObjectStorage) EncodedObject(t plumbing.ObjectType, h plumbing.Hash) (plumbing.EncodedObject, error) {
57 var object database.Object
58 var err error
59
60 if t == plumbing.AnyObject {
61 // Retrieve the first object that matches the hash across any object type
62 err = s.db.Where("hash = ? AND url = ?", h.String(), s.namespacePrefix).First(&object).Error
63 } else {
64 // Specific object type requested
65 err = s.db.Where("hash = ? AND type = ? AND url = ?", h.String(), t.String(), s.namespacePrefix).First(&object).Error
66 }
67
68 if err != nil {
69 if errors.Is(err, gorm.ErrRecordNotFound) {
70 return nil, plumbing.ErrObjectNotFound
71 }
72 return nil, err
73 }
74
75 o := &plumbing.MemoryObject{}
76 oType, err := plumbing.ParseObjectType(object.Type)
77 if err != nil {
78 return nil, err
79 }
80 o.SetType(oType)
81 o.SetSize(object.Size)
82 _, err = o.Write(object.Blob)
83 if err != nil {
84 return nil, err
85 }
86
87 return o, nil
88}
89
90// HasEncodedObject checks if an encoded object exists in the database by its hash.
91func (s *ObjectStorage) HasEncodedObject(hash plumbing.Hash) error {

Callers

nothing calls this directly

Calls 1

StringMethod · 0.45

Tested by

no test coverage detected