From 4097bac1dce126e79ce1550fad8b2034a311ff80 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Thu, 20 Dec 2007 11:15:41 +0000 Subject: [PATCH] Implemented update action : adelete. New action action_delete added. zebra_extract_file honors it and deletes a record as usual but does not return error code if a record does not exist already. Function zebra_extract_file takes action parameter rather then deleteFlag. The file/path update is changed accordingly. zebraidx honors command 'adelete'. --- NEWS | 3 +++ doc/zebraidx.xml | 13 ++++++++++++- include/idzebra/api.h | 6 +++++- include/idzebra/recctrl.h | 11 +++++++++-- index/extract.c | 24 +++++++++++++++--------- index/index.h | 8 ++++---- index/update_file.c | 15 +++++++-------- index/update_path.c | 40 +++++++++++++++------------------------- index/zebraapi.c | 33 ++++++++++++++++++++++----------- index/zebraidx.c | 11 ++++++++--- 10 files changed, 100 insertions(+), 64 deletions(-) diff --git a/NEWS b/NEWS index 9ef7e85..6248ac8 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,6 @@ +Added zebraidx command, adelete, which attempts to delete records. +It's like 'delete', but does not fail if a record does not exist. + --- 2.0.20 2007/12/19 Added experimental support for faceted results. This is implemented diff --git a/doc/zebraidx.xml b/doc/zebraidx.xml index 261ae76..0d6ea8a 100644 --- a/doc/zebraidx.xml +++ b/doc/zebraidx.xml @@ -8,7 +8,7 @@ %idcommon; ]> - + zebra @@ -79,6 +79,17 @@ + adelete directory + + + Remove the records corresponding to the files found under + directory from the register. + Unlike command delete this command does not + fail if a record does not exist (but which is attempted deleted). + + + + commit diff --git a/include/idzebra/api.h b/include/idzebra/api.h index e3e976b..5488fd9 100644 --- a/include/idzebra/api.h +++ b/include/idzebra/api.h @@ -1,4 +1,4 @@ -/* $Id: api.h,v 1.53 2007-10-31 16:56:14 adam Exp $ +/* $Id: api.h,v 1.54 2007-12-20 11:15:41 adam Exp $ Copyright (C) 1995-2007 Index Data ApS @@ -398,6 +398,10 @@ YAZ_EXPORT ZEBRA_RES zebra_compact(ZebraHandle zh); YAZ_EXPORT +ZEBRA_RES zebra_repository_index(ZebraHandle zh, const char *path, + enum zebra_recctrl_action_t action); + +YAZ_EXPORT ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path); YAZ_EXPORT diff --git a/include/idzebra/recctrl.h b/include/idzebra/recctrl.h index b74e831..86f882b 100644 --- a/include/idzebra/recctrl.h +++ b/include/idzebra/recctrl.h @@ -1,4 +1,4 @@ -/* $Id: recctrl.h,v 1.39 2007-11-30 12:19:08 adam Exp $ +/* $Id: recctrl.h,v 1.40 2007-12-20 11:15:41 adam Exp $ Copyright (C) 1995-2007 Index Data ApS @@ -84,11 +84,18 @@ struct ZebraRecStream { void (*destroy)(struct ZebraRecStream *s); }; +/** record update action */ enum zebra_recctrl_action_t { + /** insert record (fail if it exists already) */ action_insert = 1, + /** replace record (fail it it does not exist) */ action_replace, + /** delete record (fail if it does not exist) */ action_delete, - action_update /* insert or replace as needed */ + /** insert or replace */ + action_update, + /** delete record (ignore if it does not exist) */ + action_a_delete }; /** \brief record extract for indexing */ diff --git a/index/extract.c b/index/extract.c index 10c4adc..be000c2 100644 --- a/index/extract.c +++ b/index/extract.c @@ -1,4 +1,4 @@ -/* $Id: extract.c,v 1.274 2007-12-17 08:44:07 adam Exp $ +/* $Id: extract.c,v 1.275 2007-12-20 11:15:42 adam Exp $ Copyright (C) 1995-2007 Index Data ApS @@ -585,7 +585,7 @@ ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh, ZEBRA_RES zebra_extract_file(ZebraHandle zh, zint *sysno, const char *fname, - int deleteFlag) + enum zebra_recctrl_action_t action) { ZEBRA_RES r = ZEBRA_OK; int i, fd; @@ -655,7 +655,7 @@ ZEBRA_RES zebra_extract_file(ZebraHandle zh, zint *sysno, const char *fname, default: yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type); } - if (sysno && deleteFlag) + if (sysno && (action == action_delete || action == action_a_delete)) { streamp = 0; fi = 0; @@ -683,8 +683,7 @@ ZEBRA_RES zebra_extract_file(ZebraHandle zh, zint *sysno, const char *fname, zebra_create_stream_fd(streamp, fd, 0); } r = zebra_extract_records_stream(zh, streamp, - deleteFlag ? - action_delete : action_update, + action, 0, /* tst_mode */ zh->m_record_type, sysno, @@ -969,14 +968,21 @@ ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh, if (! *sysno) { - /* new record */ + /* new record AKA does not exist already */ if (action == action_delete) { - yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType, - pr_fname, (zint) start_offset); + yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType, + pr_fname, (zint) start_offset); yaz_log(YLOG_WARN, "cannot delete record above (seems new)"); return ZEBRA_FAIL; } + else if (action == action_a_delete) + { + if (show_progress) + yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType, + pr_fname, (zint) start_offset); + return ZEBRA_OK; + } else if (action == action_replace) { yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType, @@ -1069,7 +1075,7 @@ ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh, extract_flush_record_keys(zh, *sysno, 0, delkeys, recordAttr->staticrank); #endif - if (action == action_delete) + if (action == action_delete || action == action_a_delete) { /* record going to be deleted */ #if FLUSH2 diff --git a/index/index.h b/index/index.h index 8ef0835..6b1848f 100644 --- a/index/index.h +++ b/index/index.h @@ -1,4 +1,4 @@ -/* $Id: index.h,v 1.213 2007-12-13 11:09:20 adam Exp $ +/* $Id: index.h,v 1.214 2007-12-20 11:15:42 adam Exp $ Copyright (C) 1995-2007 Index Data ApS @@ -363,7 +363,7 @@ ZEBRA_RES zebra_snippets_hit_vector(ZebraHandle zh, const char *setname, ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n); ZEBRA_RES zebra_extract_file(ZebraHandle zh, zint *sysno, const char *fname, - int deleteFlag); + enum zebra_recctrl_action_t action); ZEBRA_RES zebra_begin_read(ZebraHandle zh); ZEBRA_RES zebra_end_read(ZebraHandle zh); @@ -406,8 +406,8 @@ ZEBRA_RES zebra_sort_get_ord(ZebraHandle zh, int *numerical); ZEBRA_RES zebra_update_file_match(ZebraHandle zh, const char *path); -ZEBRA_RES zebra_update_from_path(ZebraHandle zh, const char *path); -ZEBRA_RES zebra_delete_from_path(ZebraHandle zh, const char *path); +ZEBRA_RES zebra_update_from_path(ZebraHandle zh, const char *path, + enum zebra_recctrl_action_t action); ZEBRA_RES zebra_remove_file_match(ZebraHandle zh); struct rpn_char_map_info diff --git a/index/update_file.c b/index/update_file.c index 5a50d04..1acf74f 100644 --- a/index/update_file.c +++ b/index/update_file.c @@ -1,4 +1,4 @@ -/* $Id: update_file.c,v 1.6 2007-10-29 09:25:41 adam Exp $ +/* $Id: update_file.c,v 1.7 2007-12-20 11:15:42 adam Exp $ Copyright (C) 1995-2007 Index Data ApS @@ -75,8 +75,7 @@ static void fileDelete_r(ZebraHandle zh, { case dirs_file: sprintf(tmppath, "%s%s", base, dst->path); - zebra_extract_file(zh, &dst->sysno, tmppath, 1); - + zebra_extract_file(zh, &dst->sysno, tmppath, action_delete); strcpy(tmppath, dst->path); dst = dirs_read(di); dirs_del(di, tmppath); @@ -172,7 +171,7 @@ static void file_update_r(ZebraHandle zh, case dirs_file: if (e_src[i_src].mtime > dst->mtime) { - if (zebra_extract_file(zh, &dst->sysno, tmppath, 0) == ZEBRA_OK) + if (zebra_extract_file(zh, &dst->sysno, tmppath, action_update) == ZEBRA_OK) { dirs_add(di, src, dst->sysno, e_src[i_src].mtime); } @@ -200,7 +199,7 @@ static void file_update_r(ZebraHandle zh, switch (e_src[i_src].kind) { case dirs_file: - if (zebra_extract_file(zh, &sysno, tmppath, 0) == ZEBRA_OK) + if (zebra_extract_file(zh, &sysno, tmppath, action_update) == ZEBRA_OK) dirs_add(di, src, sysno, e_src[i_src].mtime); break; case dirs_dir: @@ -219,7 +218,7 @@ static void file_update_r(ZebraHandle zh, switch (dst->kind) { case dirs_file: - zebra_extract_file(zh, &dst->sysno, tmppath, 1); + zebra_extract_file(zh, &dst->sysno, tmppath, action_delete); dirs_del(di, dst->path); dst = dirs_read(di); break; @@ -268,13 +267,13 @@ static void file_update_top(ZebraHandle zh, Dict dict, const char *path) if (e_dst) { if (sbuf.st_mtime > e_dst->mtime) - if (zebra_extract_file(zh, &e_dst->sysno, src, 0) == ZEBRA_OK) + if (zebra_extract_file(zh, &e_dst->sysno, src, action_update) == ZEBRA_OK) dirs_add(di, src, e_dst->sysno, sbuf.st_mtime); } else { zint sysno = 0; - if (zebra_extract_file(zh, &sysno, src, 0) == ZEBRA_OK) + if (zebra_extract_file(zh, &sysno, src, action_update) == ZEBRA_OK) dirs_add(di, src, sysno, sbuf.st_mtime); } dirs_free(&di); diff --git a/index/update_path.c b/index/update_path.c index 93d6b7f..591b050 100644 --- a/index/update_path.c +++ b/index/update_path.c @@ -1,4 +1,4 @@ -/* $Id: update_path.c,v 1.4 2007-10-29 09:25:41 adam Exp $ +/* $Id: update_path.c,v 1.5 2007-12-20 11:15:42 adam Exp $ Copyright (C) 1995-2007 Index Data ApS @@ -37,8 +37,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include "index.h" -static void repositoryExtractR(ZebraHandle zh, int deleteFlag, char *rep, - int level) +static void repositoryExtractR(ZebraHandle zh, char *rep, int level, + enum zebra_recctrl_action_t action) { struct dir_entry *e; int i; @@ -63,10 +63,10 @@ static void repositoryExtractR(ZebraHandle zh, int deleteFlag, char *rep, switch (e[i].kind) { case dirs_file: - zebra_extract_file(zh, NULL, rep, deleteFlag); + zebra_extract_file(zh, NULL, rep, action); break; case dirs_dir: - repositoryExtractR(zh, deleteFlag, rep, level+1); + repositoryExtractR(zh, rep, level+1, action); break; } } @@ -107,7 +107,9 @@ void repositoryShow(ZebraHandle zh, const char *path) } static void repositoryExtract(ZebraHandle zh, - int deleteFlag, const char *path) + const char *path, + enum zebra_recctrl_action_t action) + { struct stat sbuf; char src[1024]; @@ -130,38 +132,26 @@ static void repositoryExtract(ZebraHandle zh, if (ret == -1) yaz_log(YLOG_WARN|YLOG_ERRNO, "Cannot access path %s", src); else if (S_ISREG(sbuf.st_mode)) - zebra_extract_file(zh, NULL, src, deleteFlag); + zebra_extract_file(zh, NULL, src, action); else if (S_ISDIR(sbuf.st_mode)) - repositoryExtractR(zh, deleteFlag, src, 0); + repositoryExtractR(zh, src, 0, action); else yaz_log(YLOG_WARN, "Skipping path %s", src); } -static void repositoryExtractG(ZebraHandle zh, const char *path, - int deleteFlag) + +ZEBRA_RES zebra_update_from_path(ZebraHandle zh, const char *path, + enum zebra_recctrl_action_t action) { if (!strcmp(path, "") || !strcmp(path, "-")) { char src[1024]; while (scanf("%1020s", src) == 1) - repositoryExtract(zh, deleteFlag, src); + repositoryExtract(zh, src, action); } else - repositoryExtract(zh, deleteFlag, path); -} - -ZEBRA_RES zebra_update_from_path(ZebraHandle zh, const char *path) -{ - assert(path); - repositoryExtractG(zh, path, 0); - return ZEBRA_OK; -} - -ZEBRA_RES zebra_delete_from_path(ZebraHandle zh, const char *path) -{ - assert(path); - repositoryExtractG(zh, path, 1); + repositoryExtract(zh, path, action); return ZEBRA_OK; } diff --git a/index/zebraapi.c b/index/zebraapi.c index 1cb58e6..dda541f 100644 --- a/index/zebraapi.c +++ b/index/zebraapi.c @@ -1,4 +1,4 @@ -/* $Id: zebraapi.c,v 1.268 2007-12-18 13:55:28 adam Exp $ +/* $Id: zebraapi.c,v 1.269 2007-12-20 11:15:42 adam Exp $ Copyright (C) 1995-2007 Index Data ApS @@ -1929,22 +1929,33 @@ ZEBRA_RES zebra_end_transaction(ZebraHandle zh, ZebraTransactionStatus *status) ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path) { - ASSERTZH; - assert(path); - yaz_log(log_level, "updating %s", path); - - if (zh->m_record_id && !strcmp(zh->m_record_id, "file")) - return zebra_update_file_match(zh, path); - else - return zebra_update_from_path(zh, path); + return zebra_repository_index(zh, path, action_update); } ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path) { + return zebra_repository_index(zh, path, action_delete); +} + +ZEBRA_RES zebra_repository_index(ZebraHandle zh, const char *path, + enum zebra_recctrl_action_t action) +{ ASSERTZH; assert(path); - yaz_log(log_level, "deleting %s", path); - return zebra_delete_from_path(zh, path); + + if (action == action_update) + yaz_log(log_level, "updating %s", path); + else if (action == action_delete) + yaz_log(log_level, "deleting %s", path); + else if (action == action_a_delete) + yaz_log(log_level, "attempt deleting %s", path); + else + yaz_log(log_level, "update action=%d", (int) action); + + if (zh->m_record_id && !strcmp(zh->m_record_id, "file")) + return zebra_update_file_match(zh, path); + else + return zebra_update_from_path(zh, path, action); } ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path) diff --git a/index/zebraidx.c b/index/zebraidx.c index 804a7e8..dd915f8 100644 --- a/index/zebraidx.c +++ b/index/zebraidx.c @@ -1,4 +1,4 @@ -/* $Id: zebraidx.c,v 1.10 2007-11-08 09:30:05 adam Exp $ +/* $Id: zebraidx.c,v 1.11 2007-12-20 11:15:43 adam Exp $ Copyright (C) 1995-2007 Index Data ApS @@ -142,6 +142,8 @@ int main(int argc, char **argv) cmd = 's'; else if (!strcmp(arg, "del") || !strcmp(arg, "delete")) cmd = 'd'; + else if (!strcmp(arg, "adelete")) + cmd = 'a'; else if (!strcmp(arg, "init")) { zebra_init(zh); @@ -196,10 +198,13 @@ int main(int argc, char **argv) switch (cmd) { case 'u': - res = zebra_repository_update(zh, arg); + res = zebra_repository_index(zh, arg, action_update); break; case 'd': - res = zebra_repository_delete(zh, arg); + res = zebra_repository_index(zh, arg, action_delete); + break; + case 'a': + res = zebra_repository_index(zh, arg, action_a_delete); break; case 's': res = zebra_repository_show(zh, arg); -- 1.7.10.4