From 4da52f2c07ff465249fd57a659c375ec45e5f7a8 Mon Sep 17 00:00:00 2001 From: eugene Date: Sat, 23 Dec 2017 13:23:10 -0600 Subject: [PATCH] hard limit to last --- app/store/bolt.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/store/bolt.go b/app/store/bolt.go index 2b55b15d..bff0c8c6 100644 --- a/app/store/bolt.go +++ b/app/store/bolt.go @@ -14,16 +14,17 @@ import ( ) // BoltDB implements store.Interface. Each instance represents one site. -// Keys built as pid-id. Each url (post) makes it's own bucket +// Keys are commendID. Each url (post) makes it's own bucket. // In addition there is a bucket "last" with reference to other buckets+keys to all cross-posts last comment extraction. // Thread safe. type BoltDB struct { *bolt.DB } -var ( +const ( lastBucketName = "last" blocksBucketPrefix = "block-" + lastLimit = 1000 ) // NewBoltDB makes persistent boltdb-based store @@ -175,6 +176,10 @@ func (b *BoltDB) Get(locator Locator, commentID string) (comment Comment, err er // Last returns up to max last comments for given locator func (b *BoltDB) Last(locator Locator, max int) (result []Comment, err error) { + if max > lastLimit || max == 0 { + max = lastLimit + } + err = b.View(func(tx *bolt.Tx) error { lastBucket := tx.Bucket([]byte(lastBucketName)) if lastBucket == nil { @@ -202,7 +207,7 @@ func (b *BoltDB) Last(locator Locator, max int) (result []Comment, err error) { return errors.Wrap(e, "failed to unmarshal") } result = append(result, comment) - if max > 0 && len(result) >= max { + if len(result) >= max { return nil } }