mount.go 4.26 KB
Newer Older
Tommi Virtanen's avatar
Tommi Virtanen committed
1 2 3 4 5 6
// Package mount provides a Datastore that has other Datastores
// mounted at various key prefixes.
package mount

import (
	"errors"
Jeromy's avatar
Jeromy committed
7
	"io"
8
	"sort"
Tommi Virtanen's avatar
Tommi Virtanen committed
9 10
	"strings"

Jeromy's avatar
Jeromy committed
11 12 13
	"github.com/ipfs/go-datastore"
	"github.com/ipfs/go-datastore/keytransform"
	"github.com/ipfs/go-datastore/query"
Tommi Virtanen's avatar
Tommi Virtanen committed
14 15 16 17 18 19 20 21 22 23 24
)

var (
	ErrNoMount = errors.New("no datastore mounted for this key")
)

type Mount struct {
	Prefix    datastore.Key
	Datastore datastore.Datastore
}

25 26 27 28 29 30 31 32 33 34 35 36 37 38
type MountSlice []Mount

func (m MountSlice) Len() int {
	return len(m)
}

func (m MountSlice) Less(i, j int) bool {
	return m[i].Prefix.String() > m[j].Prefix.String()
}

func (m MountSlice) Swap(i, j int) {
	m[i], m[j] = m[j], m[i]
}

Tommi Virtanen's avatar
Tommi Virtanen committed
39 40 41 42 43 44
func New(mounts []Mount) *Datastore {
	// make a copy so we're sure it doesn't mutate
	m := make([]Mount, len(mounts))
	for i, v := range mounts {
		m[i] = v
	}
45
	sort.Sort(MountSlice(m))
Tommi Virtanen's avatar
Tommi Virtanen committed
46 47 48 49 50 51 52 53 54
	return &Datastore{mounts: m}
}

type Datastore struct {
	mounts []Mount
}

var _ datastore.Datastore = (*Datastore)(nil)

55
func (d *Datastore) lookup(key datastore.Key) (ds datastore.Datastore, mountpoint, rest datastore.Key) {
Tommi Virtanen's avatar
Tommi Virtanen committed
56
	for _, m := range d.mounts {
57
		if m.Prefix.Equal(key) || m.Prefix.IsAncestorOf(key) {
Tommi Virtanen's avatar
Tommi Virtanen committed
58 59
			s := strings.TrimPrefix(key.String(), m.Prefix.String())
			k := datastore.NewKey(s)
60
			return m.Datastore, m.Prefix, k
Tommi Virtanen's avatar
Tommi Virtanen committed
61 62
		}
	}
63
	return nil, datastore.NewKey("/"), key
Tommi Virtanen's avatar
Tommi Virtanen committed
64 65 66
}

func (d *Datastore) Put(key datastore.Key, value interface{}) error {
67
	ds, _, k := d.lookup(key)
Tommi Virtanen's avatar
Tommi Virtanen committed
68 69 70 71 72 73 74
	if ds == nil {
		return ErrNoMount
	}
	return ds.Put(k, value)
}

func (d *Datastore) Get(key datastore.Key) (value interface{}, err error) {
75
	ds, _, k := d.lookup(key)
Tommi Virtanen's avatar
Tommi Virtanen committed
76 77 78 79 80 81 82
	if ds == nil {
		return nil, datastore.ErrNotFound
	}
	return ds.Get(k)
}

func (d *Datastore) Has(key datastore.Key) (exists bool, err error) {
83
	ds, _, k := d.lookup(key)
Tommi Virtanen's avatar
Tommi Virtanen committed
84 85 86 87
	if ds == nil {
		return false, nil
	}
	return ds.Has(k)
Tommi Virtanen's avatar
Tommi Virtanen committed
88 89 90
}

func (d *Datastore) Delete(key datastore.Key) error {
91
	ds, _, k := d.lookup(key)
Tommi Virtanen's avatar
Tommi Virtanen committed
92 93 94 95
	if ds == nil {
		return datastore.ErrNotFound
	}
	return ds.Delete(k)
Tommi Virtanen's avatar
Tommi Virtanen committed
96 97 98
}

func (d *Datastore) Query(q query.Query) (query.Results, error) {
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	if len(q.Filters) > 0 ||
		len(q.Orders) > 0 ||
		q.Limit > 0 ||
		q.Offset > 0 {
		// TODO this is overly simplistic, but the only caller is
		// `ipfs refs local` for now, and this gets us moving.
		return nil, errors.New("mount only supports listing all prefixed keys in random order")
	}
	key := datastore.NewKey(q.Prefix)
	ds, mount, k := d.lookup(key)
	if ds == nil {
		return nil, errors.New("mount only supports listing a mount point")
	}
	// TODO support listing cross mount points too

	// delegate the query to the mounted datastore, while adjusting
	// keys in and out
	q2 := q
	q2.Prefix = k.String()
	wrapDS := keytransform.Wrap(ds, &keytransform.Pair{
		Convert: func(datastore.Key) datastore.Key {
			panic("this should never be called")
		},
		Invert: func(k datastore.Key) datastore.Key {
			return mount.Child(k)
		},
	})

	r, err := wrapDS.Query(q2)
	if err != nil {
		return nil, err
	}
	r = query.ResultsReplaceQuery(r, q)
	return r, nil
Tommi Virtanen's avatar
Tommi Virtanen committed
133
}
Jeromy's avatar
Jeromy committed
134

Jeromy's avatar
Jeromy committed
135 136 137 138 139 140 141 142 143 144 145 146
func (d *Datastore) Close() error {
	for _, d := range d.mounts {
		if c, ok := d.Datastore.(io.Closer); ok {
			err := c.Close()
			if err != nil {
				return err
			}
		}
	}
	return nil
}

Jeromy's avatar
Jeromy committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
type mountBatch struct {
	mounts map[string]datastore.Batch

	d *Datastore
}

func (d *Datastore) Batch() (datastore.Batch, error) {
	return &mountBatch{
		mounts: make(map[string]datastore.Batch),
		d:      d,
	}, nil
}

func (mt *mountBatch) lookupBatch(key datastore.Key) (datastore.Batch, datastore.Key, error) {
	child, loc, rest := mt.d.lookup(key)
	t, ok := mt.mounts[loc.String()]
	if !ok {
Jeromy's avatar
Jeromy committed
164
		bds, ok := child.(datastore.Batching)
Jeromy's avatar
Jeromy committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
		if !ok {
			return nil, datastore.NewKey(""), datastore.ErrBatchUnsupported
		}
		var err error
		t, err = bds.Batch()
		if err != nil {
			return nil, datastore.NewKey(""), err
		}
		mt.mounts[loc.String()] = t
	}
	return t, rest, nil
}

func (mt *mountBatch) Put(key datastore.Key, val interface{}) error {
	t, rest, err := mt.lookupBatch(key)
	if err != nil {
		return err
	}

	return t.Put(rest, val)
}

func (mt *mountBatch) Delete(key datastore.Key) error {
	t, rest, err := mt.lookupBatch(key)
	if err != nil {
		return err
	}

	return t.Delete(rest)
}

func (mt *mountBatch) Commit() error {
	for _, t := range mt.mounts {
		err := t.Commit()
		if err != nil {
			return err
		}
	}
	return nil
}