Replace AVL tree in RSP query by a static hash using gperf

This commit is contained in:
Julien BLACHE
2011-03-31 18:30:18 +02:00
parent b8758a2aa2
commit cc0555d986
7 changed files with 73 additions and 166 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2010 Julien BLACHE <jb@jblache.org>
* Copyright (C) 2009-2011 Julien BLACHE <jb@jblache.org>
*
* 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
@@ -25,8 +25,6 @@
#include <string.h>
#include <errno.h>
#include <avl.h>
#include "logger.h"
#include "misc.h"
#include "rsp_query.h"
@@ -36,85 +34,9 @@
#include "RSP2SQL.h"
static struct rsp_query_field_map rsp_query_fields[] =
{
{ 0, RSP_TYPE_INT, "id" },
{ 0, RSP_TYPE_STRING, "path" },
{ 0, RSP_TYPE_STRING, "fname" },
{ 0, RSP_TYPE_STRING, "title" },
{ 0, RSP_TYPE_STRING, "artist" },
{ 0, RSP_TYPE_STRING, "album" },
{ 0, RSP_TYPE_STRING, "genre" },
{ 0, RSP_TYPE_STRING, "comment" },
{ 0, RSP_TYPE_STRING, "type" },
{ 0, RSP_TYPE_STRING, "composer" },
{ 0, RSP_TYPE_STRING, "orchestra" },
{ 0, RSP_TYPE_STRING, "grouping" },
{ 0, RSP_TYPE_STRING, "url" },
{ 0, RSP_TYPE_INT, "bitrate" },
{ 0, RSP_TYPE_INT, "samplerate" },
{ 0, RSP_TYPE_INT, "song_length" },
{ 0, RSP_TYPE_INT, "file_size" },
{ 0, RSP_TYPE_INT, "year" },
{ 0, RSP_TYPE_INT, "track" },
{ 0, RSP_TYPE_INT, "total_tracks" },
{ 0, RSP_TYPE_INT, "disc" },
{ 0, RSP_TYPE_INT, "total_discs" },
{ 0, RSP_TYPE_INT, "bpm" },
{ 0, RSP_TYPE_INT, "compilation" },
{ 0, RSP_TYPE_INT, "rating" },
{ 0, RSP_TYPE_INT, "play_count" },
{ 0, RSP_TYPE_INT, "data_kind" },
{ 0, RSP_TYPE_INT, "item_kind" },
{ 0, RSP_TYPE_STRING, "description" },
{ 0, RSP_TYPE_DATE, "time_added" },
{ 0, RSP_TYPE_DATE, "time_modified" },
{ 0, RSP_TYPE_DATE, "time_played" },
{ 0, RSP_TYPE_DATE, "db_timestamp" },
{ 0, RSP_TYPE_INT, "sample_count" },
{ 0, RSP_TYPE_STRING, "codectype" },
{ 0, RSP_TYPE_INT, "idx" },
{ 0, RSP_TYPE_INT, "has_video" },
{ 0, RSP_TYPE_INT, "contentrating" },
{ 0, RSP_TYPE_INT, "bits_per_sample" },
{ 0, RSP_TYPE_STRING, "album_artist" },
/* gperf static hash, rsp_query.gperf */
#include "rsp_query_hash.c"
{ -1, -1, NULL }
};
static avl_tree_t *rsp_query_fields_hash;
static int
rsp_query_field_map_compare(const void *aa, const void *bb)
{
struct rsp_query_field_map *a = (struct rsp_query_field_map *)aa;
struct rsp_query_field_map *b = (struct rsp_query_field_map *)bb;
if (a->hash < b->hash)
return -1;
if (a->hash > b->hash)
return 1;
return 0;
}
struct rsp_query_field_map *
rsp_query_field_lookup(char *field)
{
struct rsp_query_field_map rqfm;
avl_node_t *node;
rqfm.hash = djb_hash(field, strlen(field));
node = avl_search(rsp_query_fields_hash, &rqfm);
if (!node)
return NULL;
return (struct rsp_query_field_map *)node->item;
}
char *
rsp_query_parse_sql(const char *rsp_query)
@@ -227,56 +149,3 @@ rsp_query_parse_sql(const char *rsp_query)
return ret;
}
int
rsp_query_init(void)
{
avl_node_t *node;
struct rsp_query_field_map *rqfm;
int i;
rsp_query_fields_hash = avl_alloc_tree(rsp_query_field_map_compare, NULL);
if (!rsp_query_fields_hash)
{
DPRINTF(E_FATAL, L_RSP, "RSP query init could not allocate AVL tree\n");
return -1;
}
for (i = 0; rsp_query_fields[i].hash == 0; i++)
{
rsp_query_fields[i].hash = djb_hash(rsp_query_fields[i].rsp_field, strlen(rsp_query_fields[i].rsp_field));
node = avl_insert(rsp_query_fields_hash, &rsp_query_fields[i]);
if (!node)
{
if (errno != EEXIST)
DPRINTF(E_FATAL, L_RSP, "RSP query init failed; AVL insert error: %s\n", strerror(errno));
else
{
node = avl_search(rsp_query_fields_hash, &rsp_query_fields[i]);
rqfm = node->item;
DPRINTF(E_FATAL, L_RSP, "RSP query init failed; WARNING: duplicate hash key\n");
DPRINTF(E_FATAL, L_RSP, "Hash %x, string %s\n", rsp_query_fields[i].hash, rsp_query_fields[i].rsp_field);
DPRINTF(E_FATAL, L_RSP, "Hash %x, string %s\n", rqfm->hash, rqfm->rsp_field);
}
goto avl_insert_fail;
}
}
return 0;
avl_insert_fail:
avl_free_tree(rsp_query_fields_hash);
return -1;
}
void
rsp_query_deinit(void)
{
avl_free_tree(rsp_query_fields_hash);
}