-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sample to show use of FASTER with fixed-length structs (#160)
* Sample to show use of FASTER with fixed-length struct keys and values, of size known at compile time
- Loading branch information
Showing
7 changed files
with
338 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" /> | ||
</startup> | ||
</configuration> |
38 changes: 38 additions & 0 deletions
38
cs/playground/FixedLenStructSample/FixedLenStructSample.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net46</TargetFramework> | ||
<Platforms>x64</Platforms> | ||
<RuntimeIdentifier>win7-x64</RuntimeIdentifier> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<RootNamespace>StructSample</RootNamespace> | ||
<ErrorReport>prompt</ErrorReport> | ||
<RestoreProjectStyle>PackageReference</RestoreProjectStyle> | ||
<Prefer32Bit>true</Prefer32Bit> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)' == 'Debug'"> | ||
<DefineConstants>TRACE;DEBUG</DefineConstants> | ||
<DebugType>full</DebugType> | ||
<DebugSymbols>true</DebugSymbols> | ||
<OutputPath>bin\x64\Debug\</OutputPath> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)' == 'Release'"> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\x64\Release\</OutputPath> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="App.config" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\core\FASTER.core.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using FASTER.core; | ||
using System; | ||
|
||
namespace FixedLenStructSample | ||
{ | ||
/// <summary> | ||
/// Callback functions for FASTER operations | ||
/// </summary> | ||
public class FixedLenFunctions : IFunctions<FixedLenKey, FixedLenValue, string, string, Empty> | ||
{ | ||
public void CheckpointCompletionCallback(Guid sessionId, long serialNum) | ||
{ | ||
} | ||
|
||
public void ConcurrentReader(ref FixedLenKey key, ref string input, ref FixedLenValue value, ref string dst) | ||
{ | ||
dst = value.ToString(); | ||
} | ||
|
||
public void ConcurrentWriter(ref FixedLenKey key, ref FixedLenValue src, ref FixedLenValue dst) | ||
{ | ||
src.CopyTo(ref dst); | ||
} | ||
|
||
public void CopyUpdater(ref FixedLenKey key, ref string input, ref FixedLenValue oldValue, ref FixedLenValue newValue) | ||
{ | ||
} | ||
|
||
public void DeleteCompletionCallback(ref FixedLenKey key, Empty ctx) | ||
{ | ||
} | ||
|
||
public void InitialUpdater(ref FixedLenKey key, ref string input, ref FixedLenValue value) | ||
{ | ||
} | ||
|
||
public void InPlaceUpdater(ref FixedLenKey key, ref string input, ref FixedLenValue value) | ||
{ | ||
} | ||
|
||
public void ReadCompletionCallback(ref FixedLenKey key, ref string input, ref string output, Empty ctx, Status status) | ||
{ | ||
} | ||
|
||
public void RMWCompletionCallback(ref FixedLenKey key, ref string input, Empty ctx, Status status) | ||
{ | ||
} | ||
|
||
public void SingleReader(ref FixedLenKey key, ref string input, ref FixedLenValue value, ref string dst) | ||
{ | ||
dst = value.ToString(); | ||
} | ||
|
||
public void SingleWriter(ref FixedLenKey key, ref FixedLenValue src, ref FixedLenValue dst) | ||
{ | ||
src.CopyTo(ref dst); | ||
} | ||
|
||
public void UpsertCompletionCallback(ref FixedLenKey key, ref FixedLenValue value, Empty ctx) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using FASTER.core; | ||
using System; | ||
|
||
namespace FixedLenStructSample | ||
{ | ||
public class Program | ||
{ | ||
// This sample uses fixed length structs for keys and values | ||
static void Main() | ||
{ | ||
var log = Devices.CreateLogDevice("hlog.log", deleteOnClose: true); | ||
var fht = new FasterKV<FixedLenKey, FixedLenValue, string, string, Empty, FixedLenFunctions> | ||
(128, new FixedLenFunctions(), | ||
new LogSettings { LogDevice = log, MemorySizeBits = 17, PageSizeBits = 12 } | ||
); | ||
fht.StartSession(); | ||
|
||
var key = new FixedLenKey("foo"); | ||
var value = new FixedLenValue("bar"); | ||
|
||
var status = fht.Upsert(ref key, ref value, Empty.Default, 0); | ||
|
||
if (status != Status.OK) | ||
Console.WriteLine("FixedLenStructSample: Error!"); | ||
|
||
var input = default(string); // unused | ||
var output = default(string); | ||
|
||
key = new FixedLenKey("xyz"); | ||
status = fht.Read(ref key, ref input, ref output, Empty.Default, 0); | ||
|
||
if (status != Status.NOTFOUND) | ||
Console.WriteLine("FixedLenStructSample: Error!"); | ||
|
||
key = new FixedLenKey("foo"); | ||
status = fht.Read(ref key, ref input, ref output, Empty.Default, 0); | ||
|
||
if (status != Status.OK) | ||
Console.WriteLine("FixedLenStructSample: Error!"); | ||
|
||
if (output.Equals(value.ToString())) | ||
Console.WriteLine("FixedLenStructSample: Success!"); | ||
else | ||
Console.WriteLine("FixedLenStructSample: Error!"); | ||
|
||
fht.StopSession(); | ||
fht.Dispose(); | ||
log.Close(); | ||
|
||
Console.WriteLine("Press <ENTER> to end"); | ||
Console.ReadLine(); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
cs/playground/FixedLenStructSample/Properties/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyCopyright("Copyright © 2019")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("17bdd0a5-98e5-464a-8a00-050d9ff4c562")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using FASTER.core; | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace FixedLenStructSample | ||
{ | ||
/// <summary> | ||
/// Represents a fixed length struct type (Length = 32) | ||
/// </summary> | ||
[StructLayout(LayoutKind.Explicit, Size = Length)] | ||
public unsafe struct FixedLenKey : IFasterEqualityComparer<FixedLenKey> | ||
{ | ||
private const int Length = 32; | ||
|
||
[FieldOffset(0)] | ||
private byte data; | ||
|
||
public FixedLenKey(string v) | ||
{ | ||
if (Length < 2 * (v.Length + 1)) | ||
throw new Exception("Insufficient space to store string"); | ||
|
||
fixed (byte* ptr = &data) | ||
{ | ||
var data = (char*)ptr; | ||
for (var i = 0; i < Length / sizeof(char); i++) | ||
{ | ||
if (i < v.Length) | ||
*(data + i) = v[i]; | ||
else | ||
*(data + i) = '\0'; | ||
} | ||
} | ||
} | ||
|
||
public void CopyTo(ref FixedLenKey dst) | ||
{ | ||
fixed (byte* source = &data, destination = &dst.data) | ||
Buffer.MemoryCopy(source, destination, Length, Length); | ||
} | ||
|
||
public bool Equals(ref FixedLenKey k1, ref FixedLenKey k2) | ||
{ | ||
fixed (byte* pk1 = &k1.data, pk2 = &k2.data) | ||
{ | ||
for (var i = 0; i < Length; i++) | ||
{ | ||
var left = *(pk1 + i); | ||
var right = *(pk2 + i); | ||
if (left != right) | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public long GetHashCode64(ref FixedLenKey k) | ||
{ | ||
fixed (byte* data = &k.data) | ||
return Utility.HashBytes(data, Length); | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
fixed (byte* ptr = &data) | ||
return new string((char*)ptr); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Represents a fixed length struct type (Length = 64) | ||
/// </summary> | ||
[StructLayout(LayoutKind.Explicit, Size = Length)] | ||
public unsafe struct FixedLenValue : IFasterEqualityComparer<FixedLenValue> | ||
{ | ||
private const int Length = 64; | ||
|
||
[FieldOffset(0)] | ||
private byte data; | ||
|
||
public FixedLenValue(string v) | ||
{ | ||
if (Length < 2 * (v.Length + 1)) | ||
throw new Exception("Insufficient space to store string"); | ||
|
||
fixed (byte* ptr = &data) | ||
{ | ||
var data = (char*)ptr; | ||
for (var i = 0; i < Length / sizeof(char); i++) | ||
{ | ||
if (i < v.Length) | ||
*(data + i) = v[i]; | ||
else | ||
*(data + i) = '\0'; | ||
} | ||
} | ||
} | ||
|
||
public void CopyTo(ref FixedLenValue dst) | ||
{ | ||
fixed (byte* source = &data, destination = &dst.data) | ||
Buffer.MemoryCopy(source, destination, Length, Length); | ||
} | ||
|
||
public bool Equals(ref FixedLenValue k1, ref FixedLenValue k2) | ||
{ | ||
fixed (byte* pk1 = &k1.data, pk2 = &k2.data) | ||
{ | ||
for (var i = 0; i < Length; i++) | ||
{ | ||
var left = *(pk1 + i); | ||
var right = *(pk2 + i); | ||
if (left != right) | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public long GetHashCode64(ref FixedLenValue k) | ||
{ | ||
fixed (byte* data = &k.data) | ||
return Utility.HashBytes(data, Length); | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
fixed (byte* ptr = &data) | ||
return new string((char*)ptr); | ||
} | ||
} | ||
} |