11import { WorkspaceFolder } from 'vscode' ;
2- import { IndexerKey , IndexerStorage , IndexedFilePath } from 'types/indexer' ;
2+ import { IndexerKey , IndexerStorage , IndexedFilePath , SavedIndex } from 'types/indexer' ;
3+ import { IndexDataSerializer } from './IndexDataSerializer' ;
4+ import Context from 'common/Context' ;
5+ import ExtensionState from 'common/ExtensionState' ;
36
47export default class IndexStorage {
58 private _indexStorage : IndexerStorage = { } ;
9+ private serializer = new IndexDataSerializer ( ) ;
610
711 public set ( workspaceFolder : WorkspaceFolder , key : IndexerKey , value : Map < IndexedFilePath , any > ) {
812 if ( ! this . _indexStorage [ workspaceFolder . uri . fsPath ] ) {
@@ -23,11 +27,44 @@ export default class IndexStorage {
2327 this . _indexStorage = { } ;
2428 }
2529
26- public async load ( ) {
27- // TODO: Implement
30+ public hasIndex ( workspaceFolder : WorkspaceFolder , key : IndexerKey ) {
31+ return ! ! this . _indexStorage [ workspaceFolder . uri . fsPath ] ?. [ key ] ;
2832 }
2933
30- public async save ( ) {
31- // TODO: Implement
34+ public saveIndex ( workspaceFolder : WorkspaceFolder , key : IndexerKey , version : number ) {
35+ const indexData = this . _indexStorage [ workspaceFolder . uri . fsPath ] [ key ] ;
36+
37+ const savedIndex : SavedIndex = {
38+ version,
39+ data : indexData ,
40+ } ;
41+ const serialized = this . serializer . serialize ( savedIndex ) ;
42+
43+ ExtensionState . context . globalState . update (
44+ `index-storage-${ workspaceFolder . uri . fsPath } -${ key } ` ,
45+ serialized
46+ ) ;
47+ }
48+
49+ public loadIndex ( workspaceFolder : WorkspaceFolder , key : IndexerKey , version : number ) {
50+ const serialized = ExtensionState . context . globalState . get < string > (
51+ `index-storage-${ workspaceFolder . uri . fsPath } -${ key } `
52+ ) ;
53+
54+ if ( ! serialized ) {
55+ return undefined ;
56+ }
57+
58+ const savedIndex = this . serializer . deserialize ( serialized ) ;
59+
60+ if ( savedIndex . version !== version ) {
61+ return undefined ;
62+ }
63+
64+ if ( ! this . _indexStorage [ workspaceFolder . uri . fsPath ] ) {
65+ this . _indexStorage [ workspaceFolder . uri . fsPath ] = { } ;
66+ }
67+
68+ this . _indexStorage [ workspaceFolder . uri . fsPath ] [ key ] = savedIndex . data ;
3269 }
3370}
0 commit comments