non-contiguous and disorder ip range supports
This commit is contained in:
parent
8adec5a3c8
commit
fa16a4a45d
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
<groupId>org.lionsoul</groupId>
|
<groupId>org.lionsoul</groupId>
|
||||||
<artifactId>ip2region-maker</artifactId>
|
<artifactId>ip2region-maker</artifactId>
|
||||||
<version>3.0.0</version>
|
<version>3.1.0</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>ip2region</name>
|
<name>ip2region</name>
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,8 @@ import java.io.*;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
import org.lionsoul.ip2region.xdb.Segment.IterateAction;
|
||||||
|
|
||||||
public class Maker {
|
public class Maker {
|
||||||
// constants define
|
// constants define
|
||||||
public static final int VersionNo = 3; // 2 for XDB 2.0, 3 for XDB 3.0
|
public static final int VersionNo = 3; // 2 for XDB 2.0, 3 for XDB 3.0
|
||||||
|
|
@ -108,7 +110,7 @@ public class Maker {
|
||||||
/// }
|
/// }
|
||||||
|
|
||||||
this.bytesCharset = Charset.forName("utf-8");
|
this.bytesCharset = Charset.forName("utf-8");
|
||||||
this.segments = new LinkedList<Segment>();
|
this.segments = new ArrayList<Segment>();
|
||||||
this.dstHandle = new RandomAccessFile(dstPath, "rw");
|
this.dstHandle = new RandomAccessFile(dstPath, "rw");
|
||||||
this.indexPolicy = policy;
|
this.indexPolicy = policy;
|
||||||
this.regionPool = new HashMap<String, DataEntry>();
|
this.regionPool = new HashMap<String, DataEntry>();
|
||||||
|
|
@ -156,8 +158,9 @@ public class Maker {
|
||||||
private void loadSegments() throws Exception {
|
private void loadSegments() throws Exception {
|
||||||
log.infof("try to load the segments ... ");
|
log.infof("try to load the segments ... ");
|
||||||
final long tStart = System.currentTimeMillis();
|
final long tStart = System.currentTimeMillis();
|
||||||
Segment.iterate(srcFile, new Segment.IterateAction() {
|
final IterateAction itAct = new Segment.IterateAction() {
|
||||||
private Segment last = null;
|
private Segment last = null;
|
||||||
|
private boolean sorting = false;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void before(String line) {
|
public void before(String line) {
|
||||||
|
|
@ -177,21 +180,50 @@ public class Maker {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (last != null && !seg.after(last)) {
|
if (last != null && !seg.after(last)) {
|
||||||
throw new Exception("discontinuous data segment: last.eip("
|
// throw new Exception("discontinuous data segment: last.eip("
|
||||||
+ Util.ipToString(last.endIP)+")+1 != seg.sip("+ Util.ipToString(seg.startIP) + ", "+ seg.region +")");
|
// + Util.ipToString(last.endIP)+")+1 != seg.sip("+ Util.ipToString(seg.startIP) + ", "+ seg.region +")");
|
||||||
}
|
|
||||||
|
|
||||||
// allow empty region
|
// @Note: If the continuity is disrupted,
|
||||||
// if (region.length() < 1) {
|
// we will sort all these segments later.
|
||||||
// throw new Exception("empty region info for segment `"+seg+"`");
|
sorting = true;
|
||||||
// }
|
}
|
||||||
|
|
||||||
segments.add(seg);
|
segments.add(seg);
|
||||||
last = seg;
|
last = seg;
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
log.infof("all segments loaded, length: %d, elapsed: %d ms", segments.size(), System.currentTimeMillis() - tStart);
|
public boolean sorting() {
|
||||||
|
return sorting;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// load iterate all the segments
|
||||||
|
Segment.iterate(srcFile, itAct);
|
||||||
|
final boolean sorting = itAct.sorting();
|
||||||
|
|
||||||
|
// check and sort all the segments
|
||||||
|
if (sorting) {
|
||||||
|
log.infof("try to sort all the segments based on its start ip ...");
|
||||||
|
segments.sort((o1, o2) -> {return Util.ipCompare(o1.startIP, o2.startIP);});
|
||||||
|
|
||||||
|
log.infof("try to check if there is overlap in the segments ...");
|
||||||
|
Segment last = null;
|
||||||
|
for (final Segment seg : segments) {
|
||||||
|
// check the order of the data segment
|
||||||
|
if (last != null && !seg.after(last)) {
|
||||||
|
throw new Exception("overlap checking: last.eip("
|
||||||
|
+ Util.ipToString(last.endIP)+") >= seg.sip("+ Util.ipToString(seg.startIP) + ", "+ seg.region +")");
|
||||||
|
}
|
||||||
|
|
||||||
|
// reset the last
|
||||||
|
last = seg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.infof(
|
||||||
|
"all segments loaded, length: %d, sorting: %s, elapsed: %d ms",
|
||||||
|
segments.size(), sorting ? "true" : "false", System.currentTimeMillis() - tStart
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// init the maker
|
// init the maker
|
||||||
|
|
|
||||||
|
|
@ -98,11 +98,18 @@ public class Segment {
|
||||||
return Util.ipCompare(ip, startIP) >= 0 && Util.ipCompare(ip, endIP) <= 0;
|
return Util.ipCompare(ip, startIP) >= 0 && Util.ipCompare(ip, endIP) <= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if the current segment just after the specified one.
|
// check if the current segment just right behind the specified one.
|
||||||
public boolean after(final Segment last) {
|
// which mean last.endIP + 1 = this.startIP
|
||||||
|
public boolean rightBehind(final Segment last) {
|
||||||
return Util.ipCompare(Util.ipAddOne(last.endIP), startIP) == 0;
|
return Util.ipCompare(Util.ipAddOne(last.endIP), startIP) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if the current segment is after the specified one.
|
||||||
|
// which means last.endIP < this.startIP
|
||||||
|
public boolean after(final Segment last) {
|
||||||
|
return Util.ipCompare(last.endIP, startIP) < 0;
|
||||||
|
}
|
||||||
|
|
||||||
// parser the Segment from an input string
|
// parser the Segment from an input string
|
||||||
public static Segment parse(String input) throws Exception {
|
public static Segment parse(String input) throws Exception {
|
||||||
final String[] ps = input.trim().split("\\|", 3);
|
final String[] ps = input.trim().split("\\|", 3);
|
||||||
|
|
@ -121,6 +128,11 @@ public class Segment {
|
||||||
|
|
||||||
// static class to handler the iterate callback
|
// static class to handler the iterate callback
|
||||||
public static interface IterateAction {
|
public static interface IterateAction {
|
||||||
|
// need sort all the iterated segments ?
|
||||||
|
default boolean sorting() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public void before(final String line);
|
public void before(final String line);
|
||||||
public String filter(final String region);
|
public String filter(final String region);
|
||||||
public void handle(final Segment seg) throws Exception;
|
public void handle(final Segment seg) throws Exception;
|
||||||
|
|
@ -186,7 +198,7 @@ public class Segment {
|
||||||
// check and automatic merging the Consecutive Segments, which means:
|
// check and automatic merging the Consecutive Segments, which means:
|
||||||
// 1, region info is the same
|
// 1, region info is the same
|
||||||
// 2, last.eip+1 = cur.sip
|
// 2, last.eip+1 = cur.sip
|
||||||
if (last.region.equals(seg.region) && seg.after(last)) {
|
if (last.region.equals(seg.region) && seg.rightBehind(last)) {
|
||||||
// last.endIP = seg.endIP;
|
// last.endIP = seg.endIP;
|
||||||
System.arraycopy(seg.endIP, 0, last.endIP, 0, seg.endIP.length);
|
System.arraycopy(seg.endIP, 0, last.endIP, 0, seg.endIP.length);
|
||||||
continue;
|
continue;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue