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
|
package gitobj
import (
"bytes"
"fmt"
"io"
"sync"
"github.com/git-lfs/gitobj/v2/errors"
)
// memoryStorer is an implementation of the storer interface that holds data for
// the object database in memory.
type memoryStorer struct {
// mu guards reads and writes to the map "fs" below.
mu *sync.Mutex
// fs maps a hex-encoded SHA to a bytes.Buffer wrapped in a no-op closer
// type.
fs map[string]*bufCloser
}
// newMemoryStorer initializes a new memoryStorer instance with the given
// initial set.
//
// A value of "nil" is acceptable and indicates that no entries shall be added
// to the memory storer at/during construction time.
func newMemoryStorer(m map[string]io.ReadWriter) *memoryStorer {
fs := make(map[string]*bufCloser, len(m))
for n, rw := range m {
fs[n] = &bufCloser{rw}
}
return &memoryStorer{
mu: new(sync.Mutex),
fs: fs,
}
}
// Store implements the storer.Store function and copies the data given in "r"
// into an object entry in the memory. If an object given by that SHA "sha" is
// already indexed in the database, Store will panic().
func (ms *memoryStorer) Store(sha []byte, r io.Reader) (n int64, err error) {
ms.mu.Lock()
defer ms.mu.Unlock()
key := fmt.Sprintf("%x", sha)
ms.fs[key] = &bufCloser{new(bytes.Buffer)}
return io.Copy(ms.fs[key], r)
}
// Open implements the storer.Open function, and returns a io.ReadWriteCloser
// for the given SHA. If a reader for the given SHA does not exist an error will
// be returned.
func (ms *memoryStorer) Open(sha []byte) (f io.ReadCloser, err error) {
ms.mu.Lock()
defer ms.mu.Unlock()
key := fmt.Sprintf("%x", sha)
if _, ok := ms.fs[key]; !ok {
return nil, errors.NoSuchObject(sha)
}
return ms.fs[key], nil
}
// Close closes the memory storer.
func (ms *memoryStorer) Close() error {
return nil
}
// IsCompressed returns true, because the memory storer returns compressed data.
func (ms *memoryStorer) IsCompressed() bool {
return true
}
// bufCloser wraps a type satisfying the io.ReadWriter interface with a no-op
// Close() function, thus implementing the io.ReadWriteCloser composite
// interface.
type bufCloser struct {
io.ReadWriter
}
// Close implements io.Closer, and returns nothing.
func (b *bufCloser) Close() error { return nil }
|