diff --git a/weed/storage/needle_map/memdb.go b/weed/storage/needle_map/memdb.go index d3d47b605..fb3d0130d 100644 --- a/weed/storage/needle_map/memdb.go +++ b/weed/storage/needle_map/memdb.go @@ -1,9 +1,11 @@ package needle_map import ( + "bytes" "fmt" "io" "os" + "sort" "github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb/iterator" @@ -34,7 +36,6 @@ func NewMemDb() *MemDb { } func (cm *MemDb) Set(key NeedleId, offset Offset, size Size) error { - bytes := ToBytes(key, offset, size) 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 } +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) { iter := cm.db.NewIterator(nil, nil) if iter.First() { @@ -122,7 +148,7 @@ func (cm *MemDb) SaveToIdx(idxName string) (ret error) { idxFile.Close() }() - return cm.AscendingVisit(func(value NeedleValue) error { + return cm.AscendingVisitByOffset(func(value NeedleValue) error { if value.Offset.IsZero() || value.Size.IsDeleted() { return nil }