• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hexojs / hexo / 12703017965

10 Jan 2025 03:59AM UTC coverage: 99.458% (-0.02%) from 99.482%
12703017965

Pull #5605

github

web-flow
Merge 3825ebe11 into bcfb0301d
Pull Request #5605: perf(PostCategory/PostTag): add binary relation index for performance

2340 of 2434 branches covered (96.14%)

145 of 148 new or added lines in 7 files covered. (97.97%)

2 existing lines in 1 file now uncovered.

9539 of 9591 relevant lines covered (99.46%)

53.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.7
/lib/models/binary_relation_index.ts
1
import type Hexo from '../hexo';
1✔
2

1✔
3
type BinaryRelationType<K extends PropertyKey, V extends PropertyKey> = {
1✔
4
  [key in K]: PropertyKey;
1✔
5
} & {
1✔
6
  [key in V]: PropertyKey;
1✔
7
};
1✔
8

1✔
9
class BinaryRelationIndex<K extends PropertyKey, V extends PropertyKey> {
1✔
10
  keyIndex: Map<PropertyKey, Set<PropertyKey>> = new Map();
1✔
11
  valueIndex: Map<PropertyKey, Set<PropertyKey>> = new Map();
1✔
12
  key: K;
1✔
13
  value: V;
1✔
14
  ctx: Hexo;
1✔
15
  schemaName: string;
1✔
16

1✔
17
  constructor(key: K, value: V, schemaName: string, ctx: Hexo) {
1✔
18
    this.key = key;
292✔
19
    this.value = value;
292✔
20
    this.schemaName = schemaName;
292✔
21
    this.ctx = ctx;
292✔
22
  }
292✔
23

1✔
24
  saveHook(data: BinaryRelationType<K, V> & { _id: PropertyKey }) {
1✔
25
    const _id = data._id;
112✔
26
    const key = data[this.key];
112✔
27
    const value = data[this.value];
112✔
28
    if (!this.keyIndex.has(key)) {
112✔
29
      this.keyIndex.set(key, new Set());
71✔
30
    }
71✔
31
    this.keyIndex.get(key).add(_id);
112✔
32

112✔
33
    if (!this.valueIndex.has(value)) {
112✔
34
      this.valueIndex.set(value, new Set());
78✔
35
    }
78✔
36
    this.valueIndex.get(value).add(_id);
112✔
37
  }
112✔
38

1✔
39
  removeHook(data: BinaryRelationType<K, V> & { _id: PropertyKey }) {
1✔
40
    const _id = data._id;
190✔
41
    const key = data[this.key];
190✔
42
    const value = data[this.value];
190✔
43
    this.keyIndex.get(key)?.delete(_id);
190✔
44
    if (this.keyIndex.get(key)?.size === 0) {
190✔
45
      this.keyIndex.delete(key);
47✔
46
    }
47✔
47
    this.valueIndex.get(value)?.delete(_id);
190✔
48
    if (this.valueIndex.get(value)?.size === 0) {
190✔
49
      this.valueIndex.delete(value);
58✔
50
    }
58✔
51
  }
190✔
52

1✔
53
  findById(_id: PropertyKey) {
1✔
54
    const raw = this.ctx.model(this.schemaName).findById(_id, { lean: true });
185✔
55
    if (!raw) return;
185!
56
    return { ...raw };
185✔
57
  }
185✔
58

1✔
59
  find(query: Partial<BinaryRelationType<K, V>>) {
1✔
60
    const key = query[this.key];
954✔
61
    const value = query[this.value];
954✔
62

954✔
63
    if (key && value) {
954✔
64
      const ids = this.keyIndex.get(key);
122✔
65
      if (!ids) return [];
122✔
66
      return Array.from(ids)
12✔
67
        .map(_id => this.findById(_id))
12✔
68
        .filter(record => record?.[this.value] === value);
12✔
69
    }
12✔
70

832✔
71
    if (key) {
954✔
72
      const ids = this.keyIndex.get(key);
571✔
73
      if (!ids) return [];
571✔
74
      return Array.from(ids).map(_id => this.findById(_id));
35✔
75
    }
35✔
76

261✔
77
    if (value) {
261✔
78
      const ids = this.valueIndex.get(value);
261✔
79
      if (!ids) return [];
261✔
80
      return Array.from(ids).map(_id => this.findById(_id));
49✔
81
    }
49✔
NEW
82

×
NEW
83
    return [];
×
NEW
84
  }
×
85

1✔
86
  findOne(query: Partial<BinaryRelationType<K, V>>) {
1✔
87
    return this.find(query)[0];
122✔
88
  }
122✔
89
}
1✔
90

1✔
91
export default BinaryRelationIndex;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc