cachemanager: base of get method

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
docker-18.09
Tonis Tiigi 2017-05-25 17:34:59 -07:00
parent 9c8b68b555
commit 81b0246dc5
2 changed files with 51 additions and 19 deletions

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"os" "os"
"path/filepath" "path/filepath"
"sync"
"time" "time"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
@ -37,25 +38,25 @@ type GCPolicy struct {
type SnapshotRef interface { type SnapshotRef interface {
Mountable Mountable
Releasable Release() error
Size() (int64, error) Size() (int64, error)
// Prepare() / ChainID() / Meta() // Prepare() / ChainID() / Meta()
} }
type ActiveRef interface { type ActiveRef interface {
Mountable Mountable
Releasable Release() (SnapshotRef, error)
ReleaseAndCommit(ctx context.Context) (SnapshotRef, error) ReleaseAndCommit(ctx context.Context) (SnapshotRef, error)
Size() (int64, error) Size() (int64, error)
} }
type Mountable interface { type Mountable interface {
Mount() (string, error) // containerd.Mount Mount() (Mount, error)
Unmount() error
} }
type Releasable interface { type Mount interface { // replace with containerd.Mount
Release() error Mount() (string, error)
Unmount() error
} }
type CacheAccessor interface { type CacheAccessor interface {
@ -77,20 +78,12 @@ type CacheManager interface {
} }
type cacheManager struct { type cacheManager struct {
db *bolt.DB // note: no particual reason for bolt db *bolt.DB // note: no particual reason for bolt
items map[string]*cacheRecord records map[string]*cacheRecord
mu sync.Mutex
CacheManagerOpt CacheManagerOpt
} }
type snapshotRef struct {
}
type cacheRecord struct {
active bool
// meta SnapMeta
refs map[*snapshotRef]struct{}
}
func NewCacheManager(opt CacheManagerOpt) (CacheManager, error) { func NewCacheManager(opt CacheManagerOpt) (CacheManager, error) {
if err := os.MkdirAll(opt.Root, 0700); err != nil { if err := os.MkdirAll(opt.Root, 0700); err != nil {
return nil, errors.Wrapf(err, "failed to create %s", opt.Root) return nil, errors.Wrapf(err, "failed to create %s", opt.Root)
@ -105,7 +98,7 @@ func NewCacheManager(opt CacheManagerOpt) (CacheManager, error) {
cm := &cacheManager{ cm := &cacheManager{
CacheManagerOpt: opt, CacheManagerOpt: opt,
db: db, db: db,
items: make(map[string]*cacheRecord), records: make(map[string]*cacheRecord),
} }
if err := cm.init(); err != nil { if err := cm.init(); err != nil {
@ -131,7 +124,14 @@ func (cm *cacheManager) Close() error {
} }
func (cm *cacheManager) Get(id string) (SnapshotRef, error) { func (cm *cacheManager) Get(id string) (SnapshotRef, error) {
return nil, errors.New("Get not implemented") cm.mu.Lock()
defer cm.mu.Unlock()
rec, ok := cm.records[id]
if !ok {
// TODO: lazy-load from Snapshotter
return nil, errors.Errorf("not found")
}
return rec.ref(), nil
} }
func (cm *cacheManager) New(id string, s SnapshotRef) (ActiveRef, error) { func (cm *cacheManager) New(id string, s SnapshotRef) (ActiveRef, error) {
return nil, errors.New("New not implemented") return nil, errors.New("New not implemented")
@ -151,3 +151,32 @@ func (cm *cacheManager) Prune(ctx context.Context) (map[string]int64, error) {
func (cm *cacheManager) GC(ctx context.Context) error { func (cm *cacheManager) GC(ctx context.Context) error {
return errors.New("GC not implemented") return errors.New("GC not implemented")
} }
type cacheRecord struct {
active bool
// meta SnapMeta
refs map[*snapshotRef]struct{}
}
// hold manager lock before calling
func (cr *cacheRecord) ref() *snapshotRef {
ref := &snapshotRef{cacheRecord: cr}
cr.refs[ref] = struct{}{}
return ref
}
type snapshotRef struct {
*cacheRecord
}
func (sr *snapshotRef) Mount() (Mount, error) {
return nil, errors.New("Mount not implemented")
}
func (sr *snapshotRef) Release() error {
return errors.New("Release not implemented")
}
func (sr *snapshotRef) Size() (int64, error) {
return -1, errors.New("Size not implemented")
}

View File

@ -16,6 +16,9 @@ func TestCacheManager(t *testing.T) {
cm, err := NewCacheManager(CacheManagerOpt{Root: tmpdir}) cm, err := NewCacheManager(CacheManagerOpt{Root: tmpdir})
assert.NoError(t, err) assert.NoError(t, err)
_, err = cm.Get("foobar")
assert.Error(t, err)
err = cm.Close() err = cm.Close()
assert.NoError(t, err) assert.NoError(t, err)
} }