public ActionResult SearchIp(string ipAddr) { try { SearchRecord binary_recorder; SearchRecord fibonacci_recorder; var ipRecords = _cacheManager.Get(IPRECORDS_ALL_KEY, () => IpRecordHelper.LoadIpRecordResult(_ipResultFilePath).ToList() ).ToArray(); IpRecordHelper.SearchIpRecord(ipAddr, ipRecords, out binary_recorder, out fibonacci_recorder); if (binary_recorder.Index == -1) { return(Json(new { success = false, message = string.Format("没有找到该ip: {0}", ipAddr) })); } SearchResultModel model = new SearchResultModel(); model.Binary_recorder = binary_recorder; model.Fibonacci_recorder = fibonacci_recorder; // model.Summary = string.Format("第{0}个找到, ip 信息为\n{1}", binary_recorder.Index + 1, binary_recorder.IpRecord); model.Summary = string.Format("第{0}个找到, ip 信息为\n{1}", binary_recorder.IpRecord.Id, binary_recorder.IpRecord); return(Json(new { success = true, message = this.RenderPartialViewToString("SearchResult", model) })); } catch (FormatException) { return(Json(new { success = false, message = "请输入正确ip地址" })); } catch (Exception ex) { return(Json(new { success = false, message = ex.Message })); } }
public object Get([FromUri] IpInfo.Web.Models.Kendoui.DataSourceRequest command) { var ipRecords_All = _cacheManager.Get(IPRECORDS_ALL_KEY, () => IpRecordHelper.LoadIpRecordResult(_ipResultFilePath).ToList() ); var gridModel = new IpInfo.Web.Models.Kendoui.DataSourceResult { Data = ipRecords_All.Skip((command.Page - 1) * command.PageSize).Take(command.PageSize), Total = ipRecords_All.Count }; return(gridModel); }
/// <summary> /// Search /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void searchToolStripButton_Click(object sender, EventArgs e) { try { SearchRecord binary_recorder; SearchRecord fibonacci_recorder; IpRecordHelper.SearchIpRecord(tbxSearch.Text.Trim(), _ipRecords, out binary_recorder, out fibonacci_recorder); if (binary_recorder.Index == -1) { MessageBox.Show(string.Format(Messages.NotFound, tbxSearch.Text.Trim())); return; } var content = (string.Format("第{0}个找到, ip 信息为", binary_recorder.Index + 1) + Environment.NewLine + string.Format("{0}-{1} {2}", binary_recorder.IpRecord.StartIp, binary_recorder.IpRecord.EndIp, binary_recorder.IpRecord.Address) + Environment.NewLine + Environment.NewLine + string.Format("{0}: 查找次数: {1}, 用时(ms): {2}", "二分法查找", binary_recorder.Count, binary_recorder.ElapsedMilliseconds) + Environment.NewLine + string.Format("{0} 查找次数: {1}, 用时(ms): {2}", "斐波那契查找", fibonacci_recorder.Count, fibonacci_recorder.ElapsedMilliseconds) ); lblContent.Text = content; } catch (FormatException) { MessageBox.Show(Messages.WrongIpFormat); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public ActionResult Index(string sortOrder, string currentFilter, string searchString, int?page) { if (_cacheManager.IsSet(IPRECORDS_ALL_KEY)) { ViewBag.LoadFromCache = true; } else { ViewBag.LoadFromCache = false; } Stopwatch sw = Stopwatch.StartNew(); var ipRecords_All = _cacheManager.Get(IPRECORDS_ALL_KEY, () => IpRecordHelper.LoadIpRecordResult(_ipResultFilePath).ToList() ); sw.Stop(); ViewBag.LoadTime = sw.Elapsed.TotalMilliseconds; var ipRecords = from ip in ipRecords_All select ip; ViewBag.CurrentSort = sortOrder; ViewBag.StartIpSortParm = String.IsNullOrEmpty(sortOrder) ? "StartIp_desc" : ""; ViewBag.EndIpSortParm = sortOrder == "EndIp" ? "EndIp_desc" : "EndIp"; if (searchString != null) { page = 1; } else { searchString = currentFilter; } ViewBag.CurrentFilter = searchString; switch (sortOrder) { case "EndIp_desc": ipRecords = ipRecords.OrderByDescending(stu => stu.EndIpNumber); break; case "EndIp": ipRecords = ipRecords.OrderBy(stu => stu.EndIpNumber); break; case "StartIp_desc": ipRecords = ipRecords.OrderByDescending(stu => stu.StartIpNumber); break; default: ipRecords = ipRecords.OrderBy(stu => stu.StartIpNumber); break; } int pageNumber = (page ?? 1); return(View(ipRecords.ToPagedList(pageNumber, PAGE_SIZE))); }
/// <summary> /// 读取数据 /// </summary> /// <param name="ipFilePath"></param> /// <param name="dictionaryFilePath"></param> /// <param name="areaAndCodeFilePath"></param> /// <returns></returns> public IpRecord[] LoadIpRecord(string ipFilePath, string dictionaryFilePath, string areaAndCodeFilePath, string specialReplaceFilePath = null) { if (string.IsNullOrEmpty(ipFilePath)) { throw new ArgumentNullException("ipFilePath"); } if (string.IsNullOrEmpty(dictionaryFilePath)) { throw new ArgumentNullException("dictionaryFilePath"); } var specialDic = IpRecordHelper.LoadDicFile(specialReplaceFilePath); var ipRecords = IpRecordHelper.LoadIpDataFile(ipFilePath, specialDic); var areaAndCode = IpRecordHelper.LoadAreaCodeFile(areaAndCodeFilePath); var dictionary = IpRecordHelper.LoadDicFile(dictionaryFilePath); Console.WriteLine("total: " + ipRecords.Length); int count = 0; foreach (var ipRecord in ipRecords) { ++count; if (count % 5000 == 0) { Console.WriteLine(count); } bool found = false; // 1. 地区编码查找 if (areaAndCode.ContainsKey(ipRecord.Address)) { ipRecord.AreaCode = areaAndCode[ipRecord.Address]; ipRecord.FoundResult = FoundResult.ExactMatch; found = true; continue; } // 2. 特殊地址查找(字典表) if (!found) { if (dictionary.ContainsKey(ipRecord.Address)) { // 编码表中查找 if (areaAndCode.ContainsKey(dictionary[ipRecord.Address])) { ipRecord.AreaCode = areaAndCode[dictionary[ipRecord.Address]]; ipRecord.FoundResult = FoundResult.Dictionary; ipRecord.Note = ipRecord.Address; ipRecord.Address = dictionary[ipRecord.Address]; found = true; continue; } } } // 3. 模糊查找 // 甘肃省天水市秦安县 // 找到编码为: // 甘肃省天水市 281200 if (!found) { foreach (string key in areaAndCode.Keys) { if (ipRecord.Address.StartsWith(key)) { ipRecord.AreaCode = areaAndCode[key]; ipRecord.FoundResult = FoundResult.Match; ipRecord.Note = ipRecord.Address; if (specialDic != null && specialDic.ContainsKey(key)) { ipRecord.Address = specialDic[key]; } else { ipRecord.Address = key; } found = true; break; } } } if (!found) { ipRecord.AreaCode = string.Empty; ipRecord.FoundResult = FoundResult.None; } } return(ipRecords); }