Skip to content

Commit

Permalink
Upd: rax
Browse files Browse the repository at this point in the history
  • Loading branch information
jcorporation committed Sep 23, 2024
1 parent 724701f commit 7b552bf
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 53 deletions.
29 changes: 11 additions & 18 deletions dist/rax/rax.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,20 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "rax.h"

#undef NDEBUG
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include <stdio.h>
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rax.h"

#ifndef RAX_MALLOC_INCLUDE
#define RAX_MALLOC_INCLUDE "rax_malloc.h"
#endif

#include RAX_MALLOC_INCLUDE

/* This is a special pointer that is guaranteed to never have the same value
* of a radix tree node. It's used in order to report "not found" error without
* requiring the function to have multiple return values. */
void *raxNotFound = (void*)"rax-not-found-pointer";

/* -------------------------------- Debugging ------------------------------ */

void raxDebugShowNode(const char *msg, raxNode *n);
Expand Down Expand Up @@ -915,18 +907,19 @@ int raxTryInsert(rax *rax, unsigned char *s, size_t len, void *data, void **old)
return raxGenericInsert(rax,s,len,data,old,0);
}

/* Find a key in the rax, returns raxNotFound special void pointer value
* if the item was not found, otherwise the value associated with the
* item is returned. */
void *raxFind(rax *rax, unsigned char *s, size_t len) {
/* Find a key in the rax: return 1 if the item is found, 0 otherwise.
* If there is an item and 'value' is passed in a non-NULL pointer,
* the value associated with the item is set at that address. */
int raxFind(rax *rax, unsigned char *s, size_t len, void **value) {
raxNode *h;

debugf("### Lookup: %.*s\n", (int)len, s);
int splitpos = 0;
size_t i = raxLowWalk(rax,s,len,&h,NULL,&splitpos,NULL);
if (i != len || (h->iscompr && splitpos != 0) || !h->iskey)
return raxNotFound;
return raxGetData(h);
return 0;
if (value != NULL) *value = raxGetData(h);
return 1;
}

/* Return the memory address where the 'parent' node stores the specified
Expand Down
5 changes: 1 addition & 4 deletions dist/rax/rax.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,12 @@ typedef struct raxIterator {
raxNodeCallback node_cb; /* Optional node callback. Normally set to NULL. */
} raxIterator;

/* A special pointer returned for not found items. */
extern void *raxNotFound;

/* Exported API. */
rax *raxNew(void);
int raxInsert(rax *rax, unsigned char *s, size_t len, void *data, void **old);
int raxTryInsert(rax *rax, unsigned char *s, size_t len, void *data, void **old);
int raxRemove(rax *rax, unsigned char *s, size_t len, void **old);
void *raxFind(rax *rax, unsigned char *s, size_t len);
int raxFind(rax *rax, unsigned char *s, size_t len, void **value);
void raxFree(rax *rax);
void raxFreeWithCallback(rax *rax, void (*free_callback)(void*));
void raxStart(raxIterator *it, rax *rt);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/cache_rax_album.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ struct mpd_song *album_cache_get_album(struct t_cache *album_cache, sds key) {
return NULL;
}
//try to get album
void *data = raxFind(album_cache->cache, (unsigned char*)key, sdslen(key));
if (data == raxNotFound) {
void *data;
if (raxFind(album_cache->cache, (unsigned char*)key, sdslen(key), &data) == 0) {
MYMPD_LOG_ERROR(NULL, "Album for key \"%s\" not found in cache", key);
return NULL;
}
Expand Down
27 changes: 12 additions & 15 deletions src/lib/webradio.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,20 @@
struct t_webradio_data *webradio_by_uri(struct t_webradios *webradio_favorites, struct t_webradios *webradiodb,
const char *uri)
{
void *data = raxNotFound;
if (webradio_favorites != NULL) {
if (webradio_favorites->idx_uris != NULL) {
data = raxFind(webradio_favorites->idx_uris, (unsigned char *)uri, strlen(uri));
}
void *data;
if (webradio_favorites != NULL &&
webradio_favorites->idx_uris != NULL &&
raxFind(webradio_favorites->idx_uris, (unsigned char *)uri, strlen(uri), &data) == 1)
{
return (struct t_webradio_data *)data;
}
if (webradiodb != NULL) {
if (data == raxNotFound) {
if (webradiodb->idx_uris != NULL) {
data = raxFind(webradiodb->idx_uris, (unsigned char *)uri, strlen(uri));
}
}
if (webradiodb != NULL &&
webradiodb->idx_uris != NULL &&
raxFind(webradiodb->idx_uris, (unsigned char *)uri, strlen(uri), &data) == 1)
{
return (struct t_webradio_data *)data;
}
if (data == raxNotFound) {
return NULL;
}
return (struct t_webradio_data *)data;
return NULL;
}

/**
Expand Down
17 changes: 9 additions & 8 deletions src/mpd_client/random_select.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,13 @@ static bool check_not_hated(rax *stickers_like, const char *uri, bool ignore_hat
{
return true;
}
void *sticker_value_hated = raxFind(stickers_like, (unsigned char *)uri, strlen(uri));
return sticker_value_hated == raxNotFound
? true
: ((sds)sticker_value_hated)[0] == '0'
? false
: true;
void *sticker_value_hated;
if (raxFind(stickers_like, (unsigned char *)uri, strlen(uri), &sticker_value_hated) == 0) {
return true;
}
return ((sds)sticker_value_hated)[0] == '0'
? false
: true;
}

/**
Expand All @@ -429,8 +430,8 @@ static bool check_last_played(rax *stickers_last_played, const char *uri, time_t
if (stickers_last_played == NULL) {
return true;
}
void *sticker_value_last_played = raxFind(stickers_last_played, (unsigned char *)uri, strlen(uri));
if (sticker_value_last_played == raxNotFound) {
void *sticker_value_last_played;
if (raxFind(stickers_last_played, (unsigned char *)uri, strlen(uri), &sticker_value_last_played) == 0) {
return true;
}
int64_t sticker_last_played;
Expand Down
8 changes: 4 additions & 4 deletions src/mympd_api/webradio.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ sds mympd_api_webradio_print(struct t_webradio_data *webradio, sds buffer, const
sds mympd_api_webradio_radio_get_by_name(struct t_webradios *webradios, sds buffer, unsigned request_id,
enum mympd_cmd_ids cmd_id, sds name)
{
void *data = raxFind(webradios->db, (unsigned char *)name, strlen(name));
if (data == raxNotFound) {
void *data;
if (raxFind(webradios->db, (unsigned char *)name, strlen(name), &data) == 0) {
return jsonrpc_respond_message(buffer, cmd_id, request_id,
JSONRPC_FACILITY_DATABASE, JSONRPC_SEVERITY_ERROR, "Webradio entry not found");
}
Expand All @@ -259,8 +259,8 @@ sds mympd_api_webradio_radio_get_by_name(struct t_webradios *webradios, sds buff
sds mympd_api_webradio_radio_get_by_uri(struct t_webradios *webradios, sds buffer, unsigned request_id,
enum mympd_cmd_ids cmd_id, sds uri)
{
void *data = raxFind(webradios->idx_uris, (unsigned char *)uri, strlen(uri));
if (data == raxNotFound) {
void *data;
if (raxFind(webradios->idx_uris, (unsigned char *)uri, strlen(uri), &data) == 0) {
return jsonrpc_respond_message(buffer, cmd_id, request_id,
JSONRPC_FACILITY_DATABASE, JSONRPC_SEVERITY_ERROR, "Webradio entry not found");
}
Expand Down
6 changes: 4 additions & 2 deletions src/mympd_api/webradio_favorites.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ bool mympd_api_webradio_favorite_save(struct t_webradios *webradio_favorites, st
list_init(&old_names);
if (sdslen(old_name) > 0) {
list_push(&old_names, old_name, 0, NULL, NULL);
struct t_webradio_data *old_radio = raxFind(webradio_favorites->idx_uris, (unsigned char *)old_name, strlen(old_name));
if (old_radio != raxNotFound) {
void *data;
if (raxFind(webradio_favorites->idx_uris, (unsigned char *)old_name, strlen(old_name), &data) == 1) {
// preserve added timestamp
struct t_webradio_data *old_radio = (struct t_webradio_data *)data;
webradio->added = old_radio->added;
}
}
Expand Down

0 comments on commit 7b552bf

Please sign in to comment.