version 1.1, 2012/06/08 10:36:23 |
version 1.2, 2012/06/08 14:14:30 |
|
|
#include "mandocdb.h" |
#include "mandocdb.h" |
#include "mansearch.h" |
#include "mansearch.h" |
|
|
|
#define BIND_TEXT(_db, _s, _i, _v) \ |
|
if (SQLITE_OK != sqlite3_bind_text \ |
|
((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \ |
|
fprintf(stderr, "%s\n", sqlite3_errmsg((_db))) |
|
#define BIND_INT64(_db, _s, _i, _v) \ |
|
if (SQLITE_OK != sqlite3_bind_int64 \ |
|
((_s), (_i)++, (_v))) \ |
|
fprintf(stderr, "%s\n", sqlite3_errmsg((_db))) |
|
|
struct expr { |
struct expr { |
int glob; /* is glob? */ |
int glob; /* is glob? */ |
uint64_t bits; /* type-mask */ |
uint64_t bits; /* type-mask */ |
Line 116 mansearch(const struct manpaths *paths, |
|
Line 125 mansearch(const struct manpaths *paths, |
|
int argc, char *argv[], |
int argc, char *argv[], |
struct manpage **res, size_t *sz) |
struct manpage **res, size_t *sz) |
{ |
{ |
int fd, rc; |
int fd, rc, c; |
int64_t id; |
int64_t id; |
char buf[MAXPATHLEN]; |
char buf[MAXPATHLEN]; |
char *sql; |
char *sql; |
Line 136 mansearch(const struct manpaths *paths, |
|
Line 145 mansearch(const struct manpaths *paths, |
|
info.hfree = hash_free; |
info.hfree = hash_free; |
info.key_offset = offsetof(struct match, id); |
info.key_offset = offsetof(struct match, id); |
|
|
*sz = 0; |
*sz = cur = maxres = 0; |
sql = NULL; |
sql = NULL; |
*res = NULL; |
*res = NULL; |
fd = -1; |
fd = -1; |
e = NULL; |
e = NULL; |
cur = maxres = 0; |
rc = 0; |
|
|
if (0 == argc) |
if (0 == argc) |
goto out; |
goto out; |
Line 175 mansearch(const struct manpaths *paths, |
|
Line 184 mansearch(const struct manpaths *paths, |
|
|
|
for (i = 0; i < paths->sz; i++) { |
for (i = 0; i < paths->sz; i++) { |
if (-1 == fchdir(fd)) { |
if (-1 == fchdir(fd)) { |
/* FIXME: will return success */ |
|
perror(buf); |
perror(buf); |
free(*res); |
free(*res); |
break; |
break; |
Line 184 mansearch(const struct manpaths *paths, |
|
Line 192 mansearch(const struct manpaths *paths, |
|
continue; |
continue; |
} |
} |
|
|
rc = sqlite3_open_v2 |
c = sqlite3_open_v2 |
(MANDOC_DB, &db, SQLITE_OPEN_READONLY, NULL); |
(MANDOC_DB, &db, |
|
SQLITE_OPEN_READONLY, NULL); |
|
|
if (SQLITE_OK != rc) { |
if (SQLITE_OK != c) { |
perror(MANDOC_DB); |
perror(MANDOC_DB); |
sqlite3_close(db); |
sqlite3_close(db); |
continue; |
continue; |
} |
} |
|
|
j = 1; |
j = 1; |
sqlite3_prepare_v2(db, sql, -1, &s, NULL); |
c = sqlite3_prepare_v2(db, sql, -1, &s, NULL); |
|
if (SQLITE_OK != c) |
|
fprintf(stderr, "%s\n", sqlite3_errmsg(db)); |
|
|
if (NULL != arch) |
if (NULL != arch) |
sqlite3_bind_text |
BIND_TEXT(db, s, j, arch); |
(s, j++, arch, -1, SQLITE_STATIC); |
|
if (NULL != sec) |
if (NULL != sec) |
sqlite3_bind_text |
BIND_TEXT(db, s, j, arch); |
(s, j++, sec, -1, SQLITE_STATIC); |
|
|
|
for (ep = e; NULL != ep; ep = ep->next) { |
for (ep = e; NULL != ep; ep = ep->next) { |
sqlite3_bind_text |
BIND_TEXT(db, s, j, ep->v); |
(s, j++, ep->v, -1, SQLITE_STATIC); |
BIND_INT64(db, s, j, ep->bits); |
sqlite3_bind_int64 |
|
(s, j++, ep->bits); |
|
} |
} |
|
|
memset(&htab, 0, sizeof(struct ohash)); |
memset(&htab, 0, sizeof(struct ohash)); |
Line 221 mansearch(const struct manpaths *paths, |
|
Line 228 mansearch(const struct manpaths *paths, |
|
* This gives good performance and preserves the |
* This gives good performance and preserves the |
* distribution of buckets in the table. |
* distribution of buckets in the table. |
*/ |
*/ |
while (SQLITE_ROW == sqlite3_step(s)) { |
while (SQLITE_ROW == (c = sqlite3_step(s))) { |
id = sqlite3_column_int64(s, 0); |
id = sqlite3_column_int64(s, 0); |
idx = ohash_lookup_memory |
idx = ohash_lookup_memory |
(&htab, (char *)&id, |
(&htab, (char *)&id, |
Line 240 mansearch(const struct manpaths *paths, |
|
Line 247 mansearch(const struct manpaths *paths, |
|
ohash_insert(&htab, idx, mp); |
ohash_insert(&htab, idx, mp); |
} |
} |
|
|
|
if (SQLITE_DONE != c) |
|
fprintf(stderr, "%s\n", sqlite3_errmsg(db)); |
|
|
sqlite3_finalize(s); |
sqlite3_finalize(s); |
sqlite3_close(db); |
sqlite3_close(db); |
|
|
Line 263 mansearch(const struct manpaths *paths, |
|
Line 273 mansearch(const struct manpaths *paths, |
|
} |
} |
ohash_delete(&htab); |
ohash_delete(&htab); |
} |
} |
|
rc = 1; |
out: |
out: |
exprfree(e); |
exprfree(e); |
if (-1 != fd) |
if (-1 != fd) |
close(fd); |
close(fd); |
free(sql); |
free(sql); |
*sz = cur; |
*sz = cur; |
return(1); |
return(rc); |
} |
} |
|
|
/* |
/* |
Line 285 sql_statement(const struct expr *e, const char *arch, |
|
Line 296 sql_statement(const struct expr *e, const char *arch, |
|
const char *eq = "(key = ? AND bits & ?)"; |
const char *eq = "(key = ? AND bits & ?)"; |
const char *andarch = "arch = ? AND "; |
const char *andarch = "arch = ? AND "; |
const char *andsec = "sec = ? AND "; |
const char *andsec = "sec = ? AND "; |
const size_t globsz = 27; |
size_t globsz; |
const size_t eqsz = 22; |
size_t eqsz; |
size_t sz; |
size_t sz; |
|
|
sql = mandoc_strdup |
sql = mandoc_strdup |
Line 295 sql_statement(const struct expr *e, const char *arch, |
|
Line 306 sql_statement(const struct expr *e, const char *arch, |
|
"INNER JOIN docs ON docs.id=keys.docid " |
"INNER JOIN docs ON docs.id=keys.docid " |
"WHERE "); |
"WHERE "); |
sz = strlen(sql); |
sz = strlen(sql); |
|
globsz = strlen(glob); |
|
eqsz = strlen(eq); |
|
|
if (NULL != arch) { |
if (NULL != arch) { |
sz += strlen(andarch) + 1; |
sz += strlen(andarch) + 1; |
sql = mandoc_realloc(sql, sz); |
sql = mandoc_realloc(sql, sz); |
strlcat(sql, andarch, sz); |
strlcat(sql, andarch, sz); |
} |
} |
|
|
if (NULL != sec) { |
if (NULL != sec) { |
sz += strlen(andsec) + 1; |
sz += strlen(andsec) + 1; |
sql = mandoc_realloc(sql, sz); |
sql = mandoc_realloc(sql, sz); |