added tag index storing + minor refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-11-07 03:42:48 +03:00
parent c58a117cb3
commit 2dbf814bf4

View File

@ -250,15 +250,22 @@ module.BaseStore = {
return this.__paths_isvalid__(t) }, return this.__paths_isvalid__(t) },
__tags: index.makeIndex('tags', __tags: index.makeIndex('tags',
async function(){ async function(){
var index = {tags: {}, paths: {}} // load index...
var update = this.__tags.options.update var path = this.__cache_path__ +'/tags_index'
for(var path of (await this.paths)){ if(this.__cache_path__
index = update.call(this, index, name, path, await this.get(path)) } && await this.exists(path)){
return index }, { return this.__tags.options.load.call(this, null, 'tags')
// generate...
} else {
var index = {tags: {}, paths: {}}
var update = this.__tags.options.update
for(var path of (await this.paths)){
index = update.call(this, index, name, path, await this.get(path)) }
return index } }, {
update: async function(data, name, path, update){ update: async function(data, name, path, update){
// do not index cache... // do not index cache...
if(this.__cache_path__ if(this.__cache_path__
&& !path.startsWith(this.__cache_path__)){ && path.startsWith(this.__cache_path__)){
return data } return data }
var {tags, paths} = await data var {tags, paths} = await data
// remove obsolete tags... // remove obsolete tags...
@ -271,10 +278,29 @@ module.BaseStore = {
.add(path) } .add(path) }
return data }, return data },
remove: async function(data, name, path){ remove: async function(data, name, path){
var {tags, paths} = await data data = await data
if(!data){
return data }
var {tags, paths} = data
for(var tag of paths[path] ?? []){ for(var tag of paths[path] ?? []){
tags[tag].delete(path) } tags[tag].delete(path) }
return data }, }), return data },
// XXX EXPERIMENTAL...
save: async function(data, name){
if(this.__cache_path__){
this.update(
this.__cache_path__ +'/'+ name+'_index',
{index: data})}
return data },
load: async function(data, name){
if(this.__cache_path__){
var path = this.__cache_path__ +'/'+ name+'_index'
var {index} = await this.get(path) ?? {}
return index ?? data }
return data },
reset: function(_, name){
this.__cache_path__
&& this.delete(this.__cache_path__ +'/'+ name+'_index') }, }),
get tags(){ get tags(){
return this.__tags() }, return this.__tags() },
@ -305,8 +331,8 @@ module.BaseStore = {
__search: index.makeIndex('search', __search: index.makeIndex('search',
// XXX do a load if present... // XXX do a load if present...
async function(){ async function(){
var path = this.__cache_path__ +'/search'
// load index... // load index...
var path = this.__cache_path__ +'/search_index'
if(this.__cache_path__ if(this.__cache_path__
&& await this.exists(path)){ && await this.exists(path)){
return this.__search.options.load.call(this, null, 'search') return this.__search.options.load.call(this, null, 'search')
@ -317,8 +343,8 @@ module.BaseStore = {
?? {}) ?? {})
var update = this.__search.options.update var update = this.__search.options.update
for(var path of (await this.paths)){ for(var path of (await this.paths)){
update.call(this, index, name, path, await this.get(path)) } } update.call(this, index, name, path, await this.get(path)) }
return index }, { return index } }, {
update: async function(data, name, path, update){ update: async function(data, name, path, update){
// do not index cache... // do not index cache...
if(this.__cache_path__ if(this.__cache_path__
@ -353,7 +379,7 @@ module.BaseStore = {
__save_changes: async function(name, action, path, ...args){ __save_changes: async function(name, action, path, ...args){
if(this.__cache_path__ if(this.__cache_path__
&& !path.startsWith(this.__cache_path__)){ && !path.startsWith(this.__cache_path__)){
var p = [this.__cache_path__, name, 'changes'].join('/') var p = [this.__cache_path__, name+'_index', 'changes'].join('/')
// XXX can we get/update in one op??? // XXX can we get/update in one op???
var {changes} = await this.get(p) ?? {} var {changes} = await this.get(p) ?? {}
changes = changes ?? [] changes = changes ?? []
@ -363,7 +389,7 @@ module.BaseStore = {
save: async function(data, name){ save: async function(data, name){
if(this.__cache_path__){ if(this.__cache_path__){
var that = this var that = this
var path = this.__cache_path__ +'/'+ name var path = this.__cache_path__ +'/'+ name+'_index'
//this.delete(path +'/changes') //this.delete(path +'/changes')
// XXX HACK this thing runs async but does not return a promise... // XXX HACK this thing runs async but does not return a promise...
// ...this is quote ugly but I can't figure out a way to // ...this is quote ugly but I can't figure out a way to
@ -383,7 +409,7 @@ module.BaseStore = {
return data }, return data },
load: async function(data, name){ load: async function(data, name){
if(this.__cache_path__){ if(this.__cache_path__){
var path = this.__cache_path__ +'/'+ name var path = this.__cache_path__ +'/'+ name+'_index'
var changes = path +'/changes' var changes = path +'/changes'
var {index} = await this.get(path) ?? {} var {index} = await this.get(path) ?? {}
var data = var data =
@ -395,7 +421,7 @@ module.BaseStore = {
return data }, return data },
reset: function(_, name){ reset: function(_, name){
this.__cache_path__ this.__cache_path__
&& this.delete(this.__cache_path__ +'/'+ name) },}), && this.delete(this.__cache_path__ +'/'+ name+'_index') }, }),
search: function(...args){ search: function(...args){
var s = this.__search() var s = this.__search()
return s instanceof Promise ? return s instanceof Promise ?