-
Notifications
You must be signed in to change notification settings - Fork 14
MB-27666: Nested Fields #339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CascadingRadium
wants to merge
19
commits into
master
Choose a base branch
from
nestedFields
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+354
−4
Open
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c50e9fe
bug fixes and new features.
CascadingRadium adbf3d9
Update nested_cache.go
CascadingRadium 5b326ae
Update nested_cache.go
CascadingRadium b84a43f
Update merge.go
CascadingRadium 4cea554
simplify
CascadingRadium 8923db7
Update nested_cache.go
CascadingRadium 2821924
Update nested_cache.go
CascadingRadium 34d406c
Update segment.go
CascadingRadium 3c1267a
Update segment.go
CascadingRadium 978473b
Update segment.go
CascadingRadium b535c42
Update segment.go
CascadingRadium ab5cc47
Update segment.go
CascadingRadium c8875c8
comments
CascadingRadium 7a27646
fix comments
CascadingRadium 61b5c91
fix count API part1
CascadingRadium 33dde9c
performance improvement
CascadingRadium be3a7df
gomod changes
CascadingRadium 9725db0
cache improvement
CascadingRadium b1c4a16
code review
CascadingRadium File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| package zap | ||
|
|
||
| import ( | ||
| "encoding/binary" | ||
| "sync" | ||
| ) | ||
|
|
||
| func newNestedIndexCache() *nestedIndexCache { | ||
| return &nestedIndexCache{} | ||
| } | ||
|
|
||
| type nestedIndexCache struct { | ||
| m sync.RWMutex | ||
|
|
||
| cache *nestedCacheEntry | ||
| } | ||
|
|
||
| // Clear clears the nested index cache, removing cached edge lists, ancestry, and descendants. | ||
| func (nc *nestedIndexCache) Clear() { | ||
| nc.m.Lock() | ||
| nc.cache = nil | ||
| nc.m.Unlock() | ||
| } | ||
|
|
||
| // Returns edgeList, ancestry, descendants | ||
| func (nc *nestedIndexCache) loadOrCreate(edgeListOffset uint64, mem []byte) *nestedCacheEntry { | ||
| nc.m.RLock() | ||
| if nc.cache != nil { | ||
| nc.m.RUnlock() | ||
| return nc.cache | ||
| } | ||
| nc.m.RUnlock() | ||
|
|
||
| nc.m.Lock() | ||
| defer nc.m.Unlock() | ||
|
|
||
| if nc.cache != nil { | ||
| return nc.cache | ||
| } | ||
|
|
||
| return nc.createAndCacheLOCKED(edgeListOffset, mem) | ||
| } | ||
|
|
||
| // createAndCacheLOCKED creates and caches a nested cache entry (edge list, ancestry, descendants) for the given edge list offset and memory slice. | ||
| func (sc *nestedIndexCache) createAndCacheLOCKED(edgeListOffset uint64, mem []byte) *nestedCacheEntry { | ||
| // pos stores the current read position | ||
| pos := edgeListOffset | ||
| // read number of subDocs which is also the number of edges | ||
| numEdges := binary.BigEndian.Uint64(mem[pos : pos+8]) | ||
| pos += 8 | ||
| // if no edges or no subDocs, return empty cache | ||
| if numEdges == 0 { | ||
| sc.cache = &nestedCacheEntry{} | ||
| return sc.cache | ||
| } | ||
| // edgeList as a map[node]parent | ||
| edgeList := make(map[uint64]uint64, numEdges) | ||
| for i := uint64(0); i < numEdges; i++ { | ||
| child := binary.BigEndian.Uint64(mem[pos : pos+8]) | ||
| pos += 8 | ||
| parent := binary.BigEndian.Uint64(mem[pos : pos+8]) | ||
| pos += 8 | ||
| edgeList[child] = parent | ||
| } | ||
| // build ancestry using DFS + memoization | ||
| ancestry := make(map[uint64][]uint64, numEdges) | ||
| // memoized DFS | ||
| var getAncestors func(uint64) []uint64 | ||
| getAncestors = func(node uint64) []uint64 { | ||
| // if already computed, return | ||
| if val, ok := ancestry[node]; ok { | ||
| return val | ||
| } | ||
| if parent, ok := edgeList[node]; ok { | ||
| // compute parent's ancestry + parent itself | ||
| res := append([]uint64{parent}, getAncestors(parent)...) | ||
| ancestry[node] = res | ||
| return res | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| for child := range edgeList { | ||
| // only store if non-empty ancestry | ||
| if v := getAncestors(child); len(v) > 0 { | ||
| ancestry[child] = v | ||
| } | ||
| } | ||
|
|
||
| descendants := make(map[uint64][]uint64, numEdges) | ||
|
|
||
| // Build descendants using ancestry | ||
| for node, parents := range ancestry { | ||
| for _, parent := range parents { | ||
| descendants[parent] = append(descendants[parent], node) | ||
| } | ||
| } | ||
|
|
||
| sc.cache = &nestedCacheEntry{ | ||
| edgeList: edgeList, | ||
| ancestry: ancestry, | ||
| descendants: descendants, | ||
| } | ||
|
|
||
| return sc.cache | ||
| } | ||
|
|
||
| func (nc *nestedIndexCache) getAncestry(edgeListOffset uint64, mem []byte, docNum uint64) []uint64 { | ||
| cache := nc.loadOrCreate(edgeListOffset, mem) | ||
| if cache == nil || cache.ancestry == nil { | ||
| return nil | ||
| } | ||
| return cache.ancestry[docNum] | ||
| } | ||
|
|
||
| func (nc *nestedIndexCache) getDescendants(edgeListOffset uint64, mem []byte, docNum uint64) []uint64 { | ||
| cache := nc.loadOrCreate(edgeListOffset, mem) | ||
| if cache == nil || cache.descendants == nil { | ||
| return nil | ||
| } | ||
| return cache.descendants[docNum] | ||
| } | ||
|
|
||
| func (nc *nestedIndexCache) getEdgeList(edgeListOffset uint64, mem []byte) map[uint64]uint64 { | ||
| cache := nc.loadOrCreate(edgeListOffset, mem) | ||
| if cache == nil || cache.edgeList == nil { | ||
| return nil | ||
| } | ||
| return cache.edgeList | ||
| } | ||
|
|
||
| func (nc *nestedIndexCache) getNumSubDocs(edgeListOffset uint64, mem []byte) uint64 { | ||
| cache := nc.loadOrCreate(edgeListOffset, mem) | ||
| if cache == nil { | ||
| return 0 | ||
| } | ||
| return uint64(len(cache.edgeList)) | ||
| } | ||
|
|
||
| type nestedCacheEntry struct { | ||
| // edgeList[node] = parent | ||
| edgeList map[uint64]uint64 | ||
| // ancestry[node] = list of parents | ||
| ancestry map[uint64][]uint64 | ||
| // descendants[parent] = list of children | ||
| descendants map[uint64][]uint64 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.