gpinkas
gpinkas

Reputation: 2591

.Net: Is there a Dictionary to efficiently store/load its contents in a file?

In (VB).NET (4.0 framework), I'd need a Dictionary<string, string> that can efficiently store its contents, or better its internal representation to a file and load it later.

Ruby has such a thing in PStore, see ruby docs.

The intention is to once prepare a lookup dictionary for e.g. text translations out of a database and store it in a disk file that can quickly be loaded multiple times for output generation. So this should not read the file line by line and fill the Dictionary with the key-value pairs, but instead load the file contents in one go directly into its internal state representation.

I think this should be possible, but I would also like to see your explanations if you think otherwise.

Upvotes: 1

Views: 1832

Answers (2)

tcarvin
tcarvin

Reputation: 10855

As I commented, I still think any benefit you get is going to be out-weighed by the effort to implement. I recommend looking into something BerklyDB or HamsterDB, both of which I googled up in a couple of minutes.

If really want to hand-implement something, I think perhaps instead of writing your own memory manager with the Marshal class to ensure that your data-structure are stored in a contiguous block that can marshaled quickly to and from a file backing, I would look at using a persistent memory-mapped file. Then you can use CreateViewAccessor to get a MemoryMappedViewAccessor instance that can be used to read and write primitive and data structures. Combine this with any college text or open source implementation of something like a B-Tree or something similar and you could get something working.

Upvotes: 2

Andrey
Andrey

Reputation: 60065

If you really want to keep it simple for this particular task, I would to it this way:

class Pair {
    public string Key { get; set; }
    public string Value { get; set; }
}

var dict = new Dictionary<string, string>() {
    {"asd", "zxc"},
    {"111", "zzs"}
}; // Populate your dictionary somehow
var list = dict.ToList().Select( p => new Pair() { Key = p.Key, Value = p.Value} );
//Then XML-serialize this list

To read it:

//De-XML-serialize that list
dict = list.ToDictionary( p => p.Key, p => p.Value );

Side advantage is that you can modify stored list with just text editor.

Upvotes: 1

Related Questions