summaryrefslogtreecommitdiff
path: root/system/db/content.go
diff options
context:
space:
mode:
authorSteve Manuel <nilslice@gmail.com>2017-04-14 17:34:16 -0700
committerSteve Manuel <nilslice@gmail.com>2017-04-14 17:34:16 -0700
commit1df78f4390b0153f48108ae1aa504719730d2b2f (patch)
tree7f9da51f9669ee4fba8dd2d82ec6618e157fe6b6 /system/db/content.go
parent167cbd869a26e2798ae56653c34eeb65d16e1d92 (diff)
parentfcb69059f0ef206efafffa6abc10dd4245280d12 (diff)
Merge branch 'master' of https://github.com/ponzu-cms/ponzu
Diffstat (limited to 'system/db/content.go')
-rw-r--r--system/db/content.go71
1 files changed, 40 insertions, 31 deletions
diff --git a/system/db/content.go b/system/db/content.go
index fbdd8dd..b503a60 100644
--- a/system/db/content.go
+++ b/system/db/content.go
@@ -9,9 +9,11 @@ import (
"sort"
"strconv"
"strings"
+ "sync"
"time"
"github.com/ponzu-cms/ponzu/system/item"
+ "github.com/ponzu-cms/ponzu/system/search"
"github.com/boltdb/bolt"
"github.com/gorilla/schema"
@@ -125,9 +127,9 @@ func update(ns, id string, data url.Values, existingContent *[]byte) (int, error
go func() {
// update data in search index
target := fmt.Sprintf("%s:%s", ns, id)
- err = UpdateSearchIndex(target, j)
+ err = search.UpdateIndex(target, j)
if err != nil {
- log.Println("[search] UpdateSearchIndex Error:", err)
+ log.Println("[search] UpdateIndex Error:", err)
}
}()
@@ -253,9 +255,9 @@ func insert(ns string, data url.Values) (int, error) {
go func() {
// add data to seach index
target := fmt.Sprintf("%s:%s", ns, cid)
- err = UpdateSearchIndex(target, j)
+ err = search.UpdateIndex(target, j)
if err != nil {
- log.Println("[search] UpdateSearchIndex Error:", err)
+ log.Println("[search] UpdateIndex Error:", err)
}
}()
@@ -322,9 +324,9 @@ func DeleteContent(target string) error {
// delete indexed data from search index
if !strings.Contains(ns, "__") {
target = fmt.Sprintf("%s:%s", ns, id)
- err = DeleteSearchIndex(target)
+ err = search.DeleteIndex(target)
if err != nil {
- log.Println("[search] DeleteSearchIndex Error:", err)
+ log.Println("[search] DeleteIndex Error:", err)
}
}
}()
@@ -566,41 +568,48 @@ func Query(namespace string, opts QueryOptions) (int, [][]byte) {
var sortContentCalls = make(map[string]time.Time)
var waitDuration = time.Millisecond * 2000
+var sortMutex = &sync.Mutex{}
-func enoughTime(key string, withDelay bool) bool {
+func setLastInvocation(key string) {
+ sortMutex.Lock()
+ sortContentCalls[key] = time.Now()
+ sortMutex.Unlock()
+}
+
+func lastInvocation(key string) (time.Time, bool) {
+ sortMutex.Lock()
last, ok := sortContentCalls[key]
+ sortMutex.Unlock()
+ return last, ok
+}
+
+func enoughTime(key string) bool {
+ last, ok := lastInvocation(key)
if !ok {
// no invocation yet
// track next invocation
- sortContentCalls[key] = time.Now()
+ setLastInvocation(key)
return true
}
- // if our required wait time has not been met, return false
- if !time.Now().After(last.Add(waitDuration)) {
- return false
+ // if our required wait time has been met, return true
+ if time.Now().After(last.Add(waitDuration)) {
+ setLastInvocation(key)
+ return true
}
- // 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 invocation
- sortContentCalls[key] = time.Now()
- SortContent(key)
- } else {
- // retrigger
- SortContent(key)
- }
- }
- }()
- }
+ // dispatch a delayed invocation in case no additional one follows
+ go func() {
+ lastInvocationBeforeTimer, _ := lastInvocation(key) // zero value can be handled, no need for ok
+ enoughTimer := time.NewTimer(waitDuration)
+ <-enoughTimer.C
+ lastInvocationAfterTimer, _ := lastInvocation(key)
+ if !lastInvocationAfterTimer.After(lastInvocationBeforeTimer) {
+ SortContent(key)
+ }
+ }()
- // track next invocation
- sortContentCalls[key] = time.Now()
- return true
+ return false
}
// SortContent sorts all content of the type supplied as the namespace by time,
@@ -608,7 +617,7 @@ func enoughTime(key string, withDelay bool) bool {
// 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) {
+ if !enoughTime(namespace) {
return
}