tags:

views:

252

answers:

4

If you need to be able to store a large amount of plain text in memory so it can be searched and edited, what kind of datatype would you use?

Let's say I load a 10000 line document into my C# application for searching with LINQ, how would you represent it in memory?

Not a string, because it's got be mutable, and strings are immutable.

+2  A: 

You could always use a MemoryStream and then use a StreamReader to read the data from memory.

You might get some lift from the following link which talks about using LINQ with a StreamReader. I'm not sure if it fits exactly what you're trying to do though.

http://blogs.msdn.com/ericwhite/archive/2006/08/31/linq-to-text-files.aspx

From the blog post:

  StreamReader sr = new StreamReader("TextFile.txt");

  var t1 =
    from line in sr.Lines()
    let items = line.Split(',')
    where ! line.StartsWith("#")
    select String.Format("{0}{1}{2}",
        items[1].PadRight(16),
        items[2].PadRight(16),
        items[3].PadRight(16));

  var t2 =
    from line in t1
    select line.ToUpper();

  foreach (var t in t2)
    Console.WriteLine(t);

  sr.Close();

You'd want to change the StreamReader instantiation to something like

StreamReader sr = new StreamReader(myMemoryStreamVar)

or something similar.

Brian Hasden
can you query the data in the memorystream with LINQ?
Tony
When you attach a StreamReader to a MemoryStream, you can use the ReadToEnd() method to read out the entire memory stream to a string and then search it. You're going to have some issues trying to use the built in .NET stuff to do high performance, high load string operations. It might be better to write the search routine yourself to search through the memory stream. Also, if you're reading the data from disk, you don't have to read it all into memory at once. The stream reader can read the data in block from disk to keep memory usage down.
Brian Hasden
Just added a link to a blog post that describes an extension method technique that allows you to use LINQ for searching through large text files without loading the whole file into memory. It uses the StreamReader technique I suggested but has an actual code sample that I updated my answer to contain. It's a pretty neat way of searching text files using LINQ. Highly recommended.
Brian Hasden
+2  A: 

Stringbuilder would fit. Internally it's a buffer, and is mutable.

chris.w.mclean
and performance wise, is this a feasible option?
Tony
If you do your searches well, it should be. The key would be to do the searches on the internal char[] of the stringbuilder, so you wouldn't be doing tons of string alloc/deallocs just to do your searches. This is the only problem I think you would have w/ the streamreader/memory stream option given by Brian.
chris.w.mclean
A: 

Try using Memory mapped files. Its a new BCL in .net 4.0.

Here's the link. http://msdn.microsoft.com/en-us/library/system.io.memorymappedfiles.memorymappedfile%28VS.100%29.aspx

Thanks

nitroxn
A: 

Hi,

StringBuilder uses heap for memory allocation, which may result in out of memory exceptions at runtime.

Thanks

nitroxn