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).

sclass LineCompSemiCached implements AutoCloseable {
  File f; // must be the raw text version
  RandomAccessFile raf;
  int[] literalOffsets;
  int[] pairs;
  Map<S, int[]> files;
  S encoding = "UTF-8";
  transient Lock lock = lock();
  
  *() {}
  *(File f) {
    if (isGZipFile(f)) fail("Must not be gzipped: " + f2s(f));
    init(f);
  }
  
  LineCompReader init(File f) {
    this.f = f;
    LineCompReader reader = new(f);
    literalOffsets = reader.literalOffsets;
    pairs = intPairsToIntArray(reader.pairs);
    files = mapValues toIntArray(reader.versions);
    ret reader;
  }
  
  void openFile {
    lock lock;
    if (raf == null)
      raf = randomAccessFileForReading(f);
  }
  
  S getLiteral(int i) {
    openFile();
    int start = literalOffsets[i];
    ret bytesToString(raf_readFilePart(raf, start, literalOffsets[i+1]-start), encoding);
  }
  
  public void close ctex {
    if (raf != null) raf.close();
  }
  
  int nLiterals() { ret literalOffsets.length-1; }
  
  S getFile(S name) {
    int[] encoded = files.get(name);
    if (encoded == null) null;
    new StringBuilder buf;
    for (int idx : encoded)
      decode(idx, buf);
    ret str(buf);
  }
  
  Set<S> files() { ret keys(files); }
  
  void decode(int idx, StringBuilder buf) {
    if (idx < nLiterals()) {
      if (empty(buf)) buf.append('\n');
      buf.append(getLiteral(idx));
    } else {
      int idx2 = (idx-nLiterals())*2;
      decode(pairs[idx2], buf);
      decode(pairs[idx2+1], buf);
    }
  }
}

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: 283 / 597
Version history: 12 change(s)
Referenced in: [show references]