Browse Source

[common] remove hash value type (use key/value lists instead)

personal/stbuehler/wip
Stefan Bühler 8 years ago
parent
commit
01788a7250
  1. 20
      include/lighttpd/value.h
  2. 88
      src/common/value.c
  3. 4
      src/main/plugin.c
  4. 2
      src/main/plugin_core.c
  5. 64
      src/main/value_lua.c
  6. 2
      src/modules/mod_memcached.c
  7. 4
      src/modules/mod_vhost.c

20
include/lighttpd/value.h

@ -19,7 +19,6 @@ typedef enum
, LI_VALUE_NUMBER
, LI_VALUE_STRING
, LI_VALUE_LIST
, LI_VALUE_HASH
#ifdef _LIGHTTPD_BASE_H_
, LI_VALUE_ACTION /**< shouldn't be used for options, but may be needed for constructing actions */
, LI_VALUE_CONDITION /**< shouldn't be used for options, but may be needed for constructing actions */
@ -34,8 +33,6 @@ struct liValue {
GString *string;
/* array of (liValue*) */
GPtrArray *list;
/* hash GString => value */
GHashTable *hash;
struct {
struct liServer *srv; /* needed for destruction */
struct liAction *action;
@ -52,7 +49,6 @@ LI_API liValue* li_value_new_bool(gboolean val);
LI_API liValue* li_value_new_number(gint64 val);
LI_API liValue* li_value_new_string(GString *val);
LI_API liValue* li_value_new_list(void);
LI_API liValue* li_value_new_hash(void);
#ifdef _LIGHTTPD_BASE_H_
LI_API liValue* li_value_new_action(liServer *srv, liAction *a);
LI_API liValue* li_value_new_condition(liServer *srv, liCondition *c);
@ -88,7 +84,6 @@ LI_API gpointer li_value_extract_ptr(liValue *val);
LI_API gpointer li_common_value_extract_ptr_(liValue *val); /* internal function */
LI_API GString* li_value_extract_string(liValue *val);
LI_API GPtrArray* li_value_extract_list(liValue *val);
LI_API GHashTable* li_value_extract_hash(liValue *val);
#ifdef _LIGHTTPD_BASE_H_
LI_API liAction* li_value_extract_action(liValue *val);
LI_API liCondition* li_value_extract_condition(liValue *val);
@ -117,6 +112,8 @@ INLINE guint li_value_list_len(liValue *val);
INLINE liValue* li_value_list_at(liValue* val, guint ndx);
/* returns type of list entry, or LI_VALUE_NONE if not a list or NULL entry or out of bounds */
INLINE liValueType li_value_list_type_at(liValue *val, guint ndx);
/* set list entry at given index */
INLINE void li_value_list_set(liValue *val, guint ndx, liValue *entry);
#define LI_VALUE_FOREACH(entry, list) { \
guint _ ## entry ## _i, _ ## entry ## _len = li_value_list_len(list); \
@ -158,4 +155,17 @@ INLINE liValueType li_value_list_type_at(liValue *val, guint ndx) {
return li_value_type(li_value_list_at(val, ndx));
}
INLINE void li_value_list_set(liValue *val, guint ndx, liValue *entry) {
GPtrArray *list;
if (NULL == val || LI_VALUE_LIST != val->type) {
li_value_free(entry);
}
list = val->data.list;
if (ndx <= list->len) {
g_ptr_array_set_size(list, ndx + 1);
}
li_value_free(g_ptr_array_index(list, ndx));
g_ptr_array_index(list, ndx) = entry;
}
#endif

88
src/common/value.c

@ -44,13 +44,6 @@ static void _value_hash_free_value(gpointer data) {
li_value_free((liValue*) data);
}
liValue* li_value_new_hash(void) {
liValue *v = g_slice_new0(liValue);
v->data.hash = li_value_new_hashtable();
v->type = LI_VALUE_HASH;
return v;
}
GHashTable *li_value_new_hashtable(void) {
return g_hash_table_new_full(
(GHashFunc) g_string_hash, (GEqualFunc) g_string_equal,
@ -89,17 +82,6 @@ liValue* li_common_value_copy_(liValue* val) {
g_ptr_array_index(n->data.list, i) = li_value_copy(g_ptr_array_index(val->data.list, i));
}
return n;
/* hash: iterate over hashtable, clone each value */
case LI_VALUE_HASH:
n = li_value_new_hash();
{
GHashTableIter iter;
gpointer k, v;
g_hash_table_iter_init(&iter, val->data.hash);
while (g_hash_table_iter_next(&iter, &k, &v))
g_hash_table_insert(n->data.hash, g_string_new_len(GSTR_LEN((GString*)k)), li_value_copy((liValue*)v));
}
return n;
}
return NULL;
}
@ -124,9 +106,6 @@ void li_common_value_clear_(liValue *val) {
case LI_VALUE_LIST:
li_value_list_free(val->data.list);
break;
case LI_VALUE_HASH:
g_hash_table_destroy(val->data.hash);
break;
}
_li_value_clear(val);
}
@ -156,8 +135,6 @@ const char* li_common_valuetype_string_(liValueType type) {
return "string";
case LI_VALUE_LIST:
return "list";
case LI_VALUE_HASH:
return "hash";
}
return "<unknown>";
}
@ -203,30 +180,6 @@ GString *li_common_value_to_string_(liValue *val) {
}
g_string_append_c(str, ')');
break;
case LI_VALUE_HASH:
{
GHashTableIter iter;
gpointer k, v;
GString *tmp;
guint i = 0;
str = g_string_new_len(CONST_STR_LEN("["));
g_hash_table_iter_init(&iter, val->data.hash);
while (g_hash_table_iter_next(&iter, &k, &v)) {
if (i)
g_string_append_len(str, CONST_STR_LEN(", "));
tmp = li_value_to_string((liValue*)v);
g_string_append_len(str, GSTR_LEN((GString*)k));
g_string_append_len(str, CONST_STR_LEN(" => "));
g_string_append_len(str, GSTR_LEN(tmp));
g_string_free(tmp, TRUE);
i++;
}
g_string_append_c(str, ']');
}
break;
}
return str;
@ -250,9 +203,6 @@ gpointer li_common_value_extract_ptr_(liValue *val) {
case LI_VALUE_LIST:
ptr = val->data.list;
break;
case LI_VALUE_HASH:
ptr = val->data.hash;
break;
}
_li_value_clear(val);
return ptr;
@ -275,14 +225,6 @@ GPtrArray* li_value_extract_list(liValue *val) {
return result;
}
GHashTable* li_value_extract_hash(liValue *val) {
GHashTable* result;
if (NULL == val || val->type != LI_VALUE_HASH) return NULL;
result = val->data.hash;
_li_value_clear(val);
return result;
}
liValue* li_value_extract(liValue *val) {
liValue *v;
if (NULL == val) return NULL;
@ -295,35 +237,7 @@ liValue* li_value_extract(liValue *val) {
liValue* li_value_to_key_value_list(liValue *val) {
if (NULL == val) return NULL;
if (LI_VALUE_HASH == val->type) {
GHashTable *table = li_value_extract_hash(val);
GPtrArray *list;
GHashTableIter hti;
gpointer hkey, hvalue;
{
/* convert val to list */
liValue *vlist = li_value_new_list();
li_value_move(val, vlist);
li_value_free(vlist);
}
list = val->data.list;
g_hash_table_iter_init(&hti, table);
while (g_hash_table_iter_next(&hti, &hkey, &hvalue)) {
GString *htkey = hkey; liValue *htval = hvalue;
liValue *hkeyval = li_value_new_string(htkey);
liValue *pair = li_value_new_list();
g_ptr_array_add(pair->data.list, hkeyval);
g_ptr_array_add(pair->data.list, htval);
g_ptr_array_add(list, pair);
}
g_hash_table_steal_all(table); /* content was moved to list */
g_hash_table_destroy(table);
return val;
} else if (LI_VALUE_LIST == val->type) {
if (LI_VALUE_LIST == val->type) {
if (li_value_list_has_len(val, 2) &&
(LI_VALUE_STRING == li_value_list_type_at(val, 0) || LI_VALUE_NONE == li_value_list_type_at(val, 0))) {
/* single key-value pair */

4
src/main/plugin.c

@ -478,10 +478,6 @@ void li_release_optionptr(liServer *srv, liOptionPtrValue *value) {
if (value->data.list)
li_value_list_free(value->data.list);
break;
case LI_VALUE_HASH:
if (value->data.hash)
g_hash_table_destroy(value->data.hash);
break;
case LI_VALUE_ACTION:
if (value->data.action)
li_action_release(srv, value->data.action);

2
src/main/plugin_core.c

@ -1342,7 +1342,7 @@ static liLogMap* logmap_from_value(liServer *srv, liValue *val) {
if (NULL != entryKeyStr && g_str_equal(entryKeyStr->str, "*")) {
WARNING(srv, "%s", "log: found entry with string key \"*\". please convert the parameter to a key-value list and use the keyword default instead.");
/* TODO: remove support for "default" (LI_VALUE_HASH) */
/* TODO: remove support for "default" */
entryKeyStr = NULL;
}

64
src/main/value_lua.c

@ -66,54 +66,44 @@ static void lua_push_kvlist_metatable(lua_State *L) {
static liValue* li_value_from_lua_table(liServer *srv, lua_State *L, int ndx) {
liValue *val = NULL, *sub_option;
GPtrArray *list = NULL;
GHashTable *hash = NULL;
liValue *val, *entry;
gboolean is_list = FALSE, is_hash = FALSE;
int ikey;
GString *skey;
liValue *kv_key, *kv_pair;
val = li_value_new_list();
ndx = li_lua_fixindex(L, ndx);
lua_pushnil(L);
while (lua_next(L, ndx) != 0) {
switch (lua_type(L, -2)) {
case LUA_TNUMBER:
if (hash) goto mixerror;
if (!list) {
val = li_value_new_list();
list = val->data.list;
}
if (is_hash) goto mixerror;
is_list = TRUE;
ikey = lua_tointeger(L, -2) - 1;
if (ikey < 0) {
ERROR(srv, "Invalid key < 0: %i - skipping entry", ikey + 1);
lua_pop(L, 1);
continue;
}
sub_option = li_value_from_lua(srv, L);
if (!sub_option) continue;
if ((size_t) ikey >= list->len) {
g_ptr_array_set_size(list, ikey + 1);
}
g_ptr_array_index(list, ikey) = sub_option;
entry = li_value_from_lua(srv, L);
if (NULL == entry) continue;
li_value_list_set(val, ikey, entry);
break;
case LUA_TSTRING:
if (list) goto mixerror;
if (!hash) {
val = li_value_new_hash();
hash = val->data.hash;
}
skey = li_lua_togstring(L, -2);
if (g_hash_table_lookup(hash, skey)) {
ERROR(srv, "Key already exists in hash: '%s' - skipping entry", skey->str);
lua_pop(L, 1);
if (is_list) goto mixerror;
is_hash = TRUE;
kv_key = li_value_new_string(li_lua_togstring(L, -2));
entry = li_value_from_lua(srv, L);
if (NULL == entry) {
li_value_free(kv_key);
continue;
}
sub_option = li_value_from_lua(srv, L);
if (!sub_option) {
g_string_free(skey, TRUE);
continue;
}
g_hash_table_insert(hash, skey, sub_option);
kv_pair = li_value_new_list();
li_value_list_append(kv_pair, kv_key);
li_value_list_append(kv_pair, entry);
li_value_list_append(val, kv_pair);
break;
default:
@ -244,20 +234,6 @@ int li_lua_push_value(lua_State *L, liValue *value) {
lua_push_kvlist_metatable(L);
lua_setmetatable(L, -2);
} break;
case LI_VALUE_HASH: {
GHashTableIter it;
gpointer pkey, pvalue;
lua_newtable(L);
g_hash_table_iter_init(&it, value->data.hash);
while (g_hash_table_iter_next(&it, &pkey, &pvalue)) {
GString *key = pkey;
liValue *subval = pvalue;
lua_pushlstring(L, GSTR_LEN(key));
li_lua_push_value(L, subval);
lua_rawset(L, -3);
}
} break;
case LI_VALUE_ACTION:
li_action_acquire(value->data.val_action.action);
li_lua_push_action(value->data.val_action.srv, L, value->data.val_action.action);

2
src/modules/mod_memcached.c

@ -567,7 +567,7 @@ static liAction* mc_lookup_create(liServer *srv, liWorker *wrk, liPlugin* p, liV
UNUSED(userdata);
if (LI_VALUE_LIST == li_value_type(val) && li_value_list_len(val) <= 3
&& (LI_VALUE_LIST == li_value_list_type_at(val, 0) || LI_VALUE_HASH == li_value_list_type_at(val, 0) || LI_VALUE_NONE == li_value_list_type_at(val, 0))
&& (LI_VALUE_LIST == li_value_list_type_at(val, 0) || LI_VALUE_NONE == li_value_list_type_at(val, 0))
&& (LI_VALUE_ACTION == li_value_list_type_at(val, 1) || LI_VALUE_NONE == li_value_list_type_at(val, 1))
&& (LI_VALUE_ACTION == li_value_list_type_at(val, 2) || LI_VALUE_NONE == li_value_list_type_at(val, 2))
) {

4
src/modules/mod_vhost.c

@ -106,7 +106,7 @@ static liAction* vhost_map_create(liServer *srv, liWorker *wrk, liPlugin* p, liV
if (NULL != entryKeyStr && g_str_equal(entryKeyStr->str, "default")) {
WARNING(srv, "%s", "vhost.map: found entry with string key \"default\". please convert the parameter to a key-value list and use the keyword default instead.");
/* TODO: remove support for "default" (LI_VALUE_HASH) */
/* TODO: remove support for "default" */
g_string_free(entryKeyStr, TRUE);
entryKeyStr = NULL;
}
@ -225,7 +225,7 @@ static liAction* vhost_map_regex_create(liServer *srv, liWorker *wrk, liPlugin*
if (NULL != entryKeyStr && g_str_equal(entryKeyStr->str, "default")) {
WARNING(srv, "%s", "vhost.map_regex: found entry with string key \"default\". please convert the parameter to a key-value list and use the keyword default instead.");
/* TODO: remove support for "default" (LI_VALUE_HASH) */
/* TODO: remove support for "default" */
g_string_free(entryKeyStr, TRUE);
entryKeyStr = NULL;
}

Loading…
Cancel
Save