mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
Fix weed fix
This commit is contained in:
parent
281a643024
commit
6c1ce18541
|
@ -1,9 +1,11 @@
|
||||||
package needle_map
|
package needle_map
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"sort"
|
||||||
|
|
||||||
"github.com/syndtr/goleveldb/leveldb"
|
"github.com/syndtr/goleveldb/leveldb"
|
||||||
"github.com/syndtr/goleveldb/leveldb/iterator"
|
"github.com/syndtr/goleveldb/leveldb/iterator"
|
||||||
|
@ -34,7 +36,6 @@ func NewMemDb() *MemDb {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *MemDb) Set(key NeedleId, offset Offset, size Size) error {
|
func (cm *MemDb) Set(key NeedleId, offset Offset, size Size) error {
|
||||||
|
|
||||||
bytes := ToBytes(key, offset, size)
|
bytes := ToBytes(key, offset, size)
|
||||||
|
|
||||||
if err := cm.db.Put(bytes[0:NeedleIdSize], bytes[NeedleIdSize:NeedleIdSize+OffsetSize+SizeSize], nil); err != nil {
|
if err := cm.db.Put(bytes[0:NeedleIdSize], bytes[NeedleIdSize:NeedleIdSize+OffsetSize+SizeSize], nil); err != nil {
|
||||||
|
@ -76,6 +77,31 @@ func doVisit(iter iterator.Iterator, visit func(NeedleValue) error) (ret error)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cm *MemDb) AscendingVisitByOffset(visit func(NeedleValue) error) (ret error) {
|
||||||
|
var needles []NeedleValue
|
||||||
|
err := cm.AscendingVisit(func(value NeedleValue) error {
|
||||||
|
needles = append(needles, value)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
sort.Slice(needles, func(i, j int) bool {
|
||||||
|
i_bytes := make([]byte, OffsetSize)
|
||||||
|
j_bytes := make([]byte, OffsetSize)
|
||||||
|
OffsetToBytes(i_bytes, needles[i].Offset)
|
||||||
|
OffsetToBytes(j_bytes, needles[j].Offset)
|
||||||
|
return bytes.Compare(i_bytes, j_bytes) < 0
|
||||||
|
})
|
||||||
|
for _, needle := range needles {
|
||||||
|
ret = visit(needle)
|
||||||
|
if ret != nil {
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (cm *MemDb) AscendingVisit(visit func(NeedleValue) error) (ret error) {
|
func (cm *MemDb) AscendingVisit(visit func(NeedleValue) error) (ret error) {
|
||||||
iter := cm.db.NewIterator(nil, nil)
|
iter := cm.db.NewIterator(nil, nil)
|
||||||
if iter.First() {
|
if iter.First() {
|
||||||
|
@ -122,7 +148,7 @@ func (cm *MemDb) SaveToIdx(idxName string) (ret error) {
|
||||||
idxFile.Close()
|
idxFile.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return cm.AscendingVisit(func(value NeedleValue) error {
|
return cm.AscendingVisitByOffset(func(value NeedleValue) error {
|
||||||
if value.Offset.IsZero() || value.Size.IsDeleted() {
|
if value.Offset.IsZero() || value.Size.IsDeleted() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue