libndb: cleanup
This commit is contained in:
parent
5aae3d344b
commit
dbfb320566
8 changed files with 57 additions and 104 deletions
|
@ -14,15 +14,12 @@ char*
|
|||
csgetvalue(char *netroot, char *attr, char *val, char *rattr, Ndbtuple **pp)
|
||||
{
|
||||
Ndbtuple *t, *first, *last;
|
||||
int n, linefound;
|
||||
char line[1024];
|
||||
int fd;
|
||||
int oops = 0;
|
||||
int fd, n;
|
||||
char *rv;
|
||||
|
||||
if(pp)
|
||||
if(pp != nil)
|
||||
*pp = nil;
|
||||
rv = nil;
|
||||
|
||||
if(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");
|
||||
fd = open(line, ORDWR);
|
||||
if(fd < 0)
|
||||
return 0;
|
||||
return nil;
|
||||
seek(fd, 0, 0);
|
||||
snprint(line, sizeof(line), "!%s=%s %s=*", attr, val, rattr);
|
||||
if(write(fd, line, strlen(line)) < 0){
|
||||
close(fd);
|
||||
return 0;
|
||||
return nil;
|
||||
}
|
||||
seek(fd, 0, 0);
|
||||
|
||||
first = last = 0;
|
||||
linefound = 0;
|
||||
rv = nil;
|
||||
first = last = nil;
|
||||
for(;;){
|
||||
n = read(fd, line, sizeof(line)-2);
|
||||
if(n <= 0)
|
||||
|
@ -49,35 +46,22 @@ csgetvalue(char *netroot, char *attr, char *val, char *rattr, Ndbtuple **pp)
|
|||
line[n+1] = 0;
|
||||
|
||||
t = _ndbparseline(line);
|
||||
if(t == 0)
|
||||
if(t == nil)
|
||||
continue;
|
||||
if(first)
|
||||
if(first != nil)
|
||||
last->entry = t;
|
||||
else
|
||||
first = t;
|
||||
last = t;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
do {
|
||||
last = t;
|
||||
if(rv == nil && strcmp(rattr, t->attr) == 0)
|
||||
rv = strdup(t->val);
|
||||
t = t->entry;
|
||||
} while(t != nil);
|
||||
}
|
||||
close(fd);
|
||||
|
||||
if(oops){
|
||||
werrstr("buffer too short");
|
||||
ndbfree(first);
|
||||
return nil;
|
||||
}
|
||||
|
||||
if(pp){
|
||||
if(pp != nil){
|
||||
setmalloctag(first, getcallerpc(&netroot));
|
||||
*pp = first;
|
||||
} else
|
||||
|
|
|
@ -129,17 +129,14 @@ doquery(int fd, char *dn, char *type)
|
|||
|
||||
t = _ndbparseline(buf);
|
||||
if(t != nil){
|
||||
if(first)
|
||||
if(first != nil)
|
||||
last->entry = t;
|
||||
else
|
||||
first = t;
|
||||
last = t;
|
||||
|
||||
while(last->entry)
|
||||
while(last->entry != nil)
|
||||
last = last->entry;
|
||||
}
|
||||
}
|
||||
|
||||
ndbsetmalloctag(first, getcallerpc(&fd));
|
||||
return first;
|
||||
}
|
||||
|
|
|
@ -25,6 +25,5 @@ ndbdiscard(Ndbtuple *t, Ndbtuple *a)
|
|||
a->entry = nil;
|
||||
ndbfree(a);
|
||||
|
||||
ndbsetmalloctag(t, getcallerpc(&t));
|
||||
return t;
|
||||
}
|
||||
|
|
|
@ -71,6 +71,6 @@ ndbnew(char *attr, char *val)
|
|||
void
|
||||
ndbsetmalloctag(Ndbtuple *t, uintptr tag)
|
||||
{
|
||||
for(; t; t=t->entry)
|
||||
for(; t != nil; t=t->entry)
|
||||
setmalloctag(t, tag);
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
* search for a tuple that has the given 'attr=val' and also 'rattr=x'.
|
||||
* copy 'x' into 'buf' and return the whole tuple.
|
||||
*
|
||||
* return 0 if not found.
|
||||
* return nil if not found.
|
||||
*/
|
||||
char*
|
||||
ndbgetvalue(Ndb *db, Ndbs *s, char *attr, char *val, char *rattr, Ndbtuple **pp)
|
||||
|
@ -21,32 +21,15 @@ ndbgetvalue(Ndb *db, Ndbs *s, char *attr, char *val, char *rattr, Ndbtuple **pp)
|
|||
if(pp)
|
||||
*pp = nil;
|
||||
t = ndbsearch(db, s, attr, val);
|
||||
while(t){
|
||||
/* first look on same line (closer binding) */
|
||||
nt = s->t;
|
||||
for(;;){
|
||||
if(strcmp(rattr, nt->attr) == 0){
|
||||
rv = strdup(nt->val);
|
||||
if(pp != nil)
|
||||
*pp = t;
|
||||
else
|
||||
ndbfree(t);
|
||||
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;
|
||||
}
|
||||
while(t != nil){
|
||||
nt = ndbfindattr(t, s->t, rattr);
|
||||
if(nt != nil){
|
||||
rv = strdup(nt->val);
|
||||
if(pp != nil)
|
||||
*pp = t;
|
||||
else
|
||||
ndbfree(t);
|
||||
return rv;
|
||||
}
|
||||
ndbfree(t);
|
||||
t = ndbsnext(s, attr, val);
|
||||
|
|
|
@ -123,26 +123,22 @@ ndbsearch(Ndb *db, Ndbs *s, char *attr, char *val)
|
|||
memset(s, 0, sizeof(*s));
|
||||
if(_ndbcachesearch(db, s, attr, val, &t) == 0){
|
||||
/* found in cache */
|
||||
if(t != nil){
|
||||
ndbsetmalloctag(t, getcallerpc(&db));
|
||||
return t; /* answer from this file */
|
||||
}
|
||||
if(t != nil)
|
||||
goto out;
|
||||
if(db->next == nil)
|
||||
return nil;
|
||||
t = ndbsearch(db->next, s, attr, val);
|
||||
ndbsetmalloctag(t, getcallerpc(&db));
|
||||
return t;
|
||||
goto out;
|
||||
}
|
||||
|
||||
s->db = db;
|
||||
s->hf = hf;
|
||||
if(s->hf){
|
||||
if(s->hf != nil){
|
||||
s->ptr = ndbhash(val, s->hf->hlen)*NDBPLEN;
|
||||
p = hfread(s->hf, s->ptr+NDBHLEN, NDBPLEN);
|
||||
if(p == 0){
|
||||
if(p == nil){
|
||||
t = _ndbcacheadd(db, s, attr, val, nil);
|
||||
ndbsetmalloctag(t, getcallerpc(&db));
|
||||
return t;
|
||||
goto out;
|
||||
}
|
||||
s->ptr = NDBGETP(p);
|
||||
s->type = Cptr1;
|
||||
|
@ -153,17 +149,17 @@ ndbsearch(Ndb *db, Ndbs *s, char *attr, char *val)
|
|||
/* advance search to next db file */
|
||||
s->ptr = NDBNAP;
|
||||
_ndbcacheadd(db, s, attr, val, nil);
|
||||
if(db->next == 0)
|
||||
if(db->next == nil)
|
||||
return nil;
|
||||
t = ndbsearch(db->next, s, attr, val);
|
||||
ndbsetmalloctag(t, getcallerpc(&db));
|
||||
return t;
|
||||
goto out;
|
||||
} else {
|
||||
s->ptr = 0;
|
||||
s->type = Dptr;
|
||||
}
|
||||
t = ndbsnext(s, attr, val);
|
||||
_ndbcacheadd(db, s, attr, val, (t != nil && s->db == db)?t:nil);
|
||||
out:
|
||||
ndbsetmalloctag(t, getcallerpc(&db));
|
||||
return t;
|
||||
}
|
||||
|
@ -173,7 +169,7 @@ match(Ndbtuple *t, char *attr, char *val)
|
|||
{
|
||||
Ndbtuple *nt;
|
||||
|
||||
for(nt = t; nt; nt = nt->entry)
|
||||
for(nt = t; nt != nil; nt = nt->entry)
|
||||
if(strcmp(attr, nt->attr) == 0
|
||||
&& strcmp(val, nt->val) == 0)
|
||||
return nt;
|
||||
|
@ -200,12 +196,10 @@ ndbsnext(Ndbs *s, char *attr, char *val)
|
|||
break;
|
||||
t = ndbparse(db);
|
||||
s->ptr = Boffset(&db->b);
|
||||
if(t == 0)
|
||||
if(t == nil)
|
||||
break;
|
||||
if(s->t = match(t, attr, val)){
|
||||
ndbsetmalloctag(t, getcallerpc(&s));
|
||||
return t;
|
||||
}
|
||||
if((s->t = match(t, attr, val)) != nil)
|
||||
goto out;
|
||||
ndbfree(t);
|
||||
} else if(s->type == Cptr){
|
||||
if(Bseek(&db->b, s->ptr, 0) < 0)
|
||||
|
@ -213,18 +207,16 @@ ndbsnext(Ndbs *s, char *attr, char *val)
|
|||
s->ptr = s->ptr1;
|
||||
s->type = Cptr1;
|
||||
t = ndbparse(db);
|
||||
if(t == 0)
|
||||
if(t == nil)
|
||||
break;
|
||||
if(s->t = match(t, attr, val)){
|
||||
ndbsetmalloctag(t, getcallerpc(&s));
|
||||
return t;
|
||||
}
|
||||
if((s->t = match(t, attr, val)) != nil)
|
||||
goto out;
|
||||
ndbfree(t);
|
||||
} else if(s->type == Cptr1){
|
||||
if(s->ptr & NDBCHAIN){ /* hash chain continuation */
|
||||
s->ptr &= ~NDBCHAIN;
|
||||
p = hfread(s->hf, s->ptr+NDBHLEN, 2*NDBPLEN);
|
||||
if(p == 0)
|
||||
if(p == nil)
|
||||
break;
|
||||
s->ptr = NDBGETP(p);
|
||||
s->ptr1 = NDBGETP(p+NDBPLEN);
|
||||
|
@ -234,12 +226,10 @@ ndbsnext(Ndbs *s, char *attr, char *val)
|
|||
break;
|
||||
s->ptr = NDBNAP;
|
||||
t = ndbparse(db);
|
||||
if(t == 0)
|
||||
if(t == nil)
|
||||
break;
|
||||
if(s->t = match(t, attr, val)){
|
||||
ndbsetmalloctag(t, getcallerpc(&s));
|
||||
return t;
|
||||
}
|
||||
if((s->t = match(t, attr, val)) != nil)
|
||||
goto out;
|
||||
ndbfree(t);
|
||||
break;
|
||||
}
|
||||
|
@ -247,14 +237,14 @@ ndbsnext(Ndbs *s, char *attr, char *val)
|
|||
}
|
||||
|
||||
nextfile:
|
||||
|
||||
/* nothing left to search? */
|
||||
s->ptr = NDBNAP;
|
||||
if(db->next == 0)
|
||||
return 0;
|
||||
if(db->next == nil)
|
||||
return nil;
|
||||
|
||||
/* advance search to next db file */
|
||||
t = ndbsearch(db->next, s, attr, val);
|
||||
out:
|
||||
ndbsetmalloctag(t, getcallerpc(&s));
|
||||
return t;
|
||||
}
|
||||
|
|
|
@ -30,9 +30,9 @@ ndbparse(Ndb *db)
|
|||
Ndbtuple *first, *last;
|
||||
int len;
|
||||
|
||||
first = last = 0;
|
||||
first = last = nil;
|
||||
for(;;){
|
||||
if((line = Brdline(&db->b, '\n')) == 0)
|
||||
if((line = Brdline(&db->b, '\n')) == nil)
|
||||
break;
|
||||
len = Blinelen(&db->b);
|
||||
if(line[len-1] != '\n')
|
||||
|
@ -42,15 +42,15 @@ ndbparse(Ndb *db)
|
|||
break;
|
||||
}
|
||||
t = _ndbparseline(line);
|
||||
if(t == 0)
|
||||
if(t == nil)
|
||||
continue;
|
||||
setmalloctag(t, getcallerpc(&db));
|
||||
if(first)
|
||||
if(first != nil)
|
||||
last->entry = t;
|
||||
else
|
||||
first = t;
|
||||
last = t;
|
||||
while(last->entry)
|
||||
while(last->entry != nil)
|
||||
last = last->entry;
|
||||
}
|
||||
ndbsetmalloctag(first, getcallerpc(&db));
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#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*
|
||||
ndbreorder(Ndbtuple *t, Ndbtuple *x)
|
||||
|
|
Loading…
Reference in a new issue