@@ -6,6 +6,7 @@ import { Emitter } from '../common/event';
6
6
import { ResourceProvider } from './provider' ;
7
7
import { IDisposable } from '../common/lifecycle' ;
8
8
import { IDataStore } from '../services/datastore' ;
9
+ import TrieMap from 'mnemonist/trie-map' ;
9
10
10
11
export class FoamWorkspace implements IDisposable {
11
12
private onDidAddEmitter = new Emitter < Resource > ( ) ;
@@ -20,7 +21,7 @@ export class FoamWorkspace implements IDisposable {
20
21
/**
21
22
* Resources by path
22
23
*/
23
- private _resources : Map < string , Resource > = new Map ( ) ;
24
+ private _resources : TrieMap < string , Resource > = new TrieMap ( ) ;
24
25
25
26
/**
26
27
* @param defaultExtension: The default extension for notes in this workspace (e.g. `.md`)
@@ -33,16 +34,19 @@ export class FoamWorkspace implements IDisposable {
33
34
34
35
set ( resource : Resource ) {
35
36
const old = this . find ( resource . uri ) ;
36
- this . _resources . set ( normalize ( resource . uri . path ) , resource ) ;
37
+
38
+ // store resource
39
+ this . _resources . set ( this . getTrieIdentifier ( resource . uri . path ) , resource ) ;
40
+
37
41
isSome ( old )
38
42
? this . onDidUpdateEmitter . fire ( { old : old , new : resource } )
39
43
: this . onDidAddEmitter . fire ( resource ) ;
40
44
return this ;
41
45
}
42
46
43
47
delete ( uri : URI ) {
44
- const deleted = this . _resources . get ( normalize ( uri . path ) ) ;
45
- this . _resources . delete ( normalize ( uri . path ) ) ;
48
+ const deleted = this . _resources . get ( this . getTrieIdentifier ( uri ) ) ;
49
+ this . _resources . delete ( this . getTrieIdentifier ( uri ) ) ;
46
50
47
51
isSome ( deleted ) && this . onDidDeleteEmitter . fire ( deleted ) ;
48
52
return deleted ?? null ;
@@ -57,7 +61,11 @@ export class FoamWorkspace implements IDisposable {
57
61
}
58
62
59
63
public resources ( ) : IterableIterator < Resource > {
60
- return this . _resources . values ( ) ;
64
+ const resources : Array < Resource > = Array . from (
65
+ this . _resources . values ( )
66
+ ) . sort ( Resource . sortByPath ) ;
67
+
68
+ return resources . values ( ) ;
61
69
}
62
70
63
71
public get ( uri : URI ) : Resource {
@@ -70,17 +78,22 @@ export class FoamWorkspace implements IDisposable {
70
78
}
71
79
72
80
public listByIdentifier ( identifier : string ) : Resource [ ] {
73
- const needle = normalize ( '/' + identifier ) ;
81
+ let needle = this . getTrieIdentifier ( identifier ) ;
82
+
74
83
const mdNeedle =
75
- getExtension ( needle ) !== this . defaultExtension
76
- ? needle + this . defaultExtension
84
+ getExtension ( normalize ( identifier ) ) !== this . defaultExtension
85
+ ? this . getTrieIdentifier ( identifier + this . defaultExtension )
77
86
: undefined ;
87
+
78
88
const resources : Resource [ ] = [ ] ;
79
- for ( const key of this . _resources . keys ( ) ) {
80
- if ( key . endsWith ( mdNeedle ) || key . endsWith ( needle ) ) {
81
- resources . push ( this . _resources . get ( normalize ( key ) ) ) ;
82
- }
89
+
90
+ this . _resources . find ( needle ) . forEach ( elm => {
91
+ resources . push ( elm [ 1 ] ) ;
92
+ } ) ;
93
+ if ( mdNeedle ) {
94
+ this . _resources . find ( mdNeedle ) . forEach ( elm => resources . push ( elm [ 1 ] ) ) ;
83
95
}
96
+
84
97
return resources . sort ( Resource . sortByPath ) ;
85
98
}
86
99
@@ -119,9 +132,32 @@ export class FoamWorkspace implements IDisposable {
119
132
return identifier ;
120
133
}
121
134
135
+ /**
136
+ * Returns a note identifier in reversed order. Used to optimise the storage of notes in
137
+ * the workspace to optimise retrieval of notes.
138
+ *
139
+ * @param reference the URI path to reverse
140
+ */
141
+ private getTrieIdentifier ( reference : URI | string ) : string {
142
+ let path : string ;
143
+ if ( reference instanceof URI ) {
144
+ path = ( reference as URI ) . path ;
145
+ } else {
146
+ path = reference as string ;
147
+ }
148
+
149
+ let reversedPath = normalize ( path ) . split ( '/' ) . reverse ( ) . join ( '/' ) ;
150
+
151
+ if ( reversedPath . indexOf ( '/' ) < 0 ) {
152
+ reversedPath = reversedPath + '/' ;
153
+ }
154
+
155
+ return reversedPath ;
156
+ }
157
+
122
158
public find ( reference : URI | string , baseUri ?: URI ) : Resource | null {
123
159
if ( reference instanceof URI ) {
124
- return this . _resources . get ( normalize ( ( reference as URI ) . path ) ) ?? null ;
160
+ return this . _resources . get ( this . getTrieIdentifier ( reference ) ) ?? null ;
125
161
}
126
162
let resource : Resource | null = null ;
127
163
const [ path , fragment ] = ( reference as string ) . split ( '#' ) ;
@@ -135,7 +171,7 @@ export class FoamWorkspace implements IDisposable {
135
171
: isSome ( baseUri )
136
172
? baseUri . resolve ( candidate ) . path
137
173
: null ;
138
- resource = this . _resources . get ( normalize ( searchKey ) ) ;
174
+ resource = this . _resources . get ( this . getTrieIdentifier ( searchKey ) ) ;
139
175
if ( resource ) {
140
176
break ;
141
177
}
0 commit comments