Skip to content

Commit

Permalink
updated names
Browse files Browse the repository at this point in the history
  • Loading branch information
harshil-goel committed Oct 25, 2024
1 parent 989f54b commit ad0191f
Showing 1 changed file with 54 additions and 33 deletions.
87 changes: 54 additions & 33 deletions posting/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,23 @@ type MutableLayer struct {

// Since we are storing the commitedEntries and currentEntries separetly. We can cache things things that are
// going to be used repeatedly.
deleteAllMarker uint64 // Stores the lastest deleteAllMarker found in the posting list
uidsH map[uint64]*pb.Posting // Stores the uid to posting mapping in commitedEntreis
uidsHtime uint64 // Stores the latest commitTs in the commitedEntries
length int // Stores the length of the posting list until commitedEntires
deleteAllMarker uint64 // Stores the lastest deleteAllMarker found in the posting list
commitedUids map[uint64]*pb.Posting // Stores the uid to posting mapping in commitedEntreis
commitedUidsTime uint64 // Stores the latest commitTs in the commitedEntries
length int // Stores the length of the posting list until commitedEntries

// We also cache some thigns requried for us to update currentEntries faster
uidMap map[uint64]int // Stores the uid to index mapping in the currentEntries posting list
currentUids map[uint64]int // Stores the uid to index mapping in the currentEntries posting list
}

func newMutableLayer() *MutableLayer {
return &MutableLayer{
commitedEntries: make(map[uint64]*pb.PostingList),
readTs: 0,
deleteAllMarker: math.MaxUint64,
length: math.MaxInt,
uidsH: make(map[uint64]*pb.Posting),
uidsHtime: math.MaxUint64,
commitedEntries: make(map[uint64]*pb.PostingList),
readTs: 0,
deleteAllMarker: math.MaxUint64,
length: math.MaxInt,
commitedUids: make(map[uint64]*pb.Posting),
commitedUidsTime: math.MaxUint64,
}
}

Expand All @@ -127,12 +127,12 @@ func (mm *MutableLayer) clone() *MutableLayer {
return nil
}
return &MutableLayer{
commitedEntries: mm.commitedEntries,
readTs: 0,
deleteAllMarker: mm.deleteAllMarker,
uidsH: mm.uidsH,
length: mm.length,
uidsHtime: mm.uidsHtime,
commitedEntries: mm.commitedEntries,
readTs: 0,
deleteAllMarker: mm.deleteAllMarker,
commitedUids: mm.commitedUids,
length: mm.length,
commitedUidsTime: mm.commitedUidsTime,
}
}

Expand All @@ -146,7 +146,7 @@ func (mm *MutableLayer) setCurrentEntries(ts uint64, pl *pb.PostingList) {

mm.readTs = ts
mm.currentEntries = pl
clear(mm.uidMap)
clear(mm.currentUids)
mm.deleteAllMarker = math.MaxUint64
}

Expand Down Expand Up @@ -275,10 +275,10 @@ func (mm *MutableLayer) insertOldPosting(pl *pb.PostingList) {
}
}
// We insert old postings in reverse order. So we only need to read the first update to an UID
if _, ok := mm.uidsH[mpost.Uid]; !ok {
mm.uidsH[mpost.Uid] = mpost
if _, ok := mm.commitedUids[mpost.Uid]; !ok {
mm.commitedUids[mpost.Uid] = mpost
}
mm.uidsHtime = x.Max(mpost.CommitTs, mm.uidsHtime)
mm.commitedUidsTime = x.Max(mpost.CommitTs, mm.commitedUidsTime)
if mm.length == math.MaxInt64 {
mm.length = 0
}
Expand All @@ -292,13 +292,13 @@ func (mm *MutableLayer) insertOldPosting(pl *pb.PostingList) {
}

func (mm *MutableLayer) populateUidMap(pl *pb.PostingList) {
if mm.uidMap != nil {
if mm.currentUids != nil {
return
}

mm.uidMap = make(map[uint64]int, len(pl.Postings))
mm.currentUids = make(map[uint64]int, len(pl.Postings))
for i, post := range pl.Postings {
mm.uidMap[post.Uid] = i
mm.currentUids[post.Uid] = i
}
}

Expand All @@ -311,11 +311,11 @@ func (mm *MutableLayer) insertPosting(mpost *pb.Posting) {

if mpost.Uid != 0 {
mm.populateUidMap(mm.currentEntries)
if postIndex, ok := mm.uidMap[mpost.Uid]; ok {
if postIndex, ok := mm.currentUids[mpost.Uid]; ok {
mm.currentEntries.Postings[postIndex] = mpost
} else {
mm.currentEntries.Postings = append(mm.currentEntries.Postings, mpost)
mm.uidMap[mpost.Uid] = len(mm.currentEntries.Postings) - 1
mm.currentUids[mpost.Uid] = len(mm.currentEntries.Postings) - 1
}
return
}
Expand All @@ -337,11 +337,29 @@ func (mm *MutableLayer) findPosting(readTs, uid uint64) (bool, *pb.Posting) {
}

getPosting := func() *pb.Posting {
posI, ok := mm.uidMap[uid]
if ok {
return mm.currentEntries.Postings[posI]
if readTs >= mm.commitedUidsTime {
posI, ok := mm.currentUids[uid]
if ok {
return mm.currentEntries.Postings[posI]
}
} else {
var posting *pb.Posting
mm.iterate(func(ts uint64, pl *pb.PostingList) {
for _, mpost := range pl.Postings {
if mpost.Uid == uid {
if posting == nil {
posting = mpost
} else {
if posting.CommitTs <= mpost.CommitTs {
posting = mpost
}
}
}
}
}, readTs)
return posting
}
return mm.uidsH[uid]
return mm.commitedUids[uid]
}

posting := getPosting()
Expand Down Expand Up @@ -861,8 +879,11 @@ func (l *List) setMutationAfterCommit(startTs, commitTs uint64, pl *pb.PostingLi
l.mutationMap.deleteAllMarker = commitTs
}

l.mutationMap.uidsH[mpost.Uid] = mpost
l.mutationMap.uidsHtime = x.Max(l.mutationMap.uidsHtime, commitTs)
if l.mutationMap.commitedUids != nil {
l.mutationMap.commitedUids[mpost.Uid] = mpost
}
l.mutationMap.commitedUidsTime = x.Max(l.mutationMap.commitedUidsTime, commitTs)

if mpost.Op == Del {
l.mutationMap.length -= 1
} else {
Expand All @@ -872,7 +893,7 @@ func (l *List) setMutationAfterCommit(startTs, commitTs uint64, pl *pb.PostingLi

l.mutationMap.currentEntries = nil
l.mutationMap.readTs = 0
l.mutationMap.uidMap = nil
l.mutationMap.currentUids = nil

if pl.CommitTs != 0 {
l.maxTs = x.Max(l.maxTs, pl.CommitTs)
Expand Down

0 comments on commit ad0191f

Please sign in to comment.