Skip to content

Commit

Permalink
badger: allow custom tag indexing and skipping.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Aug 28, 2024
1 parent 11716da commit 6f49db1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion badger/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (b *BadgerBackend) DeleteEvent(ctx context.Context, evt *nostr.Event) error
deletionHappened = true

// calculate all index keys we have for this event and delete them
for _, k := range getIndexKeysForEvent(evt, idx[1:]) {
for _, k := range b.getIndexKeysForEvent(evt, idx[1:]) {
if err := txn.Delete(k); err != nil {
return err
}
Expand Down
17 changes: 14 additions & 3 deletions badger/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func getTagIndexPrefix(tagValue string) ([]byte, int) {
return k, offset
}

func getIndexKeysForEvent(evt *nostr.Event, idx []byte) [][]byte {
func (b BadgerBackend) getIndexKeysForEvent(evt *nostr.Event, idx []byte) [][]byte {
keys := make([][]byte, 0, 18)

// indexes
Expand Down Expand Up @@ -86,17 +86,28 @@ func getIndexKeysForEvent(evt *nostr.Event, idx []byte) [][]byte {
}

// ~ by tagvalue+date
customIndex := b.IndexLongerTag != nil
customSkip := b.SkipIndexingTag != nil

for i, tag := range evt.Tags {
if len(tag) < 2 || len(tag[0]) != 1 || len(tag[1]) == 0 || len(tag[1]) > 100 {
// not indexable
continue
if !customIndex || !b.IndexLongerTag(evt, tag[0], tag[1]) {
// not indexable
continue
}
}

firstIndex := slices.IndexFunc(evt.Tags, func(t nostr.Tag) bool { return len(t) >= 2 && t[1] == tag[1] })
if firstIndex != i {
// duplicate
continue
}

if customSkip && b.SkipIndexingTag(evt, tag[0], tag[1]) {
// purposefully skipped
continue
}

// get key prefix (with full length) and offset where to write the last parts
k, offset := getTagIndexPrefix(tag[1])

Expand Down
6 changes: 6 additions & 0 deletions badger/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/dgraph-io/badger/v4"
"github.com/fiatjaf/eventstore"
"github.com/nbd-wtf/go-nostr"
)

const (
Expand All @@ -27,6 +28,11 @@ type BadgerBackend struct {
Path string
MaxLimit int

// Experimental
SkipIndexingTag func(event *nostr.Event, tagName string, tagValue string) bool
// Experimental
IndexLongerTag func(event *nostr.Event, tagName string, tagValue string) bool

*badger.DB
seq *badger.Sequence
}
Expand Down
2 changes: 1 addition & 1 deletion badger/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (b *BadgerBackend) SaveEvent(ctx context.Context, evt *nostr.Event) error {
return err
}

for _, k := range getIndexKeysForEvent(evt, idx[1:]) {
for _, k := range b.getIndexKeysForEvent(evt, idx[1:]) {
if err := txn.Set(k, nil); err != nil {
return err
}
Expand Down

0 comments on commit 6f49db1

Please sign in to comment.