From ad5284b2da642763a3010fb4d84a203bc8aaec71 Mon Sep 17 00:00:00 2001 From: Marc Cromme Date: Thu, 19 Apr 2007 19:42:30 +0000 Subject: [PATCH] added conf_service creator inclusive metadata and sortkey assigners, added unittests on the same as well. creators for record construction still needed --- src/Makefile.am | 8 ++- src/config.c | 98 ++++++++++++++++++++++-------- src/config.h | 61 ++++++++++++------- src/test_config.c | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/test_relevance.c | 91 ++++++++-------------------- 5 files changed, 307 insertions(+), 113 deletions(-) create mode 100644 src/test_config.c diff --git a/src/Makefile.am b/src/Makefile.am index d1125aa..f006545 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,7 +1,7 @@ -# $Id: Makefile.am,v 1.8 2007-04-16 13:58:20 marc Exp $ +# $Id: Makefile.am,v 1.9 2007-04-19 19:42:30 marc Exp $ bin_PROGRAMS = pazpar2 -check_PROGRAMS = test_relevance +check_PROGRAMS = test_config test_relevance TESTS = $(check_PROGRAMS) noinst_LIBRARIES = libpazpar2.a @@ -21,5 +21,9 @@ libpazpar2_a_SOURCES = config.c config.h eventl.c eventl.h \ pazpar2_SOURCES = pazpar2.c pazpar2_LDADD = libpazpar2.a $(YAZLIB) +test_config_SOURCES = test_config.c +test_config_LDADD = libpazpar2.a $(YAZLIB) + test_relevance_SOURCES = test_relevance.c test_relevance_LDADD = libpazpar2.a $(YAZLIB) + diff --git a/src/config.c b/src/config.c index b09d47f..98cb041 100644 --- a/src/config.c +++ b/src/config.c @@ -1,4 +1,4 @@ -/* $Id: config.c,v 1.25 2007-04-19 11:57:53 marc Exp $ +/* $Id: config.c,v 1.26 2007-04-19 19:42:30 marc Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -19,7 +19,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* $Id: config.c,v 1.25 2007-04-19 11:57:53 marc Exp $ */ +/* $Id: config.c,v 1.26 2007-04-19 19:42:30 marc Exp $ */ #include @@ -44,19 +44,9 @@ static char confdir[256] = "."; struct conf_config *config = 0; -struct conf_service * conf_service_create(NMEM nmem) -{ - struct conf_service * service - = nmem_malloc(nmem, sizeof(struct conf_service)); - service->num_metadata = 0; - service->metadata = 0; - service->num_sortkeys = 0; - service->sortkeys = 0; - return service; -} - -struct conf_metadata * conf_metadata_create(NMEM nmem, +struct conf_metadata * conf_metadata_assign(NMEM nmem, + struct conf_metadata * metadata, const char *name, enum conf_metadata_type type, enum conf_metadata_merge merge, @@ -65,10 +55,9 @@ struct conf_metadata * conf_metadata_create(NMEM nmem, int rank, int sortkey_offset) { - - struct conf_metadata * metadata - = nmem_malloc(nmem, sizeof(struct conf_metadata)); - + if (!nmem || !metadata || !name) + return 0; + metadata->name = nmem_strdup(nmem, name); metadata->type = type; metadata->merge = merge; @@ -76,11 +65,49 @@ struct conf_metadata * conf_metadata_create(NMEM nmem, metadata->termlist = termlist; metadata->rank = rank; metadata->sortkey_offset = sortkey_offset; + return metadata; } -struct conf_metadata* conf_service_add_metadata(NMEM nmem, + +struct conf_sortkey * conf_sortkey_assign(NMEM nmem, + struct conf_sortkey * sortkey, + const char *name, + enum conf_sortkey_type type) +{ + if (!nmem || !sortkey || !name) + return 0; + + sortkey->name = nmem_strdup(nmem, name); + sortkey->type = type; + + return sortkey; +} + + +struct conf_service * conf_service_create(NMEM nmem, + int num_metadata, int num_sortkeys) +{ + struct conf_service * service + = nmem_malloc(nmem, sizeof(struct conf_service)); + service->num_metadata = num_metadata; + service->metadata = 0; + if (service->num_metadata) + service->metadata + = nmem_malloc(nmem, + sizeof(struct conf_metadata) * service->num_metadata); + service->num_sortkeys = num_sortkeys; + service->sortkeys = 0; + if (service->num_sortkeys) + service->sortkeys + = nmem_malloc(nmem, + sizeof(struct conf_sortkey) * service->num_sortkeys); + return service; +} + +struct conf_metadata* conf_service_add_metadata(NMEM nmem, struct conf_service *service, + int position, const char *name, enum conf_metadata_type type, enum conf_metadata_merge merge, @@ -89,22 +116,43 @@ struct conf_metadata* conf_service_add_metadata(NMEM nmem, int rank, int sortkey_offset) { - struct conf_metadata * m = 0; + struct conf_metadata * md = 0; - if (!service) - return m; + if (!service || !service->metadata || !service->num_metadata + || position < 0 || !(position < service->num_metadata)) + return 0; - m = conf_metadata_create(nmem, name, type, merge, + //md = &((service->metadata)[position]); + md = service->metadata + position; + md = conf_metadata_assign(nmem, md, name, type, merge, brief, termlist, rank, sortkey_offset); + return md; +} + + +struct conf_sortkey * conf_service_add_sortkey(NMEM nmem, + struct conf_service *service, + int position, + const char *name, + enum conf_sortkey_type type) +{ + struct conf_sortkey * sk = 0; + + if (!service || !service->sortkeys || !service->num_sortkeys + || position < 0 || !(position < service->num_sortkeys)) + return 0; - // Not finished, checked temporarily in for file move // if (m) + //sk = &((service->sortkeys)[position]); + sk = service->sortkeys + position; + sk = conf_sortkey_assign(nmem, sk, name, type); - return m; + return sk; } + /* Code to parse configuration file */ /* ==================================================== */ diff --git a/src/config.h b/src/config.h index 33014df..4e1d1ad 100644 --- a/src/config.h +++ b/src/config.h @@ -1,4 +1,4 @@ -/* $Id: config.h,v 1.18 2007-04-19 11:57:53 marc Exp $ +/* $Id: config.h,v 1.19 2007-04-19 19:42:30 marc Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -26,29 +26,29 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include #include -enum conf_sortkey_type -{ +#include + +enum conf_metadata_type { + Metadata_type_generic, // Generic text field + Metadata_type_number, // A number + Metadata_type_year // A number +}; + +enum conf_metadata_merge { + Metadata_merge_no, // Don't merge + Metadata_merge_unique, // Include unique elements in merged block + Metadata_merge_longest, // Include the longest (strlen) value + Metadata_merge_range, // Store value as a range of lowest-highest + Metadata_merge_all // Just include all elements found +}; + +enum conf_sortkey_type { Metadata_sortkey_relevance, Metadata_sortkey_numeric, // Standard numerical sorting Metadata_sortkey_skiparticle, // Skip leading article when sorting Metadata_sortkey_string // Flat string }; -enum conf_metadata_type - { - Metadata_type_generic, // Generic text field - Metadata_type_number, // A number - Metadata_type_year // A number - }; - -enum conf_metadata_merge - { - Metadata_merge_no, // Don't merge - Metadata_merge_unique, // Include unique elements in merged block - Metadata_merge_longest, // Include the longest (strlen) value - Metadata_merge_range, // Store value as a range of lowest-highest - Metadata_merge_all // Just include all elements found - }; // Describes known metadata elements and how they are to be manipulated @@ -70,7 +70,9 @@ struct conf_metadata }; -struct conf_metadata * conf_metadata_create(NMEM nmem, + +struct conf_metadata * conf_metadata_assign(NMEM nmem, + struct conf_metadata * metadata, const char *name, enum conf_metadata_type type, enum conf_metadata_merge merge, @@ -80,6 +82,7 @@ struct conf_metadata * conf_metadata_create(NMEM nmem, int sortkey_offset); + // Controls sorting struct conf_sortkey { @@ -87,6 +90,13 @@ struct conf_sortkey enum conf_sortkey_type type; }; +struct conf_sortkey * conf_sortkey_assign(NMEM nmem, + struct conf_sortkey * sortkey, + const char *name, + enum conf_sortkey_type type); + + + // It is conceivable that there will eventually be several 'services' // offered from one server, with separate configuration -- possibly // more than one services associated with the same port. For now, @@ -99,10 +109,12 @@ struct conf_service struct conf_sortkey *sortkeys; }; -struct conf_service * conf_service_create(NMEM nmem); +struct conf_service * conf_service_create(NMEM nmem, + int num_metadata, int num_sortkeys); -struct conf_metadata* conf_service_add_metadata(NMEM nmem, +struct conf_metadata* conf_service_add_metadata(NMEM nmem, struct conf_service *service, + int position, const char *name, enum conf_metadata_type type, enum conf_metadata_merge merge, @@ -111,6 +123,13 @@ struct conf_metadata* conf_service_add_metadata(NMEM nmem, int rank, int sortkey_offset); +struct conf_sortkey * conf_service_add_sortkey(NMEM nmem, + struct conf_service *service, + int position, + const char *name, + enum conf_sortkey_type type); + + struct conf_server { diff --git a/src/test_config.c b/src/test_config.c new file mode 100644 index 0000000..b2ada52 --- /dev/null +++ b/src/test_config.c @@ -0,0 +1,162 @@ +/* $Id: test_config.c,v 1.1 2007-04-19 19:42:30 marc Exp $ + Copyright (c) 2006-2007, Index Data. + +This file is part of Pazpar2. + +Pazpar2 is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 2, or (at your option) any later +version. + +Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Pazpar2; see the file LICENSE. If not, write to the +Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA +02111-1307, USA. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if HAVE_CONFIG_H +#include "cconfig.h" +#endif + +#define USE_TIMING 0 +#if USE_TIMING +#include +#endif + +#include + + +//#include "pazpar2.h" +#include "config.h" + + + +void test_conf_service(int argc, char **argv) +{ + NMEM nmem = nmem_create(); + + struct conf_service *service = 0; + service = conf_service_create(nmem, 4, 1); + + YAZ_CHECK(service); + + // expected metadata failures + YAZ_CHECK(!conf_service_add_metadata(0, service, 0, "dead_nmem", + Metadata_type_generic, + Metadata_merge_unique, + 1, 1, 1, 0)); + + YAZ_CHECK(!conf_service_add_metadata(nmem, 0, 0, "service_needed", + Metadata_type_generic, + Metadata_merge_unique, + 1, 1, 1, 0)); + + YAZ_CHECK(!conf_service_add_metadata(nmem, service, -1, "out_of_bounds", + Metadata_type_generic, + Metadata_merge_unique, + 1, 1, 1, 0)); + + YAZ_CHECK(!conf_service_add_metadata(nmem, service, 4, "out_of_bounds", + Metadata_type_generic, + Metadata_merge_unique, + 1, 1, 1, 0)); + + YAZ_CHECK(!conf_service_add_metadata(nmem, service, 0, 0, //missing name + Metadata_type_generic, + Metadata_merge_unique, + 1, 1, 1, 0)); + + // expected metadata sucesses + YAZ_CHECK(conf_service_add_metadata(nmem, service, 0, "title", + Metadata_type_generic, + Metadata_merge_unique, + 1, 1, 1, 0)); + + YAZ_CHECK(conf_service_add_metadata(nmem, service, 1, "author", + Metadata_type_generic, + Metadata_merge_longest, + 1, 1, 1, 0)); + + YAZ_CHECK(conf_service_add_metadata(nmem, service, 2, "isbn", + Metadata_type_number, + Metadata_merge_no, + 1, 1, 1, 0)); + + YAZ_CHECK(conf_service_add_metadata(nmem, service, 3, "year", + Metadata_type_year, + Metadata_merge_range, + 1, 1, 1, 0)); + + + // expected sortkey failures + YAZ_CHECK(!conf_service_add_sortkey(0, service, 0, "dead_nmem", + Metadata_sortkey_relevance)); + + YAZ_CHECK(!conf_service_add_sortkey(nmem, 0, 0, "service_neeeded", + Metadata_sortkey_numeric)); + + YAZ_CHECK(!conf_service_add_sortkey(nmem, service, -1, "out_of_bounds", + Metadata_sortkey_skiparticle)); + + YAZ_CHECK(!conf_service_add_sortkey(nmem, service, -1, "out_of_bounds", + Metadata_sortkey_string)); + + YAZ_CHECK(!conf_service_add_sortkey(nmem, service, 1, "out_of_bounds", + Metadata_sortkey_relevance)); + + YAZ_CHECK(!conf_service_add_sortkey(nmem, service, 0, 0, //missing name + Metadata_sortkey_relevance)); + + + // expected sortkey sucess + YAZ_CHECK(conf_service_add_sortkey(nmem, service, 0, "title", + Metadata_sortkey_relevance)); + + + + + + nmem_destroy(nmem); + + //YAZ_CHECK(0 == 0); + //YAZ_CHECK_EQ(0, 1); + +} + + +int main(int argc, char **argv) +{ + YAZ_CHECK_INIT(argc, argv); + YAZ_CHECK_LOG(); + + test_conf_service(argc, argv); + + YAZ_CHECK_TERM; +} + + + + +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ diff --git a/src/test_relevance.c b/src/test_relevance.c index af391bf..fc5954d 100644 --- a/src/test_relevance.c +++ b/src/test_relevance.c @@ -1,4 +1,4 @@ -/* $Id: test_relevance.c,v 1.3 2007-04-19 11:57:53 marc Exp $ +/* $Id: test_relevance.c,v 1.4 2007-04-19 19:42:30 marc Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -43,15 +43,11 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA //#include "pazpar2.h" +#include "config.h" #include "relevance.h" - - - - - void test_relevance(int argc, char **argv) { NMEM nmem = nmem_create(); @@ -72,63 +68,30 @@ void test_relevance(int argc, char **argv) rel = relevance_create(nmem, queryterms, numrecs); list = reclist_create(nmem, numrecs); - - service = conf_service_create(nmem); - - // setting up service - sic! - // this should have been done by a nice service_create function !!! - - //service->num_metadata = 1; - //service->metadata = 0; - //service->num_sortkeys = 0; - //service->sortkeys = 0; - - - //if (service->num_metadata) - // service->metadata - // = nmem_malloc(nmem, - // sizeof(struct conf_metadata) * service->num_metadata); - //else - // service->metadata = 0; - //service->metadata->name = nmem_strdup(nmem, "aname"); - //service->metadata->brief = 1; - //service->metadata->termlist = 1; - //service->metadata->rank = 1; - //service->metadata->type = Metadata_type_generic; - //service->metadata->type = Metadata_type_year; - //service->metadata->merge = Metadata_merge_no; - //service->metadata->merge = Metadata_merge_unique; - //service->metadata->merge = Metadata_merge_longest; - //service->metadata->merge = Metadata_merge_range; - //service->metadata->merge = Metadata_merge_all; - - service->metadata - = conf_metadata_create(nmem, "name", - Metadata_type_generic, Metadata_merge_unique, - 1, 1, 1, 0); - service->num_metadata = 1; - - conf_service_add_metadata(nmem, service, "name", - Metadata_type_generic, Metadata_merge_unique, - 1, 1, 1, 0); - - - if (service->num_sortkeys) - service->sortkeys - = nmem_malloc(nmem, - sizeof(struct conf_sortkey) * service->num_sortkeys); - else - service->sortkeys = 0; - // service->sortkeys.type = Metadata_sortkey_numeric; - // service->sortkeys.type = Metadata_sortkey_skiparticle; - // service->sortkeys.name = service->metadata->name; - //service->metadata->sortkey_offset = sk_node; + service = conf_service_create(nmem, 4, 1); + conf_service_add_metadata(nmem, service, 0, "title", + Metadata_type_generic, Metadata_merge_unique, + 1, 1, 1, 0); + + conf_service_add_metadata(nmem, service, 1, "author", + Metadata_type_generic, Metadata_merge_longest, + 1, 1, 1, 0); + + conf_service_add_metadata(nmem, service, 2, "isbn", + Metadata_type_number, Metadata_merge_no, + 1, 1, 1, 0); + + conf_service_add_metadata(nmem, service, 3, "year", + Metadata_type_year, Metadata_merge_range, + 1, 1, 1, 0); +#if 0 // preparing one record // this should have been done by a nice record_create function - // why the heck does the record know which client it belongs to ?? + // why the heck does the record know which client it belongs to ?? + record = nmem_malloc(nmem, sizeof(struct record)); record->next = 0; @@ -141,7 +104,7 @@ void test_relevance(int argc, char **argv) sizeof(struct record_metadata*) * service->num_metadata); memset(record->metadata, 0, sizeof(struct record_metadata*) * service->num_metadata); - +#endif // now we need to put some actual data into the record ... how ?? // there is a hell of a lot spagetti code in logic.c ingest_record() @@ -268,12 +231,11 @@ void test_relevance(int argc, char **argv) //normalize_mergekey((char *) mergekey_norm, 0); +#if 0 // insert one record into recordlist/cluster - what's a cluster, exactly?? cluster = reclist_insert(list, service, record, (char *)mergekey, &total); - - relevance_newrec(rel, cluster); - +#endif @@ -291,9 +253,8 @@ void test_relevance(int argc, char **argv) nmem_destroy(nmem); - YAZ_CHECK(0 == 0); - YAZ_CHECK_EQ(0, 1); - + //YAZ_CHECK(0 == 0); + //YAZ_CHECK_EQ(0, 1); } -- 1.7.10.4