Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using FASTER.core;
  9.  
  10. namespace Marius.Fug
  11. {
  12. [StructLayout(LayoutKind.Explicit, Size = 32 * sizeof(char))]
  13. public unsafe struct FugKey
  14. {
  15. [FieldOffset(0)]
  16. public byte Data;
  17. }
  18.  
  19. [StructLayout(LayoutKind.Explicit, Size = 96 * sizeof(char))]
  20. public unsafe struct FugValue
  21. {
  22. [FieldOffset(0)]
  23. public byte Data;
  24. }
  25.  
  26. public unsafe class FugFunctions : IFunctions<FugKey, FugValue, string, string, Empty>
  27. {
  28. public void SingleReader(ref FugKey key, ref string input, ref FugValue value, ref string dst)
  29. {
  30. fixed (byte* data = &value.Data)
  31. dst = new string((char*)data, 0, 96);
  32. }
  33.  
  34. public void ConcurrentReader(ref FugKey key, ref string input, ref FugValue value, ref string dst)
  35. {
  36. fixed (byte* data = &value.Data)
  37. dst = new string((char*)data, 0, 96);
  38. }
  39.  
  40. public void SingleWriter(ref FugKey key, ref FugValue src, ref FugValue dst)
  41. {
  42. fixed (byte* source = &src.Data, destination = &dst.Data)
  43. Buffer.MemoryCopy(source, destination, 96 * sizeof(char), 96 * sizeof(char));
  44. }
  45.  
  46. public void ConcurrentWriter(ref FugKey key, ref FugValue src, ref FugValue dst)
  47. {
  48. fixed (byte* source = &src.Data, destination = &dst.Data)
  49. Buffer.MemoryCopy(source, destination, 96 * sizeof(char), 96 * sizeof(char));
  50. }
  51.  
  52. public void CheckpointCompletionCallback(Guid sessionId, long serialNum)
  53. {
  54. }
  55.  
  56. public void CopyUpdater(ref FugKey key, ref string input, ref FugValue oldValue, ref FugValue newValue)
  57. {
  58. }
  59.  
  60. public void DeleteCompletionCallback(ref FugKey key, Empty ctx)
  61. {
  62. }
  63.  
  64. public void InitialUpdater(ref FugKey key, ref string input, ref FugValue value)
  65. {
  66. }
  67.  
  68. public void InPlaceUpdater(ref FugKey key, ref string input, ref FugValue value)
  69. {
  70. }
  71.  
  72. public void ReadCompletionCallback(ref FugKey key, ref string input, ref string output, Empty ctx, Status status)
  73. {
  74. }
  75.  
  76. public void RMWCompletionCallback(ref FugKey key, ref string input, Empty ctx, Status status)
  77. {
  78. }
  79.  
  80. public void UpsertCompletionCallback(ref FugKey key, ref FugValue value, Empty ctx)
  81. {
  82. }
  83. }
  84.  
  85. public unsafe class FugEqualityComparer : IFasterEqualityComparer<FugKey>
  86. {
  87. public bool Equals(ref FugKey k1, ref FugKey k2)
  88. {
  89. fixed (byte* pk1 = &k1.Data, pk2 = &k2.Data)
  90. {
  91. for (var i = 0; i < 32 * sizeof(char); i++)
  92. {
  93. var left = *(pk1 + i);
  94. var right = *(pk2 + i);
  95. if (left != right)
  96. return false;
  97. }
  98. }
  99.  
  100. return true;
  101. }
  102.  
  103. public long GetHashCode64(ref FugKey k)
  104. {
  105. fixed (byte* data = &k.Data)
  106. return Utility.HashBytes(data, 32 * sizeof(char));
  107. }
  108. }
  109.  
  110. public class Program
  111. {
  112. public static void Main(string[] args)
  113. {
  114. var mainLogPath = Path.Combine(Directory.GetCurrentDirectory(), @"logs\main.hlog");
  115. var checkpointPath = Path.Combine(Directory.GetCurrentDirectory(), @"logs");
  116.  
  117. var isSecond = Directory.Exists(checkpointPath);
  118.  
  119. var mainLog = Devices.CreateLogDevice(mainLogPath);
  120. var faster = new FasterKV<FugKey, FugValue, string, string, Empty, FugFunctions>(
  121. 1 << 21,
  122. new FugFunctions(),
  123. new LogSettings() { LogDevice = mainLog },
  124. new CheckpointSettings() { CheckpointDir = checkpointPath, CheckPointType = CheckpointType.Snapshot },
  125. null,
  126. new FugEqualityComparer(),
  127. null);
  128.  
  129. if (isSecond)
  130. faster.Recover();
  131.  
  132. faster.StartSession();
  133.  
  134. var key = new FugKey();
  135. var value = new FugValue();
  136.  
  137. CreateKey(ref key, "SomeKey");
  138. CreateValue(ref value, "SomeValue");
  139.  
  140. faster.Upsert(ref key, ref value, Empty.Default, 0);
  141.  
  142. var input = default(string);
  143. var output = default(string);
  144.  
  145. faster.Read(ref key, ref input, ref output, Empty.Default, 0);
  146.  
  147. faster.TakeFullCheckpoint(out var _);
  148. faster.CompleteCheckpoint(true);
  149.  
  150. faster.StopSession();
  151.  
  152. faster.Dispose();
  153.  
  154. mainLog.Close();
  155. }
  156.  
  157. private unsafe static void CreateValue(ref FugValue value, string v)
  158. {
  159. fixed (byte* ptr = &value.Data)
  160. {
  161. var data = (char*)ptr;
  162. for (var i = 0; i < 96; i++)
  163. {
  164. if (i < v.Length)
  165. *(data + i) = v[i];
  166. else
  167. *(data + i) = '\0';
  168. }
  169. }
  170. }
  171.  
  172. private unsafe static void CreateKey(ref FugKey key, string v)
  173. {
  174. fixed (byte* ptr = &key.Data)
  175. {
  176. var data = (char*)ptr;
  177. for (var i = 0; i < 32; i++)
  178. {
  179. if (i < v.Length)
  180. *(data + i) = v[i];
  181. else
  182. *(data + i) = '\0';
  183. }
  184. }
  185. }
  186. }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement