/
storage.go
97 lines (79 loc) · 2.74 KB
/
storage.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package storage
import (
"io"
"github.com/juju/blobstore/v3"
"github.com/juju/mgo/v3"
)
const (
// metadataDB is the name of the blobstore metadata database.
metadataDB = "juju"
// blobstoreDB is the name of the blobstore GridFS database.
blobstoreDB = "blobstore"
)
// Storage is an interface providing methods for storing and retrieving
// data by path.
type Storage interface {
// Get returns an io.ReadCloser for data at path, namespaced to the
// model.
//
// If the data is still being uploaded and is not fully written yet, a
// blobstore.ErrUploadPending error is returned. This means the path is
// valid but the caller should try again later to retrieve the data.
Get(path string) (r io.ReadCloser, length int64, err error)
// Put stores data from reader at path, namespaced to the model.
Put(path string, r io.Reader, length int64) error
// PutAndCheckHash stores data from reader at path, namespaced to
// the model. It also ensures the stored data has the correct
// hash.
PutAndCheckHash(path string, r io.Reader, length int64, hash string) error
// Remove removes data at path, namespaced to the model.
Remove(path string) error
}
// NewStorage returns a Storage for the model with the specified UUID.
func NewStorage(modelUUID string, session *mgo.Session) Storage {
return stateStorage{modelUUID, session}
}
type stateStorage struct {
modelUUID string
session *mgo.Session
}
func (s stateStorage) blobstore() (*mgo.Session, blobstore.ManagedStorage) {
session := s.session.Copy()
rs := blobstore.NewGridFS(blobstoreDB, blobstoreDB, session)
db := session.DB(metadataDB)
return session, blobstore.NewManagedStorage(db, rs)
}
func (s stateStorage) Get(path string) (r io.ReadCloser, length int64, err error) {
session, ms := s.blobstore()
r, length, err = ms.GetForBucket(s.modelUUID, path)
if err != nil {
session.Close()
return nil, -1, err
}
return &stateStorageReadCloser{r, session}, length, nil
}
func (s stateStorage) Put(path string, r io.Reader, length int64) error {
session, ms := s.blobstore()
defer session.Close()
return ms.PutForBucket(s.modelUUID, path, r, length)
}
func (s stateStorage) PutAndCheckHash(path string, r io.Reader, length int64, hash string) error {
session, ms := s.blobstore()
defer session.Close()
return ms.PutForBucketAndCheckHash(s.modelUUID, path, r, length, hash)
}
func (s stateStorage) Remove(path string) error {
session, ms := s.blobstore()
defer session.Close()
return ms.RemoveForBucket(s.modelUUID, path)
}
type stateStorageReadCloser struct {
io.ReadCloser
session *mgo.Session
}
func (r *stateStorageReadCloser) Close() error {
r.session.Close()
return r.ReadCloser.Close()
}