Skip to content
Snippets Groups Projects
Commit 1cca7f9e authored by Daniel Salzman's avatar Daniel Salzman
Browse files

utils: add general lookup container

parent 0c752a76
No related branches found
No related tags found
1 merge request!520Interactive knotc
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include "utils/common/lookup.h"
#include "contrib/mempattern.h"
#include "contrib/ucw/mempool.h"
#include "libknot/error.h"
int lookup_init(lookup_t *lookup)
{
if (lookup == NULL) {
return KNOT_EINVAL;
}
memset(lookup, 0, sizeof(*lookup));
mm_ctx_mempool(&lookup->mm, MM_DEFAULT_BLKSIZE);
lookup->trie = hattrie_create_n(TRIE_BUCKET_SIZE, &lookup->mm);
if (lookup->trie == NULL) {
mp_delete(lookup->mm.ctx);
return KNOT_ENOMEM;
}
return KNOT_EOK;
}
static void reset_output(lookup_t *lookup)
{
if (lookup == NULL) {
return;
}
mm_free(&lookup->mm, lookup->found.key);
lookup->found.key = NULL;
lookup->found.data = NULL;
lookup->iter.count = 0;
mm_free(&lookup->mm, lookup->iter.first_key);
lookup->iter.first_key = NULL;
hattrie_iter_free(lookup->iter.it);
lookup->iter.it = NULL;
}
void lookup_deinit(lookup_t *lookup)
{
if (lookup == NULL) {
return;
}
reset_output(lookup);
hattrie_free(lookup->trie);
mp_delete(lookup->mm.ctx);
}
int lookup_insert(lookup_t *lookup, const char *str, void *data)
{
if (lookup == NULL || str == NULL) {
return KNOT_EINVAL;
}
size_t str_len = strlen(str);
if (str_len == 0) {
return KNOT_EINVAL;
}
value_t *val = hattrie_get(lookup->trie, str, str_len);
if (val == NULL) {
return KNOT_ENOMEM;
}
*val = data;
return KNOT_EOK;
}
void lookup_index(lookup_t *lookup)
{
if (lookup == NULL) {
return;
}
hattrie_build_index(lookup->trie);
}
static int set_key(lookup_t *lookup, char **dst, const char *key, size_t key_len)
{
if (*dst != NULL) {
mm_free(&lookup->mm, *dst);
}
*dst = mm_alloc(&lookup->mm, key_len + 1);
if (*dst == NULL) {
return KNOT_ENOMEM;
}
memcpy(*dst, key, key_len);
(*dst)[key_len] = '\0';
return KNOT_EOK;
}
int lookup_search(lookup_t *lookup, const char *str, size_t str_len)
{
if (lookup == NULL) {
return KNOT_EINVAL;
}
// Change NULL string to the empty one.
if (str == NULL) {
str = "";
}
reset_output(lookup);
size_t new_len = 0;
hattrie_iter_t *it = hattrie_iter_begin(lookup->trie, true);
for (; !hattrie_iter_finished(it); hattrie_iter_next(it)) {
size_t len;
const char *key = hattrie_iter_key(it, &len);
// Compare with a shorter key.
if (len < str_len) {
int ret = memcmp(str, key, len);
if (ret >= 0) {
continue;
} else {
break;
}
}
// Compare with an equal length or longer key.
int ret = memcmp(str, key, str_len);
if (ret == 0) {
lookup->iter.count++;
// First candidate.
if (lookup->iter.count == 1) {
ret = set_key(lookup, &lookup->found.key, key, len);
if (ret != KNOT_EOK) {
break;
}
lookup->found.data = *hattrie_iter_val(it);
new_len = len;
// Another candidate.
} else if (new_len > str_len) {
if (new_len > len) {
new_len = len;
}
while (memcmp(lookup->found.key, key, new_len) != 0) {
new_len--;
}
}
// Stop if greater than the key, and also than all the following keys.
} else if (ret < 0) {
break;
}
}
hattrie_iter_free(it);
switch (lookup->iter.count) {
case 0:
return KNOT_ENOENT;
case 1:
return KNOT_EOK;
default:
// Store full name of the first candidate.
if (set_key(lookup, &lookup->iter.first_key, lookup->found.key,
strlen(lookup->found.key)) != KNOT_EOK) {
return KNOT_ENOMEM;
}
lookup->found.key[new_len] = '\0';
lookup->found.data = NULL;
return KNOT_EFEWDATA;
}
}
void lookup_list(lookup_t *lookup)
{
if (lookup == NULL || lookup->iter.first_key == NULL) {
return;
}
if (lookup->iter.it != NULL) {
if (hattrie_iter_finished(lookup->iter.it)) {
hattrie_iter_free(lookup->iter.it);
lookup->iter.it = NULL;
return;
}
hattrie_iter_next(lookup->iter.it);
size_t len;
const char *key = hattrie_iter_key(lookup->iter.it, &len);
int ret = set_key(lookup, &lookup->found.key, key, len);
if (ret == KNOT_EOK) {
lookup->found.data = *hattrie_iter_val(lookup->iter.it);
}
return;
}
lookup->iter.it = hattrie_iter_begin(lookup->trie, true);
while (!hattrie_iter_finished(lookup->iter.it)) {
size_t len;
const char *key = hattrie_iter_key(lookup->iter.it, &len);
if (strcmp(key, lookup->iter.first_key) == 0) {
int ret = set_key(lookup, &lookup->found.key, key, len);
if (ret == KNOT_EOK) {
lookup->found.data = *hattrie_iter_val(lookup->iter.it);
}
break;
}
hattrie_iter_next(lookup->iter.it);
}
}
static void print_options(lookup_t *lookup, EditLine *el)
{
// Get terminal lines.
unsigned lines = 0;
if (el_get(el, EL_GETTC, "li", &lines) != 0 || lines < 3) {
return;
}
for (size_t i = 1; i <= lookup->iter.count; i++) {
lookup_list(lookup);
printf("\n%s", lookup->found.key);
if (i > 1 && i % (lines - 1) == 0 && i < lookup->iter.count) {
printf("\n Display next from %zu possibilities? (y or n)",
lookup->iter.count);
char next;
el_getc(el, &next);
if (next != 'y') {
break;
}
}
}
printf("\n");
fflush(stdout);
}
void lookup_complete(lookup_t *lookup, const char *str, size_t str_len,
EditLine *el, bool add_space)
{
if (lookup == NULL || el == NULL) {
return;
}
// Try to complete the command name.
int ret = lookup_search(lookup, str, str_len);
switch (ret) {
case KNOT_EOK:
el_deletestr(el, str_len);
el_insertstr(el, lookup->found.key);
if (add_space) {
el_insertstr(el, " ");
}
break;
case KNOT_EFEWDATA:
if (strlen(lookup->found.key) > str_len) {
el_deletestr(el, str_len);
el_insertstr(el, lookup->found.key);
} else {
print_options(lookup, el);
}
break;
default:
break;
}
}
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
* \file
*
* \brief Lookup container for textual strings.
*
* \addtogroup knot_utils
* @{
*/
#pragma once
#include <histedit.h>
#include "libknot/mm_ctx.h"
#include "contrib/hat-trie/hat-trie.h"
/*! Lookup context. */
typedef struct {
/*! Memory pool context. */
knot_mm_t mm;
/*! Main hat-trie storage. */
hattrie_t *trie;
/*! Current (iteration) data context. */
struct {
/*! Stored key. */
char *key;
/*! Corresponding key data. */
void *data;
} found;
/*! Iteration context. */
struct {
/*! Total number of possibilies. */
size_t count;
/*! The first possibility. */
char *first_key;
/*! Hat-trie iterator. */
hattrie_iter_t *it;
} iter;
} lookup_t;
/*!
* Initializes the lookup context.
*
* \param[in] lookup Lookup context.
*
* \return Error code, KNOT_EOK if successful.
*/
int lookup_init(lookup_t *lookup);
/*!
* Deinitializes the lookup context.
*
* \param[in] lookup Lookup context.
*/
void lookup_deinit(lookup_t *lookup);
/*!
* Inserts given key and data into the lookup.
*
* \param[in] lookup Lookup context.
* \param[in] str Textual key.
* \param[in] data Key textual data.
*
* \return Error code, KNOT_EOK if successful.
*/
int lookup_insert(lookup_t *lookup, const char *str, void *data);
/*!
* Indexes the lookup container.
*
* \param[in] lookup Lookup context.
*/
void lookup_index(lookup_t *lookup);
/*!
* Searches the lookup container for the given key.
*
* \note If one candidate, lookup.found contains the key/data,
* if more candidates, lookup.found contains the common key prefix and
* lookup.iter.first_key is the first candidate key.
*
* \param[in] lookup Lookup context.
* \param[in] str Textual key.
* \param[in] str_len Textual key length.
*
* \return Error code, KNOT_EOK if 1 candidate, KNOT_ENOENT if no candidate,
* and KNOT_EFEWDATA if more candidates are possible.
*/
int lookup_search(lookup_t *lookup, const char *str, size_t str_len);
/*!
* Moves the lookup iterator to the next key candidate.
*
* \note lookup.found is updated.
*
* \param[in] lookup Lookup context.
*/
void lookup_list(lookup_t *lookup);
/*!
* Completes the string based on the lookup content or prints all candidates.
*
* \param[in] lookup Lookup context.
* \param[in] str Textual key.
* \param[in] str_len Textual key length.
* \param[in] el Editline context.
* \param[in] add_space Add one space after completed string flag.
*/
void lookup_complete(lookup_t *lookup, const char *str, size_t str_len,
EditLine *el, bool add_space);
/*! @} */
......@@ -59,3 +59,4 @@ zone_timers
zone_update
zonedb
ztree
utils/test_lookup
......@@ -64,7 +64,13 @@ check_PROGRAMS = \
ztree
check_PROGRAMS += \
modules/online_sign
modules/online_sign \
utils/test_lookup
utils_test_lookup_LDADD = \
$(top_builddir)/libtap/libtap.la \
$(top_builddir)/src/libknotus.la \
$(libedit_LIBS)
check-compile: $(check_PROGRAMS)
......
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <tap/basic.h>
#include "libknot/error.h"
#include "utils/common/lookup.h"
static void check_search_ok(lookup_t *l, const char *in, const char *out)
{
diag("Search for '%s'", in);
int ret = lookup_search(l, in, strlen(in));
ok(ret == KNOT_EOK, "Check found");
ok(strcmp(out, l->found.key) == 0, "Compare key");
ok(strcmp(out, l->found.data) == 0, "Compare data");
ok(l->iter.first_key == NULL, "Compare no first key");
ok(l->iter.count == 1, "Compare 1 count");
}
static void check_search_multi(lookup_t *l, const char *in, const char *out,
const char *first, size_t count)
{
diag("Search for '%s'", in);
int ret = lookup_search(l, in, strlen(in));
ok(ret == KNOT_EFEWDATA, "Check found multi");
ok(strcmp(out, l->found.key) == 0, "Compare key");
ok(l->found.data == NULL, "Compare no data");
ok(strcmp(first, l->iter.first_key) == 0, "Compare first key");
ok(l->iter.count == count, "Compare count");
}
static void check_search_none(lookup_t *l, const char *in)
{
diag("Search for '%s'", in);
int ret = lookup_search(l, in, strlen(in));
ok(ret == KNOT_ENOENT, "Check not found");
ok(l->found.key == NULL, "Check no key");
ok(l->found.data == NULL, "Check no data");
}
static void init(lookup_t *l, const char **table)
{
int ret = lookup_init(l);
ok(ret == KNOT_EOK, "Init");
while (*table != NULL) {
ret = lookup_insert(l, *table, (void *)*table);
ok(ret == KNOT_EOK, "Insert '%s'", *table);
table++;
}
lookup_index(l);
}
static void test_search_basic(void)
{
const char* table[] = {
"aa",
"bb",
NULL
};
lookup_t l;
init(&l, table);
check_search_ok(&l, "a", "aa");
check_search_ok(&l, "aa", "aa");
check_search_ok(&l, "b", "bb");
check_search_ok(&l, "bb", "bb");
check_search_none(&l, "0");
check_search_none(&l, "000");
check_search_none(&l, "00000000000000000000000000000000000000000000");
check_search_none(&l, "a0");
check_search_none(&l, "ab");
check_search_none(&l, "aaa");
check_search_none(&l, "bbb");
check_search_none(&l, "cc");
check_search_none(&l, "ccc");
check_search_none(&l, "cccccccccccccccccccccccccccccccccccccccccccc");
check_search_multi(&l, "", "", "aa", 2);
lookup_deinit(&l);
}
static void test_search_iter(void)
{
const char* table[] = {
"0",
"ab",
"abc",
"abcd",
"abc-1",
"abc-99",
"z",
NULL
};
lookup_t l;
init(&l, table);
check_search_multi(&l, "", "", "0", 7);
check_search_multi(&l, "a", "ab", "ab", 5);
check_search_multi(&l, "ab", "ab", "ab", 5);
check_search_multi(&l, "abc", "abc", "abc", 4);
check_search_multi(&l, "abc-", "abc-", "abc-1", 2);
lookup_deinit(&l);
}
int main(int argc, char *argv[])
{
plan_lazy();
diag("Search tests basic");
test_search_basic();
diag("Search tests multi-result");
test_search_iter();
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment