Skip to content

Commit 29cce74

Browse files
committed
wip: faff
1 parent aaa3e1a commit 29cce74

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

demo/index.html

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212

1313
<script type="module">
1414
import { newHasher } from '../dist/hash.js';
15+
import { identity } from '@openenergytools/scl-lib';
1516

1617
const scl1 = document.getElementById('scl1');
1718
const result = document.getElementById('result');
1819

19-
const { hash, db, idDb } = newHasher();
20+
const { hash, db, eDb } = newHasher();
2021

2122
function display(description) {
22-
console.log('displaying', description);
2323
const elements = [];
2424
Object.entries(description).forEach(([key, value]) => {
2525
if (key.startsWith('@')) {
@@ -29,12 +29,11 @@
2929
const sum = document.createElement('summary');
3030
det.append(sum);
3131
const d = db[tag][hash];
32-
sum.textContent = tag + ' ' + (idDb[hash] || ' ') + ' (' + hash + ')';
32+
sum.textContent = tag + ' ' + (identity(eDb.h2e.get(hash)) || ' ') + ' (' + hash + ')';
3333
det.addEventListener('toggle', () => {if (det.children.length < 2) det.append(...display(db[tag][hash]))})
3434
elements.push(det);
3535
}
3636
} else if ( key === 'eNS') {
37-
console.log('ens!', value);
3837
const det = document.createElement('details');
3938
const sum = document.createElement('summary');
4039
det.setAttribute('open', '');
@@ -76,7 +75,11 @@
7675
scl1.addEventListener('change', e => {
7776
for (const file of scl1.files) {
7877
file.text().then(text => {
78+
const t0 = (new Date().getTime());
7979
console.log(hash(new DOMParser().parseFromString(text, 'application/xml').documentElement))
80+
const t1 = (new Date().getTime());
81+
console.warn((t1 - t0) / 60000, 'min')
82+
console.warn((t1 - t0) / 1000, 'sec')
8083
Object.entries(db.SCL).forEach(([sclHash, description]) => {
8184
const details = document.createElement('details');
8285
const summary = document.createElement('summary');
@@ -90,12 +93,11 @@
9093
const det = document.createElement('details');
9194
const sum = document.createElement('summary');
9295
det.append(sum);
93-
sum.textContent = tag + ' ' + (idDb[hash] || ' ') + ' (' + hash + ')';
94-
det.addEventListener('toggle', () => {console.log('toggled!'); if (det.children.length < 2) det.append(...display(db[tag][hash]))})
96+
sum.textContent = tag + ' ' + (identity(eDb.h2e.get(hash)) || ' ') + ' (' + hash + ')';
97+
det.addEventListener('toggle', () => {if (det.children.length < 2) det.append(...display(db[tag][hash]))})
9598
details.append(det);
9699
}
97100
} else if ( key === 'eNS') {
98-
console.log('ens!', value);
99101
const det = document.createElement('details');
100102
const sum = document.createElement('summary');
101103
det.setAttribute('open', '');

hash.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import xxhash from "xxhash-wasm";
2-
import { identity } from "@openenergytools/scl-lib";
32

43
const xxh = await xxhash();
54

@@ -56,9 +55,14 @@ const attributes: {
5655
defaults: { fc: "ST", timeout: 30 },
5756
};
5857

58+
interface ElementDB {
59+
e2h: WeakMap<Element, string>;
60+
h2e: Map<string, Element>;
61+
}
62+
5963
export function hasher(
6064
db: HashDB,
61-
idDb: IdentityDB,
65+
eDb: ElementDB,
6266
{
6367
ignoreAttrs = new Set([
6468
"desc",
@@ -176,18 +180,10 @@ export function hasher(
176180
return description;
177181
}
178182

179-
function describeDA(e: Element) {
180-
const description = {
181-
...describeBDA(e),
182-
} as Record<string, unknown>;
183-
184-
return description;
185-
}
186-
187183
const descriptions: Record<string, (e: Element) => object> = {
188184
AccessPoint: describeNaming,
189185
BDA: describeBDA,
190-
DA: describeDA,
186+
DA: describeBDA,
191187
DataTypeTemplates: describeNaming,
192188
DAType: describeNaming,
193189
DO: (e) => {
@@ -251,7 +247,8 @@ export function hasher(
251247
return describeNaming(e);
252248
}
253249

254-
function hash(e: Element) {
250+
function hash(e: Element): string {
251+
if (eDb.e2h.has(e)) return eDb.e2h.get(e)!;
255252
const tag =
256253
e.namespaceURI === e.ownerDocument.documentElement.namespaceURI
257254
? e.localName
@@ -260,8 +257,9 @@ export function hasher(
260257
const digest = xxh.h64ToString(JSON.stringify(description));
261258
if (!(tag in db)) db[tag] = {};
262259
if (!(digest in db[tag])) db[tag][digest] = description;
263-
if (!(digest in idDb)) {
264-
idDb[digest] = identity(e);
260+
if (!eDb.h2e.has(digest)) {
261+
eDb.h2e.set(digest, e);
262+
eDb.e2h.set(e, digest);
265263
}
266264
return digest;
267265
}
@@ -272,9 +270,9 @@ export function hasher(
272270
export function newHasher(options = {}): {
273271
hash: Hasher;
274272
db: HashDB;
275-
idDb: IdentityDB;
273+
eDb: ElementDB;
276274
} {
277275
const db: HashDB = {};
278-
const idDb: IdentityDB = {};
279-
return { hash: hasher(db, idDb, options), db, idDb };
276+
const eDb: ElementDB = { e2h: new WeakMap(), h2e: new Map() };
277+
return { hash: hasher(db, eDb, options), db, eDb };
280278
}

0 commit comments

Comments
 (0)