@cryptotaxi247 / kubo / commits / 4ba4ee3a0

feat(fsrepo/component.datastore) basic shell

Brian Tiger Chow committed Jan 14, 2015 at 08:13 UTC 4ba4ee3a0dfbf6506d15103471b3c7ad5f8a4c25
1 file changed +64
repo/fsrepo/component/datastore.go new
+64
@@ -0,0 +1,64 @@
1 +package component
2 +
3 +import (
4 + datastore "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
5 + levelds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/leveldb"
6 + ldbopts "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/syndtr/goleveldb/leveldb/opt"
7 + config "github.com/jbenet/go-ipfs/repo/config"
8 + dir "github.com/jbenet/go-ipfs/repo/fsrepo/dir"
9 + util "github.com/jbenet/go-ipfs/util"
10 + ds2 "github.com/jbenet/go-ipfs/util/datastore2"
11 + debugerror "github.com/jbenet/go-ipfs/util/debugerror"
12 +)
13 +
14 +var _ Component = &DatastoreComponent{}
15 +var _ Initializer = InitDatastoreComponent
16 +var _ InitializationChecker = DatastoreComponentIsInitialized
17 +
18 +func InitDatastoreComponent(path string, conf *config.Config) error {
19 + // The actual datastore contents are initialized lazily when Opened.
20 + // During Init, we merely check that the directory is writeable.
21 + dspath, err := config.DataStorePath(path)
22 + if err != nil {
23 + return err
24 + }
25 + if err := dir.Writable(dspath); err != nil {
26 + return debugerror.Errorf("datastore: %s", err)
27 + }
28 + return nil
29 +}
30 +
31 +// DatastoreComponentIsInitialized returns true if the datastore dir exists.
32 +func DatastoreComponentIsInitialized(path string) bool {
33 + dspath, err := config.DataStorePath(path)
34 + if err != nil {
35 + return false
36 + }
37 + if !util.FileExists(dspath) {
38 + return false
39 + }
40 + return true
41 +}
42 +
43 +// DatastoreComponent abstracts the datastore component of the FSRepo.
44 +// NB: create with makeDatastoreComponent function.
45 +type DatastoreComponent struct {
46 + path string
47 + ds ds2.ThreadSafeDatastoreCloser
48 +}
49 +
50 +// Open returns an error if the config file is not present.
51 +func (dsc *DatastoreComponent) Open() error {
52 + ds, err := levelds.NewDatastore(dsc.path, &levelds.Options{
53 + Compression: ldbopts.NoCompression,
54 + })
55 + if err != nil {
56 + return err
57 + }
58 + dsc.ds = ds
59 + return nil
60 +}
61 +
62 +func (dsc *DatastoreComponent) Close() error { return dsc.ds.Close() }
63 +func (dsc *DatastoreComponent) SetPath(p string) { dsc.path = p }
64 +func (dsc *DatastoreComponent) Datastore() datastore.ThreadSafeDatastore { return dsc.ds }