storage.zig 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. const std = @import("std");
  2. const index = @import("index.zig");
  3. const hashing = @import("hashing.zig");
  4. const persistence = @import("persistence.zig");
  5. const INITIAL_BUCKETS = 1_048_576;
  6. const Entry = struct {
  7. key: []const u8,
  8. value: []const u8,
  9. hash: u32,
  10. next: ?*Entry,
  11. };
  12. var buckets: []?*Entry = undefined;
  13. var buckets_initialized: bool = false;
  14. var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
  15. const allocator = arena.allocator();
  16. var rwlock: std.Thread.RwLock = .{};
  17. var init_mutex: std.Thread.Mutex = .{};
  18. pub fn init() void {
  19. if (buckets_initialized) return;
  20. init_mutex.lock();
  21. defer init_mutex.unlock();
  22. if (!buckets_initialized) {
  23. buckets = allocator.alloc(?*Entry, INITIAL_BUCKETS) catch unreachable;
  24. @memset(buckets, null);
  25. buckets_initialized = true;
  26. }
  27. }
  28. pub fn restore(key: []const u8, value: []const u8) bool {
  29. rwlock.lock();
  30. defer rwlock.unlock();
  31. const entry = writeVolatile(key, value);
  32. if (entry != null) {
  33. index.insert(entry.?.key);
  34. return true;
  35. }
  36. return false;
  37. }
  38. pub fn restoreDelete(key: []const u8) bool {
  39. rwlock.lock();
  40. defer rwlock.unlock();
  41. const deleted = deleteVolatile(key);
  42. if (deleted) {
  43. index.delete(key);
  44. return true;
  45. }
  46. return false;
  47. }
  48. pub fn writeVolatile(key: []const u8, value: []const u8) ?*Entry {
  49. const hash = hashing.hashKey(key);
  50. const bucketIdx = hash % buckets.len;
  51. var current = buckets[bucketIdx];
  52. while (current) |entry| {
  53. if (entry.hash == hash and std.mem.eql(u8, entry.key, key)) {
  54. allocator.free(entry.value);
  55. entry.value = allocator.dupe(u8, value) catch return null;
  56. return entry;
  57. }
  58. current = entry.next;
  59. }
  60. const newEntry = allocator.create(Entry) catch return null;
  61. errdefer allocator.destroy(newEntry);
  62. newEntry.* = Entry{
  63. .key = allocator.dupe(u8, key) catch return null,
  64. .value = allocator.dupe(u8, value) catch return null,
  65. .hash = hash, // Cache hash value
  66. .next = buckets[bucketIdx],
  67. };
  68. buckets[bucketIdx] = newEntry;
  69. return newEntry;
  70. }
  71. pub fn write(key: []const u8, value: []const u8) bool {
  72. rwlock.lock();
  73. defer rwlock.unlock();
  74. const entry = writeVolatile(key, value);
  75. if (entry == null) {
  76. return false;
  77. }
  78. index.insert(entry.?.key);
  79. persistence.persist('W', entry.?.key, entry.?.value);
  80. return true;
  81. }
  82. pub fn read(key: []const u8) ?[]const u8 {
  83. rwlock.lockShared();
  84. defer rwlock.unlockShared();
  85. if (!buckets_initialized) return null;
  86. const hash = hashing.hashKey(key);
  87. var current = buckets[hash % buckets.len];
  88. while (current) |entry| {
  89. if (entry.hash == hash and std.mem.eql(u8, entry.key, key)) {
  90. return entry.value;
  91. }
  92. current = entry.next;
  93. }
  94. return null;
  95. }
  96. pub fn deleteVolatile(key: []const u8) bool {
  97. if (!buckets_initialized) return false;
  98. const hash = hashing.hashKey(key);
  99. const bucketIdx = hash % buckets.len;
  100. var current = buckets[bucketIdx];
  101. var prev: ?*Entry = null;
  102. while (current) |entry| {
  103. if (entry.hash == hash and std.mem.eql(u8, entry.key, key)) {
  104. if (prev) |p| {
  105. p.next = entry.next;
  106. } else {
  107. buckets[bucketIdx] = entry.next;
  108. }
  109. allocator.free(entry.key);
  110. allocator.free(entry.value);
  111. allocator.destroy(entry);
  112. return true;
  113. }
  114. prev = entry;
  115. current = entry.next;
  116. }
  117. return false;
  118. }
  119. pub fn delete(key: []const u8) bool {
  120. rwlock.lock();
  121. defer rwlock.unlock();
  122. const deleted = deleteVolatile(key);
  123. if (deleted) {
  124. index.delete(key);
  125. persistence.persist('D', key, "");
  126. }
  127. return deleted;
  128. }