libndb: cleanup

This commit is contained in:
cinap_lenrek 2018-04-28 04:38:34 +02:00
parent 5aae3d344b
commit dbfb320566
8 changed files with 57 additions and 104 deletions

View file

@ -14,15 +14,12 @@ char*
csgetvalue(char *netroot, char *attr, char *val, char *rattr, Ndbtuple **pp) csgetvalue(char *netroot, char *attr, char *val, char *rattr, Ndbtuple **pp)
{ {
Ndbtuple *t, *first, *last; Ndbtuple *t, *first, *last;
int n, linefound;
char line[1024]; char line[1024];
int fd; int fd, n;
int oops = 0;
char *rv; char *rv;
if(pp) if(pp != nil)
*pp = nil; *pp = nil;
rv = nil;
if(netroot) if(netroot)
snprint(line, sizeof(line), "%s/cs", netroot); snprint(line, sizeof(line), "%s/cs", netroot);
@ -30,17 +27,17 @@ csgetvalue(char *netroot, char *attr, char *val, char *rattr, Ndbtuple **pp)
strcpy(line, "/net/cs"); strcpy(line, "/net/cs");
fd = open(line, ORDWR); fd = open(line, ORDWR);
if(fd < 0) if(fd < 0)
return 0; return nil;
seek(fd, 0, 0); seek(fd, 0, 0);
snprint(line, sizeof(line), "!%s=%s %s=*", attr, val, rattr); snprint(line, sizeof(line), "!%s=%s %s=*", attr, val, rattr);
if(write(fd, line, strlen(line)) < 0){ if(write(fd, line, strlen(line)) < 0){
close(fd); close(fd);
return 0; return nil;
} }
seek(fd, 0, 0); seek(fd, 0, 0);
first = last = 0; rv = nil;
linefound = 0; first = last = nil;
for(;;){ for(;;){
n = read(fd, line, sizeof(line)-2); n = read(fd, line, sizeof(line)-2);
if(n <= 0) if(n <= 0)
@ -49,35 +46,22 @@ csgetvalue(char *netroot, char *attr, char *val, char *rattr, Ndbtuple **pp)
line[n+1] = 0; line[n+1] = 0;
t = _ndbparseline(line); t = _ndbparseline(line);
if(t == 0) if(t == nil)
continue; continue;
if(first) if(first != nil)
last->entry = t; last->entry = t;
else else
first = t; first = t;
do {
last = t; last = t;
if(rv == nil && strcmp(rattr, t->attr) == 0)
while(last->entry)
last = last->entry;
for(; t; t = t->entry){
if(linefound == 0){
if(strcmp(rattr, t->attr) == 0){
linefound = 1;
rv = strdup(t->val); rv = strdup(t->val);
} t = t->entry;
} } while(t != nil);
}
} }
close(fd); close(fd);
if(oops){ if(pp != nil){
werrstr("buffer too short");
ndbfree(first);
return nil;
}
if(pp){
setmalloctag(first, getcallerpc(&netroot)); setmalloctag(first, getcallerpc(&netroot));
*pp = first; *pp = first;
} else } else

View file

@ -129,17 +129,14 @@ doquery(int fd, char *dn, char *type)
t = _ndbparseline(buf); t = _ndbparseline(buf);
if(t != nil){ if(t != nil){
if(first) if(first != nil)
last->entry = t; last->entry = t;
else else
first = t; first = t;
last = t; last = t;
while(last->entry != nil)
while(last->entry)
last = last->entry; last = last->entry;
} }
} }
ndbsetmalloctag(first, getcallerpc(&fd));
return first; return first;
} }

View file

@ -25,6 +25,5 @@ ndbdiscard(Ndbtuple *t, Ndbtuple *a)
a->entry = nil; a->entry = nil;
ndbfree(a); ndbfree(a);
ndbsetmalloctag(t, getcallerpc(&t));
return t; return t;
} }

View file

@ -71,6 +71,6 @@ ndbnew(char *attr, char *val)
void void
ndbsetmalloctag(Ndbtuple *t, uintptr tag) ndbsetmalloctag(Ndbtuple *t, uintptr tag)
{ {
for(; t; t=t->entry) for(; t != nil; t=t->entry)
setmalloctag(t, tag); setmalloctag(t, tag);
} }

View file

@ -7,7 +7,7 @@
* search for a tuple that has the given 'attr=val' and also 'rattr=x'. * search for a tuple that has the given 'attr=val' and also 'rattr=x'.
* copy 'x' into 'buf' and return the whole tuple. * copy 'x' into 'buf' and return the whole tuple.
* *
* return 0 if not found. * return nil if not found.
*/ */
char* char*
ndbgetvalue(Ndb *db, Ndbs *s, char *attr, char *val, char *rattr, Ndbtuple **pp) ndbgetvalue(Ndb *db, Ndbs *s, char *attr, char *val, char *rattr, Ndbtuple **pp)
@ -21,11 +21,9 @@ ndbgetvalue(Ndb *db, Ndbs *s, char *attr, char *val, char *rattr, Ndbtuple **pp)
if(pp) if(pp)
*pp = nil; *pp = nil;
t = ndbsearch(db, s, attr, val); t = ndbsearch(db, s, attr, val);
while(t){ while(t != nil){
/* first look on same line (closer binding) */ nt = ndbfindattr(t, s->t, rattr);
nt = s->t; if(nt != nil){
for(;;){
if(strcmp(rattr, nt->attr) == 0){
rv = strdup(nt->val); rv = strdup(nt->val);
if(pp != nil) if(pp != nil)
*pp = t; *pp = t;
@ -33,21 +31,6 @@ ndbgetvalue(Ndb *db, Ndbs *s, char *attr, char *val, char *rattr, Ndbtuple **pp)
ndbfree(t); ndbfree(t);
return rv; return rv;
} }
nt = nt->line;
if(nt == s->t)
break;
}
/* search whole tuple */
for(nt = t; nt; nt = nt->entry){
if(strcmp(rattr, nt->attr) == 0){
rv = strdup(nt->val);
if(pp != nil)
*pp = t;
else
ndbfree(t);
return rv;
}
}
ndbfree(t); ndbfree(t);
t = ndbsnext(s, attr, val); t = ndbsnext(s, attr, val);
} }

View file

@ -123,26 +123,22 @@ ndbsearch(Ndb *db, Ndbs *s, char *attr, char *val)
memset(s, 0, sizeof(*s)); memset(s, 0, sizeof(*s));
if(_ndbcachesearch(db, s, attr, val, &t) == 0){ if(_ndbcachesearch(db, s, attr, val, &t) == 0){
/* found in cache */ /* found in cache */
if(t != nil){ if(t != nil)
ndbsetmalloctag(t, getcallerpc(&db)); goto out;
return t; /* answer from this file */
}
if(db->next == nil) if(db->next == nil)
return nil; return nil;
t = ndbsearch(db->next, s, attr, val); t = ndbsearch(db->next, s, attr, val);
ndbsetmalloctag(t, getcallerpc(&db)); goto out;
return t;
} }
s->db = db; s->db = db;
s->hf = hf; s->hf = hf;
if(s->hf){ if(s->hf != nil){
s->ptr = ndbhash(val, s->hf->hlen)*NDBPLEN; s->ptr = ndbhash(val, s->hf->hlen)*NDBPLEN;
p = hfread(s->hf, s->ptr+NDBHLEN, NDBPLEN); p = hfread(s->hf, s->ptr+NDBHLEN, NDBPLEN);
if(p == 0){ if(p == nil){
t = _ndbcacheadd(db, s, attr, val, nil); t = _ndbcacheadd(db, s, attr, val, nil);
ndbsetmalloctag(t, getcallerpc(&db)); goto out;
return t;
} }
s->ptr = NDBGETP(p); s->ptr = NDBGETP(p);
s->type = Cptr1; s->type = Cptr1;
@ -153,17 +149,17 @@ ndbsearch(Ndb *db, Ndbs *s, char *attr, char *val)
/* advance search to next db file */ /* advance search to next db file */
s->ptr = NDBNAP; s->ptr = NDBNAP;
_ndbcacheadd(db, s, attr, val, nil); _ndbcacheadd(db, s, attr, val, nil);
if(db->next == 0) if(db->next == nil)
return nil; return nil;
t = ndbsearch(db->next, s, attr, val); t = ndbsearch(db->next, s, attr, val);
ndbsetmalloctag(t, getcallerpc(&db)); goto out;
return t;
} else { } else {
s->ptr = 0; s->ptr = 0;
s->type = Dptr; s->type = Dptr;
} }
t = ndbsnext(s, attr, val); t = ndbsnext(s, attr, val);
_ndbcacheadd(db, s, attr, val, (t != nil && s->db == db)?t:nil); _ndbcacheadd(db, s, attr, val, (t != nil && s->db == db)?t:nil);
out:
ndbsetmalloctag(t, getcallerpc(&db)); ndbsetmalloctag(t, getcallerpc(&db));
return t; return t;
} }
@ -173,7 +169,7 @@ match(Ndbtuple *t, char *attr, char *val)
{ {
Ndbtuple *nt; Ndbtuple *nt;
for(nt = t; nt; nt = nt->entry) for(nt = t; nt != nil; nt = nt->entry)
if(strcmp(attr, nt->attr) == 0 if(strcmp(attr, nt->attr) == 0
&& strcmp(val, nt->val) == 0) && strcmp(val, nt->val) == 0)
return nt; return nt;
@ -200,12 +196,10 @@ ndbsnext(Ndbs *s, char *attr, char *val)
break; break;
t = ndbparse(db); t = ndbparse(db);
s->ptr = Boffset(&db->b); s->ptr = Boffset(&db->b);
if(t == 0) if(t == nil)
break; break;
if(s->t = match(t, attr, val)){ if((s->t = match(t, attr, val)) != nil)
ndbsetmalloctag(t, getcallerpc(&s)); goto out;
return t;
}
ndbfree(t); ndbfree(t);
} else if(s->type == Cptr){ } else if(s->type == Cptr){
if(Bseek(&db->b, s->ptr, 0) < 0) if(Bseek(&db->b, s->ptr, 0) < 0)
@ -213,18 +207,16 @@ ndbsnext(Ndbs *s, char *attr, char *val)
s->ptr = s->ptr1; s->ptr = s->ptr1;
s->type = Cptr1; s->type = Cptr1;
t = ndbparse(db); t = ndbparse(db);
if(t == 0) if(t == nil)
break; break;
if(s->t = match(t, attr, val)){ if((s->t = match(t, attr, val)) != nil)
ndbsetmalloctag(t, getcallerpc(&s)); goto out;
return t;
}
ndbfree(t); ndbfree(t);
} else if(s->type == Cptr1){ } else if(s->type == Cptr1){
if(s->ptr & NDBCHAIN){ /* hash chain continuation */ if(s->ptr & NDBCHAIN){ /* hash chain continuation */
s->ptr &= ~NDBCHAIN; s->ptr &= ~NDBCHAIN;
p = hfread(s->hf, s->ptr+NDBHLEN, 2*NDBPLEN); p = hfread(s->hf, s->ptr+NDBHLEN, 2*NDBPLEN);
if(p == 0) if(p == nil)
break; break;
s->ptr = NDBGETP(p); s->ptr = NDBGETP(p);
s->ptr1 = NDBGETP(p+NDBPLEN); s->ptr1 = NDBGETP(p+NDBPLEN);
@ -234,12 +226,10 @@ ndbsnext(Ndbs *s, char *attr, char *val)
break; break;
s->ptr = NDBNAP; s->ptr = NDBNAP;
t = ndbparse(db); t = ndbparse(db);
if(t == 0) if(t == nil)
break; break;
if(s->t = match(t, attr, val)){ if((s->t = match(t, attr, val)) != nil)
ndbsetmalloctag(t, getcallerpc(&s)); goto out;
return t;
}
ndbfree(t); ndbfree(t);
break; break;
} }
@ -247,14 +237,14 @@ ndbsnext(Ndbs *s, char *attr, char *val)
} }
nextfile: nextfile:
/* nothing left to search? */ /* nothing left to search? */
s->ptr = NDBNAP; s->ptr = NDBNAP;
if(db->next == 0) if(db->next == nil)
return 0; return nil;
/* advance search to next db file */ /* advance search to next db file */
t = ndbsearch(db->next, s, attr, val); t = ndbsearch(db->next, s, attr, val);
out:
ndbsetmalloctag(t, getcallerpc(&s)); ndbsetmalloctag(t, getcallerpc(&s));
return t; return t;
} }

View file

@ -30,9 +30,9 @@ ndbparse(Ndb *db)
Ndbtuple *first, *last; Ndbtuple *first, *last;
int len; int len;
first = last = 0; first = last = nil;
for(;;){ for(;;){
if((line = Brdline(&db->b, '\n')) == 0) if((line = Brdline(&db->b, '\n')) == nil)
break; break;
len = Blinelen(&db->b); len = Blinelen(&db->b);
if(line[len-1] != '\n') if(line[len-1] != '\n')
@ -42,15 +42,15 @@ ndbparse(Ndb *db)
break; break;
} }
t = _ndbparseline(line); t = _ndbparseline(line);
if(t == 0) if(t == nil)
continue; continue;
setmalloctag(t, getcallerpc(&db)); setmalloctag(t, getcallerpc(&db));
if(first) if(first != nil)
last->entry = t; last->entry = t;
else else
first = t; first = t;
last = t; last = t;
while(last->entry) while(last->entry != nil)
last = last->entry; last = last->entry;
} }
ndbsetmalloctag(first, getcallerpc(&db)); ndbsetmalloctag(first, getcallerpc(&db));

View file

@ -4,7 +4,7 @@
#include <ndb.h> #include <ndb.h>
/* /*
* reorder the tuple to put x's line first in the entry and x fitst in its line * reorder the tuple to put x's line first in the entry and x first in its line
*/ */
Ndbtuple* Ndbtuple*
ndbreorder(Ndbtuple *t, Ndbtuple *x) ndbreorder(Ndbtuple *t, Ndbtuple *x)