@@ -10,6 +10,9 @@ const CHUNK_SIZE = 65536
1010const ADDRESS_SIZE = 10
1111
1212export default class Trix {
13+ private decoder = new TextDecoder ( 'utf8' )
14+ private indexCache ?: readonly ( readonly [ string , number ] ) [ ]
15+
1316 constructor (
1417 public ixxFile : GenericFilehandle ,
1518 public ixFile : GenericFilehandle ,
@@ -29,11 +32,9 @@ export default class Trix {
2932
3033 let { end, buffer } = res
3134 let done = false
32- const decoder = new TextDecoder ( 'utf8' )
3335 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
3436 while ( ! done ) {
35- let foundSomething = false
36- const str = decoder . decode ( buffer )
37+ const str = this . decoder . decode ( buffer )
3738
3839 // slice to lastIndexOf('\n') to make sure we get complete records
3940 // since the buffer fetch could get halfway into a record
@@ -46,9 +47,6 @@ export default class Trix {
4647 for ( const line of lines ) {
4748 const word = line . split ( ' ' ) [ 0 ]
4849 const match = word . startsWith ( searchWord )
49- if ( ! foundSomething && match ) {
50- foundSomething = true
51- }
5250
5351 // we are done scanning if we are lexicographically greater than the
5452 // search string
@@ -91,11 +89,14 @@ export default class Trix {
9189 }
9290
9391 private async getIndex ( opts ?: { signal ?: AbortSignal } ) {
92+ if ( this . indexCache ) {
93+ return this . indexCache
94+ }
9495 const file = await this . ixxFile . readFile ( {
9596 encoding : 'utf8' ,
9697 ...opts ,
9798 } )
98- return file
99+ const result = file
99100 . split ( '\n' )
100101 . filter ( f => ! ! f )
101102 . map ( line => {
@@ -105,20 +106,22 @@ export default class Trix {
105106 const pos = Number . parseInt ( posStr , 16 )
106107 return [ prefix , pos ] as const
107108 } )
109+ this . indexCache = result
110+ return result
108111 }
109112
110113 private async _getBuffer (
111114 searchWord : string ,
112115 opts ?: { signal ?: AbortSignal } ,
113116 ) {
114117 let start = 0
115- let end = 65536
118+ let end = CHUNK_SIZE
116119 const indexes = await this . getIndex ( opts )
117120 for ( const [ key , value ] of indexes ) {
118121 const trimmedKey = key . slice ( 0 , searchWord . length )
119122 if ( trimmedKey < searchWord ) {
120123 start = value
121- end = value + 65536
124+ end = value + CHUNK_SIZE
122125 }
123126 }
124127
0 commit comments