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

33
LINES

< > BotCompany Repo | #1032616 // ipToCountry2021

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

Libraryless. Click here for Pure Java version (6848L/41K).

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

Author comment

Began life as a copy of #1028338

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1032616
Snippet name: ipToCountry2021
Eternal ID of this version: #1032616/9
Text MD5: afde5653318e8e0f8190664a7cd89e3a
Transpilation MD5: ef75c8d701712b8f34e3e8c00457732e
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-06 04:04:21
Source code size: 1041 bytes / 33 lines
Pitched / IR pitched: No / No
Views / Downloads: 122 / 190
Version history: 8 change(s)
Referenced in: [show references]