datastore.go 1.79 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package core

import (
4 5 6 7 8
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
	fsds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/fs"
	ktds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/keytransform"
	lds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/leveldb"
	syncds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
9
	ldbopts "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/syndtr/goleveldb/leveldb/opt"
10

11
	config "github.com/jbenet/go-ipfs/repo/config"
12
	u "github.com/jbenet/go-ipfs/util"
13
	ds2 "github.com/jbenet/go-ipfs/util/datastore2"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
14
	"github.com/jbenet/go-ipfs/util/debugerror"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15 16
)

17
func makeDatastore(cfg config.Datastore) (ds2.ThreadSafeDatastoreCloser, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
18
	if len(cfg.Type) == 0 {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
19
		return nil, debugerror.Errorf("config datastore.type required")
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
20
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
22 23 24
	switch cfg.Type {
	case "leveldb":
		return makeLevelDBDatastore(cfg)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
26
	case "memory":
27
		return ds2.CloserWrap(syncds.MutexWrap(ds.NewMapDatastore())), nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
28 29 30 31 32 33 34

	case "fs":
		log.Warning("using fs.Datastore at .datastore for testing.")
		d, err := fsds.NewDatastore(".datastore") // for testing!!
		if err != nil {
			return nil, err
		}
35
		ktd := ktds.Wrap(d, u.B58KeyConverter)
36
		return ds2.CloserWrap(syncds.MutexWrap(ktd)), nil
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
37
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38

Brian Tiger Chow's avatar
Brian Tiger Chow committed
39
	return nil, debugerror.Errorf("Unknown datastore type: %s", cfg.Type)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40 41
}

42
func makeLevelDBDatastore(cfg config.Datastore) (ds2.ThreadSafeDatastoreCloser, error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
43
	if len(cfg.Path) == 0 {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
44
		return nil, debugerror.Errorf("config datastore.path required for leveldb")
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
45
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46

47
	ds, err := lds.NewDatastore(cfg.Path, &lds.Options{
48 49 50
		// TODO don't import ldbopts. Get from go-datastore.leveldb
		Compression: ldbopts.NoCompression,
	})
Brian Tiger Chow's avatar
Brian Tiger Chow committed
51
	return ds, debugerror.Wrap(err)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
52
}