small attribute optimizations

This commit is contained in:
zadam 2023-01-27 08:46:04 +01:00
parent ad887c4b12
commit 2b4dfaef7f
3 changed files with 34 additions and 26 deletions

View File

@ -16,7 +16,7 @@ function isNotePathArchived(notePath) {
const note = becca.notes[notePath[i]]; const note = becca.notes[notePath[i]];
// this is going through parents so archived must be inheritable // this is going through parents so archived must be inheritable
if (note.hasInheritableOwnedArchivedLabel()) { if (note.hasInheritableArchivedLabel()) {
return true; return true;
} }
} }

View File

@ -386,11 +386,10 @@ class BNote extends AbstractBeccaEntity {
*/ */
getAttributes(type, name) { getAttributes(type, name) {
this.__validateTypeName(type, name); this.__validateTypeName(type, name);
this.__ensureAttributeCacheIsAvailable();
this.__getAttributes([]);
if (type && name) { if (type && name) {
return this.__attributeCache.filter(attr => attr.type === type && attr.name === name); return this.__attributeCache.filter(attr => attr.name === name && attr.type === type);
} }
else if (type) { else if (type) {
return this.__attributeCache.filter(attr => attr.type === type); return this.__attributeCache.filter(attr => attr.type === type);
@ -404,6 +403,13 @@ class BNote extends AbstractBeccaEntity {
} }
} }
/** @private */
__ensureAttributeCacheIsAvailable() {
if (!this.__attributeCache) {
this.__getAttributes([]);
}
}
/** @private */ /** @private */
__getAttributes(path) { __getAttributes(path) {
if (path.includes(this.noteId)) { if (path.includes(this.noteId)) {
@ -498,9 +504,9 @@ class BNote extends AbstractBeccaEntity {
*/ */
hasAttribute(type, name, value = null) { hasAttribute(type, name, value = null) {
return !!this.getAttributes().find(attr => return !!this.getAttributes().find(attr =>
attr.type === type attr.name === name
&& attr.name === name
&& (value === undefined || value === null || attr.value === value) && (value === undefined || value === null || attr.value === value)
&& attr.type === type
); );
} }
@ -509,13 +515,13 @@ class BNote extends AbstractBeccaEntity {
value = value ? value.toLowerCase() : null; value = value ? value.toLowerCase() : null;
return this.getAttributes().find( return this.getAttributes().find(
attr => attr.type === type attr => attr.name.toLowerCase() === name
&& attr.name.toLowerCase() === name && (!value || attr.value.toLowerCase() === value)
&& (!value || attr.value.toLowerCase() === value)); && attr.type === type);
} }
getRelationTarget(name) { getRelationTarget(name) {
const relation = this.getAttributes().find(attr => attr.type === 'relation' && attr.name === name); const relation = this.getAttributes().find(attr => attr.name === name && attr.type === 'relation');
return relation ? relation.targetNote : null; return relation ? relation.targetNote : null;
} }
@ -614,7 +620,7 @@ class BNote extends AbstractBeccaEntity {
getAttribute(type, name) { getAttribute(type, name) {
const attributes = this.getAttributes(); const attributes = this.getAttributes();
return attributes.find(attr => attr.type === type && attr.name === name); return attributes.find(attr => attr.name === name && attr.type === type);
} }
/** /**
@ -697,10 +703,10 @@ class BNote extends AbstractBeccaEntity {
this.__validateTypeName(type, name); this.__validateTypeName(type, name);
if (type && name && value !== undefined && value !== null) { if (type && name && value !== undefined && value !== null) {
return this.ownedAttributes.filter(attr => attr.type === type && attr.name === name && attr.value === value); return this.ownedAttributes.filter(attr => attr.name === name && attr.value === value && attr.type === type);
} }
else if (type && name) { else if (type && name) {
return this.ownedAttributes.filter(attr => attr.type === type && attr.name === name); return this.ownedAttributes.filter(attr => attr.name === name && attr.type === type);
} }
else if (type) { else if (type) {
return this.ownedAttributes.filter(attr => attr.type === type); return this.ownedAttributes.filter(attr => attr.type === type);
@ -728,16 +734,21 @@ class BNote extends AbstractBeccaEntity {
return this.hasAttribute('label', 'archived'); return this.hasAttribute('label', 'archived');
} }
hasInheritableOwnedArchivedLabel() { hasInheritableArchivedLabel() {
return !!this.ownedAttributes.find(attr => attr.type === 'label' && attr.name === 'archived' && attr.isInheritable); for (const attr of this.getAttributes()) {
if (attr.name === 'archived' && attr.type === LABEL && attr.isInheritable) {
return true;
}
}
return false;
} }
// will sort the parents so that non-search & non-archived are first and archived at the end // will sort the parents so that the non-archived are first and archived at the end
// this is done so that non-search & non-archived paths are always explored as first when looking for note path // this is done so that the non-archived paths are always explored as first when looking for note path
sortParents() { sortParents() {
this.parentBranches.sort((a, b) => this.parentBranches.sort((a, b) =>
a.branchId.startsWith('virt-') // FIXME: search virtual notes appear only in froca so this is probably not necessary a.parentNote?.hasInheritableArchivedLabel() ? 1 : -1);
|| a.parentNote?.hasInheritableOwnedArchivedLabel() ? 1 : -1);
this.parents = this.parentBranches this.parents = this.parentBranches
.map(branch => branch.parentNote) .map(branch => branch.parentNote)
@ -1032,7 +1043,7 @@ class BNote extends AbstractBeccaEntity {
} }
get ownedAttributeCount() { get ownedAttributeCount() {
return this.getAttributes().length; return this.getOwnedAttributes().length;
} }
/** @returns {BNote[]} */ /** @returns {BNote[]} */

View File

@ -126,7 +126,9 @@ class SNote extends AbstractShacaEntity {
* @returns {SAttribute[]} all note's attributes, including inherited ones * @returns {SAttribute[]} all note's attributes, including inherited ones
*/ */
getAttributes(type, name) { getAttributes(type, name) {
this.__getAttributes([]); if (!this.__attributeCache) {
this.__getAttributes([]);
}
if (type && name) { if (type && name) {
return this.__attributeCache.filter(attr => attr.type === type && attr.name === name && !isCredentials(attr)); return this.__attributeCache.filter(attr => attr.type === type && attr.name === name && !isCredentials(attr));
@ -428,11 +430,6 @@ class SNote extends AbstractShacaEntity {
return this.hasAttribute('label', 'archived'); return this.hasAttribute('label', 'archived');
} }
/** @returns {boolean} */
hasInheritableOwnedArchivedLabel() {
return !!this.ownedAttributes.find(attr => attr.type === 'label' && attr.name === 'archived' && attr.isInheritable);
}
/** @returns {boolean} */ /** @returns {boolean} */
isInherited() { isInherited() {
return !!this.targetRelations.find(rel => rel.name === 'template' || rel.name === 'inherit'); return !!this.targetRelations.find(rel => rel.name === 'template' || rel.name === 'inherit');