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

34
LINES

< > BotCompany Repo | #1028338 // ipToCountry2020 - use ipToCountry2021 instead!

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

Transpiled version (6849L) is out of date.

1  
scope ipToCountry2020.
2  
3  
sS #dataSnippetID = #1400400;
4  
static Map<Long, S> #cache = mruCache(100);
5  
static Lock #lock = lock();
6  
7  
static File #dataDir() {
8  
  ret javaxCachesDir("ipToCountry2020");
9  
}
10  
11  
sS ipToCountry2020(S ip) {
12  
  ret ipToCountry2020(ipToInt(ip));
13  
}
14  
15  
sS ipToCountry2020(long ipNum) {
16  
  ret mapGetOrCreate(cache, ipNum, () -> uncached(ipNum));
17  
}
18  
  
19  
sS #uncached(long ipNum) {
20  
  {
21  
    lock lock;
22  
    if (directoryEmpty(dataDir()))
23  
      unzipSnippet(dataSnippetID, dataDir());
24  
  }
25  
  S line = pairB(binarySearchForLineInTextFile(newFile(dataDir(), "IP2LOCATION-LITE-DB1.CSV"), s -> {
26  
    LS l = tok_splitAtComma_unquote(s);
27  
    long a = parseLongOpt(first(l)), b = parseLongOpt(second(l));
28  
    ifdef ipToCountry2020_debug
29  
      printVars_str("ipToCountry2020: ", +a, +b, +ipNum);
30  
    endifdef
31  
    ret ipNum > b ? 1 : ipNum < a ? -1 : 0;
32  
  }));
33  
  ret get(tok_splitAtComma_unquote(line), 2);
34  
}

Author comment

Began life as a copy of #1008738

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: #1028338
Snippet name: ipToCountry2020 - use ipToCountry2021 instead!
Eternal ID of this version: #1028338/15
Text MD5: 587d2a4d97c6c2b1e6740fd512a32788
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-06 03:56:33
Source code size: 932 bytes / 34 lines
Pitched / IR pitched: No / No
Views / Downloads: 170 / 305
Version history: 14 change(s)
Referenced in: [show references]