-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathhashtable_op_delete.c
319 lines (252 loc) · 11 KB
/
hashtable_op_delete.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/**
* Copyright (C) 2018-2023 Daniele Salvatore Albano
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the BSD license. See the LICENSE file for details.
**/
#include <stdint.h>
#include <stdbool.h>
#include <stdatomic.h>
#include <string.h>
#include <numa.h>
#include "misc.h"
#include "exttypes.h"
#include "memory_fences.h"
#include "xalloc.h"
#include "log/log.h"
#include "spinlock.h"
#include "transaction.h"
#include "hashtable.h"
#include "hashtable_support_index.h"
#include "hashtable_op_delete.h"
#include "hashtable_support_hash.h"
#include "hashtable_support_op.h"
bool hashtable_mcmp_op_delete(
hashtable_t* hashtable,
hashtable_database_number_t database_number,
transaction_t *transaction,
hashtable_key_data_t* key,
hashtable_key_length_t key_length,
hashtable_value_data_t *current_value) {
hashtable_hash_t hash;
hashtable_chunk_index_t chunk_index = 0;
hashtable_chunk_slot_index_t chunk_slot_index = 0;
hashtable_half_hashes_chunk_volatile_t* half_hashes_chunk;
hashtable_key_value_volatile_t* key_value;
bool deleted = false;
// TODO: the deletion algorithm needs to be updated to compact the keys stored in further away slots relying on the
// distance.
// Once a slot has been empty-ed, it has to use the AVX2/AVX instructions if available, if not a simple loop,
// to search if chunks ahead (within the overflowed_chunks_counter margin) contain keys that can be moved
// back to fill the slot.
// At the end it has to update the original over overflowed_chunks_counter to restrict the search range if
// there was any compaction.
hash = hashtable_mcmp_support_hash_calculate(database_number, key, key_length);
LOG_DI("key (%d) = %s", key_length, key);
LOG_DI("hash = 0x%016x", hash);
volatile hashtable_data_t* hashtable_data_list[] = {
hashtable->ht_current,
hashtable->ht_old
};
uint8_t hashtable_data_list_size = 2;
for (
uint8_t hashtable_data_index = 0;
hashtable_data_index < hashtable_data_list_size && deleted == false;
hashtable_data_index++) {
volatile hashtable_data_t *hashtable_data = hashtable_data_list[hashtable_data_index];
LOG_DI("hashtable_data_index = %u", hashtable_data_index);
LOG_DI("hashtable_data = 0x%016x", hashtable_data);
if (hashtable_data_index > 0 && (!hashtable->is_resizing || hashtable_data == NULL)) {
LOG_DI("not resizing, skipping check on the current hashtable_data");
continue;
}
if (hashtable_mcmp_support_op_search_key(
hashtable_data,
database_number,
key,
key_length,
hash,
transaction,
&chunk_index,
&chunk_slot_index,
&key_value) == false) {
LOG_DI("key not found, continuing");
continue;
}
LOG_DI("key found, deleting hash and setting flags to deleted");
half_hashes_chunk = &hashtable_data->half_hashes_chunk[chunk_index];
if (half_hashes_chunk->half_hashes[chunk_slot_index].filled == 0) {
return false;
}
if (unlikely(!transaction_rwspinlock_is_owned_by_transaction(&half_hashes_chunk->lock, transaction))) {
if (unlikely(!transaction_upgrade_lock_for_write(transaction, &half_hashes_chunk->lock))) {
return false;
}
}
// The hashtable_mcmp_support_op_search_key operation is lockless, it's necessary to set the lock and validate
// that the hash initially found it's the same to avoid a potential race condition.
hashtable_hash_quarter_t quarter_hash =
hashtable_mcmp_support_hash_quarter(hashtable_mcmp_support_hash_half(hash));
if (likely(half_hashes_chunk->half_hashes[chunk_slot_index].quarter_hash == quarter_hash)) {
if (current_value != NULL) {
*current_value = key_value->data;
}
half_hashes_chunk->metadata.slots_occupied--;
assert(half_hashes_chunk->metadata.slots_occupied <= HASHTABLE_MCMP_HALF_HASHES_CHUNK_SLOTS_COUNT);
half_hashes_chunk->metadata.is_full = 0;
half_hashes_chunk->half_hashes[chunk_slot_index].slot_id = 0;
MEMORY_FENCE_STORE();
key_value->flags = HASHTABLE_KEY_VALUE_FLAG_DELETED;
MEMORY_FENCE_STORE();
xalloc_free((hashtable_key_data_t*)key_value->key);
key_value->database_number = 0;
key_value->key = NULL;
key_value->key_length = 0;
deleted = true;
}
if (likely(deleted)) {
break;
}
}
LOG_DI("deleted = %s", deleted ? "YES" : "NO");
LOG_DI("chunk_index = 0x%016x", chunk_index);
LOG_DI("chunk_slot_index = 0x%016x", chunk_slot_index);
return deleted;
}
bool hashtable_mcmp_op_delete_by_index(
hashtable_t* hashtable,
hashtable_database_number_t database_number,
transaction_t *transaction,
bool already_locked_for_read,
hashtable_bucket_index_t bucket_index,
hashtable_value_data_t *current_value) {
hashtable_chunk_index_t chunk_index = 0;
hashtable_chunk_slot_index_t chunk_slot_index = 0;
hashtable_half_hashes_chunk_volatile_t* half_hashes_chunk;
hashtable_key_value_volatile_t* key_value;
bool deleted = false;
volatile hashtable_data_t* hashtable_data_list[] = {
hashtable->ht_current,
hashtable->ht_old
};
uint8_t hashtable_data_list_size = 2;
for (
uint8_t hashtable_data_index = 0;
hashtable_data_index < hashtable_data_list_size;
hashtable_data_index++) {
volatile hashtable_data_t *hashtable_data = hashtable_data_list[hashtable_data_index];
LOG_DI("hashtable_data_index = %u", hashtable_data_index);
LOG_DI("hashtable_data = 0x%016x", hashtable_data);
if (hashtable_data_index > 0 && (!hashtable->is_resizing || hashtable_data == NULL)) {
LOG_DI("not resizing, skipping check on the current hashtable_data");
continue;
}
chunk_index = bucket_index / HASHTABLE_MCMP_HALF_HASHES_CHUNK_SLOTS_COUNT;
chunk_slot_index = bucket_index % HASHTABLE_MCMP_HALF_HASHES_CHUNK_SLOTS_COUNT;
half_hashes_chunk = &hashtable_data->half_hashes_chunk[chunk_index];
if (half_hashes_chunk->half_hashes[chunk_slot_index].filled == 0) {
return false;
}
if (already_locked_for_read) {
if (unlikely(!transaction_upgrade_lock_for_write(transaction, &half_hashes_chunk->lock))) {
return false;
}
} else {
if (unlikely(!transaction_rwspinlock_is_owned_by_transaction(&half_hashes_chunk->lock, transaction))) {
if (unlikely(!transaction_lock_for_write(transaction, &half_hashes_chunk->lock))) {
return false;
}
}
}
key_value = &hashtable_data->keys_values[bucket_index];
if (unlikely(key_value->database_number != database_number)) {
return false;
}
if (current_value != NULL) {
*current_value = key_value->data;
}
half_hashes_chunk->metadata.slots_occupied--;
assert(half_hashes_chunk->metadata.slots_occupied <= HASHTABLE_MCMP_HALF_HASHES_CHUNK_SLOTS_COUNT);
half_hashes_chunk->metadata.is_full = 0;
half_hashes_chunk->half_hashes[chunk_slot_index].slot_id = 0;
MEMORY_FENCE_STORE();
key_value->flags = HASHTABLE_KEY_VALUE_FLAG_DELETED;
MEMORY_FENCE_STORE();
xalloc_free((hashtable_key_data_t*)key_value->key);
key_value->database_number = 0;
key_value->key = NULL;
key_value->key_length = 0;
deleted = true;
break;
}
LOG_DI("deleted = %s", deleted ? "YES" : "NO");
LOG_DI("chunk_index = 0x%016x", chunk_index);
LOG_DI("chunk_slot_index = 0x%016x", chunk_slot_index);
return deleted;
}
bool hashtable_mcmp_op_delete_by_index_all_databases(
hashtable_t* hashtable,
transaction_t *transaction,
bool already_locked_for_read,
hashtable_bucket_index_t bucket_index,
hashtable_value_data_t *current_value) {
hashtable_chunk_index_t chunk_index = 0;
hashtable_chunk_slot_index_t chunk_slot_index = 0;
hashtable_half_hashes_chunk_volatile_t* half_hashes_chunk;
hashtable_key_value_volatile_t* key_value;
bool deleted = false;
volatile hashtable_data_t* hashtable_data_list[] = {
hashtable->ht_current,
hashtable->ht_old
};
uint8_t hashtable_data_list_size = 2;
for (
uint8_t hashtable_data_index = 0;
hashtable_data_index < hashtable_data_list_size;
hashtable_data_index++) {
volatile hashtable_data_t *hashtable_data = hashtable_data_list[hashtable_data_index];
LOG_DI("hashtable_data_index = %u", hashtable_data_index);
LOG_DI("hashtable_data = 0x%016x", hashtable_data);
if (hashtable_data_index > 0 && (!hashtable->is_resizing || hashtable_data == NULL)) {
LOG_DI("not resizing, skipping check on the current hashtable_data");
continue;
}
chunk_index = bucket_index / HASHTABLE_MCMP_HALF_HASHES_CHUNK_SLOTS_COUNT;
chunk_slot_index = bucket_index % HASHTABLE_MCMP_HALF_HASHES_CHUNK_SLOTS_COUNT;
half_hashes_chunk = &hashtable_data->half_hashes_chunk[chunk_index];
if (half_hashes_chunk->half_hashes[chunk_slot_index].filled == 0) {
return false;
}
if (already_locked_for_read) {
if (unlikely(!transaction_upgrade_lock_for_write(transaction, &half_hashes_chunk->lock))) {
return false;
}
} else {
if (unlikely(!transaction_lock_for_write(transaction, &half_hashes_chunk->lock))) {
return false;
}
}
key_value = &hashtable_data->keys_values[bucket_index];
if (current_value != NULL) {
*current_value = key_value->data;
}
half_hashes_chunk->metadata.slots_occupied--;
assert(half_hashes_chunk->metadata.slots_occupied <= HASHTABLE_MCMP_HALF_HASHES_CHUNK_SLOTS_COUNT);
half_hashes_chunk->metadata.is_full = 0;
half_hashes_chunk->half_hashes[chunk_slot_index].slot_id = 0;
MEMORY_FENCE_STORE();
key_value->flags = HASHTABLE_KEY_VALUE_FLAG_DELETED;
MEMORY_FENCE_STORE();
xalloc_free((hashtable_key_data_t*)key_value->key);
key_value->database_number = 0;
key_value->key = NULL;
key_value->key_length = 0;
deleted = true;
break;
}
LOG_DI("deleted = %s", deleted ? "YES" : "NO");
LOG_DI("chunk_index = 0x%016x", chunk_index);
LOG_DI("chunk_slot_index = 0x%016x", chunk_slot_index);
return deleted;
}