public FileHashDB(string FullFilePath, short _BlockSize, OnReadyHandler rCB) { Map = new Hashtable(); ObjectReadCB = new OnReadHandler(ReadObjectSink); OnReady += rCB; MaxBlockSize = _BlockSize; fstream = new FileStream(FullFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read); DBFileInfo = new FileInfo(FullFilePath); DeleteBuffer = new byte[MaxBlockSize]; DeleteCB = new AsyncCallback(DeleteSink); ClearCB = new AsyncCallback(ClearSink); MemoryStream ms = new MemoryStream(); byte[] x = BitConverter.GetBytes((short)0); ms.Write(x, 0, x.Length); ms.Write(x, 0, x.Length); ClearBlock = ms.ToArray(); ms.Close(); if (fstream.Length > 0) { // Already Exists SeekLock.WaitOne(); fstream.Seek(0, SeekOrigin.Begin); fstream.BeginRead(InitBuffer, 0, 10, new AsyncCallback(InitSink), null); SeekLock.ReleaseMutex(); } else { IndexFilePath = DBFileInfo.FullName.Substring(0, DBFileInfo.FullName.Length - DBFileInfo.Extension.Length) + ".idx"; // Initialize File Header byte[] BlockSize = BitConverter.GetBytes((short)MaxBlockSize); byte[] bUpdateID = BitConverter.GetBytes(UpdateID); byte[] eVersion = BitConverter.GetBytes(EngineVersion); SeekLock.WaitOne(); fstream.Seek(0, SeekOrigin.Begin); InitializeCounter = 3; fstream.BeginWrite(BlockSize, 0, 2, new AsyncCallback(InitHeaderSink), null); fstream.BeginWrite(bUpdateID, 0, 4, new AsyncCallback(InitHeaderSink), null); fstream.BeginWrite(eVersion, 0, 4, new AsyncCallback(InitHeaderSink), null); SeekLock.ReleaseMutex(); if (Interlocked.Decrement(ref InitializeCounter) == 0) { if (OnReady != null) { OnReady(this, null); } } } }
public FileHashDB(string FullFilePath, short _BlockSize, OnReadyHandler rCB) { Map = new Hashtable(); ObjectReadCB = new OnReadHandler(ReadObjectSink); OnReady += rCB; MaxBlockSize = _BlockSize; fstream = new FileStream(FullFilePath,FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.Read); DBFileInfo = new FileInfo(FullFilePath); DeleteBuffer = new byte[MaxBlockSize]; DeleteCB = new AsyncCallback(DeleteSink); ClearCB = new AsyncCallback(ClearSink); MemoryStream ms = new MemoryStream(); byte[] x = BitConverter.GetBytes((short)0); ms.Write(x,0,x.Length); ms.Write(x,0,x.Length); ClearBlock = ms.ToArray(); ms.Close(); if(fstream.Length>0) { // Already Exists SeekLock.WaitOne(); fstream.Seek(0,SeekOrigin.Begin); fstream.BeginRead(InitBuffer,0,10,new AsyncCallback(InitSink),null); SeekLock.ReleaseMutex(); } else { IndexFilePath = DBFileInfo.FullName.Substring(0,DBFileInfo.FullName.Length-DBFileInfo.Extension.Length) + ".idx"; // Initialize File Header byte[] BlockSize = BitConverter.GetBytes((short)MaxBlockSize); byte[] bUpdateID = BitConverter.GetBytes(UpdateID); byte[] eVersion = BitConverter.GetBytes(EngineVersion); SeekLock.WaitOne(); fstream.Seek(0,SeekOrigin.Begin); InitializeCounter = 3; fstream.BeginWrite(BlockSize,0,2,new AsyncCallback(InitHeaderSink),null); fstream.BeginWrite(bUpdateID,0,4,new AsyncCallback(InitHeaderSink),null); fstream.BeginWrite(eVersion,0,4,new AsyncCallback(InitHeaderSink),null); SeekLock.ReleaseMutex(); if(Interlocked.Decrement(ref InitializeCounter)==0) { if(OnReady!=null) OnReady(this,null); } } }