-
Notifications
You must be signed in to change notification settings - Fork 7
/
get.go
53 lines (48 loc) · 1.21 KB
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package filesystem
import (
"errors"
"io"
"os"
"path"
"github.com/Cloud-Foundations/Dominator/lib/hash"
"github.com/Cloud-Foundations/Dominator/lib/objectcache"
)
func (objSrv *ObjectServer) getObjects(hashes []hash.Hash) (
*ObjectsReader, error) {
objectsReader := ObjectsReader{
objectServer: objSrv,
hashes: hashes,
nextIndex: -1,
sizes: make([]uint64, 0, len(hashes)),
}
for _, hashVal := range hashes {
size, err := objSrv.checkObject(hashVal)
if err != nil {
return nil, err
}
if size < 1 {
hashStr, _ := hashVal.MarshalText()
return nil, errors.New("missing object: " + string(hashStr))
}
objectsReader.sizes = append(objectsReader.sizes, size)
}
return &objectsReader, nil
}
func (or *ObjectsReader) nextObject() (uint64, io.ReadCloser, error) {
or.nextIndex++
if or.nextIndex >= int64(len(or.hashes)) {
return 0, nil, errors.New("all objects have been consumed")
}
filename := path.Join(or.objectServer.BaseDirectory,
objectcache.HashToFilename(or.hashes[or.nextIndex]))
file, err := os.Open(filename)
if err != nil {
return 0, nil, err
}
fi, err := file.Stat()
if err != nil {
file.Close()
return 0, nil, err
}
return uint64(fi.Size()), file, nil
}