Free hash after usage and dont use it at all if no readline present

This commit is contained in:
morty 2018-05-30 17:45:43 +03:00
parent 12a68a9e23
commit be0f0ecef9
3 changed files with 28 additions and 16 deletions

View File

@ -26,10 +26,10 @@ namespace Completion
ht->buckets = (Bucket **)calloc(size, sizeof(Bucket));
if (!ht->buckets) {
ht->initialized = false;
return FAILURE;
return HASH_FAILURE;
}
ht->initialized = true;
return SUCCESS;
return HASH_SUCCESS;
}
void hash_add_word(HashTable *ht, char *word)
@ -48,7 +48,7 @@ namespace Completion
Bucket *bucket;
if (keyLength <= 0) {
return FAILURE;
return HASH_FAILURE;
}
hash = ht->hashFunction(key, keyLength);
bucketIndex = hash % ht->tableSize;
@ -58,20 +58,20 @@ namespace Completion
if (!memcmp(bucket->key, key, keyLength)) {
auto *entry = (HashEntry *) calloc(1, sizeof(HashEntry));
if (entry == nullptr) {
return FAILURE;
return HASH_FAILURE;
}
entry->text = word;
entry->next = bucket->entry;
bucket->entry = entry;
return SUCCESS;
return HASH_SUCCESS;
}
}
bucket = bucket->next;
}
bucket = (Bucket *) calloc(1, sizeof(Bucket));
if (bucket == nullptr) {
return FAILURE;
return HASH_FAILURE;
}
bucket->key = key;
bucket->keyLength = keyLength;
@ -79,7 +79,7 @@ namespace Completion
bucket->entry = (HashEntry *) calloc(1, sizeof(HashEntry));
if (bucket->entry == nullptr) {
return FAILURE;
return HASH_FAILURE;
}
bucket->entry->text = word;
@ -89,7 +89,7 @@ namespace Completion
ht->buckets[bucketIndex] = bucket;
return SUCCESS;
return HASH_SUCCESS;
}
Bucket * hash_find_all_matches(HashTable *ht, const char *word, uint length, uint *res_length)
@ -117,6 +117,11 @@ namespace Completion
return (Bucket *) nullptr;
}
void hash_free(HashTable *ht)
{
free(ht->buckets);
}
}

View File

@ -1,11 +1,12 @@
#ifndef CLICKHOUSE_COMPLETION_H
#define CLICKHOUSE_COMPLETION_H
#define SUCCESS 0
#define FAILURE 1
#define HASH_SUCCESS 0
#define HASH_FAILURE 1
#include <sys/types.h>
//All of functionality for hash was taken from mysql-server project from completion_hash.cpp file
namespace Completion
{
struct HashEntry {
@ -31,6 +32,7 @@ namespace Completion
int init_hash_table(HashTable *ht, size_t size);
void hash_add_word(HashTable *ht, char *word);
int hash_insert_key(HashTable *ht, char *key, uint keyLength, char* word);
void hash_free(HashTable *ht);
Bucket * hash_find_all_matches(HashTable *ht, const char *word, uint length, uint *res_length);
}

View File

@ -58,8 +58,8 @@
#include <ext/scope_guard.h>
static Completion::HashTable ht;
char ** commands_completion(const char *, int, int);
char * commands_generator(const char *, int);
char ** query_parts_completion(const char *, int, int);
char * query_parts_generator(const char *, int);
/// http://en.wikipedia.org/wiki/ANSI_escape_code
@ -348,7 +348,7 @@ private:
Completion::hash_add_word(ht, qP->name);
qP++;
}
rl_attempted_completion_function = commands_completion;
rl_attempted_completion_function = query_parts_completion;
}
@ -461,7 +461,9 @@ private:
throw Exception("query_id could be specified only in non-interactive mode", ErrorCodes::BAD_ARGUMENTS);
if (print_time_to_stderr)
throw Exception("time option could be specified only in non-interactive mode", ErrorCodes::BAD_ARGUMENTS);
#if USE_READLINE
init_suggestions(&ht);
#endif
/// Turn tab completion off.
// rl_bind_key('\t', rl_insert);
@ -487,6 +489,9 @@ private:
}
loop();
#if USE_READLINE
Completion::hash_free(&ht);
#endif
std::cout << (isNewYearMode() ? "Happy new year." : "Bye.") << std::endl;
@ -1566,13 +1571,13 @@ int mainEntryClickHouseClient(int argc, char ** argv)
return client.run();
}
char ** commands_completion(const char *text, int start, int end)
char ** query_parts_completion(const char *text, int start, int end)
{
rl_attempted_completion_over = start + end + 1;
return rl_completion_matches(text, commands_generator);
return rl_completion_matches(text, query_parts_generator);
}
char * commands_generator(const char * text, int state)
char * query_parts_generator(const char *text, int state)
{
static int text_length;
static Completion::Bucket *bucket;