Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using FASTER.core;
  8.  
  9. namespace Marius.Fug
  10. {
  11. public struct FugObject
  12. {
  13. public string Value;
  14. }
  15.  
  16. public class FugSerializer : BinaryObjectSerializer<FugObject>
  17. {
  18. public override void Deserialize(ref FugObject obj)
  19. {
  20. obj.Value = reader.ReadString();
  21. }
  22.  
  23. public override void Serialize(ref FugObject obj)
  24. {
  25. writer.Write(obj.Value);
  26. }
  27. }
  28.  
  29. public class FugFunctions : IFunctions<FugObject, FugObject, string, string, Empty>
  30. {
  31. public void SingleReader(ref FugObject key, ref string input, ref FugObject value, ref string dst)
  32. {
  33. dst = value.Value;
  34. }
  35.  
  36. public void ConcurrentReader(ref FugObject key, ref string input, ref FugObject value, ref string dst)
  37. {
  38. dst = value.Value;
  39. }
  40.  
  41. public void SingleWriter(ref FugObject key, ref FugObject src, ref FugObject dst)
  42. {
  43. dst.Value = src.Value;
  44. }
  45.  
  46. public void ConcurrentWriter(ref FugObject key, ref FugObject src, ref FugObject dst)
  47. {
  48. dst.Value = src.Value;
  49. }
  50.  
  51. public void CheckpointCompletionCallback(Guid sessionId, long serialNum)
  52. {
  53. }
  54.  
  55. public void CopyUpdater(ref FugObject key, ref string input, ref FugObject oldValue, ref FugObject newValue)
  56. {
  57. }
  58.  
  59. public void DeleteCompletionCallback(ref FugObject key, Empty ctx)
  60. {
  61. }
  62.  
  63. public void InitialUpdater(ref FugObject key, ref string input, ref FugObject value)
  64. {
  65. }
  66.  
  67. public void InPlaceUpdater(ref FugObject key, ref string input, ref FugObject value)
  68. {
  69. }
  70.  
  71. public void ReadCompletionCallback(ref FugObject key, ref string input, ref string output, Empty ctx, Status status)
  72. {
  73. }
  74.  
  75. public void RMWCompletionCallback(ref FugObject key, ref string input, Empty ctx, Status status)
  76. {
  77. }
  78.  
  79. public void UpsertCompletionCallback(ref FugObject key, ref FugObject value, Empty ctx)
  80. {
  81. }
  82. }
  83.  
  84. public class FugEqualityComparer : IFasterEqualityComparer<FugObject>
  85. {
  86. public bool Equals(ref FugObject k1, ref FugObject k2)
  87. {
  88. if (k1.Value == null && k2.Value == null)
  89. return true;
  90.  
  91. if (k1.Value == null || k2.Value == null)
  92. return false;
  93.  
  94. return StringComparer.Ordinal.Compare(k1.Value, k2.Value) == 0;
  95. }
  96.  
  97. public long GetHashCode64(ref FugObject k)
  98. {
  99. var hashCode = StringComparer.Ordinal.GetHashCode(k.Value);
  100. return Utility.GetHashCode(hashCode);
  101. }
  102. }
  103.  
  104. public class Program
  105. {
  106. public static void Main(string[] args)
  107. {
  108. var mainLogPath = Path.Combine(Directory.GetCurrentDirectory(), @"logs\main.hlog");
  109. var objectLogPath = Path.Combine(Directory.GetCurrentDirectory(), @"logs\object.hlog");
  110. var checkpointPath = Path.Combine(Directory.GetCurrentDirectory(), @"logs");
  111.  
  112. var mainLog = Devices.CreateLogDevice(mainLogPath);
  113. var objectLog = Devices.CreateLogDevice(objectLogPath);
  114. var faster = new FasterKV<FugObject, FugObject, string, string, Empty, FugFunctions>(
  115. 1 << 21,
  116. new FugFunctions(),
  117. new LogSettings() { LogDevice = mainLog, ObjectLogDevice = objectLog },
  118. new CheckpointSettings() { CheckpointDir = checkpointPath, CheckPointType = CheckpointType.Snapshot },
  119. new SerializerSettings<FugObject, FugObject>() { keySerializer = () => new FugSerializer(), valueSerializer = () => new FugSerializer() },
  120. new FugEqualityComparer(),
  121. null);
  122.  
  123. faster.StartSession();
  124.  
  125. var valueString = "3.14159265359";
  126. var sequence = 0L;
  127. for (var i = 0; i < 4000000; i++)
  128. {
  129. var key = new FugObject() { Value = i.ToString() };
  130. var value = new FugObject() { Value = valueString };
  131.  
  132. faster.Upsert(ref key, ref value, Empty.Default, sequence);
  133. sequence++;
  134. }
  135.  
  136. faster.TakeFullCheckpoint(out var _);
  137. faster.CompleteCheckpoint(true); // <- exception here
  138.  
  139. faster.StopSession();
  140. faster.Dispose();
  141.  
  142. objectLog.Close();
  143. mainLog.Close();
  144. }
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement