Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

64
LINES

< > BotCompany Repo | #1028795 // LineCompSemiCached - cache all except the literals

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5835L/37K).

1  
sclass LineCompSemiCached implements AutoCloseable {
2  
  File f; // must be the raw text version
3  
  RandomAccessFile raf;
4  
  int[] literalOffsets;
5  
  int[] pairs;
6  
  Map<S, int[]> files;
7  
  S encoding = "UTF-8";
8  
  transient Lock lock = lock();
9  
  
10  
  *() {}
11  
  *(File f) {
12  
    if (isGZipFile(f)) fail("Must not be gzipped: " + f2s(f));
13  
    init(f);
14  
  }
15  
  
16  
  LineCompReader init(File f) {
17  
    this.f = f;
18  
    LineCompReader reader = new(f);
19  
    literalOffsets = reader.literalOffsets;
20  
    pairs = intPairsToIntArray(reader.pairs);
21  
    files = mapValues toIntArray(reader.versions);
22  
    ret reader;
23  
  }
24  
  
25  
  void openFile {
26  
    lock lock;
27  
    if (raf == null)
28  
      raf = randomAccessFileForReading(f);
29  
  }
30  
  
31  
  S getLiteral(int i) {
32  
    openFile();
33  
    int start = literalOffsets[i];
34  
    ret bytesToString(raf_readFilePart(raf, start, literalOffsets[i+1]-start), encoding);
35  
  }
36  
  
37  
  public void close ctex {
38  
    if (raf != null) raf.close();
39  
  }
40  
  
41  
  int nLiterals() { ret literalOffsets.length-1; }
42  
  
43  
  S getFile(S name) {
44  
    int[] encoded = files.get(name);
45  
    if (encoded == null) null;
46  
    new StringBuilder buf;
47  
    for (int idx : encoded)
48  
      decode(idx, buf);
49  
    ret str(buf);
50  
  }
51  
  
52  
  Set<S> files() { ret keys(files); }
53  
  
54  
  void decode(int idx, StringBuilder buf) {
55  
    if (idx < nLiterals()) {
56  
      if (empty(buf)) buf.append('\n');
57  
      buf.append(getLiteral(idx));
58  
    } else {
59  
      int idx2 = (idx-nLiterals())*2;
60  
      decode(pairs[idx2], buf);
61  
      decode(pairs[idx2+1], buf);
62  
    }
63  
  }
64  
}

Author comment

Began life as a copy of #1028182

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1028795
Snippet name: LineCompSemiCached - cache all except the literals
Eternal ID of this version: #1028795/13
Text MD5: 1fc5778a65e85afb17059b3d73651f0a
Transpilation MD5: f45a576866b725e3459ffc5a04c5d518
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-16 14:45:33
Source code size: 1566 bytes / 64 lines
Pitched / IR pitched: No / No
Views / Downloads: 216 / 506
Version history: 12 change(s)
Referenced in: [show references]