summaryrefslogtreecommitdiff
path: root/system/db/content.go
diff options
context:
space:
mode:
Diffstat (limited to 'system/db/content.go')
-rw-r--r--system/db/content.go99
1 files changed, 96 insertions, 3 deletions
diff --git a/system/db/content.go b/system/db/content.go
index 97ea9b4..994860f 100644
--- a/system/db/content.go
+++ b/system/db/content.go
@@ -9,6 +9,7 @@ import (
"sort"
"strconv"
"strings"
+ "time"
"github.com/ponzu-cms/ponzu/system/item"
@@ -121,6 +122,15 @@ func update(ns, id string, data url.Values, existingContent *[]byte) (int, error
return 0, err
}
+ go func() {
+ // update data in search index
+ target := fmt.Sprintf("%s:%s", ns, id)
+ err = UpdateSearchIndex(target, string(j))
+ if err != nil {
+ log.Println("[search] UpdateSearchIndex Error:", err)
+ }
+ }()
+
return cid, nil
}
@@ -128,7 +138,7 @@ func mergeData(ns string, data url.Values, existingContent []byte) ([]byte, erro
var j []byte
t, ok := item.Types[ns]
if !ok {
- return nil, fmt.Errorf("namespace type not found:", ns)
+ return nil, fmt.Errorf("Namespace type not found: %s", ns)
}
// Unmarsal the existing values
@@ -169,6 +179,8 @@ func insert(ns string, data url.Values) (int, error) {
specifier = "__" + spec[1]
}
+ var j []byte
+ var cid string
err := store.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte(ns + specifier))
if err != nil {
@@ -181,7 +193,7 @@ func insert(ns string, data url.Values) (int, error) {
if err != nil {
return err
}
- cid := strconv.FormatUint(id, 10)
+ cid = strconv.FormatUint(id, 10)
effectedID, err = strconv.Atoi(cid)
if err != nil {
return err
@@ -197,7 +209,7 @@ func insert(ns string, data url.Values) (int, error) {
data.Set("__specifier", specifier)
}
- j, err := postToJSON(ns, data)
+ j, err = postToJSON(ns, data)
if err != nil {
return err
}
@@ -238,6 +250,15 @@ func insert(ns string, data url.Values) (int, error) {
return 0, err
}
+ go func() {
+ // add data to seach index
+ target := fmt.Sprintf("%s:%s", ns, cid)
+ err = UpdateSearchIndex(target, string(j))
+ if err != nil {
+ log.Println("[search] UpdateSearchIndex Error:", err)
+ }
+ }()
+
return effectedID, nil
}
@@ -297,6 +318,17 @@ func DeleteContent(target string) error {
return err
}
+ go func() {
+ // delete indexed data from search index
+ if !strings.Contains(ns, "__") {
+ target = fmt.Sprintf("%s:%s", ns, id)
+ err = DeleteSearchIndex(target)
+ if err != nil {
+ log.Println("[search] DeleteSearchIndex Error:", err)
+ }
+ }
+ }()
+
// exception to typical "run in goroutine" pattern:
// we want to have an updated admin view as soon as this is deleted, so
// in some cases, the delete and redirect is faster than the sort,
@@ -334,6 +366,23 @@ func Content(target string) ([]byte, error) {
return val.Bytes(), nil
}
+// ContentMulti returns a set of content based on the the targets / identifiers
+// provided in Ponzu target string format: Type:ID
+// NOTE: All targets should be of the same type
+func ContentMulti(targets []string) ([][]byte, error) {
+ var contents [][]byte
+ for i := range targets {
+ b, err := Content(targets[i])
+ if err != nil {
+ return nil, err
+ }
+
+ contents = append(contents, b)
+ }
+
+ return contents, nil
+}
+
// ContentBySlug does a lookup in the content index to find the type and id of
// the requested content. Subsequently, issues the lookup in the type bucket and
// returns the the type and data at that ID or nil if nothing exists.
@@ -515,10 +564,54 @@ func Query(namespace string, opts QueryOptions) (int, [][]byte) {
return total, posts
}
+var sortContentCalls = make(map[string]time.Time)
+var waitDuration = time.Millisecond * 4000
+
+func enoughTime(key string, withDelay bool) bool {
+ last, ok := sortContentCalls[key]
+ if !ok {
+ // no envocation yet
+ // track next evocation
+ sortContentCalls[key] = time.Now()
+ return true
+ }
+
+ // if our required wait time has not been met, return false
+ if !time.Now().After(last.Add(waitDuration)) {
+ return false
+ }
+
+ // dispatch a delayed envocation in case no additional one follows
+ if withDelay {
+ go func() {
+ select {
+ case <-time.After(waitDuration):
+ if enoughTime(key, false) {
+ // track next evocation
+ sortContentCalls[key] = time.Now()
+ SortContent(key)
+ } else {
+ // retrigger
+ SortContent(key)
+ }
+ }
+ }()
+ }
+
+ // track next evocation
+ sortContentCalls[key] = time.Now()
+ return true
+}
+
// SortContent sorts all content of the type supplied as the namespace by time,
// in descending order, from most recent to least recent
// Should be called from a goroutine after SetContent is successful
func SortContent(namespace string) {
+ // wait if running too frequently per namespace
+ if !enoughTime(namespace, true) {
+ return
+ }
+
// only sort main content types i.e. Post
if strings.Contains(namespace, "__") {
return