Skip to content

Commit

Permalink
modify out date template, and clean search index cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwei committed Nov 19, 2024
1 parent 44184f1 commit c3dfbc0
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 45 deletions.
2 changes: 0 additions & 2 deletions internal/domain/content/entity/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,8 @@ func (c *Content) copyFiles(dir string, parentPath string, files []string) {
defer wg.Done()

if err := c.copyFile(src, dst); err != nil {
c.Log.Debugf("file %s copy failed: %v\n", src, err)
return
}
c.Log.Debugf("file %s copied to %s successfully\n", src, dst)
}(src, dst)
}

Expand Down
45 changes: 36 additions & 9 deletions internal/domain/content/entity/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"path/filepath"
"strings"
"sync"
)

type TypeService interface {
Expand All @@ -26,13 +27,17 @@ type Search struct {
Repo repository.Repository
Log loggers.Logger

mu sync.RWMutex
IndicesMap map[string]map[string]bleve.Index
}

// TypeQuery conducts a search and returns a set of Ponzu "targets", Type:ID pairs,
// and an error. If there is no search index for the typeName (Type) provided,
// db.ErrNoIndex will be returned as the error
func (s *Search) TypeQuery(typeName, query string, count, offset int) ([]content.Identifier, error) {
s.mu.RLock()
defer s.mu.Unlock()

s.setup()

idx, ok := s.IndicesMap[s.getSearchDir(typeName)][typeName]
Expand All @@ -54,12 +59,20 @@ func (s *Search) TypeQuery(typeName, query string, count, offset int) ([]content
results = append(results, valueobject.CreateIndex(hit.ID))
}

if closeErr := idx.Close(); closeErr != nil {
s.Log.Printf("Failed to close index: %v", closeErr)
}
delete(s.IndicesMap, s.getSearchDir(typeName))

return results, nil
}

// UpdateIndex sets data into a content type's search index at the given
// identifier
func (s *Search) UpdateIndex(ns, id string, data []byte) error {
s.mu.RLock()
defer s.mu.Unlock()

s.setup()

idx, ok := s.IndicesMap[s.getSearchDir(ns)][ns]
Expand All @@ -78,7 +91,14 @@ func (s *Search) UpdateIndex(ns, id string, data []byte) error {

// add data to search index
i := valueobject.NewIndex(ns, id)
return idx.Index(i.String(), p)
err = idx.Index(i.String(), p)

if closeErr := idx.Close(); closeErr != nil {
s.Log.Printf("Failed to close index: %v", closeErr)
}
delete(s.IndicesMap, s.getSearchDir(ns))

return err
}

return nil
Expand All @@ -87,6 +107,9 @@ func (s *Search) UpdateIndex(ns, id string, data []byte) error {
// DeleteIndex removes data from a content type's search index at the
// given identifier
func (s *Search) DeleteIndex(id string) error {
s.mu.RLock()
defer s.mu.Unlock()

s.setup()

// check if there is a search index to work with
Expand All @@ -96,7 +119,14 @@ func (s *Search) DeleteIndex(id string) error {
idx, ok := s.IndicesMap[s.getSearchDir(ns)][ns]
if ok {
// add data to search index
return idx.Delete(id)
err := idx.Delete(id)

if closeErr := idx.Close(); closeErr != nil {
s.Log.Printf("Failed to close index: %v", closeErr)
}
delete(s.IndicesMap, s.getSearchDir(ns))

return err
}

return nil
Expand All @@ -117,11 +147,6 @@ func (s *Search) adminSearchDir() string {
return filepath.Join(s.Repo.AdminDataDir(), "Search")
}

// Setup initializes Search Index for search to be functional
// This was moved out of db.Init and put to main(), because addon checker was initializing db together with
// search indexing initialisation in time when there were no item.Types defined so search index was always
// empty when using addons. We still have no guarentee whatsoever that item.Types is defined
// Should be called from a goroutine after SetContent is successful (SortContent requirement)
func (s *Search) setup() {
s.setupAdminIndices()
s.setupUserIndices()
Expand All @@ -138,6 +163,7 @@ func (s *Search) setupAdminIndices() {
func (s *Search) setupIndices(dir string, typeNames []string) {
_, ok := s.IndicesMap[dir]
if ok {
s.Log.Debugln("[search] Setup found: Index already exists for ", dir)
return
}

Expand Down Expand Up @@ -195,15 +221,16 @@ func (s *Search) mapIndex(typeName string) (bleve.Index, error) {
if err != nil {
return nil, err
}
idx.SetName(idxName)
s.Log.Debugf("[search] Index new created for %s\n", typeName)
} else {
idx, err = bleve.Open(idxPath)
if err != nil {
return nil, err
}
s.Log.Debugf("[search] Index open created for %s\n", typeName)
}

s.Log.Debugf("[search] Index created for %s\n", typeName)
idx.SetName(idxName)

return idx, nil
}
4 changes: 0 additions & 4 deletions internal/domain/content/valueobject/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package valueobject
import (
"fmt"
"github.com/gohugonet/hugoverse/pkg/editor"
"log"
"net/http"
)

Expand Down Expand Up @@ -92,9 +91,6 @@ func (d *Domain) BeforeAPICreate(res http.ResponseWriter, req *http.Request) err
// The request has a context containing the databse 'target' affected by the
// request. Ex. Song__pending:3 or Song:8 depending if Song implements api.Trustable
func (d *Domain) AfterAPICreate(res http.ResponseWriter, req *http.Request) error {
addr := req.RemoteAddr
log.Println("[AfterAPICreate] Domain sent by:", addr, "sub domain:", req.PostFormValue("sub"))

return nil
}

Expand Down
4 changes: 0 additions & 4 deletions internal/domain/content/valueobject/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"github.com/gohugonet/hugoverse/pkg/editor"
"log"
"net/http"
"text/template"
)
Expand Down Expand Up @@ -111,9 +110,6 @@ func (s *Post) BeforeAPICreate(res http.ResponseWriter, req *http.Request) error
// The request has a context containing the databse 'target' affected by the
// request. Ex. Song__pending:3 or Song:8 depending if Song implements api.Trustable
func (s *Post) AfterAPICreate(res http.ResponseWriter, req *http.Request) error {
addr := req.RemoteAddr
log.Println("[AfterAPICreate] Post sent by:", addr, "titled:", req.PostFormValue("title"))

return nil
}

Expand Down
4 changes: 0 additions & 4 deletions internal/domain/content/valueobject/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"github.com/gohugonet/hugoverse/pkg/editor"
"log"
"net/http"
"text/template"
)
Expand Down Expand Up @@ -130,9 +129,6 @@ func (s *Site) BeforeAPICreate(res http.ResponseWriter, req *http.Request) error
// The request has a context containing the databse 'target' affected by the
// request. Ex. Song__pending:3 or Song:8 depending if Song implements api.Trustable
func (s *Site) AfterAPICreate(res http.ResponseWriter, req *http.Request) error {
addr := req.RemoteAddr
log.Println("[AfterAPICreate] Site sent by:", addr, "titled:", req.PostFormValue("title"))

return nil
}

Expand Down
4 changes: 0 additions & 4 deletions internal/domain/content/valueobject/sitelanguage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package valueobject
import (
"fmt"
"github.com/gohugonet/hugoverse/pkg/editor"
"log"
"net/http"
"strings"
)
Expand Down Expand Up @@ -124,9 +123,6 @@ func (s *SiteLanguage) BeforeAPICreate(res http.ResponseWriter, req *http.Reques
// The request has a context containing the databse 'target' affected by the
// request. Ex. Song__pending:3 or Song:8 depending if Song implements api.Trustable
func (s *SiteLanguage) AfterAPICreate(res http.ResponseWriter, req *http.Request) error {
addr := req.RemoteAddr
log.Println("[AfterAPICreate] SiteLanguage sent by:", addr, "titled:", req.PostFormValue("title"))

return nil
}

Expand Down
4 changes: 0 additions & 4 deletions internal/domain/content/valueobject/sitepost.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package valueobject
import (
"fmt"
"github.com/gohugonet/hugoverse/pkg/editor"
"log"
"net/http"
"strings"
)
Expand Down Expand Up @@ -116,9 +115,6 @@ func (s *SitePost) BeforeAPICreate(res http.ResponseWriter, req *http.Request) e
// The request has a context containing the databse 'target' affected by the
// request. Ex. Song__pending:3 or Song:8 depending if Song implements api.Trustable
func (s *SitePost) AfterAPICreate(res http.ResponseWriter, req *http.Request) error {
addr := req.RemoteAddr
log.Println("AfterAPICreate: SitePost sent by:", addr, "titled:", req.PostFormValue("title"))

return nil
}

Expand Down
4 changes: 0 additions & 4 deletions internal/domain/content/valueobject/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package valueobject
import (
"fmt"
"github.com/gohugonet/hugoverse/pkg/editor"
"log"
"net/http"
)

Expand Down Expand Up @@ -115,9 +114,6 @@ func (s *Theme) BeforeAPICreate(res http.ResponseWriter, req *http.Request) erro
// The request has a context containing the databse 'target' affected by the
// request. Ex. Song__pending:3 or Song:8 depending if Song implements api.Trustable
func (s *Theme) AfterAPICreate(res http.ResponseWriter, req *http.Request) error {
addr := req.RemoteAddr
log.Println("[AfterAPICreate] Theme sent by:", addr, "titled:", req.PostFormValue("title"))

return nil
}

Expand Down
3 changes: 1 addition & 2 deletions internal/domain/template/entity/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package entity
import (
"bytes"
"embed"
"fmt"
"github.com/gohugonet/hugoverse/internal/domain/fs"
"github.com/gohugonet/hugoverse/internal/domain/template"
"github.com/gohugonet/hugoverse/internal/domain/template/valueobject"
Expand Down Expand Up @@ -216,7 +215,7 @@ func (t *Template) addTemplate(name string, tinfo valueobject.TemplateInfo) erro
t.Main.addTemplate(tinfo.Name, state)

if err := t.Parser.Transform(t.Main, state); err != nil {
fmt.Println(tinfo.ErrWithFileContext("ast transform parse failed", err))
return tinfo.ErrWithFileContext("ast transform parse failed", err)
}

return nil
Expand Down
1 change: 1 addition & 0 deletions internal/domain/template/factory/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (b *builder) withCfs(cfs template.CustomizedFunctions) *builder {
}

func (b *builder) buildFunctions() *builder {
valueobject.ResetTemplateFuncsNamespaceRegistry()
valueobject.RegisterNamespaces()
valueobject.RegisterCallbackNamespaces(b.tmpl.Execute)
valueobject.RegisterExtendedNamespaces(b.cfs)
Expand Down
4 changes: 4 additions & 0 deletions internal/domain/template/valueobject/nsreg.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (

var TemplateFuncsNamespaceRegistry []func() *TemplateFuncsNamespace

func ResetTemplateFuncsNamespaceRegistry() {
TemplateFuncsNamespaceRegistry = nil
}

func AddTemplateFuncsNamespace(ns func() *TemplateFuncsNamespace) {
TemplateFuncsNamespaceRegistry = append(TemplateFuncsNamespaceRegistry, ns)
}
Expand Down
2 changes: 0 additions & 2 deletions internal/interfaces/api/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ func (d *Database) PutContent(ci any, data []byte) error {
bucket = fmt.Sprintf("%s%s", ns, bucketNameWithPrefix(string(status)))
}

d.log.Printf(" === bucket: %s\n", bucket)

if err := d.getStore(ns).Set(
&item{
bucket: bucket,
Expand Down
2 changes: 0 additions & 2 deletions internal/interfaces/api/handler/handlecontent.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ func (s *Handler) postContent(res http.ResponseWriter, req *http.Request) {
req.PostForm.Set("updated", ts)

urlPaths, err := s.StoreFiles(req)
fmt.Printf("=== post form: %v\n", urlPaths)
if err != nil {
s.log.Errorf("Error storing files: %v", err)
res.WriteHeader(http.StatusInternalServerError)
Expand Down Expand Up @@ -303,7 +302,6 @@ func (s *Handler) postContent(res http.ResponseWriter, req *http.Request) {
}

req.PostForm.Set("namespace", t)
s.log.Printf("PostForm: %+v", req.PostForm)

id, err := s.contentApp.NewContent(t, req.PostForm)
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions internal/interfaces/api/handler/handleedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ func (s *Handler) EditHandler(res http.ResponseWriter, req *http.Request) {
}

req.PostForm.Set("namespace", pt)
s.log.Printf("PostForm: %+v", req.PostForm)

if cid == "-1" {
id, err := s.contentApp.NewContent(pt, req.PostForm)
Expand Down Expand Up @@ -478,7 +477,6 @@ func (s *Handler) ApproveContentHandler(res http.ResponseWriter, req *http.Reque

req.PostForm.Set("namespace", t)
req.PostForm.Set("status", "public")
s.log.Printf("PostForm: %+v", req.PostForm)

// Store the content in the bucket t
id, err := s.contentApp.NewContent(t, req.PostForm)
Expand Down
2 changes: 1 addition & 1 deletion internal/interfaces/cli/vercurr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package cli
var CurrentVersion = Version{
Major: 0,
Minor: 0,
PatchLevel: 9,
PatchLevel: 10,
Suffix: "",
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.0.9",
"version": "0.0.10",
"name": "Hugoverse",
"description": "Headless CMS for Hugo",
"author": "sunwei",
Expand Down

0 comments on commit c3dfbc0

Please sign in to comment.