From b4d2de6ec86c37aa9358655aa66253e1493819f9 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Sun, 3 Mar 2019 12:51:25 -0500 Subject: [PATCH] add limit to search --- contrib/backends/srndv2/src/srnd/database.go | 4 ++-- .../backends/srndv2/src/srnd/frontend_http.go | 5 +++-- contrib/backends/srndv2/src/srnd/postgres.go | 20 +++++++++---------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/contrib/backends/srndv2/src/srnd/database.go b/contrib/backends/srndv2/src/srnd/database.go index 76ec26e..4c4feb8 100644 --- a/contrib/backends/srndv2/src/srnd/database.go +++ b/contrib/backends/srndv2/src/srnd/database.go @@ -323,10 +323,10 @@ type Database interface { GetPostingStats(granularity, begin, end int64) (PostingStats, error) // peform search query - SearchQuery(prefix, group, text string, chnl chan PostModel) error + SearchQuery(prefix, group, text string, chnl chan PostModel, limit int) error // find posts with similar hash - SearchByHash(prefix, group, posthash string, chnl chan PostModel) error + SearchByHash(prefix, group, posthash string, chnl chan PostModel, limit int) error // get full thread model GetThreadModel(prefix, root_msgid string) (ThreadModel, error) diff --git a/contrib/backends/srndv2/src/srnd/frontend_http.go b/contrib/backends/srndv2/src/srnd/frontend_http.go index 5ef20f7..ab8702a 100644 --- a/contrib/backends/srndv2/src/srnd/frontend_http.go +++ b/contrib/backends/srndv2/src/srnd/frontend_http.go @@ -1112,10 +1112,11 @@ func (self *httpFrontend) handle_api_find(wr http.ResponseWriter, r *http.Reques } donechnl <- 0 }(wr) + limit := 50 if len(h) > 0 { - self.daemon.database.SearchByHash(self.prefix, g, h, chnl) + self.daemon.database.SearchByHash(self.prefix, g, h, chnl, limit) } else { - self.daemon.database.SearchQuery(self.prefix, g, s, chnl) + self.daemon.database.SearchQuery(self.prefix, g, s, chnl, limit) } chnl <- nil <-donechnl diff --git a/contrib/backends/srndv2/src/srnd/postgres.go b/contrib/backends/srndv2/src/srnd/postgres.go index a719128..64c7ffd 100644 --- a/contrib/backends/srndv2/src/srnd/postgres.go +++ b/contrib/backends/srndv2/src/srnd/postgres.go @@ -219,10 +219,10 @@ func (self *PostgresDatabase) prepareStatements() { GetMessageIDByCIDR: "SELECT message_id FROM ArticlePosts WHERE addr IN ( SELECT encaddr FROM EncryptedAddrs WHERE addr_cidr <<= cidr($1) )", GetMessageIDByEncryptedIP: "SELECT message_id FROM ArticlePosts WHERE addr = $1", GetPostsBefore: "SELECT message_id FROM ArticlePosts WHERE time_posted < $1", - SearchQuery_1: "SELECT newsgroup, message_id, ref_id FROM ArticlePosts WHERE message LIKE $1 ORDER BY time_posted DESC", - SearchQuery_2: "SELECT newsgroup, message_id, ref_id FROM ArticlePosts WHERE newsgroup = $1 AND message LIKE $2 ORDER BY time_posted DESC", - SearchByHash_1: "SELECT message_newsgroup, message_id, message_ref_id FROM Articles WHERE message_id_hash LIKE $1 ORDER BY time_obtained DESC", - SearchByHash_2: "SELECT message_newsgroup, message_id, message_ref_id FROM Articles WHERE message_newsgroup = $2 AND message_id_hash LIKE $1 ORDER BY time_obtained DESC", + SearchQuery_1: "SELECT newsgroup, message_id, ref_id FROM ArticlePosts WHERE message LIKE $1 ORDER BY time_posted DESC LIMIT $2", + SearchQuery_2: "SELECT newsgroup, message_id, ref_id FROM ArticlePosts WHERE newsgroup = $1 AND message LIKE $2 ORDER BY time_posted DESC LIMIT $3", + SearchByHash_1: "SELECT message_newsgroup, message_id, message_ref_id FROM Articles WHERE message_id_hash LIKE $1 ORDER BY time_obtained DESC LIMIT $2", + SearchByHash_2: "SELECT message_newsgroup, message_id, message_ref_id FROM Articles WHERE message_newsgroup = $2 AND message_id_hash LIKE $1 ORDER BY time_obtained DESC LIMIT $3", GetNNTPPostsInGroup: "SELECT message_no, ArticlePosts.message_id, subject, time_posted, ref_id, name, path FROM ArticleNumbers INNER JOIN ArticlePosts ON ArticleNumbers.message_id = ArticlePosts.message_id WHERE ArticlePosts.newsgroup = $1 ORDER BY message_no", GetCitesByPostHashLike: "SELECT message_id, message_ref_id FROM Articles WHERE message_id_hash LIKE $1", GetYearlyPostHistory: "WITH times(endtime, begintime) AS ( SELECT CAST(EXTRACT(epoch from i) AS BIGINT) AS endtime, CAST(EXTRACT(epoch from i - interval '1 month') AS BIGINT) AS begintime FROM generate_series(now() - interval '10 year', now(), '1 month'::interval) i ) SELECT begintime, endtime, ( SELECT count(*) FROM ArticlePosts WHERE time_posted > begintime AND time_posted < endtime) FROM times", @@ -1970,14 +1970,14 @@ func (self *PostgresDatabase) GetPostingStats(gran, begin, end int64) (st Postin return } -func (self *PostgresDatabase) SearchQuery(prefix, group string, text string, chnl chan PostModel) (err error) { +func (self *PostgresDatabase) SearchQuery(prefix, group string, text string, chnl chan PostModel, limit int) (err error) { if text != "" && strings.Count(text, "%") == 0 { text = "%" + text + "%" var rows *sql.Rows if group == "" { - rows, err = self.conn.Query(self.stmt[SearchQuery_1], text) + rows, err = self.conn.Query(self.stmt[SearchQuery_1], text, limit) } else { - rows, err = self.conn.Query(self.stmt[SearchQuery_2], group, text) + rows, err = self.conn.Query(self.stmt[SearchQuery_2], group, text, limit) } if err == nil { for rows.Next() { @@ -1991,15 +1991,15 @@ func (self *PostgresDatabase) SearchQuery(prefix, group string, text string, chn close(chnl) return } -func (self *PostgresDatabase) SearchByHash(prefix, group, text string, chnl chan PostModel) (err error) { +func (self *PostgresDatabase) SearchByHash(prefix, group, text string, chnl chan PostModel, limit int) (err error) { if text != "" && strings.Count(text, "%") == 0 { text = "%" + text + "%" var rows *sql.Rows if group == "" { - rows, err = self.conn.Query(self.stmt[SearchByHash_1], text) + rows, err = self.conn.Query(self.stmt[SearchByHash_1], text, limit) } else { - rows, err = self.conn.Query(self.stmt[SearchByHash_2], text, group) + rows, err = self.conn.Query(self.stmt[SearchByHash_2], text, group, limit) } if err == nil { for rows.Next() {