added persistent search index, still experimenting...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-11-06 01:06:56 +03:00
parent 1efe56bfe3
commit 5446537a33
2 changed files with 59 additions and 11 deletions

View File

@ -120,6 +120,7 @@ module.BaseStore = {
set data(value){
this.__data = value },
__cache_path__: '.cache',
// XXX not sure if it is better to set these here or use index.IndexManagerMixin(..)
get index_attrs(){
return [...index.iter(this)] },
@ -300,14 +301,23 @@ module.BaseStore = {
},
//*/
__search: index.makeIndex('search',
// XXX do a load if present...
async function(){
var path = this.__cache_path__ +'/search'
// load index...
if(this.__cache_path__
&& await this.exists(path)){
return this.__search.options.load()
// generate...
} else {
var index = new flexsearch.Index(
this.__search_options
?? {})
var update = this.__search.options.update
for(var path of (await this.paths)){
update.call(this, index, path, await this.get(path)) }
update.call(this, index, path, await this.get(path)) } }
return index }, {
// XXX do a save???
update: async function(data, path, update){
var {text, tags} = update
text = [
@ -322,8 +332,43 @@ module.BaseStore = {
].join('\n\n')
;(await data).add(path, update.text)
return data },
// XXX do a save???
remove: async function(data, path){
;(await data).remove(path)
return data },
// XXX EXPERIMENTAL...
save: async function(data){
if(this.__cache_path__){
var that = this
var path = this.__cache_path__ +'/search'
/*/ XXX this thing runs async but does not return a promise...
var index = {}
data.export(
function(key, value){
index[key] = value })
this.update(path, {index})
/*/
// XXX this is quote ugly but can't figure out a way to
// track when exporting is done...
var index = {}
data.export(
function(key, value){
index[key] = value
return that.update(path, {index}) })
//*/
}
return data },
load: async function(data){
if(this.__cache_path__){
var path = this.__cache_path__ +'/search'
var {index} =
await this.get(path)
?? {index: {}}
var data = new flexsearch.Index(
this.__search_options
?? {})
for(var [key, value] of Object.entries(index)){
data.import(key, value) } }
return data }, }),
search: function(){
return this.__search().search(...arguments) },

View File

@ -25,12 +25,15 @@
* -
*
*
* XXX stored index cache: need to define the save/load strategy + stored
* cache validation...
* XXX FEATURE: to avoid reinventing the wheel, serialize stuff as pages...
* something like:
* /.index/<name>
* would be logical for indexes or:
* /.data/<name>
* for general stuff...
* .paths - ???
* .names - ???
* .tags -
* .search() - DONE
* XXX index: pass name to all the handlers...
* ...this will simplify generic handlers...
* XXX macro: macro:count / macro:index vars can be overridden by count/index
* attributes....
* ...this needs testing...