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

Fix TestAboutNames #309

Merged
merged 1 commit into from
Jan 28, 2023
Merged
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
34 changes: 32 additions & 2 deletions plugins2/names/about.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"fmt"
"log"
"strings"
"sync"
"time"

"github.com/dgraph-io/badger/v3"
"github.com/ssbc/margaret"
Expand All @@ -35,10 +37,27 @@ type AboutAttribute struct {
}

var idxKeyPrefix = []byte("idx-abouts")
var idxInSync sync.WaitGroup

func (ab aboutStore) waitForIndexes() {
idxInSync.Wait()
}

func (ab aboutStore) startIndexing() {
idxInSync.Add(1)
}

func (ab aboutStore) doneIndexing() {
time.AfterFunc(100 * time.Millisecond, func() {
idxInSync.Done()
})
}

func (ab aboutStore) ImageFor(ref *refs.FeedRef) (*refs.BlobRef, error) {
var br refs.BlobRef

ab.waitForIndexes()

err := ab.kv.View(func(txn *badger.Txn) error {

addr := ref.Sigil()
Expand Down Expand Up @@ -68,6 +87,8 @@ func (ab aboutStore) ImageFor(ref *refs.FeedRef) (*refs.BlobRef, error) {
}

func (ab aboutStore) All() (client.NamesGetResult, error) {
ab.waitForIndexes()

var ngr = make(client.NamesGetResult)
err := ab.kv.View(func(txn *badger.Txn) error {
iter := txn.NewIterator(badger.DefaultIteratorOptions)
Expand Down Expand Up @@ -122,6 +143,8 @@ func (ab aboutStore) All() (client.NamesGetResult, error) {
}

func (ab aboutStore) CollectedFor(ref refs.FeedRef) (*AboutInfo, error) {
ab.waitForIndexes()

addr := append(idxKeyPrefix, []byte(ref.Sigil()+":")...)

// direct badger magic
Expand Down Expand Up @@ -198,16 +221,23 @@ const FolderNameAbout = "about"

func (plug *Plugin) OpenSharedIndex(db *badger.DB) (librarian.Index, librarian.SinkIndex) {
aboutIdx := libbadger.NewIndexWithKeyPrefix(db, 0, idxKeyPrefix)
update := librarian.NewSinkIndex(updateAboutMessage, aboutIdx)

plug.about = aboutStore{db}

plug.about.startIndexing()
defer plug.about.doneIndexing()

update := librarian.NewSinkIndex(plug.about.updateAboutMessage, aboutIdx)

return aboutIdx, update
}

func updateAboutMessage(ctx context.Context, seq int64, msgv interface{}, idx librarian.SetterIndex) error {
func (ab aboutStore) updateAboutMessage(ctx context.Context, seq int64, msgv interface{}, idx librarian.SetterIndex) error {
var msg refs.Message

ab.startIndexing()
defer ab.doneIndexing()

switch tv := msgv.(type) {
case refs.Message:
msg = tv
Expand Down