Skip to content

Commit c7b3748

Browse files
committed
storage: add a storage abstraction
There are several places where we want to expose an abstraction for storage for the object database. Add read-write and read-only storage abstractions and a backend abstraction that encapsulates one or more of these abstractions as interfaces that can be implemented. Provide a method to determine whether the data read from the storage is compressed, since pack data will be uncompressed for us, but most other storage will not be.
1 parent 29004d9 commit c7b3748

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

storage/backend.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package storage
2+
3+
// Backend is an encapsulation of a set of read-only and read-write interfaces
4+
// for reading and writing objects.
5+
type Backend interface {
6+
// Storage returns a read source and optionally a write source.
7+
// Generally, the write location, if present, should also be a read
8+
// location.
9+
Storage() (Storage, WritableStorage)
10+
}

storage/storage.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package storage
2+
3+
import "io"
4+
5+
// Storage implements an interface for reading, but not writing, objects in an
6+
// object database.
7+
type Storage interface {
8+
// Open returns a handle on an existing object keyed by the given object
9+
// ID. It returns an error if that file does not already exist.
10+
Open(oid []byte) (f io.ReadCloser, err error)
11+
12+
// Close closes the filesystem, after which no more operations are
13+
// allowed.
14+
Close() error
15+
16+
// Compressed indicates whether data read from this storage source will
17+
// be zlib-compressed.
18+
IsCompressed() bool
19+
}
20+
21+
// WritableStorage implements an interface for reading and writing objects in
22+
// an object database.
23+
type WritableStorage interface {
24+
Storage
25+
26+
// Store copies the data given in "r" to the unique object path given by
27+
// "oid". It returns an error if that file already exists (acting as if
28+
// the `os.O_EXCL` mode is given in a bitmask to os.Open).
29+
Store(oid []byte, r io.Reader) (n int64, err error)
30+
}

0 commit comments

Comments
 (0)