Skip to content

Commit

Permalink
removed ew_del, enhanced readability of ab_trie
Browse files Browse the repository at this point in the history
  • Loading branch information
fabio-sassi committed Oct 11, 2019
1 parent bdc0377 commit e8a2ce2
Show file tree
Hide file tree
Showing 9 changed files with 341 additions and 475 deletions.
33 changes: 14 additions & 19 deletions lib/ab_trie.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ static const char *ab_loStatusName(int status)
AB_CASE(AB_LKUP_EMPTY);
AB_CASE(AB_LKUP_FOUND);
AB_CASE(AB_LKUP_NOVAL);
AB_CASE(AB_LKUP_BRANCH_A_AB);
AB_CASE(AB_LKUP_BRANCH_AB_A);
AB_CASE(AB_LKUP_BRANCH_AB_AC);
AB_CASE(AB_LKUP_BRANCH_OVER);
AB_CASE(AB_LKUP_BRANCH_INTO);
AB_CASE(AB_LKUP_BRANCH_DIFF);
AB_CASE(AB_LKUP_NODE_NOITEM);
AB_CASE(AB_LKUP_NODE_NOSUB);
default:
Expand All @@ -116,11 +116,6 @@ static const char *ab_loStatusName(int status)
#undef AB_CASE







/*
* DEBUG PRINT #SECTION
*/
Expand Down Expand Up @@ -441,8 +436,8 @@ static int ab_loBranch(ab_Look *lo)
position inside this branch */
lo->ipos--;
if (!next) {
/* branch = abc, data = abcdef */
lo->status = AB_LKUP_BRANCH_A_AB;
/* branch = abc, lookup = abcdef */
lo->status = AB_LKUP_BRANCH_OVER;
return false;
}

Expand All @@ -456,7 +451,7 @@ static int ab_loBranch(ab_Look *lo)

if (lo->key[lo->ipos] != b->kdata[lo->bpos]) {
/* branch = abcdef, data = abcxyz */
lo->status = AB_LKUP_BRANCH_AB_AC;
lo->status = AB_LKUP_BRANCH_DIFF;
AB_D printf("lu-b: different char\n");
return false;
}
Expand All @@ -469,8 +464,8 @@ static int ab_loBranch(ab_Look *lo)
else
lo->status = AB_LKUP_NOVAL;
} else {
/* branch = abcdef, data = abc */
lo->status = AB_LKUP_BRANCH_AB_A;
/* branch = abcdef, lookup = abc */
lo->status = AB_LKUP_BRANCH_INTO;
}
return false;
}
Expand Down Expand Up @@ -845,7 +840,7 @@ static void ab_addOnBranch(ab_Look *lo, void *value)

switch(lo->status) {

case AB_LKUP_BRANCH_A_AB: {
case AB_LKUP_BRANCH_OVER: {
/* bpos over branch length: abc + abcdef */
int off = lo->ipos + 1;

Expand All @@ -857,7 +852,7 @@ static void ab_addOnBranch(ab_Look *lo, void *value)
return;
}

case AB_LKUP_BRANCH_AB_A: {
case AB_LKUP_BRANCH_INTO: {
/* abcdef + abc */
ab_Branch *head;
AB_D printf("branchAdd: abcdef + abc\n");
Expand All @@ -867,7 +862,7 @@ static void ab_addOnBranch(ab_Look *lo, void *value)
break;
}

case AB_LKUP_BRANCH_AB_AC: {
case AB_LKUP_BRANCH_DIFF: {
/* abcdef + abcxyz */
ab_Node *node;
ab_Wood *head;
Expand Down Expand Up @@ -1493,9 +1488,9 @@ void* ab_set(ab_Look *lo, void *val)
ab_setValue(lo, val);
break;

case AB_LKUP_BRANCH_A_AB:
case AB_LKUP_BRANCH_AB_A:
case AB_LKUP_BRANCH_AB_AC:
case AB_LKUP_BRANCH_OVER:
case AB_LKUP_BRANCH_INTO:
case AB_LKUP_BRANCH_DIFF:
case AB_LKUP_NODE_NOITEM:
case AB_LKUP_NODE_NOSUB:
ab_addOn(lo, val);
Expand Down
87 changes: 48 additions & 39 deletions lib/ab_trie.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@
#endif


typedef struct {
uint8_t flag;
char letter;
uint8_t n;
uint8_t v;
} ab_NodeItem;

#define AB_NODE 1
#define AB_BRANCH 2

Expand All @@ -59,6 +52,52 @@ typedef struct {
#define AB_ITEM_SUB 4
#define AB_ITEM_MASK (AB_ITEM_ON | AB_ITEM_VAL | AB_ITEM_SUB)

#define AB_MIN(x, y) (((x) < (y)) ? (x) : (y))

enum {
/* lookup initialized but not performed */
AB_LKUP_INIT,

/* lookup out of sync - used in a set or del operation */
AB_LKUP_UNSYNC,

/* found - pattern exists in trie and have value */
AB_LKUP_FOUND,

/* not found - trie is empty */
AB_LKUP_EMPTY,

/* not found - reached leaf-branch end (b = ab, lk = abc) */
AB_LKUP_BRANCH_OVER,

/* not found - stand inside branch (b = abc, lk = ab) */
AB_LKUP_BRANCH_INTO,

/* not found - different chars in lookup branch (b = abc, lk = axy) */
AB_LKUP_BRANCH_DIFF,

/* not found - char not found in node */
AB_LKUP_NODE_NOITEM,

/* not found - reached a leaf-node */
AB_LKUP_NODE_NOSUB,

/* not found - pattern exists in trie but have no value */
AB_LKUP_NOVAL
};



typedef struct {
uint8_t flag;
char letter;
uint8_t n;
uint8_t v;
} ab_NodeItem;





typedef struct {
uint8_t flag;
Expand Down Expand Up @@ -91,37 +130,6 @@ typedef struct {



enum {
/* lookup initialized but not performed */
AB_LKUP_INIT,

/* lookup out of sync - used in a set or del operation */
AB_LKUP_UNSYNC,

/* found - pattern exists in trie and have value */
AB_LKUP_FOUND,

/* not found - trie is empty */
AB_LKUP_EMPTY,

/* not found - reached leaf-branch end (b = ab, lk = abc) */
AB_LKUP_BRANCH_A_AB,

/* not found - stand inside branch (b = abc, lk = ab) */
AB_LKUP_BRANCH_AB_A,

/* not found - different chars in lookup branch (b = abc, lk = axy) */
AB_LKUP_BRANCH_AB_AC,

/* not found - char not found in node */
AB_LKUP_NODE_NOITEM,

/* not found - reached a leaf-node */
AB_LKUP_NODE_NOSUB,

/* not found - pattern exists in trie but have no value */
AB_LKUP_NOVAL
};


/* TODO
Expand Down Expand Up @@ -157,7 +165,8 @@ typedef struct {
ab_Cursor path[3];
} ab_Look;

#define AB_MIN(x, y) (((x) < (y)) ? (x) : (y))



void ab_printWood(ab_Wood *w, int indent, int recursive);
void ab_printKeys(ab_Wood *w, int indent);
Expand Down
12 changes: 6 additions & 6 deletions lib/ew.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@

/* wrapping for trigger event notification system */

#ifdef __sun
#error "Solaris based systems are not supported"
#endif

#ifdef __linux__
#include <sys/epoll.h>
#include <sys/epoll.h> /* linux epoll */
typedef struct epoll_event ew_Event;
#else
#ifdef __sun
#error "SunOS is not supported"
#endif
#include <sys/event.h> /* kqueue */
#include <sys/event.h> /* xBSD kqueue */
typedef struct kevent ew_Event;
#endif

Expand All @@ -50,7 +51,6 @@ void* ew_data(ew_Event *ev);
const char* ew_flags(ew_Event *ev);

void ew_add(int efd, int sock, int flag, void *ptr);
void ew_del(int efd, int sock);

int ew_wait(int epfd, ew_Event *events, int maxevents, int timeout);

Expand Down
2 changes: 2 additions & 0 deletions lib/ew_epoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ void* ew_data(ew_Event *ev)

}

#if 0
void ew_del(int efd, int fd)
{
if (epoll_ctl(efd, EPOLL_CTL_DEL, fd, NULL) == -1)
ea_pfatal("ew_del: error in epoll_ctl del");
}
#endif

void ew_add(int efd, int fd, int ewflag, void *ptr)
{
Expand Down
15 changes: 4 additions & 11 deletions lib/ew_kqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "ea.h"

#include "ew.h"

typedef struct kevent ew_Event;

/*---------------------------------------------------------------------------
* epoll and socket
Expand Down Expand Up @@ -59,6 +61,7 @@ void* ew_data(ew_Event *ev)
return ((struct kevent*)ev)->udata;
}

#if 0
void ew_del(int kq, int fd)
{
struct kevent kevdel;
Expand All @@ -72,15 +75,6 @@ void ew_del(int kq, int fd)
Levin sets always READ | WRITE so as a temporary workaround
remove READ and WRITE
*/
#if 0
kevdel.ident = fd;
/* this is a temporary workaround for OpenBSD FIXME */
kevdel.filter = EVFILT_READ | EVFILT_WRITE;
kevdel.fflags = 0;
kevdel.flags = EV_DELETE;
kevdel.data = 0;
kevdel.udata = NULL;
#else
EV_SET(&kevdel, fd, EVFILT_READ, EV_DELETE, 0, 0, 0);
if (kevent(kq, &kevdel, 1, NULL, 0, NULL) == -1)
ea_pfatal("ew_del: error in kevent del");
Expand All @@ -89,9 +83,8 @@ void ew_del(int kq, int fd)
EV_SET(&kevdel, fd, EVFILT_WRITE, EV_DELETE, 0, 0, 0);
if (kevent(kq, &kevdel, 1, NULL, 0, NULL) == -1)
ea_pfatal("ew_del: error in kevent del");
#endif

}
#endif

void ew_add(int kq, int fd, int flag, void *ptr)
{
Expand Down
4 changes: 0 additions & 4 deletions server.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ void connClose(int fd)
{
DBG1 report("close connection socket user=%d", fd);

ew_del(evfd, fd);

io_closeConnectionSocket(fd);
}

Expand Down Expand Up @@ -163,8 +161,6 @@ static void closeListenSocket()
{
if (evfd) {
DBG0 report("unregister listen socket");
if (listensocket)
ew_del(evfd, listensocket);

ew_free(evfd);
evfd = 0;
Expand Down
2 changes: 1 addition & 1 deletion server.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#include "zm.h"
#include "log.h"

#define LEVIN_VERSION "0.4"
#define LEVIN_VERSION "0.5"

#define PORT 5210
#define LISTEN_BACKLOG 50
Expand Down
Loading

0 comments on commit e8a2ce2

Please sign in to comment.