Skip to content
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

Remove full index loading for next rank in link index #277

Merged
merged 1 commit into from
Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions x/link/keeper/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ const (
LinksCountBytesSize = uint64(8)
)

type LinkFilter func(l CompactLink) bool

var DefaultLinkFilter = func(l CompactLink) bool { return true }

type LinkKeeper interface {
PutLink(ctx sdk.Context, link CompactLink)
IsLinkExist(ctx sdk.Context, link CompactLink) bool
GetAllLinks(ctx sdk.Context) (Links, Links, error)
GetAllLinksFiltered(ctx sdk.Context, filter LinkFilter) (Links, Links, error)
GetLinksCount(ctx sdk.Context) uint64
Iterate(ctx sdk.Context, process func(link CompactLink))
WriteLinks(ctx sdk.Context, writer io.Writer) (err error)
Expand Down Expand Up @@ -50,13 +55,19 @@ func (lk BaseLinkKeeper) IsLinkExist(ctx sdk.Context, link CompactLink) bool {
}

func (lk BaseLinkKeeper) GetAllLinks(ctx sdk.Context) (Links, Links, error) {
return lk.GetAllLinksFiltered(ctx, DefaultLinkFilter)
}

func (lk BaseLinkKeeper) GetAllLinksFiltered(ctx sdk.Context, filter LinkFilter) (Links, Links, error) {

inLinks := make(map[CidNumber]CidLinks)
outLinks := make(map[CidNumber]CidLinks)

lk.Iterate(ctx, func(link CompactLink) {
Links(outLinks).Put(link.From(), link.To(), link.Acc())
Links(inLinks).Put(link.To(), link.From(), link.Acc())
if filter(link) {
Links(outLinks).Put(link.From(), link.To(), link.Acc())
Links(inLinks).Put(link.To(), link.From(), link.Acc())
}
})

return inLinks, outLinks, nil
Expand Down
23 changes: 13 additions & 10 deletions x/link/keeper/link_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ type LinkIndexedKeeper struct {
currentRankOutLinks Links

// New links for the next rank calculation.
// Actually, do we need them in memory?
// TODO: optimize to not store whole index (store just new links)
nextRankInLinks Links
nextRankOutLinks Links

Expand All @@ -38,7 +36,10 @@ func (i *LinkIndexedKeeper) Load(rankCtx sdk.Context, freshCtx sdk.Context) {
i.currentRankInLinks = inLinks
i.currentRankOutLinks = outLinks

newInLinks, newOutLinks, err := i.LinkKeeper.GetAllLinks(freshCtx)
newInLinks, newOutLinks, err := i.LinkKeeper.GetAllLinksFiltered(freshCtx, func(l CompactLink) bool {
return !i.currentRankOutLinks.IsLinkExist(l.From(), l.To(), l.Acc())
})

if err != nil {
cmn.Exit(err.Error())
}
Expand All @@ -48,17 +49,18 @@ func (i *LinkIndexedKeeper) Load(rankCtx sdk.Context, freshCtx sdk.Context) {
}

func (i *LinkIndexedKeeper) FixLinks() {
// todo state copied
i.currentRankInLinks = Links(i.nextRankInLinks).Copy()
i.currentRankOutLinks = Links(i.nextRankOutLinks).Copy()
i.currentRankInLinks.PutAll(i.nextRankInLinks)
i.currentRankOutLinks.PutAll(i.nextRankOutLinks)
i.nextRankInLinks = make(Links)
i.nextRankOutLinks = make(Links)
}

// return true if this block has new links
func (i *LinkIndexedKeeper) EndBlocker() bool {
hasNewLinks := len(i.currentBlockLinks) > 0
for _, link := range i.currentBlockLinks {
Links(i.nextRankOutLinks).Put(link.From(), link.To(), link.Acc())
Links(i.nextRankInLinks).Put(link.To(), link.From(), link.Acc())
i.nextRankOutLinks.Put(link.From(), link.To(), link.Acc())
i.nextRankInLinks.Put(link.To(), link.From(), link.Acc())
}
i.currentBlockLinks = make([]CompactLink, 0, 1000) // todo: 1000 hardcoded value
return hasNewLinks
Expand Down Expand Up @@ -102,11 +104,12 @@ func (i *LinkIndexedKeeper) GetCurrentBlockNewLinks() []CompactLink {
}

func (i *LinkIndexedKeeper) IsAnyLinkExist(from CidNumber, to CidNumber) bool {
return i.nextRankOutLinks.IsAnyLinkExist(from, to)
return i.currentRankOutLinks.IsAnyLinkExist(from, to) || i.nextRankOutLinks.IsAnyLinkExist(from, to)
}

func (i *LinkIndexedKeeper) IsLinkExist(link CompactLink) bool {
return i.nextRankOutLinks.IsLinkExist(link.From(), link.To(), link.Acc())
return i.currentRankOutLinks.IsLinkExist(link.From(), link.To(), link.Acc()) ||
i.nextRankOutLinks.IsLinkExist(link.From(), link.To(), link.Acc())
}

//todo: remove duplicated method (BaseLinksKeeper)
Expand Down
10 changes: 10 additions & 0 deletions x/link/types/cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ func (links Links) Put(from CidNumber, to CidNumber, acc AccNumber) {
links[from] = cidLinks
}

func (links Links) PutAll(newLinks Links) {
for from := range newLinks {
for to := range newLinks[from] {
for u := range newLinks[from][to] {
links.Put(from, to, u)
}
}
}
}

func (links Links) Copy() Links {

linksCopy := make(Links, len(links))
Expand Down