public Reader(string path) { this.path = path; FileInfo file = new FileInfo(path); if (!file.Exists) { throw new Exception("Reader passed path '" + path + "' which does not exist!"); } _size = Marshal.SizeOf(typeof(TModel)); _buffer = new byte[_size]; _stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.Read); _count = (int)file.Length / _size; _index = new Dictionary <int, long>(_count); FileInfo ifile = ModelUtility.GetIndexFile(path); int isize = Marshal.SizeOf(typeof(Element)); byte[] ibuffer = new byte[isize]; using (FileStream istream = ifile.Open(FileMode.Open, FileAccess.Read, FileShare.Read)) { while (istream.Read(ibuffer, 0, isize) != 0) { Element element = ModelUtility.PtrToStructure <Element>(ref ibuffer); _index.Add(element.Id, element.Position); } } }
private TModel Seek(long location) { long offset = location - _position; if (offset != 0) { _stream.Seek(offset, SeekOrigin.Current); } _position = location + _size; _stream.Read(_buffer, 0, _size); _model = ModelUtility.PtrToStructure <TModel>(ref _buffer); return(_model); }
public void Import(string path) { FileInfo inputFile = new FileInfo(_inputPath); FileInfo ifile = ModelUtility.GetIndexFile(path); FileInfo file = new FileInfo(path); if (Global.PrintFile != null) { Global.PrintFile.WriteLine(@"Importing ""{0}"" into ""{1}"" for type {2}.", inputFile.Name, file.Name, typeof(TModel).Name); Global.PrintFile.WriteLine(@"Creating file ""{0}"".", file.FullName); Global.PrintFile.WriteLine(@"Creating index file ""{0}"" for primary key.", ifile.FullName); } using (CountingReader reader = new CountingReader(inputFile.Open(FileMode.Open, FileAccess.Read, FileShare.Read))) { using (BinaryWriter iwriter = new BinaryWriter(ifile.Open(FileMode.Create, FileAccess.Write, FileShare.Read))) { using (BinaryWriter writer = new BinaryWriter(file.Open(FileMode.Create, FileAccess.Write, FileShare.Read))) { long position = 0L; reader.ReadLine(); string line; while ((line = reader.ReadLine()) != null) { string[] row = line.Split(new[] { _delimiter }); TModel model = new TModel(); SetModel(model, row); Write(iwriter, new Element(model.Id, position)); Write(writer, model); position += _size; if (reader.LineNumber % 1024 == 0) { iwriter.Flush(); writer.Flush(); } } } } } }