99 lines
2.7 KiB
C#
99 lines
2.7 KiB
C#
using FASTER.core;
|
||
|
||
// 定义数据结构
|
||
public struct MyKey : IEquatable<MyKey>
|
||
{
|
||
public int Id;
|
||
public bool Equals(MyKey other) => Id == other.Id;
|
||
public override int GetHashCode() => Id.GetHashCode();
|
||
}
|
||
|
||
public struct MyValue
|
||
{
|
||
public byte[] Data;
|
||
public DateTime Timestamp;
|
||
}
|
||
|
||
public struct MyInput
|
||
{
|
||
public byte[] NewData;
|
||
}
|
||
|
||
public struct MyOutput
|
||
{
|
||
public MyValue Value;
|
||
public bool Updated;
|
||
}
|
||
|
||
public struct MyContext
|
||
{
|
||
public string OperationId;
|
||
public DateTime StartTime;
|
||
}
|
||
|
||
// 实现自定义 Functions
|
||
public class MyCustomFunctions : FunctionsBase<MyKey, MyValue, MyInput, MyOutput, MyContext>
|
||
{
|
||
// 读取完成回调
|
||
public override void ReadCompletionCallback(ref MyKey key, ref MyInput input, ref MyOutput output, MyContext ctx, Status status, RecordMetadata recordMetadata)
|
||
{
|
||
base.ReadCompletionCallback(ref key, ref input, ref output, ctx, status, recordMetadata);
|
||
}
|
||
|
||
|
||
//// 写入完成回调
|
||
//public override void UpsertCompletionCallback(
|
||
// ref MyKey key, ref MyValue value, MyContext ctx)
|
||
//{
|
||
// Console.WriteLine($"Upsert completed for key {key.Id}");
|
||
//}
|
||
|
||
//// 删除完成回调
|
||
//public override void DeleteCompletionCallback(
|
||
// ref MyKey key, MyContext ctx)
|
||
//{
|
||
// Console.WriteLine($"Delete completed for key {key.Id}");
|
||
//}
|
||
|
||
//// RMW (Read-Modify-Write) 操作 - 核心功能!
|
||
//public override void RMWCompletionCallback(
|
||
// ref MyKey key, ref MyInput input, ref MyOutput output,
|
||
// MyContext ctx, Status status)
|
||
//{
|
||
// if (status == Status.OK)
|
||
// {
|
||
// Console.WriteLine($"RMW completed for key {key.Id}");
|
||
// }
|
||
//}
|
||
|
||
//// 并发控制:当多个线程同时修改相同键时调用
|
||
//public override bool ConcurrentWriter(
|
||
// ref MyKey key, ref MyValue value, ref MyValue newValue)
|
||
//{
|
||
// // 返回 true 表示接受新值,false 表示拒绝
|
||
// return newValue.Timestamp > value.Timestamp;
|
||
//}
|
||
|
||
//// 读取时复制:允许在读取时修改返回值
|
||
//public override bool SingleReader(
|
||
// ref MyKey key, ref MyInput input, ref MyValue value, ref MyOutput dst)
|
||
//{
|
||
// dst.Value = value;
|
||
// dst.Updated = false;
|
||
// return true;
|
||
//}
|
||
|
||
//// 写入时处理:自定义写入逻辑
|
||
//public override bool ConcurrentWriter(
|
||
// ref MyKey key, ref MyValue value, ref MyValue newValue)
|
||
//{
|
||
// // 只有新数据的时间戳更晚时才更新
|
||
// if (newValue.Timestamp > value.Timestamp)
|
||
// {
|
||
// value = newValue;
|
||
// return true;
|
||
// }
|
||
// return false;
|
||
//}
|
||
}
|