generated from RTradeLtd/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanup git branch for leveldb stuff
- Loading branch information
postables
committed
May 7, 2020
1 parent
4671873
commit 862d402
Showing
8 changed files
with
770 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License | ||
|
||
Copyright (c) 2016 Jeromy Johnson | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package leveldb | ||
|
||
import ( | ||
ds "github.com/ipfs/go-datastore" | ||
|
||
"github.com/syndtr/goleveldb/leveldb" | ||
"github.com/syndtr/goleveldb/leveldb/opt" | ||
) | ||
|
||
type leveldbBatch struct { | ||
b *leveldb.Batch | ||
db *leveldb.DB | ||
syncWrites bool | ||
} | ||
|
||
// Batch returns a new levelDB batcher | ||
func (d *Datastore) Batch() (ds.Batch, error) { | ||
return &leveldbBatch{ | ||
b: new(leveldb.Batch), | ||
db: d.db, | ||
syncWrites: d.syncWrites, | ||
}, nil | ||
} | ||
|
||
func (b *leveldbBatch) Put(key ds.Key, value []byte) error { | ||
b.b.Put(key.Bytes(), value) | ||
return nil | ||
} | ||
|
||
func (b *leveldbBatch) Commit() error { | ||
return b.db.Write(b.b, &opt.WriteOptions{Sync: b.syncWrites}) | ||
} | ||
|
||
func (b *leveldbBatch) Delete(key ds.Key) error { | ||
b.b.Delete(key.Bytes()) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package leveldb | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
|
||
ds "github.com/ipfs/go-datastore" | ||
dsq "github.com/ipfs/go-datastore/query" | ||
"github.com/syndtr/goleveldb/leveldb" | ||
"github.com/syndtr/goleveldb/leveldb/opt" | ||
"github.com/syndtr/goleveldb/leveldb/util" | ||
"go.uber.org/atomic" | ||
) | ||
|
||
var ( | ||
_ ds.Datastore = (*Datastore)(nil) | ||
_ ds.TxnDatastore = (*Datastore)(nil) | ||
// ErrClosed is an error message returned when the datastore is no longer open | ||
ErrClosed = errors.New("datastore closed") | ||
) | ||
|
||
// Datastore is a go-datastore implement using leveldb | ||
type Datastore struct { | ||
db *leveldb.DB | ||
path string | ||
closed *atomic.Bool | ||
syncWrites bool | ||
close sync.Once | ||
closeLock sync.RWMutex | ||
} | ||
|
||
// Options is an alias of syndtr/goleveldb/opt.Options which might be extended | ||
// in the future. | ||
type Options = opt.Options | ||
|
||
// NewDatastore returns a new datastore backed by leveldb | ||
func NewDatastore(path string, opts *Options) (*Datastore, error) { | ||
noSync := opts.NoSync | ||
db, err := leveldb.OpenFile(path, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ds := Datastore{ | ||
db: db, | ||
path: path, | ||
closed: atomic.NewBool(false), | ||
// if noSync is false this will be true | ||
syncWrites: !noSync, | ||
} | ||
return &ds, nil | ||
} | ||
|
||
// Put stores a key-value pair in leveldb | ||
func (d *Datastore) Put(key ds.Key, value []byte) (err error) { | ||
if d.closed.Load() { | ||
return ErrClosed | ||
} | ||
return d.db.Put(key.Bytes(), value, &opt.WriteOptions{Sync: d.syncWrites}) | ||
} | ||
|
||
// Sync is a noop | ||
func (d *Datastore) Sync(prefix ds.Key) error { | ||
return nil | ||
} | ||
|
||
// Get returns the value corresponding to the key | ||
func (d *Datastore) Get(key ds.Key) (value []byte, err error) { | ||
if d.closed.Load() { | ||
return nil, ErrClosed | ||
} | ||
val, err := d.db.Get(key.Bytes(), nil) | ||
if err != nil { | ||
return nil, handleGetError(err) | ||
} | ||
return val, nil | ||
} | ||
|
||
// Has returns whether or not we have the key | ||
func (d *Datastore) Has(key ds.Key) (exists bool, err error) { | ||
if d.closed.Load() { | ||
return false, ErrClosed | ||
} | ||
return ds.GetBackedHas(d, key) | ||
} | ||
|
||
// GetSize returns the size of the associated key | ||
func (d *Datastore) GetSize(key ds.Key) (size int, err error) { | ||
if d.closed.Load() { | ||
return 0, ErrClosed | ||
} | ||
return ds.GetBackedSize(d, key) | ||
} | ||
|
||
// Delete removed the key from our datastore | ||
func (d *Datastore) Delete(key ds.Key) (err error) { | ||
if d.closed.Load() { | ||
return ErrClosed | ||
} | ||
return d.db.Delete(key.Bytes(), &opt.WriteOptions{Sync: d.syncWrites}) | ||
} | ||
|
||
// Query searches for keys in our datastore | ||
func (d *Datastore) Query(q dsq.Query) (dsq.Results, error) { | ||
if d.closed.Load() { | ||
return nil, ErrClosed | ||
} | ||
// closing is only unsafe when there are pending iterators | ||
// so we only lock when closing, and invoking iterators (query) | ||
d.closeLock.Lock() | ||
var rnge *util.Range | ||
|
||
// make a copy of the query for the fallback naive query implementation. | ||
// don't modify the original so res.Query() returns the correct results. | ||
qNaive := q | ||
prefix := ds.NewKey(q.Prefix).String() | ||
if prefix != "/" { | ||
rnge = util.BytesPrefix([]byte(prefix + "/")) | ||
qNaive.Prefix = "" | ||
} | ||
iter := d.db.NewIterator(rnge, nil) | ||
res, err := query(iter, q, qNaive) | ||
d.closeLock.Unlock() | ||
return res, err | ||
} | ||
|
||
// DiskUsage returns the current disk size used by this levelDB. | ||
// For in-mem datastores, it will return 0. | ||
func (d *Datastore) DiskUsage() (du uint64, err error) { | ||
if d.closed.Load() { | ||
return 0, ErrClosed | ||
} | ||
err = filepath.Walk(d.path, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
du += uint64(info.Size()) | ||
return nil | ||
}) | ||
return | ||
} | ||
|
||
// Close shuts down leveldb | ||
func (d *Datastore) Close() (err error) { | ||
if d.closed.Load() { | ||
err = ErrClosed | ||
} | ||
d.close.Do(func() { | ||
d.closeLock.Lock() | ||
err = d.db.Close() | ||
d.closed.Store(true) | ||
d.closeLock.Unlock() | ||
}) | ||
return | ||
} |
Oops, something went wrong.