From: Marc Cromme Date: Mon, 23 Apr 2007 12:33:00 +0000 (+0000) Subject: finalized helper utilities for record class, and added unit tests X-Git-Tag: PAZPAR2.1.0.0~223 X-Git-Url: http://lists.indexdata.dk/?a=commitdiff_plain;h=f0f51e820f69d1661b94da14424b4109c16ab7bd;p=pazpar2-moved-to-github.git finalized helper utilities for record class, and added unit tests --- diff --git a/src/Makefile.am b/src/Makefile.am index 038d0a1..2892fe6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,7 +1,7 @@ -# $Id: Makefile.am,v 1.13 2007-04-21 12:00:54 adam Exp $ +# $Id: Makefile.am,v 1.14 2007-04-23 12:33:00 marc Exp $ bin_PROGRAMS = pazpar2 -check_PROGRAMS = test_config test_relevance test_sel_thread +check_PROGRAMS = test_config test_record test_relevance test_sel_thread TESTS = $(check_PROGRAMS) noinst_LIBRARIES = libpazpar2.a @@ -27,6 +27,9 @@ test_config_LDADD = libpazpar2.a $(YAZLIB) test_relevance_SOURCES = test_relevance.c test_relevance_LDADD = libpazpar2.a $(YAZLIB) +test_record_SOURCES = test_record.c +test_record_LDADD = libpazpar2.a $(YAZLIB) + test_sel_thread_SOURCES = test_sel_thread.c test_sel_thread_LDADD = libpazpar2.a $(YAZLIB) diff --git a/src/config.c b/src/config.c index b054335..d4fa7c1 100644 --- a/src/config.c +++ b/src/config.c @@ -1,4 +1,4 @@ -/* $Id: config.c,v 1.29 2007-04-23 08:48:50 marc Exp $ +/* $Id: config.c,v 1.30 2007-04-23 12:33:00 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.29 2007-04-23 08:48:50 marc Exp $ */ +/* $Id: config.c,v 1.30 2007-04-23 12:33:00 marc Exp $ */ #include @@ -154,7 +154,7 @@ struct conf_sortkey * conf_service_add_sortkey(NMEM nmem, } -int conf_service_field_id(struct conf_service *service, const char * name) +int conf_service_metadata_field_id(struct conf_service *service, const char * name) { int i = 0; @@ -170,6 +170,22 @@ int conf_service_field_id(struct conf_service *service, const char * name) }; +int conf_service_sortkey_field_id(struct conf_service *service, const char * name) +{ + int i = 0; + + if (!service || !service->sortkeys || !service->num_sortkeys) + return -1; + + for(i = 0; i < service->num_sortkeys; i++) { + if (!strcmp(name, (service->sortkeys[i]).name)) + return i; + } + + return -1; +}; + + /* Code to parse configuration file */ /* ==================================================== */ diff --git a/src/config.h b/src/config.h index a1a3448..67a9735 100644 --- a/src/config.h +++ b/src/config.h @@ -1,4 +1,4 @@ -/* $Id: config.h,v 1.20 2007-04-23 08:48:50 marc Exp $ +/* $Id: config.h,v 1.21 2007-04-23 12:33:00 marc Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -130,7 +130,9 @@ struct conf_sortkey * conf_service_add_sortkey(NMEM nmem, enum conf_sortkey_type type); -int conf_service_field_id(struct conf_service *service, const char * name); +int conf_service_metadata_field_id(struct conf_service *service, const char * name); + +int conf_service_sortkey_field_id(struct conf_service *service, const char * name); struct conf_server diff --git a/src/record.c b/src/record.c index 2d83012..fdaad9d 100644 --- a/src/record.c +++ b/src/record.c @@ -1,4 +1,4 @@ -/* $Id: record.c,v 1.2 2007-04-23 08:48:50 marc Exp $ +/* $Id: record.c,v 1.3 2007-04-23 12:33:00 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: record.c,v 1.2 2007-04-23 08:48:50 marc Exp $ */ +/* $Id: record.c,v 1.3 2007-04-23 12:33:00 marc Exp $ */ #include @@ -36,6 +36,25 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include "record.h" + +union data_types * data_types_assign(NMEM nmem, + union data_types * data1, + union data_types data2) +{ + // assert(nmem); + + if (!data1){ + if (!nmem) + return 0; + else + data1 = nmem_malloc(nmem, sizeof(union data_types)); + } + + *data1 = data2; + return data1; +} + + struct record * record_create(NMEM nmem, int num_metadata, int num_sortkeys) { struct record * record = 0; @@ -87,6 +106,17 @@ struct record_metadata * record_metadata_insert(NMEM nmem, return tmp_rmd; } +struct record_metadata * record_add_metadata_field_id(NMEM nmem, + struct record * record, + int field_id, + union data_types data) +{ + if (field_id < 0 || !record || !record->metadata) + return 0; + + return record_metadata_insert(nmem, &(record->metadata[field_id]), data); +} + struct record_metadata * record_add_metadata(NMEM nmem, struct record * record, @@ -99,7 +129,7 @@ struct record_metadata * record_add_metadata(NMEM nmem, if (!record || !record->metadata || !service || !name) return 0; - field_id = conf_service_field_id(service, name); + field_id = conf_service_metadata_field_id(service, name); if (-1 == field_id) return 0; @@ -109,16 +139,41 @@ struct record_metadata * record_add_metadata(NMEM nmem, -struct record_metadata * record_add_metadata_field_id(NMEM nmem, - struct record * record, - int field_id, - union data_types data) + + + +union data_types * record_assign_sortkey_field_id(NMEM nmem, + struct record * record, + int field_id, + union data_types data) { - if (field_id < 0 || !record || !record->metadata) + if (field_id < 0 || !record || !record->sortkeys) return 0; - return record_metadata_insert(nmem, &(record->metadata[field_id]), data); -}; + return data_types_assign(nmem, record->sortkeys[field_id], data); +} + + + +union data_types * record_assign_sortkey(NMEM nmem, + struct record * record, + struct conf_service * service, + const char * name, + union data_types data) +{ + int field_id = 0; + + if (!record || !service || !name) + return 0; + + field_id = conf_service_sortkey_field_id(service, name); + + if (!(field_id < service->num_sortkeys)) + return 0; + + return record_assign_sortkey_field_id(nmem, record, field_id, data); +} + /* diff --git a/src/record.h b/src/record.h index 9a34467..552450a 100644 --- a/src/record.h +++ b/src/record.h @@ -1,4 +1,4 @@ -/* $Id: record.h,v 1.3 2007-04-23 08:48:50 marc Exp $ +/* $Id: record.h,v 1.4 2007-04-23 12:33:00 marc Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -35,12 +35,19 @@ union data_types { } number; }; + + struct record_metadata { union data_types data; // next item of this name struct record_metadata *next; }; +union data_types * data_types_assign(NMEM nmem, + union data_types * data1, + union data_types data2); + + struct record { struct client *client; // Array mirrors list of metadata fields in config @@ -72,6 +79,22 @@ struct record_metadata * record_add_metadata(NMEM nmem, union data_types data); + +union data_types * record_assign_sortkey_field_id(NMEM nmem, + struct record * record, + int field_id, + union data_types data); + + +union data_types * record_assign_sortkey(NMEM nmem, + struct record * record, + struct conf_service * service, + const char * name, + union data_types data); + + + + struct record_cluster { // Array mirrors list of metadata fields in config diff --git a/src/test_config.c b/src/test_config.c index b2ada52..d608d98 100644 --- a/src/test_config.c +++ b/src/test_config.c @@ -1,4 +1,4 @@ -/* $Id: test_config.c,v 1.1 2007-04-19 19:42:30 marc Exp $ +/* $Id: test_config.c,v 1.2 2007-04-23 12:33:00 marc Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -52,7 +52,7 @@ void test_conf_service(int argc, char **argv) NMEM nmem = nmem_create(); struct conf_service *service = 0; - service = conf_service_create(nmem, 4, 1); + service = conf_service_create(nmem, 4, 3); YAZ_CHECK(service); @@ -117,7 +117,7 @@ void test_conf_service(int argc, char **argv) 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", + YAZ_CHECK(!conf_service_add_sortkey(nmem, service, 3, "out_of_bounds", Metadata_sortkey_relevance)); YAZ_CHECK(!conf_service_add_sortkey(nmem, service, 0, 0, //missing name @@ -125,9 +125,16 @@ void test_conf_service(int argc, char **argv) // expected sortkey sucess - YAZ_CHECK(conf_service_add_sortkey(nmem, service, 0, "title", + YAZ_CHECK(conf_service_add_sortkey(nmem, service, 0, "relevance", Metadata_sortkey_relevance)); + + YAZ_CHECK(conf_service_add_sortkey(nmem, service, 1, "title", + Metadata_sortkey_string)); + YAZ_CHECK(conf_service_add_sortkey(nmem, service, 2, "year", + Metadata_sortkey_numeric)); + + diff --git a/src/test_record.c b/src/test_record.c new file mode 100644 index 0000000..0427512 --- /dev/null +++ b/src/test_record.c @@ -0,0 +1,159 @@ +/* $Id: test_record.c,v 1.1 2007-04-23 12:33:00 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" +#include "record.h" +//#include "pazpar2.h" + + +void test_record(int argc, char **argv) +{ + NMEM nmem = nmem_create(); + + struct conf_service *service = 0; + struct record *record = 0; + + struct client *client = 0; + + service = conf_service_create(nmem, 4, 3); + YAZ_CHECK(service); + + 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)); + + YAZ_CHECK(conf_service_add_sortkey(nmem, service, 0, "relevance", + Metadata_sortkey_relevance)); + + YAZ_CHECK(conf_service_add_sortkey(nmem, service, 1, "title", + Metadata_sortkey_string)); + + YAZ_CHECK(conf_service_add_sortkey(nmem, service, 2, "year", + Metadata_sortkey_numeric)); + + + + + // testing record things + record = record_create(nmem, 4, 3); + YAZ_CHECK(record); + + // why on earth do we have a client dangeling from the record ?? + record->client = client; + + char * bla = "blabla"; + union data_types data_text; + data_text.text = bla; + + + union data_types data_num; + data_num.number.min = 2; + data_num.number.max = 5; + + struct record_metadata * tmp_md = 0; + tmp_md = record_metadata_insert(nmem, &(record->metadata[0]), data_text); + YAZ_CHECK(tmp_md); + tmp_md = record_metadata_insert(nmem, &tmp_md, data_text); + YAZ_CHECK(tmp_md); + + YAZ_CHECK(record_add_metadata_field_id(nmem, record, 3, data_num)); + YAZ_CHECK(record_add_metadata_field_id(nmem, record, 3, data_num)); + + YAZ_CHECK(record_add_metadata(nmem, record, service, "author", data_text)); + YAZ_CHECK(record_add_metadata(nmem, record, service, "author", data_text)); + + + YAZ_CHECK(record_assign_sortkey_field_id(nmem, record, 0, data_text)); + YAZ_CHECK(record_assign_sortkey_field_id(nmem, record, 1, data_text)); + YAZ_CHECK(record_assign_sortkey_field_id(nmem, record, 2, data_num)); + + YAZ_CHECK(record_assign_sortkey(nmem, record, service, "relevance", data_text)); + YAZ_CHECK(record_assign_sortkey(nmem, record, service, "title", data_text)); + YAZ_CHECK(record_assign_sortkey(nmem, record, service, "year", data_num)); + + + + + 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_record(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 c903ac6..f5b1668 100644 --- a/src/test_relevance.c +++ b/src/test_relevance.c @@ -1,4 +1,4 @@ -/* $Id: test_relevance.c,v 1.6 2007-04-23 08:48:50 marc Exp $ +/* $Id: test_relevance.c,v 1.7 2007-04-23 12:33:00 marc Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -70,56 +70,81 @@ void test_relevance(int argc, char **argv) rel = relevance_create(nmem, queryterms, numrecs); + YAZ_CHECK(rel); + list = reclist_create(nmem, numrecs); + YAZ_CHECK(list); + + service = conf_service_create(nmem, 4, 3); + YAZ_CHECK(service); - service = conf_service_create(nmem, 4, 1); - conf_service_add_metadata(nmem, service, 0, "title", + YAZ_CHECK(conf_service_add_metadata(nmem, service, 0, "title", Metadata_type_generic, Metadata_merge_unique, - 1, 1, 1, 0); + 1, 1, 1, 0)); - conf_service_add_metadata(nmem, service, 1, "author", + YAZ_CHECK(conf_service_add_metadata(nmem, service, 1, "author", Metadata_type_generic, Metadata_merge_longest, - 1, 1, 1, 0); + 1, 1, 1, 0)); - conf_service_add_metadata(nmem, service, 2, "isbn", + YAZ_CHECK(conf_service_add_metadata(nmem, service, 2, "isbn", Metadata_type_number, Metadata_merge_no, - 1, 1, 1, 0); + 1, 1, 1, 0)); - conf_service_add_metadata(nmem, service, 3, "year", + YAZ_CHECK(conf_service_add_metadata(nmem, service, 3, "year", Metadata_type_year, Metadata_merge_range, - 1, 1, 1, 0); + 1, 1, 1, 0)); + + YAZ_CHECK(conf_service_add_sortkey(nmem, service, 0, "relevance", + Metadata_sortkey_relevance)); + + YAZ_CHECK(conf_service_add_sortkey(nmem, service, 1, "title", + Metadata_sortkey_string)); + + YAZ_CHECK(conf_service_add_sortkey(nmem, service, 2, "year", + Metadata_sortkey_numeric)); + + // testing record things - record = record_create(nmem, 4, 1); + record = record_create(nmem, 4, 3); YAZ_CHECK(record); // why on earth do we have a client dangeling from the record ?? record->client = client; - //union data_types data; - //data.text = "sometext"; - char * bla = "blabla"; - union data_types data; - data.text = bla; + union data_types data_text; + data_text.text = bla; - union data_types data2; - data.number.min = 2; - data.number.max = 5; + union data_types data_num; + data_num.number.min = 2; + data_num.number.max = 5; struct record_metadata * tmp_md = 0; - tmp_md = record_metadata_insert(nmem, &(record->metadata[0]), data); + tmp_md = record_metadata_insert(nmem, &(record->metadata[0]), data_text); YAZ_CHECK(tmp_md); - tmp_md = record_metadata_insert(nmem, &tmp_md, data); + tmp_md = record_metadata_insert(nmem, &tmp_md, data_text); YAZ_CHECK(tmp_md); - YAZ_CHECK(record_add_metadata_field_id(nmem, record, 3, data2)); - YAZ_CHECK(record_add_metadata_field_id(nmem, record, 3, data2)); + YAZ_CHECK(record_add_metadata_field_id(nmem, record, 3, data_num)); + YAZ_CHECK(record_add_metadata_field_id(nmem, record, 3, data_num)); + + YAZ_CHECK(record_add_metadata(nmem, record, service, "author", data_text)); + YAZ_CHECK(record_add_metadata(nmem, record, service, "author", data_text)); + + + YAZ_CHECK(record_assign_sortkey_field_id(nmem, record, 0, data_text)); + YAZ_CHECK(record_assign_sortkey_field_id(nmem, record, 1, data_text)); + YAZ_CHECK(record_assign_sortkey_field_id(nmem, record, 2, data_num)); + + YAZ_CHECK(record_assign_sortkey(nmem, record, service, "relevance", data_text)); + YAZ_CHECK(record_assign_sortkey(nmem, record, service, "title", data_text)); + YAZ_CHECK(record_assign_sortkey(nmem, record, service, "year", data_num)); + + - YAZ_CHECK(record_add_metadata(nmem, record, service, "author", data)); - YAZ_CHECK(record_add_metadata(nmem, record, service, "author", data)); // now we need to put some actual data into the record ... how ??