淘宝IP地址库 - 代码物语-Let code talk - ITeye技术网站
于是萌生了一个想法: 处理方式如下: 要声明的是,本人不对数据的正确性作任何保证。 /** * 合并连续的、在同一城市的IP */ public void merge() { IpExample example = new IpExample(); example.createCriteria().andStateEqualTo(IpConst.State.OK); example.setOrderByClause("ipstartdigital asc"); List
list = mapper.selectByExample2(example); logger.info("size={}", list.size()); Ip startIp = null; Ip endIp = null; int count = 0; for (Ip ip : list) { if (startIp == null) { startIp = ip; endIp = ip; } else { if (isMatched(endIp, ip) && isContinuous(endIp, ip) ) { endIp = ip; continue; } else { count++; insert(startIp, endIp); startIp = ip; endIp = ip; } } } if (startIp != null && endIp != null) { count++; insert(startIp, endIp); } logger.info("count={}", count); } private boolean isContinuous(Ip endIp, Ip ip) { return endIp.getIpenddigital().equals(ip.getIpstartdigital() - 1); } private void insert(Ip startIp, Ip endIp) { Ip ip = new Ip(); Helper.copyFromNotNull(ip, startIp); ip.setIpend(endIp.getIpend()); ip.setIpenddigital(endIp.
Read full article from 淘宝IP地址库 - 代码物语-Let code talk - ITeye技术网站
No comments:
Post a Comment