added cleanup to dict handling...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2018-12-25 01:25:52 +03:00
parent 1851a4943d
commit 095aa58ee0

View File

@ -291,6 +291,7 @@ var BaseTagsPrototype = {
// Format: // Format:
// Set([ <tag>, ... ]) // Set([ <tag>, ... ])
// //
// NOTE: this is expected to contain normalized values only...
persistent: null, persistent: null,
// Tag definitions... // Tag definitions...
@ -781,6 +782,8 @@ var BaseTagsPrototype = {
return Object.keys(this.__index || {}) return Object.keys(this.__index || {})
//.concat([...(this.persistentAll || [])] //.concat([...(this.persistentAll || [])]
.concat([...(this.persistent || [])] .concat([...(this.persistent || [])]
// XXX do we need to normalize here???
// ...do we trust .persistent???
.map(function(t){ .map(function(t){
return that.normalize(t) })) return that.normalize(t) }))
.unique() .unique()
@ -1819,11 +1822,9 @@ module.TagsWithHandlers =
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// XXX EXPERIMENTAL... // XXX EXPERIMENTAL...
// XXX need a .dict cleanup strategy...
var TagsWithDictPrototype = { var TagsWithDictPrototype = {
__proto__: BaseTagsPrototype, __proto__: BaseTagsPrototype,
// Tag dictionary... // Tag dictionary...
// //
// Format: // Format:
@ -1892,54 +1893,83 @@ var TagsWithDictPrototype = {
res[0] res[0]
: res }, : res },
// XXX batch remove???
// XXX
// Remove orphaned .dict values... // Remove orphaned .dict values...
// //
// NOTE: an orphan is a dict entry for a tag that is no longer used. // NOTE: an orphan is a dict entry for a tag that is no longer used.
cleanupDict: function(){ // NOTE: this will not remove tags that are not orphaned...
// get tags list... // XXX check which of the branches is faster...
var tags = new Set(this.singleTags() removeOrphansFromDict: function(...tags){
.concat(this.splitTag(this.definitionPaths()) if(!this.dict){
.flat())) return this
}
var that = this
var dict = this.dict var dict = this.dict
dict
&& Object.keys(dict) tags = tags.length == 0 ?
Object.keys(dict)
: this.normalize(this.splitTag(normalizeSplit(tags)))
// check all...
// XXX tune the threshold or make it dynamic...
// XXX do we need both branches???
if(tags.length > 3){
var index = new Set(this.splitTag(
...this.tags(),
...this.definitionPaths()))
tags = tags
.filter(function(tag){ .filter(function(tag){
return !tags.has(tag) }) return !index.has(tag) })
.forEach(function(tag){
delete dict[tag] }) // check specific tags...
// NOTE: this is geared towards a small number of input tags...
} else {
tags = tags
.filter(function(tag){
return that.match(tag).length == 0 })
}
tags
.forEach(function(tag){
delete dict[tag] })
return this return this
}, },
// Hooks... // Save/clean dict on prototype methods...
tag: function(tags, value){ tag: function(tags, value){
this.normalizeSave(tags) this.normalizeSave(tags)
return object.parent(TagsWithDictPrototype.tag, this).call(this, ...arguments) }, return object.parent(TagsWithDictPrototype.tag, this).call(this, ...arguments) },
// XXX cleanup .dict...
untag: function(tags, value){ untag: function(tags, value){
// XXX cleanup .dict... var res = object.parent(TagsWithDictPrototype.untag, this).call(this, ...arguments)
return object.parent(TagsWithDictPrototype.untag, this).call(this, ...arguments) }, this.removeOrphansFromDict(tags)
// XXX cleanup .dict... return res
},
rename: function(from, to, ...tags){ rename: function(from, to, ...tags){
arguments.length == 2 arguments.length == 2
&& this.normalizeSave(to) && this.normalizeSave(to)
// XXX cleanup dict... var res = object.parent(TagsWithDictPrototype.rename, this).call(this, ...arguments)
return object.parent(TagsWithDictPrototype.rename, this).call(this, ...arguments) }, this.removeOrphansFromDict(from)
// XXX cleanup .dict... return res
},
togglePersistent: function(...tags){ togglePersistent: function(...tags){
// XXX remove action...
this.normalizeSave(tags) this.normalizeSave(tags)
// XXX cleanup dict... var res = object.parent(TagsWithDictPrototype.togglePersistent, this).call(this, ...arguments)
return object.parent(TagsWithDictPrototype.togglePersistent, this).call(this, ...arguments) }, this.removeOrphansFromDict(res
// XXX cleanup .dict... .map(function(r, i){
return r == 'off' ? tags[i] : [] })
.flat())
return res
},
define: function(tag, value){ define: function(tag, value){
arguments.length > 1 arguments.length > 1
&& value != null && value != null
&& this.normalizeSave(tag, value) && this.normalizeSave(tag, value)
return object.parent(TagsWithDictPrototype.define, this).call(this, ...arguments) }, var res = object.parent(TagsWithDictPrototype.define, this).call(this, ...arguments)
value == null
&& this.removeOrphansFromDict(tag)
return res
},
} }