Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using FASTER.core;
- namespace Marius.Fug
- {
- [StructLayout(LayoutKind.Explicit, Size = 32 * sizeof(char))]
- public unsafe struct FugKey
- {
- [FieldOffset(0)]
- public byte Data;
- }
- [StructLayout(LayoutKind.Explicit, Size = 96 * sizeof(char))]
- public unsafe struct FugValue
- {
- [FieldOffset(0)]
- public byte Data;
- }
- public unsafe class FugFunctions : IFunctions<FugKey, FugValue, string, string, Empty>
- {
- public void SingleReader(ref FugKey key, ref string input, ref FugValue value, ref string dst)
- {
- fixed (byte* data = &value.Data)
- dst = new string((char*)data, 0, 96);
- }
- public void ConcurrentReader(ref FugKey key, ref string input, ref FugValue value, ref string dst)
- {
- fixed (byte* data = &value.Data)
- dst = new string((char*)data, 0, 96);
- }
- public void SingleWriter(ref FugKey key, ref FugValue src, ref FugValue dst)
- {
- fixed (byte* source = &src.Data, destination = &dst.Data)
- Buffer.MemoryCopy(source, destination, 96 * sizeof(char), 96 * sizeof(char));
- }
- public void ConcurrentWriter(ref FugKey key, ref FugValue src, ref FugValue dst)
- {
- fixed (byte* source = &src.Data, destination = &dst.Data)
- Buffer.MemoryCopy(source, destination, 96 * sizeof(char), 96 * sizeof(char));
- }
- public void CheckpointCompletionCallback(Guid sessionId, long serialNum)
- {
- }
- public void CopyUpdater(ref FugKey key, ref string input, ref FugValue oldValue, ref FugValue newValue)
- {
- }
- public void DeleteCompletionCallback(ref FugKey key, Empty ctx)
- {
- }
- public void InitialUpdater(ref FugKey key, ref string input, ref FugValue value)
- {
- }
- public void InPlaceUpdater(ref FugKey key, ref string input, ref FugValue value)
- {
- }
- public void ReadCompletionCallback(ref FugKey key, ref string input, ref string output, Empty ctx, Status status)
- {
- }
- public void RMWCompletionCallback(ref FugKey key, ref string input, Empty ctx, Status status)
- {
- }
- public void UpsertCompletionCallback(ref FugKey key, ref FugValue value, Empty ctx)
- {
- }
- }
- public unsafe class FugEqualityComparer : IFasterEqualityComparer<FugKey>
- {
- public bool Equals(ref FugKey k1, ref FugKey k2)
- {
- fixed (byte* pk1 = &k1.Data, pk2 = &k2.Data)
- {
- for (var i = 0; i < 32 * sizeof(char); i++)
- {
- var left = *(pk1 + i);
- var right = *(pk2 + i);
- if (left != right)
- return false;
- }
- }
- return true;
- }
- public long GetHashCode64(ref FugKey k)
- {
- fixed (byte* data = &k.Data)
- return Utility.HashBytes(data, 32 * sizeof(char));
- }
- }
- public class Program
- {
- public static void Main(string[] args)
- {
- var mainLogPath = Path.Combine(Directory.GetCurrentDirectory(), @"logs\main.hlog");
- var checkpointPath = Path.Combine(Directory.GetCurrentDirectory(), @"logs");
- var isSecond = Directory.Exists(checkpointPath);
- var mainLog = Devices.CreateLogDevice(mainLogPath);
- var faster = new FasterKV<FugKey, FugValue, string, string, Empty, FugFunctions>(
- 1 << 21,
- new FugFunctions(),
- new LogSettings() { LogDevice = mainLog },
- new CheckpointSettings() { CheckpointDir = checkpointPath, CheckPointType = CheckpointType.Snapshot },
- null,
- new FugEqualityComparer(),
- null);
- if (isSecond)
- faster.Recover();
- faster.StartSession();
- var key = new FugKey();
- var value = new FugValue();
- CreateKey(ref key, "SomeKey");
- CreateValue(ref value, "SomeValue");
- faster.Upsert(ref key, ref value, Empty.Default, 0);
- var input = default(string);
- var output = default(string);
- faster.Read(ref key, ref input, ref output, Empty.Default, 0);
- faster.TakeFullCheckpoint(out var _);
- faster.CompleteCheckpoint(true);
- faster.StopSession();
- faster.Dispose();
- mainLog.Close();
- }
- private unsafe static void CreateValue(ref FugValue value, string v)
- {
- fixed (byte* ptr = &value.Data)
- {
- var data = (char*)ptr;
- for (var i = 0; i < 96; i++)
- {
- if (i < v.Length)
- *(data + i) = v[i];
- else
- *(data + i) = '\0';
- }
- }
- }
- private unsafe static void CreateKey(ref FugKey key, string v)
- {
- fixed (byte* ptr = &key.Data)
- {
- var data = (char*)ptr;
- for (var i = 0; i < 32; i++)
- {
- if (i < v.Length)
- *(data + i) = v[i];
- else
- *(data + i) = '\0';
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement