mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-07-31 13:22:46 +00:00
Preparations for removing BaseDendrite
(#3016)
Preparations to actually remove/replace `BaseDendrite`. Quite a few changes: - SyncAPI accepts an `fulltext.Indexer` interface (fulltext is removed from `BaseDendrite`) - Caches are removed from `BaseDendrite` - Introduces a `Router` struct (likely to change) - also fixes #2903 - Introduces a `sqlutil.ConnectionManager`, which should remove `base.DatabaseConnection` later on - probably more
This commit is contained in:
parent
d88f71ab71
commit
5579121c6f
85 changed files with 722 additions and 470 deletions
|
@ -18,10 +18,10 @@
|
|||
package fulltext
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/blevesearch/bleve/v2"
|
||||
|
||||
// side effect imports to allow all possible languages
|
||||
_ "github.com/blevesearch/bleve/v2/analysis/lang/ar"
|
||||
_ "github.com/blevesearch/bleve/v2/analysis/lang/cjk"
|
||||
|
@ -55,6 +55,13 @@ type Search struct {
|
|||
FulltextIndex bleve.Index
|
||||
}
|
||||
|
||||
type Indexer interface {
|
||||
Index(elements ...IndexElement) error
|
||||
Delete(eventID string) error
|
||||
Search(term string, roomIDs, keys []string, limit, from int, orderByStreamPos bool) (*bleve.SearchResult, error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
// IndexElement describes the layout of an element to index
|
||||
type IndexElement struct {
|
||||
EventID string
|
||||
|
@ -77,12 +84,18 @@ func (i *IndexElement) SetContentType(v string) {
|
|||
}
|
||||
|
||||
// New opens a new/existing fulltext index
|
||||
func New(cfg config.Fulltext) (fts *Search, err error) {
|
||||
func New(ctx context.Context, cfg config.Fulltext) (fts *Search, err error) {
|
||||
fts = &Search{}
|
||||
fts.FulltextIndex, err = openIndex(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
go func() {
|
||||
// Wait for the context (should be from process.ProcessContext) to be
|
||||
// done, indicating that Dendrite is shutting down.
|
||||
<-ctx.Done()
|
||||
_ = fts.Close()
|
||||
}()
|
||||
return fts, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@ import (
|
|||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/matrix-org/dendrite/setup/process"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
"github.com/matrix-org/util"
|
||||
|
||||
|
@ -25,7 +26,7 @@ import (
|
|||
"github.com/matrix-org/dendrite/setup/config"
|
||||
)
|
||||
|
||||
func mustOpenIndex(t *testing.T, tempDir string) *fulltext.Search {
|
||||
func mustOpenIndex(t *testing.T, tempDir string) (*fulltext.Search, *process.ProcessContext) {
|
||||
t.Helper()
|
||||
cfg := config.Fulltext{
|
||||
Enabled: true,
|
||||
|
@ -36,11 +37,12 @@ func mustOpenIndex(t *testing.T, tempDir string) *fulltext.Search {
|
|||
cfg.IndexPath = config.Path(tempDir)
|
||||
cfg.InMemory = false
|
||||
}
|
||||
fts, err := fulltext.New(cfg)
|
||||
ctx := process.NewProcessContext()
|
||||
fts, err := fulltext.New(ctx.Context(), cfg)
|
||||
if err != nil {
|
||||
t.Fatal("failed to open fulltext index:", err)
|
||||
}
|
||||
return fts
|
||||
return fts, ctx
|
||||
}
|
||||
|
||||
func mustAddTestData(t *testing.T, fts *fulltext.Search, firstStreamPos int64) (eventIDs, roomIDs []string) {
|
||||
|
@ -93,19 +95,17 @@ func mustAddTestData(t *testing.T, fts *fulltext.Search, firstStreamPos int64) (
|
|||
|
||||
func TestOpen(t *testing.T) {
|
||||
dataDir := t.TempDir()
|
||||
fts := mustOpenIndex(t, dataDir)
|
||||
if err := fts.Close(); err != nil {
|
||||
t.Fatal("unable to close fulltext index", err)
|
||||
}
|
||||
_, ctx := mustOpenIndex(t, dataDir)
|
||||
ctx.ShutdownDendrite()
|
||||
|
||||
// open existing index
|
||||
fts = mustOpenIndex(t, dataDir)
|
||||
defer fts.Close()
|
||||
_, ctx = mustOpenIndex(t, dataDir)
|
||||
ctx.ShutdownDendrite()
|
||||
}
|
||||
|
||||
func TestIndex(t *testing.T) {
|
||||
fts := mustOpenIndex(t, "")
|
||||
defer fts.Close()
|
||||
fts, ctx := mustOpenIndex(t, "")
|
||||
defer ctx.ShutdownDendrite()
|
||||
|
||||
// add some data
|
||||
var streamPos int64 = 1
|
||||
|
@ -128,8 +128,8 @@ func TestIndex(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
fts := mustOpenIndex(t, "")
|
||||
defer fts.Close()
|
||||
fts, ctx := mustOpenIndex(t, "")
|
||||
defer ctx.ShutdownDendrite()
|
||||
eventIDs, roomIDs := mustAddTestData(t, fts, 0)
|
||||
res1, err := fts.Search("lorem", roomIDs[:1], nil, 50, 0, false)
|
||||
if err != nil {
|
||||
|
@ -224,7 +224,8 @@ func TestSearch(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
f := mustOpenIndex(t, "")
|
||||
f, ctx := mustOpenIndex(t, "")
|
||||
defer ctx.ShutdownDendrite()
|
||||
eventIDs, roomIDs := mustAddTestData(t, f, 0)
|
||||
var searchRooms []string
|
||||
for _, x := range tt.args.roomIndex {
|
||||
|
|
|
@ -15,8 +15,9 @@
|
|||
package fulltext
|
||||
|
||||
import (
|
||||
"github.com/matrix-org/dendrite/setup/config"
|
||||
"time"
|
||||
|
||||
"github.com/matrix-org/dendrite/setup/config"
|
||||
)
|
||||
|
||||
type Search struct{}
|
||||
|
@ -28,6 +29,13 @@ type IndexElement struct {
|
|||
StreamPosition int64
|
||||
}
|
||||
|
||||
type Indexer interface {
|
||||
Index(elements ...IndexElement) error
|
||||
Delete(eventID string) error
|
||||
Search(term string, roomIDs, keys []string, limit, from int, orderByStreamPos bool) (SearchResult, error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
type SearchResult struct {
|
||||
Status interface{} `json:"status"`
|
||||
Request *interface{} `json:"request"`
|
||||
|
@ -48,7 +56,7 @@ func (f *Search) Close() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (f *Search) Index(e IndexElement) error {
|
||||
func (f *Search) Index(e ...IndexElement) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue