S3-вложения и сверка Postgresql с MinIO
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
package blob
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type memObj struct {
|
||||
body []byte
|
||||
modified time.Time
|
||||
}
|
||||
|
||||
// Memory - двойник bucket для тестов без MinIO.
|
||||
type Memory struct {
|
||||
mu sync.Mutex
|
||||
m map[string]memObj
|
||||
}
|
||||
|
||||
func NewMemory() *Memory {
|
||||
return &Memory{m: map[string]memObj{}}
|
||||
}
|
||||
|
||||
func (m *Memory) Ping(ctx context.Context) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return ErrUnavailable
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Memory) Put(ctx context.Context, key, _ string, body []byte) error {
|
||||
return m.PutAt(ctx, key, body, time.Now().UTC())
|
||||
}
|
||||
|
||||
// PutAt записывает объект с заданным временем.
|
||||
func (m *Memory) PutAt(ctx context.Context, key string, body []byte, modified time.Time) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return ErrUnavailable
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.m[key] = memObj{body: append([]byte(nil), body...), modified: modified.UTC()}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Memory) Get(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, ErrUnavailable
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
obj, ok := m.m[key]
|
||||
if !ok {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return io.NopCloser(bytes.NewReader(obj.body)), nil
|
||||
}
|
||||
|
||||
func (m *Memory) Delete(ctx context.Context, key string) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return ErrUnavailable
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
delete(m.m, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Memory) List(ctx context.Context) ([]Object, int, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, 0, ErrUnavailable
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
out := make([]Object, 0, len(m.m))
|
||||
for key, obj := range m.m {
|
||||
out = append(out, Object{Key: key, Size: int64(len(obj.body)), Modified: obj.modified})
|
||||
}
|
||||
pages := 1
|
||||
if len(out) == 0 {
|
||||
pages = 1
|
||||
}
|
||||
return out, pages, nil
|
||||
}
|
||||
Reference in New Issue
Block a user