在 C# 中寻找一个简单的独立持久字典实现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/100235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:57:39  来源:igfitidea点击:

Looking for a simple standalone persistent dictionary implementation in C#

提问by Sam Saffron

For an open source project I am looking for a good, simple implementation of a Dictionary that is backed by a file. Meaning, if an application crashes or restarts the dictionary will keep its state. I would like it to update the underlying file every time the dictionary is touched. (Add a value or remove a value). A FileWatcher is not required but it could be useful.

对于一个开源项目,我正在寻找一个由文件支持的字典的良好、简单的实现。意思是,如果应用程序崩溃或重新启动,字典将保持其状态。我希望它在每次接触字典时更新底层文件。(添加值或删除值)。FileWatcher 不是必需的,但它可能很有用。

class PersistentDictionary<T,V> : IDictionary<T,V>
{
    public PersistentDictionary(string filename)
    {

    } 
}

Requirements:

要求:

  • Open Source, with no dependency on native code (no sqlite)
  • Ideally a very short and simple implementation
  • When setting or clearing a value it should not re-write the entire underlying file, instead it should seek to the position in the file and update the value.
  • 开源,不依赖本机代码(无 sqlite)
  • 理想情况下是一个非常简短和简单的实现
  • 当设置或清除一个值时,它不应该重写整个底层文件,而是应该寻找文件中的位置并更新值。

Similar Questions

类似问题

采纳答案by lubos hasko

  • bplustreedotnet

    The bplusdotnet package is a library of cross compatible data structure implementations in C#, java, and Python which are useful for applications which need to store and retrieve persistent information. The bplusdotnet data structures make it easy to store string keys associated with values permanently.

  • ESENT Managed Interface

    Not 100% managed code but it's worth mentioning it as unmanaged library itself is already part of every windows XP/2003/Vista/7 box

    ESENT is an embeddable database storage engine (ISAM) which is part of Windows. It provides reliable, transacted, concurrent, high-performance data storage with row-level locking, write-ahead logging and snapshot isolation. This is a managed wrapper for the ESENT Win32 API.

  • Akavache

    *Akavache is an asynchronous, persistent key-value cache created for writing native desktop and mobile applications in C#. Think of it like memcached for desktop apps.

  • bplustreedotnet

    bplusdotnet 包是 C#、java 和 Python 中的交叉兼容数据结构实现库,对于需要存储和检索持久信息的应用程序非常有用。bplusdotnet 数据结构使永久存储与值关联的字符串键变得容易。

  • ESENT 管理接口

    不是 100% 托管代码,但值得一提的是非托管库本身已经是每个 windows XP/2003/Vista/7 框的一部分

    ESENT 是一个嵌入式数据库存储引擎 (ISAM),它是 Windows 的一部分。它通过行级锁定、预写日志记录和快照隔离提供可靠的、事务处理的、并发的、高性能的数据存储。这是 ESENT Win32 API 的托管包装器。

  • 阿卡瓦什

    *Akavache 是一种异步、持久的键值缓存,专为用 C# 编写本机桌面和移动应用程序而创建。把它想象成桌面应用程序的 memcached。

- The C5 Generic Collection Library

- C5 通用集合库

C5 provides functionality and data structures not provided by the standard .Net System.Collections.Genericnamespace, such as persistent tree data structures, heap based priority queues, hash indexed array lists and linked lists, and events on collection changes.

C5 提供了标准 .NetSystem.Collections.Generic命名空间未提供的功能和数据结构,例如持久树数据结构、基于堆的优先级队列、散列索引数组列表和链表,以及集合更改事件。

回答by chrisb

Sounds cool, but how will you get around changes to the stored value (if it was a reference type) itself? If its immutable then all is well but if not you're kinda stuffed :-)

听起来很酷,但是您将如何解决对存储值(如果它是引用类型)本身的更改?如果它是不可变的,那么一切都很好,但如果不是,你就有点饱了:-)

If you're not dealing with immutable values, I would suspect a better approach would be to handle persistence at the value level and to just rebuild the dictionary as necessary.

如果您不处理不可变值,我怀疑更好的方法是在值级别处理持久性并根据需要重建字典。

(edited to add a clarification)

(编辑以添加说明)

回答by leppie

Just use serialization. Look at the BinaryFormatter class.

只需使用序列化。查看 BinaryFormatter 类。

回答by Douglas Leeder

I don't know of anything to solve your problem. It will need to be a fixed size structure, so that you can meet the requirements of being able to rewrite records without rewriting the entire file.

我不知道有什么可以解决你的问题。它需要是一个固定大小的结构,以便您可以满足无需重写整个文件即可重写记录的要求。

This means normal strings are out.

这意味着正常的字符串已用完。

回答by Omer van Kloeten

Like Douglas said, you need to know the fixed size of your types (both T and V). Also, variable-length instances in the object grid referenced by any of those instances are out.

就像道格拉斯所说,你需要知道你的类型(T 和 V)的固定大小。此外,任何这些实例引用的对象网格中的可变长度实例都已失效。

Still, implementing a dictionary backed by a file is quite simple and you can use the BinaryWriterclass to write the types to disk, after inheriting or encapsulating the Dictionary<TKey, TValue>class.

尽管如此,实现由文件支持的字典非常简单BinaryWriter,在继承或封装Dictionary<TKey, TValue>类之后,您可以使用该类将类型写入磁盘。

回答by Mladen Prajdic

one way is to use the Extensible Storage Enginebuilt into windoows to store your stuff. It's a native win database that supports indexing, transactions etc...

一种方法是使用内置于 windows的可扩展存储引擎来存储您的东西。它是一个支持索引、事务等的原生 win 数据库......

回答by kenny

Consider a memory mapped file. I'm not sure if there is direct support in .NET, but you could pinvoke the Win32 calls.

考虑一个内存映射文件。我不确定 .NET 中是否有直接支持,但您可以调用 Win32 调用。

回答by axel_c

I haven't actually used it, but this project apparently provides an mmap()-like implementation in C#

我实际上并没有使用它,但是这个项目显然在 C# 中提供了一个类似 mmap() 的实现

Mmap

映射

回答by axel_c

I am not much of a programmer, but wouldn't creating a really simple XML format to store your data do the trick?

我不是一个程序员,但是创建一个非常简单的 XML 格式来存储您的数据不会成功吗?

<dico> 
   <dicEntry index="x">
     <key>MyKey</key>
     <val type="string">My val</val>
   </dicEntry>
   ...
</dico>

From there, you load the XML file DOM and fill up your dictionary as you like,

从那里,您加载 XML 文件 DOM 并根据需要填写您的字典,

XmlDocument xdocDico = new XmlDocument();
string sXMLfile;
public loadDico(string sXMLfile, [other args...])
{
   xdocDico.load(sXMLfile);
   // Gather whatever you need and load it into your dico
}
public flushDicInXML(string sXMLfile, dictionary dicWhatever)
{
   // Dump the dic in the XML doc & save
}
public updateXMLDOM(index, key, value)
{
   // Update a specific value of the XML DOM based on index or key
}

Then whenever you want, you can update the DOM and save it on disk.

然后,您可以随时更新 DOM 并将其保存在磁盘上。

xdocDico.save(sXMLfile);

xdocDico.save(sXMLfile);

If you can afford to keep the DOM in memory performance-wise, it's pretty easy to deal with. Depending on your requirements, you may not even need the dictionary at all.

如果您能负担得起将 DOM 保留在内存中的性能方面,那么处理起来就很容易了。根据您的要求,您甚至可能根本不需要字典。

回答by GvS

Let me analyze this:

我来分析一下:

  1. Retrieve information by key
  2. Persistant storage
  3. Do not want to write back the whole file when 1 value changes
  4. Should survive crashes
  1. 按键检索信息
  2. 持久存储
  3. 不想在 1 个值更改时写回整个文件
  4. 应该在崩溃中幸存下来

I think you want a database.

我想你想要一个数据库。

Edit: I think you are searching for the wrong thing. Search for a database that fits your requirements. And change some of your requirements, because I think it will be difficult to meet them all.

编辑:我认为您正在寻找错误的东西。搜索符合您要求的数据库。并更改您的一些要求,因为我认为很难满足所有要求。