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. 在CentOS 7 / Gnome 3 双屏时设置主屏

    在Windows中设置扩展显示器为主屏的方式非常清楚,但在Linux中就不是那么明显了,下面介绍如何完成这个设置 ------------------------------------------- ...

  2. 一个好用的hash函数(C语言)

    typedef unsigned int DWORD; typedef unsigned char BYTE; /******************************************* ...

  3. Linux同平台Oracle数据库整体物理迁移

    Linux同平台数据库整体物理迁移需求:A机器不再使用,要将A机器的Oracle迁移到B机器.之前写过类似需求的文章: http://www.linuxidc.com/Linux/2015-05/11 ...

  4. delete了,析构函数却没有调用

    析构函数在对象的生命结束时,会自动调用,大家所熟知的智能指针就是根据析构函数的这种特性而实现的,包括Qt的内存管理机制,也都是利用了析构函数的这一机制来实现的.c++创始人Bjarne Stroust ...

  5. CCNA实验(10) -- Access List

    使用包过滤技术在路由器上读取三层及四层报头的信息如源地址.目的地址.源端口.目的端口根据预先定义好的规则对包进行过滤 三种类型:1.标准ACL:表号范围1-99或1300-1999.仅对源IP地址进行 ...

  6. java进制转换器 图形用户界面 十进制及其相反数分别转化为二,四,八,十六进制

    package com.rgy.Test; import java.awt.Color; import java.awt.FlowLayout; import java.awt.GridLayout; ...

  7. MyEclipse性能调优初体验

    MyEclipse性能调优初体验 简介一下工作环境,MyEclipse2014,你懂的 项目中有一个基于web的工作流引擎,仅仅要执行起来就CPU差点儿耗尽(尽管看似27%,事实上已经把俺4核的CPU ...

  8. java载入XML文件并解析xml

    import java.io.File; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentExce ...

  9. HTML静态网页(图片热点、网页划区、拼接及表单的使用)

    图片热点: 规划出图片上的一个区域,可以做出超链接,直接点击图片区域就可以完成跳转的效果. 示例: 网页划区: 在一个网页里,规划出一个区域用来展示另一个网页的内容. 示例:   网页的拼接: 在一个 ...

  10. Java学习之异常处理

    在 Java 中,所有的异常都有一个共同的祖先 Throwable(可抛出),Throwable 指定代码中可用异常传播机制通过 Java 应用程序传输的任何问题的共性.       Throwabl ...