From 51589a2246fccb2d4453992b3a6f9f0bec3091da Mon Sep 17 00:00:00 2001 From: Harshil Goel Date: Fri, 31 Jan 2025 04:13:07 +0530 Subject: [PATCH 01/15] uids interesect --- posting/list.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/posting/list.go b/posting/list.go index 34f75880c93..13d80e18be5 100644 --- a/posting/list.go +++ b/posting/list.go @@ -1712,8 +1712,21 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { // Pre-assign length to make it faster. l.RLock() // Use approximate length for initial capacity. - res := make([]uint64, 0, l.mutationMap.len()+codec.ApproxLen(l.plist.Pack)) + res := make([]uint64, 0, l.ApproxLen()) out := &pb.List{} + + if opt.Intersect != nil && len(opt.Intersect) < l.ApproxLen() { + for _, uid := range opt.Intersect { + ok, _, err := l.findPosting(uid, opt.ReadTs) + if err != nil { + return nil, err + } + res = append(res, p.Uid) + } + out.Uids = res + return out, nil + } + if l.mutationMap.len() == 0 && opt.Intersect != nil && len(l.plist.Splits) == 0 { if opt.ReadTs < l.minTs { l.RUnlock() @@ -1724,9 +1737,22 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { return out, nil } + var uidMin, uidMax uint64 = 0, 0 + if opt.Intersect != nil && len(opt.Intersect.Uids) > 0 { + uidMin = opt.Intersect.Uids[0] + uidMax = opt.Intersect.Uids[len(opt.Intersect.Uids)-1] + } + err := l.iterate(opt.ReadTs, opt.AfterUid, func(p *pb.Posting) error { if p.PostingType == pb.Posting_REF { + if p.Uid < uidMin { + return nil + } + if p.Uid > uidMax && uidMax > 0 { + return ErrStopIteration + } res = append(res, p.Uid) + if opt.First < 0 { // We need the last N. // TODO: This could be optimized by only considering some of the last UidBlocks. From 3aaeb0dcb9c7587b0e19567d82376691f0cffd7f Mon Sep 17 00:00:00 2001 From: Harshil Goel Date: Fri, 31 Jan 2025 04:17:48 +0530 Subject: [PATCH 02/15] fixed build --- posting/list.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/posting/list.go b/posting/list.go index 13d80e18be5..7e06db881d7 100644 --- a/posting/list.go +++ b/posting/list.go @@ -1715,13 +1715,15 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { res := make([]uint64, 0, l.ApproxLen()) out := &pb.List{} - if opt.Intersect != nil && len(opt.Intersect) < l.ApproxLen() { - for _, uid := range opt.Intersect { + if opt.Intersect != nil && len(opt.Intersect.Uids) < l.ApproxLen() { + for _, uid := range opt.Intersect.Uids { ok, _, err := l.findPosting(uid, opt.ReadTs) if err != nil { return nil, err } - res = append(res, p.Uid) + if ok { + res = append(res, uid) + } } out.Uids = res return out, nil From 679ab1884a78e6d98898fc7db8ee4202e53ddde0 Mon Sep 17 00:00:00 2001 From: Harshil Goel Date: Fri, 31 Jan 2025 05:12:55 +0530 Subject: [PATCH 03/15] fixed locks --- posting/list.go | 111 +++++++++++++++++++++++++----------------------- 1 file changed, 59 insertions(+), 52 deletions(-) diff --git a/posting/list.go b/posting/list.go index 7e06db881d7..653a4b0947b 100644 --- a/posting/list.go +++ b/posting/list.go @@ -1709,72 +1709,79 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { if opt.First == 0 { opt.First = math.MaxInt32 } - // Pre-assign length to make it faster. - l.RLock() - // Use approximate length for initial capacity. - res := make([]uint64, 0, l.ApproxLen()) - out := &pb.List{} - if opt.Intersect != nil && len(opt.Intersect.Uids) < l.ApproxLen() { - for _, uid := range opt.Intersect.Uids { - ok, _, err := l.findPosting(uid, opt.ReadTs) - if err != nil { - return nil, err - } - if ok { - res = append(res, uid) + getUidList := func() (*pb.List, error, bool) { + // Pre-assign length to make it faster. + l.RLock() + defer l.RUnlock() + // Use approximate length for initial capacity. + res := make([]uint64, 0, l.ApproxLen()) + out := &pb.List{} + + if opt.Intersect != nil && len(opt.Intersect.Uids) < l.ApproxLen() { + for _, uid := range opt.Intersect.Uids { + ok, _, err := l.findPosting(uid, opt.ReadTs) + if err != nil { + return nil, err, false + } + if ok { + res = append(res, uid) + } } + out.Uids = res + return out, nil, false } - out.Uids = res - return out, nil - } - if l.mutationMap.len() == 0 && opt.Intersect != nil && len(l.plist.Splits) == 0 { - if opt.ReadTs < l.minTs { - l.RUnlock() - return out, errors.Wrapf(ErrTsTooOld, "While reading UIDs") + if l.mutationMap.len() == 0 && opt.Intersect != nil && len(l.plist.Splits) == 0 { + if opt.ReadTs < l.minTs { + return out, errors.Wrapf(ErrTsTooOld, "While reading UIDs") + } + algo.IntersectCompressedWith(l.plist.Pack, opt.AfterUid, opt.Intersect, out) + return out, nil, false } - algo.IntersectCompressedWith(l.plist.Pack, opt.AfterUid, opt.Intersect, out) - l.RUnlock() - return out, nil - } - var uidMin, uidMax uint64 = 0, 0 - if opt.Intersect != nil && len(opt.Intersect.Uids) > 0 { - uidMin = opt.Intersect.Uids[0] - uidMax = opt.Intersect.Uids[len(opt.Intersect.Uids)-1] - } + var uidMin, uidMax uint64 = 0, 0 + if opt.Intersect != nil && len(opt.Intersect.Uids) > 0 { + uidMin = opt.Intersect.Uids[0] + uidMax = opt.Intersect.Uids[len(opt.Intersect.Uids)-1] + } - err := l.iterate(opt.ReadTs, opt.AfterUid, func(p *pb.Posting) error { - if p.PostingType == pb.Posting_REF { - if p.Uid < uidMin { - return nil - } - if p.Uid > uidMax && uidMax > 0 { - return ErrStopIteration - } - res = append(res, p.Uid) + err := l.iterate(opt.ReadTs, opt.AfterUid, func(p *pb.Posting) error { + if p.PostingType == pb.Posting_REF { + if p.Uid < uidMin { + return nil + } + if p.Uid > uidMax && uidMax > 0 { + return ErrStopIteration + } + res = append(res, p.Uid) - if opt.First < 0 { - // We need the last N. - // TODO: This could be optimized by only considering some of the last UidBlocks. - if len(res) > -opt.First { - res = res[1:] + if opt.First < 0 { + // We need the last N. + // TODO: This could be optimized by only considering some of the last UidBlocks. + if len(res) > -opt.First { + res = res[1:] + } + } else if len(res) > opt.First { + return ErrStopIteration } - } else if len(res) > opt.First { - return ErrStopIteration } + return nil + }) + if err != nil { + return out, errors.Wrapf(err, "cannot retrieve UIDs from list with key %s", + hex.EncodeToString(l.key)), false } - return nil - }) - l.RUnlock() - if err != nil { - return out, errors.Wrapf(err, "cannot retrieve UIDs from list with key %s", - hex.EncodeToString(l.key)) + out.Uids = res + return out, nil, true } // Do The intersection here as it's optimized. - out.Uids = res + out, err, applyIntersectWith := getUidList() + if err != nil || !applyIntersectWith { + return out, err + } + lenBefore := len(res) if opt.Intersect != nil { algo.IntersectWith(out, opt.Intersect, out) From 64c49efcffda29b23a88fcd5a290541444a4d879 Mon Sep 17 00:00:00 2001 From: Harshil Goel Date: Fri, 31 Jan 2025 05:58:43 +0530 Subject: [PATCH 04/15] first --- posting/list.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/posting/list.go b/posting/list.go index 653a4b0947b..a6e40336bb9 100644 --- a/posting/list.go +++ b/posting/list.go @@ -1734,7 +1734,7 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { if l.mutationMap.len() == 0 && opt.Intersect != nil && len(l.plist.Splits) == 0 { if opt.ReadTs < l.minTs { - return out, errors.Wrapf(ErrTsTooOld, "While reading UIDs") + return out, errors.Wrapf(ErrTsTooOld, "While reading UIDs"), false } algo.IntersectCompressedWith(l.plist.Pack, opt.AfterUid, opt.Intersect, out) return out, nil, false @@ -1782,7 +1782,7 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { return out, err } - lenBefore := len(res) + lenBefore := len(out.Uids) if opt.Intersect != nil { algo.IntersectWith(out, opt.Intersect, out) } From 24a714dc456fc764ceb9b90877f6ef651127f8a6 Mon Sep 17 00:00:00 2001 From: Harshil Goel Date: Sat, 1 Feb 2025 08:42:27 +0530 Subject: [PATCH 05/15] added comments --- posting/list.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/posting/list.go b/posting/list.go index a6e40336bb9..9ad0a9f0ba3 100644 --- a/posting/list.go +++ b/posting/list.go @@ -1718,20 +1718,6 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { res := make([]uint64, 0, l.ApproxLen()) out := &pb.List{} - if opt.Intersect != nil && len(opt.Intersect.Uids) < l.ApproxLen() { - for _, uid := range opt.Intersect.Uids { - ok, _, err := l.findPosting(uid, opt.ReadTs) - if err != nil { - return nil, err, false - } - if ok { - res = append(res, uid) - } - } - out.Uids = res - return out, nil, false - } - if l.mutationMap.len() == 0 && opt.Intersect != nil && len(l.plist.Splits) == 0 { if opt.ReadTs < l.minTs { return out, errors.Wrapf(ErrTsTooOld, "While reading UIDs"), false @@ -1740,6 +1726,20 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { return out, nil, false } + //if opt.Intersect != nil && len(opt.Intersect.Uids) < l.ApproxLen() { + // for _, uid := range opt.Intersect.Uids { + // ok, _, err := l.findPosting(uid, opt.ReadTs) + // if err != nil { + // return nil, err, false + // } + // if ok { + // res = append(res, uid) + // } + // } + // out.Uids = res + // return out, nil, false + //} + var uidMin, uidMax uint64 = 0, 0 if opt.Intersect != nil && len(opt.Intersect.Uids) > 0 { uidMin = opt.Intersect.Uids[0] From 7bb9a5f9084723242ccf9b773af8f9051ed574c4 Mon Sep 17 00:00:00 2001 From: Harshil Goel Date: Sat, 1 Feb 2025 09:43:12 +0530 Subject: [PATCH 06/15] temp --- posting/list.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/posting/list.go b/posting/list.go index 9ad0a9f0ba3..543725223c5 100644 --- a/posting/list.go +++ b/posting/list.go @@ -1726,19 +1726,19 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { return out, nil, false } - //if opt.Intersect != nil && len(opt.Intersect.Uids) < l.ApproxLen() { - // for _, uid := range opt.Intersect.Uids { - // ok, _, err := l.findPosting(uid, opt.ReadTs) - // if err != nil { - // return nil, err, false - // } - // if ok { - // res = append(res, uid) - // } - // } - // out.Uids = res - // return out, nil, false - //} + if opt.Intersect != nil && len(opt.Intersect.Uids) < l.ApproxLen() { + for _, uid := range opt.Intersect.Uids { + ok, _, err := l.findPosting(uid, opt.ReadTs) + if err != nil { + return nil, err, false + } + if ok { + res = append(res, uid) + } + } + out.Uids = res + return out, nil, false + } var uidMin, uidMax uint64 = 0, 0 if opt.Intersect != nil && len(opt.Intersect.Uids) > 0 { From 7d9ce9e9fdf4bd0cb37cd46f4794f698076f3f46 Mon Sep 17 00:00:00 2001 From: Harshil Goel Date: Sun, 2 Feb 2025 09:35:29 +0530 Subject: [PATCH 07/15] debugging something --- posting/list.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/posting/list.go b/posting/list.go index 543725223c5..409afc35350 100644 --- a/posting/list.go +++ b/posting/list.go @@ -1716,6 +1716,7 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { defer l.RUnlock() // Use approximate length for initial capacity. res := make([]uint64, 0, l.ApproxLen()) + resFromIterate := make([]uint64, 0) out := &pb.List{} if l.mutationMap.len() == 0 && opt.Intersect != nil && len(l.plist.Splits) == 0 { @@ -1734,10 +1735,12 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { } if ok { res = append(res, uid) + resFromIterate = append(resFromIterate, uid) } } - out.Uids = res - return out, nil, false + + //out.Uids = res + //return out, nil, false } var uidMin, uidMax uint64 = 0, 0 @@ -1772,6 +1775,20 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { return out, errors.Wrapf(err, "cannot retrieve UIDs from list with key %s", hex.EncodeToString(l.key)), false } + if len(resFromIterate) > 0 { + if len(res) != len(resFromIterate) { + fmt.Println(l.key, l.Print(), res, resFromIterate) + panic("hi") + } + if len(res) == len(resFromIterate) { + for i := range res { + if res[i] != resFromIterate[i] { + fmt.Println(l.key, l.Print(), res, resFromIterate) + panic("hi") + } + } + } + } out.Uids = res return out, nil, true } From c53ce4292bfe6d1def4a3e1e6fa8372d92bddca1 Mon Sep 17 00:00:00 2001 From: Harshil Goel Date: Sun, 2 Feb 2025 09:40:06 +0530 Subject: [PATCH 08/15] fixed debugging --- posting/list.go | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/posting/list.go b/posting/list.go index 409afc35350..c27a9612fdd 100644 --- a/posting/list.go +++ b/posting/list.go @@ -1775,17 +1775,15 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { return out, errors.Wrapf(err, "cannot retrieve UIDs from list with key %s", hex.EncodeToString(l.key)), false } - if len(resFromIterate) > 0 { - if len(res) != len(resFromIterate) { - fmt.Println(l.key, l.Print(), res, resFromIterate) - panic("hi") - } - if len(res) == len(resFromIterate) { - for i := range res { - if res[i] != resFromIterate[i] { - fmt.Println(l.key, l.Print(), res, resFromIterate) - panic("hi") - } + if len(res) != len(resFromIterate) { + fmt.Println(l.key, l.Print(), res, resFromIterate) + panic("hi") + } + if len(res) == len(resFromIterate) { + for i := range res { + if res[i] != resFromIterate[i] { + fmt.Println(l.key, l.Print(), res, resFromIterate) + panic("hi") } } } From 44c5d7c109c93dc416bdce7252ecdb0d719f5a20 Mon Sep 17 00:00:00 2001 From: Harshil Goel Date: Sun, 2 Feb 2025 09:45:22 +0530 Subject: [PATCH 09/15] fixed build --- posting/list.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/posting/list.go b/posting/list.go index c27a9612fdd..5888e08e435 100644 --- a/posting/list.go +++ b/posting/list.go @@ -1776,13 +1776,13 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { hex.EncodeToString(l.key)), false } if len(res) != len(resFromIterate) { - fmt.Println(l.key, l.Print(), res, resFromIterate) + fmt.Println(l.key, l.print(), res, resFromIterate) panic("hi") } if len(res) == len(resFromIterate) { for i := range res { if res[i] != resFromIterate[i] { - fmt.Println(l.key, l.Print(), res, resFromIterate) + fmt.Println(l.key, l.print(), res, resFromIterate) panic("hi") } } From b6f33effdbcd8949bf462c3c9c543455c4af8b30 Mon Sep 17 00:00:00 2001 From: Harshil Goel Date: Sun, 2 Feb 2025 09:53:53 +0530 Subject: [PATCH 10/15] fixed debugging --- posting/list.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/posting/list.go b/posting/list.go index 5888e08e435..5b40d9527b5 100644 --- a/posting/list.go +++ b/posting/list.go @@ -1716,6 +1716,7 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { defer l.RUnlock() // Use approximate length for initial capacity. res := make([]uint64, 0, l.ApproxLen()) + ranIterate := false resFromIterate := make([]uint64, 0) out := &pb.List{} @@ -1728,6 +1729,7 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { } if opt.Intersect != nil && len(opt.Intersect.Uids) < l.ApproxLen() { + ranIterate = true for _, uid := range opt.Intersect.Uids { ok, _, err := l.findPosting(uid, opt.ReadTs) if err != nil { @@ -1775,15 +1777,17 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { return out, errors.Wrapf(err, "cannot retrieve UIDs from list with key %s", hex.EncodeToString(l.key)), false } - if len(res) != len(resFromIterate) { - fmt.Println(l.key, l.print(), res, resFromIterate) - panic("hi") - } - if len(res) == len(resFromIterate) { - for i := range res { - if res[i] != resFromIterate[i] { - fmt.Println(l.key, l.print(), res, resFromIterate) - panic("hi") + if ranIterate { + if len(res) != len(resFromIterate) { + fmt.Println(l.key, l.print(), res, resFromIterate) + panic("hi") + } + if len(res) == len(resFromIterate) { + for i := range res { + if res[i] != resFromIterate[i] { + fmt.Println(l.key, l.print(), res, resFromIterate) + panic("hi") + } } } } From bc3b0cc57e38990ce644cb2a753a73712b90c585 Mon Sep 17 00:00:00 2001 From: Harshil Goel Date: Sun, 2 Feb 2025 10:11:24 +0530 Subject: [PATCH 11/15] fixed shit --- posting/list.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/posting/list.go b/posting/list.go index 5b40d9527b5..4dc2c22d453 100644 --- a/posting/list.go +++ b/posting/list.go @@ -1778,6 +1778,10 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { hex.EncodeToString(l.key)), false } if ranIterate { + if opt.Intersect != nil { + out.Uids = res + algo.IntersectWith(out, opt.Intersect, out) + } if len(res) != len(resFromIterate) { fmt.Println(l.key, l.print(), res, resFromIterate) panic("hi") From 2dd7f50097f25a3fcda1e3f9483e249504e62e96 Mon Sep 17 00:00:00 2001 From: Harshil Goel Date: Sun, 2 Feb 2025 14:09:01 +0530 Subject: [PATCH 12/15] fixed bug --- posting/list.go | 26 ++------------------------ 1 file changed, 2 insertions(+), 24 deletions(-) diff --git a/posting/list.go b/posting/list.go index 4dc2c22d453..271fd527437 100644 --- a/posting/list.go +++ b/posting/list.go @@ -1716,8 +1716,6 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { defer l.RUnlock() // Use approximate length for initial capacity. res := make([]uint64, 0, l.ApproxLen()) - ranIterate := false - resFromIterate := make([]uint64, 0) out := &pb.List{} if l.mutationMap.len() == 0 && opt.Intersect != nil && len(l.plist.Splits) == 0 { @@ -1729,7 +1727,6 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { } if opt.Intersect != nil && len(opt.Intersect.Uids) < l.ApproxLen() { - ranIterate = true for _, uid := range opt.Intersect.Uids { ok, _, err := l.findPosting(uid, opt.ReadTs) if err != nil { @@ -1737,12 +1734,11 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { } if ok { res = append(res, uid) - resFromIterate = append(resFromIterate, uid) } } - //out.Uids = res - //return out, nil, false + out.Uids = res + return out, nil, false } var uidMin, uidMax uint64 = 0, 0 @@ -1777,24 +1773,6 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { return out, errors.Wrapf(err, "cannot retrieve UIDs from list with key %s", hex.EncodeToString(l.key)), false } - if ranIterate { - if opt.Intersect != nil { - out.Uids = res - algo.IntersectWith(out, opt.Intersect, out) - } - if len(res) != len(resFromIterate) { - fmt.Println(l.key, l.print(), res, resFromIterate) - panic("hi") - } - if len(res) == len(resFromIterate) { - for i := range res { - if res[i] != resFromIterate[i] { - fmt.Println(l.key, l.print(), res, resFromIterate) - panic("hi") - } - } - } - } out.Uids = res return out, nil, true } From 5b6b8e65d05a704488063a88fa934f461cae19d0 Mon Sep 17 00:00:00 2001 From: Harshil Goel Date: Sun, 2 Feb 2025 14:09:43 +0530 Subject: [PATCH 13/15] fixed bug --- posting/list.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posting/list.go b/posting/list.go index 271fd527437..c33bbac6a3b 100644 --- a/posting/list.go +++ b/posting/list.go @@ -1728,7 +1728,7 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { if opt.Intersect != nil && len(opt.Intersect.Uids) < l.ApproxLen() { for _, uid := range opt.Intersect.Uids { - ok, _, err := l.findPosting(uid, opt.ReadTs) + ok, _, err := l.findPosting(opt.ReadTs, uid) if err != nil { return nil, err, false } From ffa3e859e0f89fb6fa99fd80bf9f662d38753634 Mon Sep 17 00:00:00 2001 From: Harshil Goel Date: Sun, 2 Feb 2025 14:20:41 +0530 Subject: [PATCH 14/15] updated --- posting/list.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/posting/list.go b/posting/list.go index c33bbac6a3b..14540181404 100644 --- a/posting/list.go +++ b/posting/list.go @@ -1727,8 +1727,9 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { } if opt.Intersect != nil && len(opt.Intersect.Uids) < l.ApproxLen() { + var pitr pIterator for _, uid := range opt.Intersect.Uids { - ok, _, err := l.findPosting(opt.ReadTs, uid) + ok, _, err := l.findPostingWithItr(opt.ReadTs, uid, pitr) if err != nil { return nil, err, false } @@ -2081,7 +2082,7 @@ func (l *List) FindPosting(readTs uint64, uid uint64) (found bool, pos *pb.Posti return l.findPosting(readTs, uid) } -func (l *List) findPosting(readTs uint64, uid uint64) (found bool, pos *pb.Posting, err error) { +func (l *List) findPostingWithItr(readTs uint64, uid uint64, pitr pIterator) (found bool, pos *pb.Posting, err error) { // Iterate starts iterating after the given argument, so we pass UID - 1 // TODO Find what happens when uid = math.MaxUint64 searchFurther, pos := l.mutationMap.findPosting(readTs, uid) @@ -2092,7 +2093,6 @@ func (l *List) findPosting(readTs uint64, uid uint64) (found bool, pos *pb.Posti return false, nil, nil } - var pitr pIterator err = pitr.seek(l, uid-1, 0) if err != nil { return false, nil, errors.Wrapf(err, @@ -2116,6 +2116,11 @@ func (l *List) findPosting(readTs uint64, uid uint64) (found bool, pos *pb.Posti return false, nil, nil } +func (l *List) findPosting(readTs uint64, uid uint64) (found bool, pos *pb.Posting, err error) { + var pitr pIterator + return l.findPostingWithItr(readTs, uid, pitr) +} + // Facets gives facets for the posting representing value. func (l *List) Facets(readTs uint64, param *pb.FacetParams, langs []string, listType bool) ([]*pb.Facets, error) { From cbe5242cb77a4a111155d074627910a37037d77d Mon Sep 17 00:00:00 2001 From: Harshil Goel Date: Sun, 2 Feb 2025 15:35:45 +0530 Subject: [PATCH 15/15] push comments --- posting/list.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/posting/list.go b/posting/list.go index 14540181404..3f7c51254c6 100644 --- a/posting/list.go +++ b/posting/list.go @@ -1726,7 +1726,10 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { return out, nil, false } + // If we need to intersect and the number of elements are small, in that case it's better to + // just check each item is present or not. if opt.Intersect != nil && len(opt.Intersect.Uids) < l.ApproxLen() { + // Cache the iterator as it makes the search space smaller each time. var pitr pIterator for _, uid := range opt.Intersect.Uids { ok, _, err := l.findPostingWithItr(opt.ReadTs, uid, pitr) @@ -1742,6 +1745,8 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { return out, nil, false } + // If we are going to iterate over the list, in that case we only need to read between min and max + // of opt.Intersect. var uidMin, uidMax uint64 = 0, 0 if opt.Intersect != nil && len(opt.Intersect.Uids) > 0 { uidMin = opt.Intersect.Uids[0]