using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace TGREER
{
[Serializable]
public class Document
{
private string _seq;
private Int32 _offset;
public Document(string _seq, Int32 _offset)
{
this._seq = _seq;
this._offset = _offset;
}
public override string ToString()
{
return _seq;
}
public string GenericSequenceNo
{
get { return _seq; }
set { _seq = value; }
}
public Int32 ByteOffset
{
get { return _offset; }
set { _offset = value; }
}
}
[Serializable]
public class Page
{
private string _seq;
private Int32 _offset;
public Page(string _seq, Int32 _offset)
{
this._seq = _seq;
this._offset = _offset;
}
public override string ToString()
{
return _seq;
}
public string GenericSequenceNo
{
get { return _seq; }
set { _seq = value; }
}
public Int32 ByteOffset
{
get { return _offset; }
set { _offset = value; }
}
}
public class Indexer
{
private string _psFilename;
private string _key;
private List<Document> _documents;
private List<Page> _pages;
private Document _document;
private Page _page;
// constructor, creates empty lists
public Indexer(string _filename)
{
_psFilename = _filename;
_documents = new List<Document>();
_pages = new List<Page>();
}
// constructor, re-creates lists from serialized files
public Indexer(string _filename, string _docName, string _pgName)
{
_psFilename = _filename;
FileStream _s = new FileStream(_docName, FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
_documents = (List<Document>)formatter.Deserialize(_s);
_s.Close();
_s = new FileStream(_pgName, FileMode.Open);
formatter = new BinaryFormatter();
_pages = (List<Page>)formatter.Deserialize(_s);
_s.Close();
}
// public method to add entry to documents list
public void addDocIndex(string _seq, Int32 _offset)
{
_document = new Document(_seq, _offset);
_documents.Add(_document);
}
// public method to add entry to pages list
public void addPageIndex(string _seq, Int32 _offset)
{
_page = new Page(_seq, _offset);
_pages.Add(_page);
}
// public method to serialize list contents to named files
public void serialize(string _docName, string _pgName)
{
FileStream _s = new FileStream(_docName, FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(_s, _documents);
_s.Close();
_s = new FileStream(_pgName, FileMode.Create);
formatter = new BinaryFormatter();
formatter.Serialize(_s, _pages);
_s.Close();
}
// public method to return a string containing a document
public string getDocument(string _key)
{
this._key = _key;
_document = _documents.Find(isKey);
Int32 _i = _documents.IndexOf(_document);
Int32 _byteStart = _document.ByteOffset;
Int32 _byteEnd = _documents[_i + 1].ByteOffset - 1;
Int32 _bytesToRead = _byteEnd - _byteStart;
FileStream _s = new FileStream(_psFilename, FileMode.Open);
StreamReader _sr = new StreamReader(_s);
_s.Seek(_byteStart, SeekOrigin.Begin);
char[] _buffer = new char[_bytesToRead];
Int32 _bytesRead = _sr.ReadBlock(_buffer, 0, _bytesToRead);
_s.Close();
return new string(_buffer);
}
// public method to return a string containing a page
public string getPage(string _key)
{
this._key = _key;
_page = _pages.Find(isKey);
Int32 _i = _pages.IndexOf(_page);
Int32 _byteStart = _page.ByteOffset;
Int32 _byteEnd = _pages[_i + 1].ByteOffset - 1;
Int32 _bytesToRead = _byteEnd - _byteStart;
FileStream _s = new FileStream(_psFilename, FileMode.Open);
StreamReader _sr = new StreamReader(_s);
_s.Seek(_byteStart, SeekOrigin.Begin);
char[] _buffer = new char[_bytesToRead];
Int32 _bytesRead = _sr.ReadBlock(_buffer, 0, _bytesToRead);
_s.Close();
return new string(_buffer);
}
// Predicate used for searching the List. Presumably, this is optimized so is faster than a forall loop?
private bool isKey(Document _d)
{
if (_d.GenericSequenceNo == this._key)
{
return true;
}
else
{
return false;
}
}
private bool isKey(Page _p)
{
if (_p.GenericSequenceNo == this._key)
{
return true;
}
else
{
return false;
}
}
}
}