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){ set data(value){
this.__data = value }, this.__data = value },
__cache_path__: '.cache',
// XXX not sure if it is better to set these here or use index.IndexManagerMixin(..) // XXX not sure if it is better to set these here or use index.IndexManagerMixin(..)
get index_attrs(){ get index_attrs(){
return [...index.iter(this)] }, return [...index.iter(this)] },
@ -300,14 +301,23 @@ module.BaseStore = {
}, },
//*/ //*/
__search: index.makeIndex('search', __search: index.makeIndex('search',
// XXX do a load if present...
async function(){ async function(){
var index = new flexsearch.Index( var path = this.__cache_path__ +'/search'
this.__search_options // load index...
?? {}) if(this.__cache_path__
var update = this.__search.options.update && await this.exists(path)){
for(var path of (await this.paths)){ return this.__search.options.load()
update.call(this, index, path, await this.get(path)) } // 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)) } }
return index }, { return index }, {
// XXX do a save???
update: async function(data, path, update){ update: async function(data, path, update){
var {text, tags} = update var {text, tags} = update
text = [ text = [
@ -322,8 +332,43 @@ module.BaseStore = {
].join('\n\n') ].join('\n\n')
;(await data).add(path, update.text) ;(await data).add(path, update.text)
return data }, return data },
// XXX do a save???
remove: async function(data, path){ remove: async function(data, path){
;(await data).remove(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 }, }), return data }, }),
search: function(){ search: function(){
return this.__search().search(...arguments) }, 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... * XXX FEATURE: to avoid reinventing the wheel, serialize stuff as pages...
* something like: * .paths - ???
* /.index/<name> * .names - ???
* would be logical for indexes or: * .tags -
* /.data/<name> * .search() - DONE
* for general stuff... * 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 * XXX macro: macro:count / macro:index vars can be overridden by count/index
* attributes.... * attributes....
* ...this needs testing... * ...this needs testing...