Skip to content

Commit

Permalink
aded CidrGeoMap asnd GeoIpData
Browse files Browse the repository at this point in the history
  • Loading branch information
kenricky-bitquill committed Nov 8, 2024
1 parent 5eda502 commit 30fd2d2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.opensearch.sql.ast.expression;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand Down
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);
}
}
}

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;
}

0 comments on commit 30fd2d2

Please sign in to comment.