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

scope ipToCountry2021.

sS #dataSnippetID = #1400509;
static Map<Long, S> #cache = mruCache(100);
static Lock #lock = lock();

static File #dataDir() {
  ret javaxCachesDir("ipToCountry2021");
}

sS ipToCountry2021(S ip) { ret ipToCountry2021(ipToInt(ip)); }
sS ipToCountry2021_uncached(S ip) { ret ipToCountry2021_uncached(ipToInt(ip)); }

sS ipToCountry2021(long ipNum) {
  ret mapGetOrCreate(cache, ipNum, () -> ipToCountry2021_uncached(ipNum));
}
  
sS ipToCountry2021_uncached(long ipNum) {
  {
    lock lock;
    if (directoryEmpty(dataDir()))
      unzipSnippet(dataSnippetID, dataDir());
  }
  S line = pairB(binarySearchForLineInTextFile(newFile(dataDir(), "IP2LOCATION-LITE-DB1.CSV"), s -> {
    LS l = tok_splitAtComma_unquote(s);
    long a = parseLongOpt(first(l)), b = parseLongOpt(second(l));
    ifdef ipToCountry2021_debug
      printVars_str("ipToCountry2021: ", +a, +b, +ipNum);
    endifdef
    ret ipNum > b ? 1 : ipNum < a ? -1 : 0;
  }));
  ret get(tok_splitAtComma_unquote(line), 2);
}

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: 117 / 183
Version history: 8 change(s)
Referenced in: [show references]