зеркало из https://github.com/microsoft/FASTER.git
Updated simple example
This commit is contained in:
Родитель
a3645815d4
Коммит
ec9bf7d25a
|
@ -6,51 +6,46 @@ using System;
|
|||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace StructSampleCore
|
||||
namespace StructSample
|
||||
{
|
||||
/// <summary>
|
||||
/// Callback functions for FASTER operations
|
||||
/// </summary>
|
||||
public class Functions : IFunctions<Key, Value, Input, Output, Empty>
|
||||
public class Sample1Funcs : IFunctions<long, long, long, long, Empty>
|
||||
{
|
||||
public void RMWCompletionCallback(ref Key key, ref Input output, Empty ctx, Status status)
|
||||
{
|
||||
}
|
||||
|
||||
public void ReadCompletionCallback(ref Key key, ref Input input, ref Output output, Empty ctx, Status status)
|
||||
{
|
||||
}
|
||||
|
||||
public void UpsertCompletionCallback(ref Key key, ref Value output, Empty ctx)
|
||||
{
|
||||
}
|
||||
|
||||
public void CheckpointCompletionCallback(Guid sessionId, long serialNum)
|
||||
{
|
||||
Debug.WriteLine("Session {0} reports persistence until {1}", sessionId, serialNum);
|
||||
}
|
||||
|
||||
// Read functions
|
||||
public void SingleReader(ref Key key, ref Input input, ref Value value, ref Output dst)
|
||||
{
|
||||
dst.value = value;
|
||||
}
|
||||
public void SingleReader(ref long key, ref long input, ref long value, ref long dst) => dst = value;
|
||||
public void ConcurrentReader(ref long key, ref long input, ref long value, ref long dst) => dst = value;
|
||||
|
||||
public void ConcurrentReader(ref Key key, ref Input input, ref Value value, ref Output dst)
|
||||
{
|
||||
dst.value = value;
|
||||
}
|
||||
// Write functions
|
||||
public void SingleWriter(ref long key, ref long src, ref long dst) => dst = src;
|
||||
public void ConcurrentWriter(ref long key, ref long src, ref long dst) => dst = src;
|
||||
|
||||
// Upsert functions
|
||||
public void SingleWriter(ref Key key, ref Value src, ref Value dst)
|
||||
{
|
||||
dst = src;
|
||||
}
|
||||
// RMW functions
|
||||
public void InitialUpdater(ref long key, ref long input, ref long value) => value = input;
|
||||
public void CopyUpdater(ref long key, ref long input, ref long oldv, ref long newv) => newv = oldv + input;
|
||||
public void InPlaceUpdater(ref long key, ref long input, ref long value) => value += input;
|
||||
|
||||
public void ConcurrentWriter(ref Key key, ref Value src, ref Value dst)
|
||||
{
|
||||
dst = src;
|
||||
}
|
||||
// Completion callbacks
|
||||
public void ReadCompletionCallback(ref long key, ref long input, ref long output, Empty ctx, Status s) { }
|
||||
public void UpsertCompletionCallback(ref long key, ref long value, Empty ctx) { }
|
||||
public void RMWCompletionCallback(ref long key, ref long input, Empty ctx, Status s) { }
|
||||
public void CheckpointCompletionCallback(Guid sessionId, long serialNum) { }
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Callback functions for FASTER operations
|
||||
/// </summary>
|
||||
public class Sample2Funcs : IFunctions<Key, Value, Input, Output, Empty>
|
||||
{
|
||||
// Read functions
|
||||
public void SingleReader(ref Key key, ref Input input, ref Value value, ref Output dst) => dst.value = value;
|
||||
public void ConcurrentReader(ref Key key, ref Input input, ref Value value, ref Output dst) => dst.value = value;
|
||||
|
||||
// Write functions
|
||||
public void SingleWriter(ref Key key, ref Value src, ref Value dst) => dst = src;
|
||||
public void ConcurrentWriter(ref Key key, ref Value src, ref Value dst) => dst = src;
|
||||
|
||||
// RMW functions
|
||||
public void InitialUpdater(ref Key key, ref Input input, ref Value value)
|
||||
|
@ -58,17 +53,21 @@ namespace StructSampleCore
|
|||
value.vfield1 = input.ifield1;
|
||||
value.vfield2 = input.ifield2;
|
||||
}
|
||||
|
||||
public void CopyUpdater(ref Key key, ref Input input, ref Value oldValue, ref Value newValue)
|
||||
{
|
||||
newValue.vfield1 = oldValue.vfield1 + input.ifield1;
|
||||
newValue.vfield2 = oldValue.vfield2 + input.ifield2;
|
||||
}
|
||||
public void InPlaceUpdater(ref Key key, ref Input input, ref Value value)
|
||||
{
|
||||
value.vfield1 += input.ifield1;
|
||||
value.vfield2 += input.ifield2;
|
||||
}
|
||||
|
||||
public void CopyUpdater(ref Key key, ref Input input, ref Value oldValue, ref Value newValue)
|
||||
{
|
||||
newValue.vfield1 = oldValue.vfield1 + input.ifield1;
|
||||
newValue.vfield2 = oldValue.vfield2 + input.ifield2;
|
||||
}
|
||||
// Completion callbacks
|
||||
public void ReadCompletionCallback(ref Key key, ref Input input, ref Output output, Empty ctx, Status status) { }
|
||||
public void UpsertCompletionCallback(ref Key key, ref Value output, Empty ctx) { }
|
||||
public void RMWCompletionCallback(ref Key key, ref Input output, Empty ctx, Status status) { }
|
||||
public void CheckpointCompletionCallback(Guid sessionId, long serialNum) { }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,16 +3,55 @@
|
|||
|
||||
using FASTER.core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
|
||||
namespace StructSampleCore
|
||||
namespace StructSample
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Sample1();
|
||||
Sample2();
|
||||
Console.WriteLine("Press <ENTER> to end");
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
static void Sample1()
|
||||
{
|
||||
long key = 1, value = 1, input = 10, output = 0;
|
||||
|
||||
// This represents the simplest possible in-memory sample of FASTER
|
||||
// Create temp file (auto-deleted on close) as hybrid log
|
||||
// Can also use null device as Devices.CreateLogDevice("")
|
||||
var log = Devices.CreateLogDevice(Path.GetTempPath() + "hlog.log", false, true);
|
||||
var fht = new FasterKV<long, long, long, long, Empty, Sample1Funcs>
|
||||
(1L << 20, new Sample1Funcs(), new LogSettings { LogDevice = log }, null, null, new LongComparer());
|
||||
|
||||
fht.StartSession();
|
||||
|
||||
|
||||
fht.Upsert(ref key, ref value, Empty.Default, 0);
|
||||
fht.Read(ref key, ref input, ref output, Empty.Default, 0);
|
||||
if (output == value)
|
||||
Console.WriteLine("Sample1: Success!");
|
||||
else
|
||||
Console.WriteLine("Sample1: Error!");
|
||||
|
||||
fht.RMW(ref key, ref input, Empty.Default, 0);
|
||||
fht.RMW(ref key, ref input, Empty.Default, 0);
|
||||
fht.Read(ref key, ref input, ref output, Empty.Default, 0);
|
||||
if (output == value + 2*input)
|
||||
Console.WriteLine("Sample1: Success!");
|
||||
else
|
||||
Console.WriteLine("Sample1: Error!");
|
||||
|
||||
fht.StopSession();
|
||||
fht.Dispose();
|
||||
log.Close();
|
||||
}
|
||||
|
||||
static void Sample2()
|
||||
{
|
||||
// This sample uses "blittable" key and value types, which enables the
|
||||
// "high speed" mode for FASTER. You can override the default key equality
|
||||
|
@ -21,9 +60,10 @@ namespace StructSampleCore
|
|||
// (2) Provide IFasterEqualityComparer<KeyStruct> instance as param to constructor
|
||||
// Serializers are not required for blittable key and value types.
|
||||
|
||||
var fht =
|
||||
new FasterKV<Key, Value, Input, Output, Empty, Functions>
|
||||
(128, new Functions(), new LogSettings { LogDevice = Devices.CreateLogDevice(""), MutableFraction = 0.5 });
|
||||
var fht =
|
||||
new FasterKV<Key, Value, Input, Output, Empty, Sample2Funcs>
|
||||
(1L << 20, new Sample2Funcs(),
|
||||
new LogSettings { LogDevice = Devices.CreateLogDevice("") }); // Use Null device
|
||||
|
||||
fht.StartSession();
|
||||
|
||||
|
@ -37,10 +77,10 @@ namespace StructSampleCore
|
|||
fht.Upsert(ref key1, ref value, Empty.Default, 0);
|
||||
fht.Read(ref key1, ref input, ref output, Empty.Default, 0);
|
||||
|
||||
if ((output.value.vfield1 != value.vfield1) || (output.value.vfield2 != value.vfield2))
|
||||
Console.WriteLine("Error!");
|
||||
if ((output.value.vfield1 == value.vfield1) && (output.value.vfield2 == value.vfield2))
|
||||
Console.WriteLine("Sample2: Success!");
|
||||
else
|
||||
Console.WriteLine("Success!");
|
||||
Console.WriteLine("Sample2: Error!");
|
||||
|
||||
var key2 = new Key { kfield1 = 15, kfield2 = 16 };
|
||||
input = new Input { ifield1 = 25, ifield2 = 26 };
|
||||
|
@ -51,14 +91,13 @@ namespace StructSampleCore
|
|||
fht.RMW(ref key2, ref input, Empty.Default, 0);
|
||||
fht.Read(ref key2, ref input, ref output, Empty.Default, 0);
|
||||
|
||||
if ((output.value.vfield1 != input.ifield1*2) || (output.value.vfield2 != input.ifield2*2))
|
||||
Console.WriteLine("Error!");
|
||||
if ((output.value.vfield1 == input.ifield1 * 2) && (output.value.vfield2 == input.ifield2 * 2))
|
||||
Console.WriteLine("Sample2: Success!");
|
||||
else
|
||||
Console.WriteLine("Success!");
|
||||
Console.WriteLine("Sample2: Error!");
|
||||
|
||||
fht.StopSession();
|
||||
|
||||
Console.ReadLine();
|
||||
fht.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,8 +6,14 @@ using System;
|
|||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace StructSampleCore
|
||||
namespace StructSample
|
||||
{
|
||||
public struct LongComparer : IFasterEqualityComparer<long>
|
||||
{
|
||||
public bool Equals(ref long k1, ref long k2) => k1 == k2;
|
||||
public long GetHashCode64(ref long k) => Utility.GetHashCode(k);
|
||||
}
|
||||
|
||||
public struct Key : IFasterEqualityComparer<Key>
|
||||
{
|
||||
public long kfield1;
|
||||
|
@ -15,7 +21,7 @@ namespace StructSampleCore
|
|||
|
||||
public long GetHashCode64(ref Key key)
|
||||
{
|
||||
return Utility.GetHashCode(key.kfield1);
|
||||
return Utility.GetHashCode(key.kfield1) ^ Utility.GetHashCode(key.kfield2);
|
||||
}
|
||||
public bool Equals(ref Key k1, ref Key k2)
|
||||
{
|
||||
|
|
Загрузка…
Ссылка в новой задаче