STSDB 缓存。。。
汪安宁
2023-12-01
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using STSdb.Data;
using System.Reflection;
namespace YQMapTile.Ags.AGS
{
/// <summary>
/// STSDB提供的层块缓存器
/// </summary>
internal class STSTileCache : IAgsTileCache<MemoryStream>
{
//protected StorageEngine cacheEngine = null;
protected XTable<long, byte[]> tileTable = null;
private string sPath;
private string sLyrName;
/// <summary>
/// 采用STS缓存来存储层块数据
/// </summary>
/// <param name="path">Path为缓存数据数据存储路</param>
/// <param name="lyrname"></param>
public STSTileCache(string path, string lyrname)
{
if (string.IsNullOrEmpty(path))
{
Assembly myAssembly = Assembly.GetAssembly(this.GetType());
path = Path.GetDirectoryName(myAssembly.Location);
string sts_dll = Path.Combine(path, "STSdb.dll");
System.Reflection.Assembly.LoadFile(sts_dll);
path = Path.Combine(path, "TileCache");
//如果目录不存在则创建
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
}
string dbFileName = Path.Combine(path,(lyrname+".vstb").ToUpper());
this.initCacheEngine(dbFileName, lyrname);
}
~STSTileCache()
{
this.Close();
}
/// <summary>
/// 关闭时直接全部清空
/// </summary>
public void ShutDown()
{
this.Close();
}
/// <summary>
/// 关闭缓存引擎
/// </summary>
public void Close()
{
try
{
if (null != tileTable)
{
tileTable.Flush();
tileTable.Commit();
tileTable.Close();
tileTable = null;
}
//if (null != cacheEngine)
//{
// cacheEngine.Scheme.Commit();
// cacheEngine.Dispose();
// cacheEngine = null;
//}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// <summary>
/// 初始化缓存引擎
/// </summary>
/// <param name="path"></param>
/// <param name="lyrname"></param>
private void initCacheEngine(string path,string lyrname)
{
try
{
sPath = path;
sLyrName = lyrname.ToUpper();
using (StorageEngine cacheEngine = StorageEngine.FromFile(path))
{
tileTable = cacheEngine.Scheme.CreateOrOpenXTable<long, byte[]>(new Locator(sLyrName));
cacheEngine.Scheme.Commit();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
#region Members of IAgsTileCache
public void Add(RawTile tile, MemoryStream data)
{
if (null == tileTable)
this.initCacheEngine(sPath,sLyrName);
if (tileTable != null)
{
tileTable[tile.TileId] = data.ToArray();
tileTable.Flush();
}
}
/// <summary>
/// 获取层块数据
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public MemoryStream GetTileData(RawTile index)
{
if (null == tileTable)
this.initCacheEngine(sPath, sLyrName);
if (null == tileTable) return null;
if (!tileTable.Exists(index.TileId)) return null;
byte[] tdata = tileTable[index.TileId];
if (tdata == null || tdata.Length <= 0) return null;
else return new MemoryStream(tdata);
}
/// <summary>
/// 删除层块数据
/// </summary>
/// <param name="index"></param>
public void Remove(RawTile index)
{
if (null == tileTable)
this.initCacheEngine(sPath, sLyrName);
if(null == tileTable)return ;
//删除数据
tileTable.Delete(index.TileId);
}
/// <summary>
/// 缓存的能力
/// </summary>
public int CacheCapacity
{
get
{
return int.MaxValue - (int)tileTable.Count;
}
set
{
//不处理
}
}
/// <summary>
/// 获取层块数据
/// </summary>
/// <param name="tile"></param>
/// <param name="data"></param>
/// <returns></returns>
public bool TryGetValue(RawTile tile, out MemoryStream data)
{
data = null;
if (null == tileTable)
this.initCacheEngine(sPath, sLyrName);
if (tileTable == null) return false;
if (!tileTable.Exists(tile.TileId)) return false;
byte[] tdata = tileTable[tile.TileId];
if (tdata == null || tdata.Length <= 0) return false;
data = new MemoryStream(tdata);
return true;
}
/// <summary>
/// 是否包含该层块
/// </summary>
/// <param name="tile"></param>
/// <returns></returns>
public bool ContainsKey(RawTile tile) {
if (null == tileTable)
this.initCacheEngine(sPath, sLyrName);
if (tileTable == null) return false;
else return tileTable.Exists(tile.TileId);
}
#endregion
}
}
通过上述方法构建的STS缓存库在ArcMap中调用时在sts 构建元数据CreateObjects时会提示stsdb.dll动态库没有加载,实在想不通