package blobs import ( "bytes" "context" "io" "nf-repo/internal/interfaces" "nf-repo/internal/model" "nf-repo/internal/opt" "sync" ) type bytesCloser struct { *bytes.Reader } func (r *bytesCloser) Close() error { return nil } type memHandler struct { m map[string][]byte lock sync.Mutex } func NewMemBlobHandler() interfaces.BlobHandler { return &memHandler{ m: map[string][]byte{}, } } func (m *memHandler) Stat(_ context.Context, _ string, h model.Hash) (int64, error) { m.lock.Lock() defer m.lock.Unlock() bs, found := m.m[h.String()] if !found { return 0, opt.ErrNotFound } return int64(len(bs)), nil } func (m *memHandler) Get(_ context.Context, _ string, h model.Hash) (io.ReadCloser, error) { m.lock.Lock() defer m.lock.Unlock() bs, found := m.m[h.String()] if !found { return nil, opt.ErrNotFound } return &bytesCloser{bytes.NewReader(bs)}, nil } func (m *memHandler) Put(_ context.Context, _ string, h model.Hash, rc io.ReadCloser) error { m.lock.Lock() defer m.lock.Unlock() defer rc.Close() all, err := io.ReadAll(rc) if err != nil { return err } m.m[h.String()] = all return nil } func (m *memHandler) Delete(_ context.Context, _ string, h model.Hash) error { m.lock.Lock() defer m.lock.Unlock() if _, found := m.m[h.String()]; !found { return opt.ErrNotFound } delete(m.m, h.String()) return nil }