2019-12-24 18:18:56 +00:00
|
|
|
package needle_map
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/opt"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/storage"
|
|
|
|
|
2020-11-17 06:26:58 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util/log"
|
2019-12-24 18:18:56 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/idx"
|
|
|
|
. "github.com/chrislusf/seaweedfs/weed/storage/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
//This map uses in memory level db
|
|
|
|
type MemDb struct {
|
|
|
|
db *leveldb.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMemDb() *MemDb {
|
|
|
|
opts := &opt.Options{}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
t := &MemDb{}
|
|
|
|
if t.db, err = leveldb.Open(storage.NewMemStorage(), opts); err != nil {
|
2020-11-17 06:26:58 +00:00
|
|
|
log.Infof("MemDb fails to open: %v", err)
|
2019-12-24 18:18:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2020-08-19 00:04:28 +00:00
|
|
|
func (cm *MemDb) Set(key NeedleId, offset Offset, size Size) error {
|
2019-12-24 18:18:56 +00:00
|
|
|
|
|
|
|
bytes := ToBytes(key, offset, size)
|
|
|
|
|
|
|
|
if err := cm.db.Put(bytes[0:NeedleIdSize], bytes[NeedleIdSize:NeedleIdSize+OffsetSize+SizeSize], nil); err != nil {
|
|
|
|
return fmt.Errorf("failed to write temp leveldb: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *MemDb) Delete(key NeedleId) error {
|
|
|
|
bytes := make([]byte, NeedleIdSize)
|
|
|
|
NeedleIdToBytes(bytes, key)
|
|
|
|
return cm.db.Delete(bytes, nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
func (cm *MemDb) Get(key NeedleId) (*NeedleValue, bool) {
|
|
|
|
bytes := make([]byte, NeedleIdSize)
|
|
|
|
NeedleIdToBytes(bytes[0:NeedleIdSize], key)
|
|
|
|
data, err := cm.db.Get(bytes, nil)
|
|
|
|
if err != nil || len(data) != OffsetSize+SizeSize {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
offset := BytesToOffset(data[0:OffsetSize])
|
2020-08-19 00:04:28 +00:00
|
|
|
size := BytesToSize(data[OffsetSize : OffsetSize+SizeSize])
|
2019-12-24 18:18:56 +00:00
|
|
|
return &NeedleValue{Key: key, Offset: offset, Size: size}, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Visit visits all entries or stop if any error when visiting
|
|
|
|
func (cm *MemDb) AscendingVisit(visit func(NeedleValue) error) (ret error) {
|
|
|
|
iter := cm.db.NewIterator(nil, nil)
|
|
|
|
for iter.Next() {
|
|
|
|
key := BytesToNeedleId(iter.Key())
|
|
|
|
data := iter.Value()
|
|
|
|
offset := BytesToOffset(data[0:OffsetSize])
|
2020-08-19 00:04:28 +00:00
|
|
|
size := BytesToSize(data[OffsetSize : OffsetSize+SizeSize])
|
2019-12-24 18:18:56 +00:00
|
|
|
|
|
|
|
needle := NeedleValue{Key: key, Offset: offset, Size: size}
|
|
|
|
ret = visit(needle)
|
|
|
|
if ret != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
iter.Release()
|
|
|
|
ret = iter.Error()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-12 19:42:36 +00:00
|
|
|
func (cm *MemDb) SaveToIdx(idxName string) (ret error) {
|
2019-12-24 18:18:56 +00:00
|
|
|
idxFile, err := os.OpenFile(idxName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer idxFile.Close()
|
|
|
|
|
|
|
|
return cm.AscendingVisit(func(value NeedleValue) error {
|
2020-09-12 19:42:36 +00:00
|
|
|
if value.Offset.IsZero() || value.Size.IsDeleted() {
|
2020-01-08 17:45:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-12-24 18:18:56 +00:00
|
|
|
_, err := idxFile.Write(value.ToBytes())
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *MemDb) LoadFromIdx(idxName string) (ret error) {
|
|
|
|
idxFile, err := os.OpenFile(idxName, os.O_RDONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer idxFile.Close()
|
|
|
|
|
2020-08-19 00:04:28 +00:00
|
|
|
return idx.WalkIndexFile(idxFile, func(key NeedleId, offset Offset, size Size) error {
|
2020-08-19 00:35:19 +00:00
|
|
|
if offset.IsZero() || size.IsDeleted() {
|
2020-01-08 17:45:03 +00:00
|
|
|
return cm.Delete(key)
|
2019-12-24 18:18:56 +00:00
|
|
|
}
|
|
|
|
return cm.Set(key, offset, size)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
2020-03-10 05:29:02 +00:00
|
|
|
|
|
|
|
func (cm *MemDb) Close() {
|
|
|
|
cm.db.Close()
|
|
|
|
}
|