-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5eda502
commit 30fd2d2
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
ppl-spark-integration/src/main/java/org/opensearch/sql/ast/expression/GeoIp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
ppl-spark-integration/src/main/java/org/opensearch/sql/common/geospatial/CidrGeoMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.opensearch.sql.common.geospatial; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class CidrGeoMap extends HashMap<String, GeoIpData> { | ||
|
||
private String toBinaryString(String ip) { | ||
StringBuilder binary = new StringBuilder(); | ||
for (String octet : ip.split("\\.")) { | ||
binary.append(String.format("%8s", Integer.toBinaryString(Integer.parseInt(octet))) | ||
.replace(' ', '0')); | ||
} | ||
return binary.toString(); | ||
} | ||
|
||
@Override | ||
public GeoIpData put(String cidr, GeoIpData data) { | ||
String[] parts = cidr.split("/"); | ||
String cidrKey = toBinaryString(parts[0]); | ||
int prefixLength = Integer.parseInt(parts[1]); | ||
cidrKey = cidrKey.substring(0, prefixLength - 1); | ||
|
||
super.put(cidrKey, data); | ||
|
||
return data; | ||
} | ||
|
||
@Override | ||
public GeoIpData get(Object ipAddress) { | ||
String binaryIP = toBinaryString(ipAddress.toString()); | ||
|
||
GeoIpData res = null; | ||
|
||
while (binaryIP.length() > 0 && res == null) { | ||
res = super.get(binaryIP); | ||
binaryIP = binaryIP.substring(0, binaryIP.length() - 2); | ||
} | ||
} | ||
} | ||
|
18 changes: 18 additions & 0 deletions
18
ppl-spark-integration/src/main/java/org/opensearch/sql/common/geospatial/GeoIpData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.opensearch.sql.common.geospatial; | ||
|
||
import lombok.*; | ||
|
||
@Getter | ||
@ToString | ||
@AllArgsConstructor | ||
public class GeoIpData { | ||
private final String country_is_code; | ||
private final String country_name; | ||
private final String continent_name; | ||
private final String region_iso_code; | ||
private final String region_name; | ||
private final String city_name; | ||
private final String time_zone; | ||
private final String lat; | ||
private final String lon; | ||
} |