Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit aa092f5

Browse files
committed
plumbing: add HasEncodedObject method to Storer
This allows the user to check whether an object exists, without reading all the object data from storage. Issue: KBFS-2445
1 parent 2de4f03 commit aa092f5

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

plumbing/storer/object_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ func (o *MockObjectStorage) SetEncodedObject(obj plumbing.EncodedObject) (plumbi
133133
return plumbing.ZeroHash, nil
134134
}
135135

136+
func (o *MockObjectStorage) HasEncodedObject(h plumbing.Hash) error {
137+
for _, o := range o.db {
138+
if o.Hash() == h {
139+
return nil
140+
}
141+
}
142+
return plumbing.ErrObjectNotFound
143+
}
144+
136145
func (o *MockObjectStorage) EncodedObject(t plumbing.ObjectType, h plumbing.Hash) (plumbing.EncodedObject, error) {
137146
for _, o := range o.db {
138147
if o.Hash() == h {

storage/filesystem/object.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,32 @@ func (s *ObjectStorage) SetEncodedObject(o plumbing.EncodedObject) (plumbing.Has
126126
return o.Hash(), err
127127
}
128128

129+
// HasEncodedObject returns nil if the object exists, without actually
130+
// reading the object data from storage.
131+
func (s *ObjectStorage) HasEncodedObject(h plumbing.Hash) (err error) {
132+
// Check unpacked objects
133+
f, err := s.dir.Object(h)
134+
if err != nil {
135+
if !os.IsNotExist(err) {
136+
return err
137+
}
138+
// Fall through to check packed objects.
139+
} else {
140+
defer ioutil.CheckClose(f, &err)
141+
return nil
142+
}
143+
144+
// Check packed objects.
145+
if err := s.requireIndex(); err != nil {
146+
return err
147+
}
148+
_, _, offset := s.findObjectInPackfile(h)
149+
if offset == -1 {
150+
return plumbing.ErrObjectNotFound
151+
}
152+
return nil
153+
}
154+
129155
// EncodedObject returns the object with the given hash, by searching for it in
130156
// the packfile and the git object directories.
131157
func (s *ObjectStorage) EncodedObject(t plumbing.ObjectType, h plumbing.Hash) (plumbing.EncodedObject, error) {

storage/memory/storage.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ func (o *ObjectStorage) SetEncodedObject(obj plumbing.EncodedObject) (plumbing.H
115115
return h, nil
116116
}
117117

118+
func (o *ObjectStorage) HasEncodedObject(h plumbing.Hash) (err error) {
119+
_, ok := o.Objects[h]
120+
if !ok {
121+
return plumbing.ErrObjectNotFound
122+
}
123+
return nil
124+
}
125+
118126
func (o *ObjectStorage) EncodedObject(t plumbing.ObjectType, h plumbing.Hash) (plumbing.EncodedObject, error) {
119127
obj, ok := o.Objects[h]
120128
if !ok || (plumbing.AnyObject != t && obj.Type() != t) {

0 commit comments

Comments
 (0)