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#实现的内存分页机制的一个实例的更多相关文章

  1. CPU内存管理和linux内存分页机制

    一.概念 物理地址(physical address)用于内存芯片级的单元寻址,与处理器和CPU连接的地址总线相对应.——这个概念应该是这几个概念中最好理解的一个,但是值得一提的是,虽然可以直接把物理 ...

  2. Oracle 分页查询的一个实例

    1.分页模板 select * from ( select rownum as rn , a.* from( 某个表名) a) where rn between 0 and 6 2 某个表名 sele ...

  3. 操作系统:x86下内存分页机制 (1)

    前置知识: 分段的概念(当然手写过肯定是坠吼的 为什么要分页 当我们写程序的时候,总是倾向于把一个完整的程序分成最基本的数据段,代码段,栈段.并且普通的分段机制就是在进程所属的LDT中把每一个段给标识 ...

  4. mysql通过“延迟关联”进行limit分页查询优化的一个实例

    最近在生产上遇见一个分页查询特别慢的问题,数据量大概有200万的样子,翻到最后一页性能很低,差不多得有4秒的样子才能出来整个页面,需要进行查询优化. 第一步,找到执行慢的sql,如下: SELECT  ...

  5. Linux内存寻址之分段机制及分页机制【转】

    前言 本文涉及的硬件平台是X86,如果是其他平台的话,如ARM,是会使用到MMU,但是没有使用到分段机制: 最近在学习Linux内核,读到<深入理解Linux内核>的内存寻址一章.原本以为 ...

  6. Intel微处理器学习笔记(四) 内存分页

    内存分页机制(memory paging mechanism)是从386开始的.线性地址通过分页机制透明转换为物理地址. 从这里知道:1. 如果不分页,则线性地址等于物理地址:2. 如果分页,则线性地 ...

  7. Linux内存管理机制简析

    Linux内存管理机制简析 本文对Linux内存管理机制做一个简单的分析,试图让你快速理解Linux一些内存管理的概念并有效的利用一些管理方法. NUMA Linux 2.6开始支持NUMA( Non ...

  8. x86 分页机制——虚拟地址到物理地址寻址

    x86下的分页机制有一个特点:PAE模式 PAE模式 物理地址扩展,是基于x86 的服务器的一种功能,它使运行 Windows Server 2003, Enterprise Edition 和 Wi ...

  9. Linux内存寻址之分页机制

    在上一篇文章Linux内存寻址之分段机制中,我们了解逻辑地址通过分段机制转换为线性地址的过程.下面,我们就来看看更加重要和复杂的分页机制. 分页机制在段机制之后进行,以完成线性—物理地址的转换过程.段 ...

随机推荐

  1. kruskal算法求最小生成树(jungle roads的kruskal解法)

    注意: 注意数组越界问题(提交出现runtimeError代表数组越界) 刚开始提交的时候,边集中边的数目和点集中点的数目用的同一个宏定义,但是宏定义是按照点的最大数定义的,所以提交的时候出现了数组越 ...

  2. C++类的常成员函数

    让一个成员函数带上常量性是什么意思呢?通常的答案是,一个常成员函数不会更改其class对象.这是一种平凡的表述,而编译器实现的手法也相当平凡. 任何非静态成员函数其实都被编译器隐式插入了一个指针类型的 ...

  3. Clamp函数

    Clamp函数可以将随机变化的数值限制在一个给定的区间[min, max]内: template<class T> T Clamp(T x, T min, T max) { if (x & ...

  4. Excel Sheet Row Numbers

    Given the sequence S1 = {a,b,c,d,…,x,y,z,aa,ab,ac…. } and given that this sequence corresponds (term ...

  5. 兼容现有jQuery API的轻量级JavaScript库:Zepo

    Zepo是一个JavaScript框架,其特点是兼容现有jQuery API的同时,自身体积十分小:它与jQuery有着类似的API.如果你会jQuery,那么也就会使用Zepto了. $('div' ...

  6. 一道Python练习题

    有关字符串与编码的,转自廖雪峰Python教程. 小明的成绩从去年的72分提升到了今年的85分,请计算小明成绩提升的百分点,并用字符串格式化显示出'xx.x%',只保留小数点后1位: # -*- co ...

  7. MyBatis good

    命名解析:为了减少输入量,MyBatis 对所有的命名配置元素(包括语句,结果映射,缓存等)使用了如下的命名解析规则. 完全限定名(比如“com.mypackage.MyMapper.selectAl ...

  8. cloneNode小结

    js原生API中有个cloneNode,还有一个可选的参数, true代表复制子节点,包括任何包裹在标签之间的东西,当然包括文本节点,也就是标签之间有什么,它就会不假思索的全部都克隆一份. false ...

  9. Codeforces Round#2

    A. Winner 题目大意:一些人在玩游戏,给出n次某人获得的分数,现在请计算出胜者,如果不止一个最高分,则取最先达到该分数的人. 题解:模拟得分过程,首先计算出最高分,然后获取先到达或超过最高分的 ...

  10. 【转载】Android Studio jar、so、library项目依赖,原文链接http://zhengxiaopeng.com/2014/12/13/Android-Studio-jar、so、library项目依赖/

    前言 Android Studio(以下简称AS)在13年I/O大会后放出预览版到现在放出的正式版1.0(PS.今天又更新到1.0.1了)历时一年多了,虽然Google官方推出的Android开发者的 ...