Skip to content

Commit

Permalink
search/bleve: make the package buildable on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Wessie committed Dec 21, 2024
1 parent 87b4713 commit ef7c33b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 87 deletions.
54 changes: 0 additions & 54 deletions search/bleve/main.go → search/bleve/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@ import (
"fmt"
"net/http"
"strings"
"syscall"
"time"

radio "github.com/R-a-dio/valkyrie"
"github.com/R-a-dio/valkyrie/config"
"github.com/R-a-dio/valkyrie/errors"
"github.com/R-a-dio/valkyrie/search"
"github.com/R-a-dio/valkyrie/util"
"github.com/Wessie/fdstore"
"github.com/blevesearch/bleve/v2"
"github.com/blevesearch/bleve/v2/mapping"
"github.com/blevesearch/bleve/v2/search/query"
Expand All @@ -23,52 +19,6 @@ import (
"go.opentelemetry.io/otel/attribute"
)

func Execute(ctx context.Context, cfg config.Config) error {
const op errors.Op = "search/bleve.Execute"

idx, err := NewIndex(cfg.Conf().Search.IndexPath)
if err != nil {
return errors.E(op, err)
}
defer idx.index.Close()

srv, err := NewServer(ctx, idx)
if err != nil {
return errors.E(op, err)
}
defer srv.Close()

fdstorage := fdstore.NewStoreListenFDs()

endpoint := cfg.Conf().Search.Endpoint.URL()
ln, _, err := util.RestoreOrListen(fdstorage, "bleve", "tcp", endpoint.Host)
if err != nil {
return errors.E(op, err)
}

errCh := make(chan error, 1)
go func() {
errCh <- srv.Serve(ln)
}()

select {
case <-ctx.Done():
return srv.Close()
case <-util.Signal(syscall.SIGUSR2):
zerolog.Ctx(ctx).Info().Msg("SIGUSR2 received")
err := fdstorage.AddListener(ln, "bleve", nil)
if err != nil {
zerolog.Ctx(ctx).Error().Err(err).Msg("failed to store listener")
}
if err = fdstorage.Send(); err != nil {
zerolog.Ctx(ctx).Error().Err(err).Msg("failed to send store")
}
return srv.Close()
case err := <-errCh:
return err
}
}

type indexSong struct {
// main fields we're searching through
Title string `bleve:"title"`
Expand Down Expand Up @@ -192,10 +142,6 @@ func (b *index) Delete(ctx context.Context, tids []radio.TrackID) error {
return nil
}

func init() {
search.Register("bleve", true, Open)
}

func mixedTextMapping() *mapping.FieldMapping {
m := bleve.NewTextFieldMapping()
m.Analyzer = "radio"
Expand Down
96 changes: 96 additions & 0 deletions search/bleve/main_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package bleve

import (
"context"
"net/http"
"syscall"

"github.com/R-a-dio/valkyrie/config"
"github.com/R-a-dio/valkyrie/errors"
"github.com/R-a-dio/valkyrie/search"
"github.com/R-a-dio/valkyrie/util"
"github.com/R-a-dio/valkyrie/website"
"github.com/Wessie/fdstore"
"github.com/go-chi/chi/v5/middleware"
"github.com/rs/zerolog"
"github.com/rs/zerolog/hlog"
)

func Execute(ctx context.Context, cfg config.Config) error {
const op errors.Op = "search/bleve.Execute"

idx, err := NewIndex(cfg.Conf().Search.IndexPath)
if err != nil {
return errors.E(op, err)
}
defer idx.index.Close()

srv, err := NewServer(ctx, idx)
if err != nil {
return errors.E(op, err)
}
defer srv.Close()

fdstorage := fdstore.NewStoreListenFDs()

endpoint := cfg.Conf().Search.Endpoint.URL()
ln, _, err := util.RestoreOrListen(fdstorage, "bleve", "tcp", endpoint.Host)
if err != nil {
return errors.E(op, err)
}

errCh := make(chan error, 1)
go func() {
errCh <- srv.Serve(ln)
}()

select {
case <-ctx.Done():
return srv.Close()
case <-util.Signal(syscall.SIGUSR2):
zerolog.Ctx(ctx).Info().Msg("SIGUSR2 received")
err := fdstorage.AddListener(ln, "bleve", nil)
if err != nil {
zerolog.Ctx(ctx).Error().Err(err).Msg("failed to store listener")
}
if err = fdstorage.Send(); err != nil {
zerolog.Ctx(ctx).Error().Err(err).Msg("failed to send store")
}
return srv.Close()
case err := <-errCh:
return err
}
}

func init() {
search.Register("bleve", true, Open)
}

func NewServer(ctx context.Context, idx *index) (*http.Server, error) {
logger := zerolog.Ctx(ctx)
r := website.NewRouter()
r.Use(middleware.Recoverer)
r.Use(
hlog.NewHandler(*logger),
hlog.RemoteAddrHandler("ip"),
hlog.UserAgentHandler("user_agent"),
hlog.RequestIDHandler("req_id", "Request-Id"),
hlog.URLHandler("url"),
hlog.MethodHandler("method"),
hlog.ProtoHandler("protocol"),
hlog.CustomHeaderHandler("is_htmx", "Hx-Request"),
hlog.AccessHandler(zerologLoggerFunc),
)

r.Get(searchPath, SearchHandler(idx))
r.Get(searchJSONPath, SearchJSONHandler(idx))
r.Get(extendedPath, ExtendedSearchHandler(idx))
r.Get(indexStatsPath, IndexStatsHandler(idx))
r.Post(deletePath, DeleteHandler(idx))
r.Post(updatePath, UpdateHandler(idx))

srv := &http.Server{
Handler: r,
}
return srv, nil
}
33 changes: 0 additions & 33 deletions search/bleve/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bleve

import (
"context"
"encoding/json"
"io"
"net/http"
Expand All @@ -12,10 +11,7 @@ import (
radio "github.com/R-a-dio/valkyrie"
"github.com/R-a-dio/valkyrie/errors"
"github.com/R-a-dio/valkyrie/util/pool"
"github.com/R-a-dio/valkyrie/website"
"github.com/blevesearch/bleve/v2"
"github.com/go-chi/chi/v5/middleware"
"github.com/rs/zerolog"
"github.com/rs/zerolog/hlog"
"github.com/vmihailenco/msgpack/v4"
)
Expand Down Expand Up @@ -56,35 +52,6 @@ func zerologLoggerFunc(r *http.Request, status, size int, duration time.Duration
Msg("http request")
}

func NewServer(ctx context.Context, idx *index) (*http.Server, error) {
logger := zerolog.Ctx(ctx)
r := website.NewRouter()
r.Use(middleware.Recoverer)
r.Use(
hlog.NewHandler(*logger),
hlog.RemoteAddrHandler("ip"),
hlog.UserAgentHandler("user_agent"),
hlog.RequestIDHandler("req_id", "Request-Id"),
hlog.URLHandler("url"),
hlog.MethodHandler("method"),
hlog.ProtoHandler("protocol"),
hlog.CustomHeaderHandler("is_htmx", "Hx-Request"),
hlog.AccessHandler(zerologLoggerFunc),
)

r.Get(searchPath, SearchHandler(idx))
r.Get(searchJSONPath, SearchJSONHandler(idx))
r.Get(extendedPath, ExtendedSearchHandler(idx))
r.Get(indexStatsPath, IndexStatsHandler(idx))
r.Post(deletePath, DeleteHandler(idx))
r.Post(updatePath, UpdateHandler(idx))

srv := &http.Server{
Handler: r,
}
return srv, nil
}

func DeleteHandler(idx *index) http.HandlerFunc {
const op errors.Op = "search/bleve.DeleteHandler"

Expand Down

0 comments on commit ef7c33b

Please sign in to comment.