-
Notifications
You must be signed in to change notification settings - Fork 1
/
datastore.go
52 lines (43 loc) · 1.79 KB
/
datastore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package core
import (
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"
ldbopts "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/syndtr/goleveldb/leveldb/opt"
config "github.com/jbenet/go-ipfs/repo/config"
u "github.com/jbenet/go-ipfs/util"
ds2 "github.com/jbenet/go-ipfs/util/datastore2"
"github.com/jbenet/go-ipfs/util/debugerror"
)
func makeDatastore(cfg config.Datastore) (ds2.ThreadSafeDatastoreCloser, error) {
if len(cfg.Type) == 0 {
return nil, debugerror.Errorf("config datastore.type required")
}
switch cfg.Type {
case "leveldb":
return makeLevelDBDatastore(cfg)
case "memory":
return ds2.CloserWrap(syncds.MutexWrap(ds.NewMapDatastore())), nil
case "fs":
log.Warning("using fs.Datastore at .datastore for testing.")
d, err := fsds.NewDatastore(".datastore") // for testing!!
if err != nil {
return nil, err
}
ktd := ktds.Wrap(d, u.B58KeyConverter)
return ds2.CloserWrap(syncds.MutexWrap(ktd)), nil
}
return nil, debugerror.Errorf("Unknown datastore type: %s", cfg.Type)
}
func makeLevelDBDatastore(cfg config.Datastore) (ds2.ThreadSafeDatastoreCloser, error) {
if len(cfg.Path) == 0 {
return nil, debugerror.Errorf("config datastore.path required for leveldb")
}
ds, err := lds.NewDatastore(cfg.Path, &lds.Options{
// TODO don't import ldbopts. Get from go-datastore.leveldb
Compression: ldbopts.NoCompression,
})
return ds, debugerror.Wrap(err)
}