-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR implements the RANDOMKEY command. It refactors the hashtable op get key function to return a copy of the key from the hashtable and to ensure that the copy is actually valid implementing a 2-phase verification. The PR also includes the tests for the new command and a minor improvement to the DBSIZE command
- Loading branch information
1 parent
93246b1
commit 5a5283d
Showing
11 changed files
with
236 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/data_structures/hashtable/mcmp/hashtable_op_get_random_key.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Copyright (C) 2018-2022 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 <stdlib.h> | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <stdatomic.h> | ||
#include <assert.h> | ||
#include <string.h> | ||
|
||
#include "random.h" | ||
#include "misc.h" | ||
#include "exttypes.h" | ||
#include "memory_fences.h" | ||
#include "spinlock.h" | ||
#include "data_structures/double_linked_list/double_linked_list.h" | ||
#include "data_structures/queue_mpmc/queue_mpmc.h" | ||
#include "slab_allocator.h" | ||
#include "data_structures/hashtable/mcmp/hashtable.h" | ||
#include "data_structures/hashtable/mcmp/hashtable_data.h" | ||
#include "data_structures/hashtable/mcmp/hashtable_op_get_key.h" | ||
|
||
#include "hashtable_op_get_random_key.h" | ||
|
||
bool hashtable_mcmp_op_get_random_key_try( | ||
hashtable_t *hashtable, | ||
char **key, | ||
hashtable_key_size_t *key_size) { | ||
uint64_t random_value = random_generate(); | ||
|
||
return hashtable_mcmp_op_get_key( | ||
hashtable, | ||
random_value % hashtable->ht_current->buckets_count_real, | ||
key, | ||
key_size); | ||
} |
17 changes: 17 additions & 0 deletions
17
src/data_structures/hashtable/mcmp/hashtable_op_get_random_key.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef CACHEGRAND_HASHTABLE_OP_GET_RANDOM_KEY_H | ||
#define CACHEGRAND_HASHTABLE_OP_GET_RANDOM_KEY_H | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
bool hashtable_mcmp_op_get_random_key_try( | ||
hashtable_t *hashtable, | ||
char **key, | ||
hashtable_key_size_t *key_size); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif //CACHEGRAND_HASHTABLE_OP_GET_RANDOM_KEY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* Copyright (C) 2018-2022 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 <stddef.h> | ||
#include <string.h> | ||
#include <strings.h> | ||
#include <arpa/inet.h> | ||
#include <assert.h> | ||
|
||
#include "misc.h" | ||
#include "exttypes.h" | ||
#include "log/log.h" | ||
#include "clock.h" | ||
#include "spinlock.h" | ||
#include "data_structures/small_circular_queue/small_circular_queue.h" | ||
#include "data_structures/double_linked_list/double_linked_list.h" | ||
#include "data_structures/queue_mpmc/queue_mpmc.h" | ||
#include "slab_allocator.h" | ||
#include "data_structures/hashtable/mcmp/hashtable.h" | ||
#include "data_structures/hashtable/mcmp/hashtable_op_get.h" | ||
#include "data_structures/hashtable/spsc/hashtable_spsc.h" | ||
#include "protocol/redis/protocol_redis.h" | ||
#include "protocol/redis/protocol_redis_reader.h" | ||
#include "protocol/redis/protocol_redis_writer.h" | ||
#include "module/module.h" | ||
#include "network/io/network_io_common.h" | ||
#include "config.h" | ||
#include "fiber.h" | ||
#include "network/channel/network_channel.h" | ||
#include "storage/io/storage_io_common.h" | ||
#include "storage/channel/storage_channel.h" | ||
#include "storage/db/storage_db.h" | ||
#include "module/redis/module_redis.h" | ||
#include "module/redis/module_redis_connection.h" | ||
#include "module/redis/module_redis_command.h" | ||
#include "network/network.h" | ||
#include "worker/worker_stats.h" | ||
#include "worker/worker_context.h" | ||
|
||
#define TAG "module_redis_command_randomkey" | ||
|
||
MODULE_REDIS_COMMAND_FUNCPTR_COMMAND_END(randomkey) { | ||
bool return_res; | ||
hashtable_key_size_t key_size = 0; | ||
char *key = storage_db_op_random_key(connection_context->db, &key_size); | ||
|
||
if (likely(key)) { | ||
return_res = module_redis_connection_send_string(connection_context, key, key_size); | ||
slab_allocator_mem_free(key); | ||
} else { | ||
return_res = module_redis_connection_send_string_null(connection_context); | ||
} | ||
|
||
return return_res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters