Skip to content

Commit

Permalink
Changed from FileStream to ReadFile / WriteFile for the ultimate I/O …
Browse files Browse the repository at this point in the history
…performance
  • Loading branch information
andrewesdaile committed Apr 3, 2017
1 parent 0697d6b commit 90b3eec
Show file tree
Hide file tree
Showing 12 changed files with 603 additions and 15 deletions.
3 changes: 3 additions & 0 deletions Haystacks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -43,6 +44,8 @@
<Compile Include="NeedleInfo.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Stacker.cs" />
<Compile Include="WinFileFlagsAndAttributes.cs" />
<Compile Include="WinFileIO.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
2 changes: 1 addition & 1 deletion Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.1.*")]
[assembly: AssemblyVersion("1.2.*")]
68 changes: 55 additions & 13 deletions Stacker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,29 @@ public NeedleInfo Write(Stream stream)
indexStream.Close();
}

////write the stack entry
//using (FileStream stackStream = File.Open(targetStack, FileMode.Open, FileAccess.Write))
//{
// stackStream.Seek(0, SeekOrigin.End);

// stream.CopyTo(stackStream);
// stackStream.Flush();

// stackStream.Close();
//}

//write the stack entry
using (FileStream stackStream = File.Open(targetStack, FileMode.Open, FileAccess.Write))
byte[] buffer = new byte[WinFileIO.BlockSize];
using (WinFileIO writer = new WinFileIO(buffer))
{
stackStream.Seek(0, SeekOrigin.End);
writer.OpenForWriting(targetStack);
writer.Position = writer.Length;

stream.CopyTo(stackStream);
stackStream.Flush();

stackStream.Close();
while (stream.Position < stream.Length)
{
int bytesRead = stream.Read(buffer, 0, WinFileIO.BlockSize);
writer.WriteBlocks(bytesRead);
}
}

return info;
Expand Down Expand Up @@ -257,20 +271,51 @@ public void Read(Stream stream, int stackNumber, int needleNumber)
indexStream.Close();
}

////read data from the stack file
//using (FileStream stackStream = File.Open(targetStack, FileMode.Open, FileAccess.Read))
//{
// //seek to the beginning of the needle
// stackStream.Seek(stackOffset, SeekOrigin.Begin);

// //read all the data for the needle
// byte[] buffer = new byte[81920];
// int chunkSize = 1;
// long bytesTransferred = 0;

// while (chunkSize > 0 && bytesTransferred <= needleLength)
// {
// chunkSize = stackStream.Read(buffer, 0, buffer.Length);
// bytesTransferred += chunkSize;

// if (bytesTransferred <= needleLength)
// stream.Write(buffer, 0, chunkSize);
// else
// {
// int lastChunkSize = (int)(needleLength % buffer.Length);
// stream.Write(buffer, 0, lastChunkSize);
// }
// }

// stream.Flush();
// stackStream.Close();
//}

//read data from the stack file
using (FileStream stackStream = File.Open(targetStack, FileMode.Open, FileAccess.Read))
byte[] buffer = new byte[WinFileIO.BlockSize];
using (WinFileIO reader = new WinFileIO(buffer))
{
reader.OpenForReading(targetStack);

//seek to the beginning of the needle
stackStream.Seek(stackOffset, SeekOrigin.Begin);
reader.Position = stackOffset;

//read all the data for the needle
byte[] buffer = new byte[81920];
int chunkSize = 1;
long bytesTransferred = 0;

while (chunkSize > 0 && bytesTransferred <= needleLength)
{
chunkSize = stackStream.Read(buffer, 0, buffer.Length);
chunkSize = reader.ReadBlocks(WinFileIO.BlockSize);
bytesTransferred += chunkSize;

if (bytesTransferred <= needleLength)
Expand All @@ -281,9 +326,6 @@ public void Read(Stream stream, int stackNumber, int needleNumber)
stream.Write(buffer, 0, lastChunkSize);
}
}

stream.Flush();
stackStream.Close();
}
}

Expand Down
3 changes: 2 additions & 1 deletion TestHarness/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ static void GeneralTest()

fileNumber++;
}
catch
catch (Exception ex)
{

}
}
}
Expand Down
Binary file modified TestHarness/bin/Debug/Haystacks.dll
Binary file not shown.
Binary file modified TestHarness/bin/Debug/Haystacks.pdb
Binary file not shown.
Binary file modified TestHarness/bin/Debug/TestHarness.exe
Binary file not shown.
Binary file modified TestHarness/bin/Debug/TestHarness.pdb
Binary file not shown.
30 changes: 30 additions & 0 deletions WinFileFlagsAndAttributes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*
* PiSearch - https://github.com/JoshKeegan/PiSearch
* WinFileFlagsAndAttributes - File Flags and attributes used by the Windows API when opening/creating a file
* For a full list of possible values, see http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx
* By Josh Keegan 03/01/2014
*/

namespace Haystacks
{
public enum WinFileFlagsAndAttributes : uint
{
FILE_FLAG_BACKUP_SEMANTICS = 0x02000000,
FILE_FLAG_DELETE_ON_CLOSE = 0x04000000,
FILE_FLAG_NO_BUFFERING = 0x20000000,
FILE_FLAG_OPEN_NO_RECALL = 0x00100000,
FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000,
FILE_FLAG_OVERLAPPED = 0x40000000,
FILE_FLAG_POSIX_SEMANTICS = 0x0100000,
FILE_FLAG_RANDOM_ACCESS = 0x10000000,
FILE_FLAG_SESSION_AWARE = 0x00800000,
FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000,
FILE_FLAG_WRITE_THROUGH = 0x80000000,
}
}
Loading

0 comments on commit 90b3eec

Please sign in to comment.