C#实现的内存分页机制的一个实例
C#实现的内存分页机制的一个实例
//多页索引表管理类(全局主索引表管理类)
public class MuliPageIndexFeatureClass : IDisposable
{
protected List<IndexPageClass> MuliPageIndexTable = new List<IndexPageClass>(); //多页索引表对象
//
protected int CurrentMemoryPageIndex = -1; //当前内存索引页(已载入内存的索引页的对象)
public int PerPageDwCount = 30000; //每页地物个数 default=3,0000 /// <summary>
/// = Application.StartupPath + "\\tempfiles\\tmp_vct";
/// </summary>
protected string tmpDir = Application.StartupPath + "\\tempfiles\\tmp_vct"; public MuliPageIndexFeatureClass()
{
this.MemoryPageList.Clear();
//txt存放目录
string dir = this.tmpDir;
if (System.IO.Directory.Exists(dir) == true)
{
try
{
System.IO.Directory.Delete(dir, true);
}
catch { }
}
if(System.IO.Directory.Exists(dir) == false)
{
System.IO.Directory.CreateDirectory(dir);
}
} //创建多页索引表(建立多页索引表) 派生类需重写
private IndexPageClass tmpIP = null;
public virtual bool CreateMuliPageIndexTable(FeatureOIDMap pfeat)
{
//txt存放目录
string dir = this.tmpDir;
if (System.IO.Directory.Exists(dir) == false)
{
System.IO.Directory.CreateDirectory(dir);
}
//创建多页索引表....
if (this.tmpIP == null)
{ //产生一个新的索引页面
this.tmpIP = new IndexPageClass();
tmpIP.RelationFileName = "vctline" + this.MuliPageIndexTable.Count.ToString() + ".txt";
tmpIP.CreatedIndex = false;
tmpIP.OpenTxt();
this.MuliPageIndexTable.Add(tmpIP);
}
else
{
if (tmpIP.dt.Count >=this.PerPageDwCount)
{ //保存原有的索引页面
tmpIP.SaveToTxt();
tmpIP.CloseTxt();
tmpIP.CreatedIndex = true;
tmpIP.dt.Clear();
//产生一个新的索引页面
tmpIP = new IndexPageClass();
tmpIP.RelationFileName = "vctline" + this.MuliPageIndexTable.Count.ToString() + ".txt";
tmpIP.CreatedIndex = false;
tmpIP.OpenTxt();
this.MuliPageIndexTable.Add(tmpIP);
}
}
if(tmpIP!=null)
{
tmpIP.dt.Add(pfeat.ObjectID,pfeat);
if (tmpIP.dt.Count % 100==0)
{
Application.DoEvents();
}
}
return true;
}
public virtual bool SaveEndPageIndexTable()
{
bool rbc = false;
if (tmpIP != null)
{
tmpIP.SaveToTxt();
tmpIP.CloseTxt();
tmpIP.CreatedIndex = true;
tmpIP.dt.Clear();
tmpIP.Dispose();
tmpIP = null;
rbc = true;
}
return rbc;
} //选择记录集 OK
private List<int> MemoryPageList = new List<int>();
private int MemoryPagesOnLine = 7; //在线的内存索引文件数
public FeatureOIDMap GetLine(int bsm)
{
FeatureOIDMap feat = null;
bool IsSearch = true; //是否要进行分页搜索操作标志 默认为是 if (this.CurrentMemoryPageIndex != -1)
{ //当前内存分页中是否有dwID地物唯一编号
if (this.MuliPageIndexTable[this.CurrentMemoryPageIndex].DwidList.Contains(bsm) == true)
{
IsSearch = false;
}
} if (IsSearch == true)
{ //进行分页搜索操作 并置为当前内存分页
for (int i = 0; i < this.MuliPageIndexTable.Count; i++)
{
if (this.MuliPageIndexTable[i].DwidList.Contains(bsm) == true)
{
if (this.CurrentMemoryPageIndex != i)
{
//需搜索页面不在当前内存页面中 (需置换页面)
if (this.CurrentMemoryPageIndex != -1)
{
//修正在线内存索引文件个数
if (this.MemoryPageList.Contains(i) == false)
{
this.MemoryPageList.Add(i);
}
if (this.MemoryPageList.Contains(this.CurrentMemoryPageIndex) == false)
{
this.MemoryPageList.Add(this.CurrentMemoryPageIndex);
}
if (this.MemoryPageList.Count > this.MemoryPagesOnLine)
{
this.MemoryPageList.RemoveAt(0);
}
//清除离线内存索引文件中坐标大数据 for (int k = 0; k < this.MuliPageIndexTable.Count; k++)
{
if (this.MemoryPageList.Contains(k) == false)
{
this.MuliPageIndexTable[k].Dispose();
}
}
}
if (this.MuliPageIndexTable[i].dt.Count <= 0)
{
this.MuliPageIndexTable[i].ReadFromTxt(); //从txt文件中读取记录
}
this.CurrentMemoryPageIndex = i;
break;
}
} //end if
} //end for
} //当前分面页页进行搜索...
if (this.CurrentMemoryPageIndex != -1)
{
feat = this.MuliPageIndexTable[this.CurrentMemoryPageIndex].GetLine(bsm);
}
return feat;
} //获取一页索引表
public IndexPageClass GetIndexPageClass(int index)
{
if (MuliPageIndexTable != null && this.MuliPageIndexTable.Count > index)
{
return this.MuliPageIndexTable[index];
}
return null;
} public void Clear()
{
this.Dispose();
}
#region IDisposable 成员 public void Dispose()
{
if (this.MuliPageIndexTable.Count > 0)
{
for (int i = 0; i < this.MuliPageIndexTable.Count; i++)
{
this.MuliPageIndexTable[i].Dispose();
}
}
this.MuliPageIndexTable.Clear();
this.CurrentMemoryPageIndex = -1;
} public void DeleteTempFile()
{
//分页对应的txt文件...
string dir = this.tmpDir;
if (System.IO.Directory.Exists(dir) == true)
{
try
{
System.IO.Directory.Delete(dir, true);
}
catch
{
}
}
} #endregion
}
//一页内索引表管理类
public class IndexPageClass : IDisposable
{
public bool CreatedIndex = false;
public List<int> DwidList = new List<int>(); //地物唯一编号的集合
public string RelationFileDir = Application.StartupPath + "\\tempfiles\\tmp_vct";
//相关的txt文件 dltb_xzdw_Relation_1.txt
public string RelationFileName = "";
//排序后的记录关系记录DataRow集合 //从_1.txt从读取内容
protected FeatureOIDMapCollection m_dt = new FeatureOIDMapCollection();
protected System.IO.StreamWriter sw = null;
private int m_index = 0; //get dt
public FeatureOIDMapCollection dt
{
get
{
return m_dt;
}
} /// <summary>
/// //打开文本分页文件写入流
/// </summary>
public bool OpenTxt()
{
string relfilename = this.RelationFileDir + "\\" + this.RelationFileName;
if (System.IO.File.Exists(relfilename) == true)
{
System.IO.File.Delete(relfilename);
}
this.sw = new StreamWriter(relfilename, true, Encoding.Default);
return true;
} /// <summary>
/// 向写入流写入一行记录
/// </summary>
private bool AppentLine(string pLine)
{
if (this.sw != null)
{
this.sw.WriteLine(pLine);
this.m_index += 1; if (this.m_index % 10000 == 0)
{
this.sw.Flush();
Application.DoEvents();
}
}
return true;
}
/// <summary>
/// 向写入流写入一行记录
/// </summary>
private bool AppentLine(FeatureOIDMap pFeatureOIDMap)
{
StringBuilder pline = new StringBuilder();
CoPointClass cpoint = null;
//常住内存索引
this.DwidList.Add(pFeatureOIDMap.ObjectID);
//
pline.Append(pFeatureOIDMap.ObjectID.ToString());
for (int i = 0; i < pFeatureOIDMap.PointArray.Count; i++)
{
cpoint=pFeatureOIDMap.PointArray[i] as CoPointClass;
pline.Append("," + cpoint.X.ToString() + "," + cpoint.Y.ToString());
}
return AppentLine(pline.ToString());
} //保存到txt文件中
public bool SaveToTxt()
{
bool rbc = false;
if (this.dt != null)
{
foreach(FeatureOIDMap featmap in this.dt.Values)
{
this.AppentLine(featmap);
}
rbc = true;
}
return rbc;
} /// <summary>
/// 关闭写入流
/// </summary>
public bool CloseTxt()
{
if (sw != null)
{
sw.Flush();
sw.Close();
sw.Dispose();
sw = null;
}
return true;
} //从txt中读取记录到m_dt表中 OK
public virtual bool ReadFromTxt()
{
this.Dispose();
int t_index = 0;
int bsm=0;
double x = 0;
double y = 0;
System.IO.StreamReader sr = new System.IO.StreamReader(this.RelationFileDir + "\\" + this.RelationFileName, Encoding.Default);
string line = sr.ReadLine();
while (line != null && line != "")
{
t_index += 1;
if (t_index % 100 == 0)
{
Application.DoEvents();
}
//向m_dt中加入一行记录
string[] lineArray = line.Split(new char[] { ',' });
bsm=int.Parse(lineArray[0]);
FeatureOIDMap feat_new = new FeatureOIDMap(bsm);
for (int i = 1; i < lineArray.Length; i+=2)
{
CoPointClass xpPoint = new CoPointClass();
x = 0;
y = 0;
double.TryParse(lineArray[i], out x);
double.TryParse(lineArray[i+1], out y);
xpPoint.X = x;
xpPoint.Y = y;
//
feat_new.PointArray.Add(xpPoint);
}
this.m_dt.Add(feat_new.ObjectID,feat_new);
//
line = sr.ReadLine();
}
sr.Close();
sr.Dispose();
sr = null;
return true;
} //从内存页面中选择记录集 OK
public FeatureOIDMap GetLine(int bsm)
{
FeatureOIDMap feat = null;
if (m_dt != null && m_dt.Count <= 0)
{
this.ReadFromTxt();
}
if (m_dt != null && m_dt.Count > 0)
{
feat = this.m_dt.GetIndexByOID(bsm);
}
return feat;
} #region IDisposable 成员 public void Dispose()
{
if (m_dt != null)
{
m_dt.Clear();
}
}
#endregion
}
C#实现的内存分页机制的一个实例的更多相关文章
- CPU内存管理和linux内存分页机制
一.概念 物理地址(physical address)用于内存芯片级的单元寻址,与处理器和CPU连接的地址总线相对应.——这个概念应该是这几个概念中最好理解的一个,但是值得一提的是,虽然可以直接把物理 ...
- Oracle 分页查询的一个实例
1.分页模板 select * from ( select rownum as rn , a.* from( 某个表名) a) where rn between 0 and 6 2 某个表名 sele ...
- 操作系统:x86下内存分页机制 (1)
前置知识: 分段的概念(当然手写过肯定是坠吼的 为什么要分页 当我们写程序的时候,总是倾向于把一个完整的程序分成最基本的数据段,代码段,栈段.并且普通的分段机制就是在进程所属的LDT中把每一个段给标识 ...
- mysql通过“延迟关联”进行limit分页查询优化的一个实例
最近在生产上遇见一个分页查询特别慢的问题,数据量大概有200万的样子,翻到最后一页性能很低,差不多得有4秒的样子才能出来整个页面,需要进行查询优化. 第一步,找到执行慢的sql,如下: SELECT ...
- Linux内存寻址之分段机制及分页机制【转】
前言 本文涉及的硬件平台是X86,如果是其他平台的话,如ARM,是会使用到MMU,但是没有使用到分段机制: 最近在学习Linux内核,读到<深入理解Linux内核>的内存寻址一章.原本以为 ...
- Intel微处理器学习笔记(四) 内存分页
内存分页机制(memory paging mechanism)是从386开始的.线性地址通过分页机制透明转换为物理地址. 从这里知道:1. 如果不分页,则线性地址等于物理地址:2. 如果分页,则线性地 ...
- Linux内存管理机制简析
Linux内存管理机制简析 本文对Linux内存管理机制做一个简单的分析,试图让你快速理解Linux一些内存管理的概念并有效的利用一些管理方法. NUMA Linux 2.6开始支持NUMA( Non ...
- x86 分页机制——虚拟地址到物理地址寻址
x86下的分页机制有一个特点:PAE模式 PAE模式 物理地址扩展,是基于x86 的服务器的一种功能,它使运行 Windows Server 2003, Enterprise Edition 和 Wi ...
- Linux内存寻址之分页机制
在上一篇文章Linux内存寻址之分段机制中,我们了解逻辑地址通过分段机制转换为线性地址的过程.下面,我们就来看看更加重要和复杂的分页机制. 分页机制在段机制之后进行,以完成线性—物理地址的转换过程.段 ...
随机推荐
- BZOJ 1176: [Balkan2007]Mokia( CDQ分治 + 树状数组 )
考虑cdq分治, 对于[l, r)递归[l, m), [m, r); 然后计算[l, m)的操作对[m, r)中询问的影响就可以了. 具体就是差分答案+排序+离散化然后树状数组维护.操作数为M的话时间 ...
- Toast的替代者Snackbar
在Android design support library中,SnackBar的使用: Part 2 – Welcome Snackbar, Goodbye Toast! BY PARESH MA ...
- Ubuntu Server修改IP、DNS、hosts
本文记录下Ubuntu Server 16.04修改IP.DNS.hosts的方法 -------- 1. Ubuntu Server 修改IP sudo vi /etc/network/interf ...
- Dos命令之Netsh
NetSH (Network Shell) 是windows系统本身提供的功能强大的网络配置命令行工具. 常用命令 1. 导出配置脚本:netsh -c interface ip dump > ...
- Qt 如何处理密集型耗时的事情(频繁调用QApplication::processEvents)
有时候需要处理一些跟界面无关的但非常耗时的事情,这些事情跟界面在同一个线程中,由于时间太长,导致界面无法响应,处于“假死”状态.例如:在应用程序中保存文件到硬盘上,从开始保存直到文件保存完毕,程序不响 ...
- C#中隐式操作CMD命令行窗口
原文:C#中隐式操作CMD命令行窗口 MS的CMD命令行是一种重要的操作界面,一些在C#中不那么方便完成的功能,在CMD中几个简单的命令或许就可以轻松搞定,如果能在C#中能完成CMD窗口的功能,那一定 ...
- Asp.net MVC中关于@Html标签的使用
@Html帮助器简单说明,记录些基本的跟HTML中对应的@html帮助器,@Html基本包含了html中的表单控件和常用Html 在@Html中,带有For的主要是针对强类型的Html类型. 用于说明 ...
- jquery 使用ajax调用c#后台方法
$.ajax({ type: "get", cache: false, ...
- Java程序猿的JavaScript学习笔记(8——jQuery选择器)
计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...
- JavaSE_ 反射 目录(27)
JavaSE学习总结第27天_反射 & 设计模式 & JDK5.7.8新特性27.01 反射_类的加载概述和加载时机27.02 反射_类加载器的概述和分类27.03 反射_反射概述27 ...