| 1 | package levelds |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "path/filepath" |
| 6 | |
| 7 | "github.com/ipfs/kubo/plugin" |
| 8 | "github.com/ipfs/kubo/repo" |
| 9 | "github.com/ipfs/kubo/repo/fsrepo" |
| 10 | |
| 11 | levelds "github.com/ipfs/go-ds-leveldb" |
| 12 | ldbopts "github.com/syndtr/goleveldb/leveldb/opt" |
| 13 | ) |
| 14 | |
| 15 | // Plugins is exported list of plugins that will be loaded. |
| 16 | var Plugins = []plugin.Plugin{ |
| 17 | &leveldsPlugin{}, |
| 18 | } |
| 19 | |
| 20 | type leveldsPlugin struct{} |
| 21 | |
| 22 | var _ plugin.PluginDatastore = (*leveldsPlugin)(nil) |
| 23 | |
| 24 | func (*leveldsPlugin) Name() string { |
| 25 | return "ds-level" |
| 26 | } |
| 27 | |
| 28 | func (*leveldsPlugin) Version() string { |
| 29 | return "0.1.0" |
| 30 | } |
| 31 | |
| 32 | func (*leveldsPlugin) Init(_ *plugin.Environment) error { |
| 33 | return nil |
| 34 | } |
| 35 | |
| 36 | func (*leveldsPlugin) DatastoreTypeName() string { |
| 37 | return "levelds" |
| 38 | } |
| 39 | |
| 40 | type datastoreConfig struct { |
| 41 | path string |
| 42 | compression ldbopts.Compression |
| 43 | } |
| 44 | |
| 45 | // DatastoreConfigParser returns a configuration stub for a badger datastore |
| 46 | // from the given parameters. |
| 47 | func (*leveldsPlugin) DatastoreConfigParser() fsrepo.ConfigFromMap { |
| 48 | return func(params map[string]any) (fsrepo.DatastoreConfig, error) { |
| 49 | var c datastoreConfig |
| 50 | var ok bool |
| 51 | |
| 52 | c.path, ok = params["path"].(string) |
| 53 | if !ok { |
| 54 | return nil, fmt.Errorf("'path' field is missing or not string") |
| 55 | } |
| 56 | |
| 57 | switch cm := params["compression"]; cm { |
| 58 | case "none": |
| 59 | c.compression = ldbopts.NoCompression |
| 60 | case "snappy": |
| 61 | c.compression = ldbopts.SnappyCompression |
| 62 | case "", nil: |
| 63 | c.compression = ldbopts.DefaultCompression |
| 64 | default: |
| 65 | return nil, fmt.Errorf("unrecognized value for compression: %s", cm) |
| 66 | } |
| 67 | |
| 68 | return &c, nil |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func (c *datastoreConfig) DiskSpec() fsrepo.DiskSpec { |
| 73 | return map[string]any{ |
| 74 | "type": "levelds", |
| 75 | "path": c.path, |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func (c *datastoreConfig) Create(path string) (repo.Datastore, error) { |
| 80 | p := c.path |
| 81 | if !filepath.IsAbs(p) { |
| 82 | p = filepath.Join(path, p) |
| 83 | } |
| 84 | |
| 85 | return levelds.NewDatastore(p, &levelds.Options{ |
| 86 | Compression: c.compression, |
| 87 | }) |
| 88 | } |