From 86f50433c08e74aa185e532ec8455bd61d343090 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 1 Nov 2016 23:34:48 -0700 Subject: moving query like code from handler to new Query func in db package so size of posts returned is just what was requested --- system/db/content.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'system/db') diff --git a/system/db/content.go b/system/db/content.go index bd1ee4b..84695cc 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -202,6 +202,67 @@ func ContentAll(namespace string) [][]byte { return posts } +// QueryOptions holds options for a query +type QueryOptions struct { + Count int + Offset int + Order string +} + +// Query retrieves a set of content from the db based on options +func Query(namespace string, opts QueryOptions) [][]byte { + var posts [][]byte + store.View(func(tx *bolt.Tx) error { + b := tx.Bucket([]byte(namespace)) + c := b.Cursor() + + i := 0 // count of num posts added + cur := 0 // count of where cursor is + switch opts.Order { + case "asc": + for k, v := c.Last(); k != nil; c.Prev() { + if cur < opts.Offset*opts.Count { + cur++ + continue + } + + if i >= opts.Count { + break + } + + posts = append(posts, v) + i++ + } + + case "desc": + for k, v := c.First(); k != nil; c.Next() { + if cur < opts.Offset*opts.Count { + cur++ + continue + } + + if i >= opts.Count { + break + } + + posts = append(posts, v) + i++ + } + } + + return nil + }) + + // if opts.order == "asc" { + // posts = []json.RawMessage{} + // for i := len(posts) - 1; i >= 0; i-- { + // posts = append(all, posts[i]) + // } + // } + + return posts +} + // 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 -- cgit v1.2.3 From 863e88194336a7baefbceb266f56d60d6a002da5 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 1 Nov 2016 23:46:13 -0700 Subject: do not allow return length longer than number of values in bucket --- system/db/content.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'system/db') diff --git a/system/db/content.go b/system/db/content.go index 84695cc..03e6a41 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -215,6 +215,7 @@ func Query(namespace string, opts QueryOptions) [][]byte { store.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(namespace)) c := b.Cursor() + n := b.Stats().KeyN i := 0 // count of num posts added cur := 0 // count of where cursor is @@ -226,7 +227,7 @@ func Query(namespace string, opts QueryOptions) [][]byte { continue } - if i >= opts.Count { + if i >= opts.Count || cur > n { break } @@ -241,7 +242,7 @@ func Query(namespace string, opts QueryOptions) [][]byte { continue } - if i >= opts.Count { + if i >= opts.Count || cur > n { break } -- cgit v1.2.3 From 3fc8755911eeaf508c5139b8e9b7e4d0f54d046c Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 1 Nov 2016 23:53:42 -0700 Subject: adding some bounds checking to the cursor location based on the expected results --- system/db/content.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'system/db') diff --git a/system/db/content.go b/system/db/content.go index 03e6a41..9aa86d8 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -217,17 +217,28 @@ func Query(namespace string, opts QueryOptions) [][]byte { c := b.Cursor() n := b.Stats().KeyN + var start, end int + switch opts.Count { + case -1: + start = 0 + end = n + + default: + start = opts.Count * opts.Offset + end = start + opts.Count + } + i := 0 // count of num posts added cur := 0 // count of where cursor is switch opts.Order { case "asc": for k, v := c.Last(); k != nil; c.Prev() { - if cur < opts.Offset*opts.Count { + if start < cur && cur < end { cur++ continue } - if i >= opts.Count || cur > n { + if i >= opts.Count { break } @@ -237,12 +248,12 @@ func Query(namespace string, opts QueryOptions) [][]byte { case "desc": for k, v := c.First(); k != nil; c.Next() { - if cur < opts.Offset*opts.Count { + if start < cur && cur < end { cur++ continue } - if i >= opts.Count || cur > n { + if i >= opts.Count { break } -- cgit v1.2.3 From 0620fa5c061b83141e00daeabc5e7ee982e0de55 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 1 Nov 2016 23:56:24 -0700 Subject: testing start / end positions on cursor --- system/db/content.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/db') diff --git a/system/db/content.go b/system/db/content.go index 9aa86d8..8058cad 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -233,7 +233,7 @@ func Query(namespace string, opts QueryOptions) [][]byte { switch opts.Order { case "asc": for k, v := c.Last(); k != nil; c.Prev() { - if start < cur && cur < end { + if cur < end { cur++ continue } @@ -248,7 +248,7 @@ func Query(namespace string, opts QueryOptions) [][]byte { case "desc": for k, v := c.First(); k != nil; c.Next() { - if start < cur && cur < end { + if cur < start { cur++ continue } -- cgit v1.2.3 From fe3e580e05cdbe1b7794f8c5f61634654377433b Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 1 Nov 2016 23:58:24 -0700 Subject: testing start / end positions on cursor - added back bounds check from prior version --- system/db/content.go | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'system/db') diff --git a/system/db/content.go b/system/db/content.go index 8058cad..f1969b5 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -228,6 +228,14 @@ func Query(namespace string, opts QueryOptions) [][]byte { end = start + opts.Count } + // bounds check on posts given the start & end count + if start > n { + start = n - opts.Count + } + if end > n { + end = n + } + i := 0 // count of num posts added cur := 0 // count of where cursor is switch opts.Order { -- cgit v1.2.3 From 01040a704ebf4ba3e856c1e18e0bb8487776ed65 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 2 Nov 2016 00:08:41 -0700 Subject: adding addtional stops for count/offset --- system/db/content.go | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'system/db') diff --git a/system/db/content.go b/system/db/content.go index f1969b5..3e22a68 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -246,12 +246,17 @@ func Query(namespace string, opts QueryOptions) [][]byte { continue } + if cur >= start { + break + } + if i >= opts.Count { break } posts = append(posts, v) i++ + cur++ } case "desc": @@ -261,12 +266,17 @@ func Query(namespace string, opts QueryOptions) [][]byte { continue } + if cur >= end { + break + } + if i >= opts.Count { break } posts = append(posts, v) i++ + cur++ } } -- cgit v1.2.3 From 4de65aa8ba77b10f847e0436bc624da3c39fe5d7 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 2 Nov 2016 00:10:35 -0700 Subject: reassigning k, v with next or prev record --- system/db/content.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/db') diff --git a/system/db/content.go b/system/db/content.go index 3e22a68..a3a4da1 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -240,7 +240,7 @@ func Query(namespace string, opts QueryOptions) [][]byte { cur := 0 // count of where cursor is switch opts.Order { case "asc": - for k, v := c.Last(); k != nil; c.Prev() { + for k, v := c.Last(); k != nil; k, v = c.Prev() { if cur < end { cur++ continue @@ -260,7 +260,7 @@ func Query(namespace string, opts QueryOptions) [][]byte { } case "desc": - for k, v := c.First(); k != nil; c.Next() { + for k, v := c.First(); k != nil; k, v = c.Next() { if cur < start { cur++ continue -- cgit v1.2.3 From 8ab68e3609c45b90dbc716b1b10db05f978bb96e Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 2 Nov 2016 00:11:59 -0700 Subject: adding test fix for asc order --- system/db/content.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/db') diff --git a/system/db/content.go b/system/db/content.go index a3a4da1..30010e8 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -246,7 +246,7 @@ func Query(namespace string, opts QueryOptions) [][]byte { continue } - if cur >= start { + if cur <= start { break } -- cgit v1.2.3 From d31212d0d147f2e727241005a62ca434c8e8a417 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 2 Nov 2016 00:14:32 -0700 Subject: adding test fix for asc order --- system/db/content.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/db') diff --git a/system/db/content.go b/system/db/content.go index 30010e8..860f830 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -246,7 +246,7 @@ func Query(namespace string, opts QueryOptions) [][]byte { continue } - if cur <= start { + if cur < start { break } -- cgit v1.2.3 From c92042d86c5d4f67ce2304c30c60273f767eb4c0 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 2 Nov 2016 00:21:14 -0700 Subject: adding test fix for asc order --- system/db/content.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'system/db') diff --git a/system/db/content.go b/system/db/content.go index 860f830..a7fbc30 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -237,16 +237,16 @@ func Query(namespace string, opts QueryOptions) [][]byte { } i := 0 // count of num posts added - cur := 0 // count of where cursor is + cur := 0 // count of num cursor moves switch opts.Order { case "asc": for k, v := c.Last(); k != nil; k, v = c.Prev() { - if cur < end { + if cur < start { cur++ continue } - if cur < start { + if cur >= end { break } -- cgit v1.2.3 From 09d6b1df9f9517ba382e996f877e1ce6c988d992 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 2 Nov 2016 00:42:59 -0700 Subject: testing fix for -1 case in count length --- system/db/content.go | 8 -------- 1 file changed, 8 deletions(-) (limited to 'system/db') diff --git a/system/db/content.go b/system/db/content.go index a7fbc30..7337f0f 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -250,10 +250,6 @@ func Query(namespace string, opts QueryOptions) [][]byte { break } - if i >= opts.Count { - break - } - posts = append(posts, v) i++ cur++ @@ -270,10 +266,6 @@ func Query(namespace string, opts QueryOptions) [][]byte { break } - if i >= opts.Count { - break - } - posts = append(posts, v) i++ cur++ -- cgit v1.2.3 From 7ec1187b1987861831a88737ffb8512e8568ad8c Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 2 Nov 2016 00:58:41 -0700 Subject: swapping order of post winding --- system/db/content.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'system/db') diff --git a/system/db/content.go b/system/db/content.go index 7337f0f..714e0c6 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -214,6 +214,10 @@ func Query(namespace string, opts QueryOptions) [][]byte { var posts [][]byte store.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(namespace)) + if b == nil { + return nil + } + c := b.Cursor() n := b.Stats().KeyN -- cgit v1.2.3