Skip to content

Commit

Permalink
Merge pull request #309 from KyleMaas/fix-about-names
Browse files Browse the repository at this point in the history
Fix TestAboutNames
  • Loading branch information
decentral1se committed Jan 28, 2023
2 parents d429bd0 + 7dde4c5 commit 4b77d40
Showing 1 changed file with 32 additions and 2 deletions.
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

0 comments on commit 4b77d40

Please sign in to comment.