git ssb

16+

Dominic / patchbay



Commit 70bc98e1fcb76213a7b487d8e8b7a8f49cc45943

Use fulltext search plugin if it is available

- Fallback to old search if fulltext plugin is not present
- Only show search counter if using linear search
cel committed on 1/19/2017, 6:13:35 AM
Parent: cab96cfe6398bece694d747fcda9a4ec2211eef3

Files changed

manifest.jsonchanged
modules_core/sbot.jschanged
modules_extra/search.jschanged
manifest.jsonView
@@ -103,8 +103,11 @@
103103 "push": "async",
104104 "changes": "source",
105105 "createWants": "source"
106106 },
107 + "fulltext": {
108 + "search": "source"
109 + },
107110 "links2": {
108111 "read": "source",
109112 "dump": "source"
110113 },
modules_core/sbot.jsView
@@ -42,8 +42,9 @@
4242 sbot_blobs_add: true,
4343 sbot_links: true,
4444 sbot_links2: true,
4545 sbot_query: true,
46 + sbot_fulltext_search: true,
4647 sbot_get: true,
4748 sbot_log: true,
4849 sbot_user_feed: true,
4950 sbot_gossip_peers: true,
@@ -134,8 +135,11 @@
134135 }),
135136 sbot_user_feed: rec.source(function (opts) {
136137 return sbot.createUserStream(opts)
137138 }),
139 + sbot_fulltext_search: rec.source(function (opts) {
140 + return sbot.fulltext.search(opts)
141 + }),
138142 sbot_get: rec.async(function (key, cb) {
139143 if('function' !== typeof cb)
140144 throw new Error('cb must be function')
141145 if(CACHE[key]) cb(null, CACHE[key])
modules_extra/search.jsView
@@ -5,9 +5,10 @@
55 var TextNodeSearcher = require('text-node-searcher')
66
77 exports.needs = {
88 message_render: 'first',
9- sbot_log: 'first'
9 + sbot_log: 'first',
10 + sbot_fulltext_search: 'first'
1011 }
1112
1213 exports.gives = 'screen_view'
1314
@@ -49,16 +50,35 @@
4950 searcher.highlight()
5051 return el
5152 }
5253
54 +function fallback(reader) {
55 + var fallbackRead
56 + return function (read) {
57 + return function (abort, cb) {
58 + read(abort, function next(end, data) {
59 + if (end && reader && (fallbackRead = reader(end))) {
60 + reader = null
61 + read = fallbackRead
62 + read(abort, next)
63 + } else {
64 + cb(end, data)
65 + }
66 + })
67 + }
68 + }
69 +}
70 +
5371 exports.create = function (api) {
5472
5573 return function (path) {
5674 if(path[0] === '?') {
57- var query = path.substr(1).trim().split(whitespace)
75 + var queryStr = path.substr(1).trim()
76 + var query = queryStr.split(whitespace)
5877 var _matches = searchFilter(query)
5978
6079 var total = 0, matches = 0
80 + var usingLinearSearch = false
6181
6282 var header = h('div.search_header', '')
6383 var content = h('div.column.scroller__content')
6484 var div = h('div.column.scroller',
@@ -72,14 +92,14 @@
7292 function matchesQuery (data) {
7393 total++
7494 var m = _matches(data)
7595 if(m) matches++
76- header.textContent = 'searched:'+total+', found:'+matches
96 + if(usingLinearSearch) {
97 + header.textContent = 'searched:'+total+', found:'+matches
98 + }
7799 return m
78100 }
79101
80-
81-
82102 function renderMsg(msg) {
83103 var el = api.message_render(msg)
84104 highlight(el, createOrRegExp(query))
85105 return el
@@ -91,10 +111,18 @@
91111 Scroller(div, content, renderMsg, true, false)
92112 )
93113
94114 pull(
95- u.next(api.sbot_log, {reverse: true, limit: 500, live: false}),
96- pull.filter(matchesQuery),
115 + u.next(api.sbot_fulltext_search, {query: queryStr, reverse: true, limit: 500, live: false}),
116 + fallback(function (err) {
117 + if (/^no source/.test(err.message)) {
118 + usingLinearSearch = true
119 + return pull(
120 + u.next(api.sbot_log, {reverse: true, limit: 500, live: false}),
121 + pull.filter(matchesQuery)
122 + )
123 + }
124 + }),
97125 Scroller(div, content, renderMsg, false, false)
98126 )
99127
100128 return div

Built with git-ssb-web