|
Class: VirtualReadOnlyTextFileContents
Object
|
+--Collection
|
+--SequenceableCollection
|
+--VirtualArray
|
+--VirtualArrayWithCache
|
+--VirtualReadOnlyTextFileContents
- Package:
- stx:libbasic2
- Category:
- Collections-Arrayed
- Version:
- rev:
1.2
date: 2022/05/05 15:20:21
- user: cg
- file: VirtualReadOnlyTextFileContents.st directory: libbasic2
- module: stx stc-classLibrary: libbasic2
- Author:
- Claus Gittinger
I can be used to represent a huge file's contents as an array of lines.
(eg. for viewing in a text view).
When initialized, I will start a background reader to collect line start positions
of chunks; I will keep the numberOfLines/chunkSize number of start positions.
I will change my (virtual) size, as the reader processes the file.
When asked for a line, I will position to that chunk in the file and read
the line(s) as needed.
I will buffer one chunk (chunkSize) of lines, to speed up the presentation
of a few pages worth of text.
[instance variables:]
lineChunkPositions file position of chunks;
each chunk consists of chunkSize lines
fileStream
lastReadPosition
lastLineRead
readProcess
chunkSize
chunkReadSemaphore
accessLock
lastChunk
lastChunkLineNr
[class variables:]
instance creation
-
on: fileStream
-
accessing
-
size
-
(comment from inherited method)
the virtual size
initialization
-
fileStream: aStream
-
lines per chunk
-
fileStream: aStream chunkSize: chunkSizeArg
-
lines per chunk
private
-
getLine: lineNr
-
I will temporarily reposition the file, read a chunk and then position back.
-
readerProcess
-
ensure that noone repositions the file between the positioning and the read
-
startReadProcess
-
<<END
|top textView fileContents|
top := StandardSystemView new.
textView := HVScrollableView for:TextView in:top.
textView origin:0@0 corner:1.0@1.0.
fileContents := VirtualReadOnlyTextFileContents on:('err' asFilename readStream).
textView list:fileContents expandTabs:false
scanForNonStrings:false includesNonStrings:false.
textView checkedLinesForWidthOfContentsComputation:0.
textView setWidthOfContents:1000.
top open.
|
Test: create a file with 1 mio lines:
|file fileContents|
"create a big file"
file := 'bigFile' asFilename writeStream.
1 to:1000000 do:[:lNr |
lNr printOn:file. file cr.
].
file close.
fileContents := VirtualReadOnlyTextFileContents on:('bigFile' asFilename readStream).
self assert:(fileContents at:10) = '10'.
self assert:(fileContents at:511) = '511'.
self assert:(fileContents at:512) = '512'.
self assert:(fileContents at:513) = '513'.
self assert:(fileContents at:10000) = '10000'.
self assert:(fileContents at:1) = '1'.
self assert:(fileContents at:2) = '2'.
self assert:(fileContents at:50000) = '50000'.
self assert:(fileContents at:999999) = '999999'.
self assert:(fileContents size = 1000000 ).
self assert:(fileContents at:1000000) = '1000000'.
|
Test: create a file with 1 mio lines and open a textView on it
(notice that the view will come up immediately, blocking if you scroll to a line
which has not yet been scanned, or which is not in the chunk buffer)
|file fileContents top textView |
"create a big file"
file := 'bigFile' asFilename writeStream.
1 to:1000000 do:[:lNr |
lNr printOn:file. file cr.
].
file close.
fileContents := VirtualReadOnlyTextFileContents on:('bigFile' asFilename readStream).
top := StandardSystemView new label:'a big file'.
textView := HVScrollableView for:TextView in:top.
textView origin:0@0 corner:1.0@1.0.
textView list:fileContents expandTabs:false
scanForNonStrings:false includesNonStrings:false.
textView checkedLinesForWidthOfContentsComputation:0.
textView setWidthOfContents:1000.
top open.
| END"
|