From 22c6fb6df0d4b56f388ea8fdd09550a5f3410ee2 Mon Sep 17 00:00:00 2001 From: Marc Cromme Date: Thu, 19 Jan 2006 12:18:09 +0000 Subject: [PATCH] added first version of query-rewrite filter, only empty shell. functionality missing --- src/Makefile.am | 6 +- src/filter_query_rewrite.cpp | 98 +++++++++++++++++++++ src/filter_query_rewrite.hpp | 40 +++++++++ src/test_filter_query_rewrite.cpp | 173 +++++++++++++++++++++++++++++++++++++ 4 files changed, 316 insertions(+), 1 deletion(-) create mode 100644 src/filter_query_rewrite.cpp create mode 100644 src/filter_query_rewrite.hpp create mode 100644 src/test_filter_query_rewrite.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 5fc6a02..cedd45a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,4 @@ -## $Id: Makefile.am,v 1.44 2006-01-16 11:22:56 adam Exp $ +## $Id: Makefile.am,v 1.45 2006-01-19 12:18:09 marc Exp $ MAINTAINERCLEANFILES = Makefile.in config.in config.hpp @@ -22,6 +22,7 @@ libyp2_la_SOURCES = \ filter_frontend_net.cpp filter_frontend_net.hpp \ filter_log.cpp filter_log.hpp \ filter_multi.cpp filter_multi.hpp \ + filter_query_rewrite.cpp filter_query_rewrite.hpp \ filter_virt_db.cpp filter_virt_db.hpp \ filter_z3950_client.cpp filter_z3950_client.hpp \ filter_backend_test.cpp filter_backend_test.hpp \ @@ -65,6 +66,7 @@ check_PROGRAMS = \ test_filter_frontend_net \ test_filter_log \ test_filter_multi \ + test_filter_query_rewrite \ test_filter_z3950_client \ test_filter_backend_test \ test_filter_virt_db \ @@ -87,6 +89,7 @@ test_filter_factory_SOURCES = test_filter_factory.cpp test_filter_frontend_net_SOURCES = test_filter_frontend_net.cpp test_filter_log_SOURCES = test_filter_log.cpp test_filter_multi_SOURCES = test_filter_multi.cpp +test_filter_query_rewrite_SOURCES = test_filter_query_rewrite.cpp test_filter_z3950_client_SOURCES = test_filter_z3950_client.cpp test_filter_backend_test_SOURCES = test_filter_backend_test.cpp test_filter_virt_db_SOURCES = test_filter_virt_db.cpp @@ -109,6 +112,7 @@ test_filter_factory_LDADD = $(TESTLDADD) test_filter_frontend_net_LDADD = $(TESTLDADD) test_filter_log_LDADD = $(TESTLDADD) test_filter_multi_LDADD = $(TESTLDADD) +test_filter_query_rewrite_LDADD = $(TESTLDADD) test_filter_z3950_client_LDADD = $(TESTLDADD) test_filter_backend_test_LDADD = $(TESTLDADD) test_filter_virt_db_LDADD = $(TESTLDADD) diff --git a/src/filter_query_rewrite.cpp b/src/filter_query_rewrite.cpp new file mode 100644 index 0000000..8ae8400 --- /dev/null +++ b/src/filter_query_rewrite.cpp @@ -0,0 +1,98 @@ +/* $Id: filter_query_rewrite.cpp,v 1.1 2006-01-19 12:18:09 marc Exp $ + Copyright (c) 2005, Index Data. + +%LICENSE% + */ + + +#include "config.hpp" +#include "filter.hpp" +#include "package.hpp" + +//#include + +#include "util.hpp" +#include "filter_query_rewrite.hpp" + +#include + +namespace yf = yp2::filter; + +namespace yp2 { + namespace filter { + class QueryRewrite::Rep { + friend class QueryRewrite; + int dummy; + }; + } +} + +yf::QueryRewrite::QueryRewrite() : m_p(new Rep) +{ + m_p->dummy = 1; +} + +yf::QueryRewrite::~QueryRewrite() +{ // must have a destructor because of boost::scoped_ptr +} + +void yf::QueryRewrite::process(yp2::Package &package) const +{ + + if (package.session().is_closed()) + { + std::cout << "Got Close.\n"; + } + + Z_GDU *gdu = package.request().get(); + + if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which == + Z_APDU_initRequest) + { + std::cout << "Got Z3950 Init PDU\n"; + //Z_InitRequest *req = gdu->u.z3950->u.initRequest; + //package.request() = gdu; + } + else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which == + Z_APDU_searchRequest) + { + std::cout << "Got Z3950 Search PDU\n"; + //Z_SearchRequest *req = gdu->u.z3950->u.searchRequest; + //package.request() = gdu; + } + else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which == + Z_APDU_scanRequest) + { + std::cout << "Got Z3950 Scan PDU\n"; + //Z_ScanRequest *req = gdu->u.z3950->u.scanRequest; + //package.request() = gdu; + } + package.move(); +} + +static yp2::filter::Base* filter_creator() +{ + return new yp2::filter::QueryRewrite; +} + +extern "C" { + struct yp2_filter_struct yp2_filter_query_rewrite = { + 0, + "query-rewrite", + filter_creator + }; +} + +extern "C" { + extern struct yp2_filter_struct yp2_filter_query_rewrite; +} + + +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * c-file-style: "stroustrup" + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ diff --git a/src/filter_query_rewrite.hpp b/src/filter_query_rewrite.hpp new file mode 100644 index 0000000..ac702de --- /dev/null +++ b/src/filter_query_rewrite.hpp @@ -0,0 +1,40 @@ +/* $Id: filter_query_rewrite.hpp,v 1.1 2006-01-19 12:18:09 marc Exp $ + Copyright (c) 2005, Index Data. + +%LICENSE% + */ + +// Filter that rewrites RPN queries using Regular Expressions +#ifndef FILTER_QUERY_REWRITE_HPP +#define FILTER_QUERY_REWRITE_HPP + +#include + +#include "filter.hpp" + +namespace yp2 { + namespace filter { + class QueryRewrite : public Base { + class Rep; + boost::scoped_ptr m_p; + public: + QueryRewrite(); + ~QueryRewrite(); + void process(yp2::Package & package) const; + }; + } +} + +extern "C" { + extern struct yp2_filter_struct yp2_filter_queryRewrite; +} + +#endif +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * c-file-style: "stroustrup" + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ diff --git a/src/test_filter_query_rewrite.cpp b/src/test_filter_query_rewrite.cpp new file mode 100644 index 0000000..8208890 --- /dev/null +++ b/src/test_filter_query_rewrite.cpp @@ -0,0 +1,173 @@ +/* $Id: test_filter_query_rewrite.cpp,v 1.1 2006-01-19 12:18:09 marc Exp $ + Copyright (c) 2005, Index Data. + +%LICENSE% + */ + +#include "config.hpp" +#include +#include + +#include "filter_query_rewrite.hpp" +#include "util.hpp" +#include "router_chain.hpp" +#include "session.hpp" +#include "package.hpp" + +#define BOOST_AUTO_TEST_MAIN +#include + +using namespace boost::unit_test; + +class FilterBounceZ3950: public yp2::filter::Base { +public: + void process(yp2::Package & package) const { + + if (package.session().is_closed()) + { + std::cout << "Got Close.\n"; + } + + Z_GDU *gdu = package.request().get(); + + if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which == + Z_APDU_initRequest) + { + std::cout << "Got Z3950 Init PDU\n"; + //Z_InitRequest *req = gdu->u.z3950->u.initRequest; + //package.request() = gdu; + } + else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which == + Z_APDU_searchRequest) + { + std::cout << "Got Z3950 Search PDU\n"; + //Z_SearchRequest *req = gdu->u.z3950->u.searchRequest; + //package.request() = gdu; + } + else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which == + Z_APDU_scanRequest) + { + std::cout << "Got Z3950 Scan PDU\n"; + //Z_ScanRequest *req = gdu->u.z3950->u.scanRequest; + //package.request() = gdu; + } + + package.move(); + }; +}; + +void check_query_rewrite_init(yp2::RouterChain &router) +{ + std::cout << "QUERY REWRITE INIT\n"; + + // Create package with Z39.50 init request in it + yp2::Package pack; + + yp2::odr odr; + Z_APDU *apdu = zget_APDU(odr, Z_APDU_initRequest); + + pack.request() = apdu; + // Done creating query. + + // Put it in router + pack.router(router).move(); + + // Inspect bounced back request + //yazpp_1::GDU *gdu = &pack.response(); + yazpp_1::GDU *gdu = &pack.request(); + + Z_GDU *z_gdu = gdu->get(); + + std::cout << "Z_GDU " << z_gdu << "\n"; + BOOST_CHECK(z_gdu); + if (z_gdu) { + BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950); + BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_initRequest); + } +} +void check_query_rewrite_search(yp2::RouterChain &router, + std::string query_in, + std::string query_out) +{ + std::cout << "QUERY REWRITE SEARCH " + << query_in << " " << query_out << "\n"; + + // Create package with Z39.50 search request in it + yp2::Package pack; + + yp2::odr odr; + Z_APDU *apdu = zget_APDU(odr, Z_APDU_searchRequest); + + // create package PQF query here + yp2::util::pqf(odr, apdu, query_in); + + + //apdu->u.searchRequest->num_databaseNames = 1; + //apdu->u.searchRequest->databaseNames = (char**) + //odr_malloc(odr, sizeof(char *)); + //apdu->u.searchRequest->databaseNames[0] = odr_strdup(odr, "Default"); + + + pack.request() = apdu; + // Done creating query. + + // Put it in router + //pack.router(router).move(); + + // Inspect bounced back request + //yazpp_1::GDU *gdu = &pack.response(); + yazpp_1::GDU *gdu = &pack.request(); + + Z_GDU *z_gdu = gdu->get(); + //std::cout << "Z_GDU " << z_gdu << "\n"; + + //BOOST_CHECK(z_gdu); + if (z_gdu) { + BOOST_CHECK_EQUAL(z_gdu->which, Z_GDU_Z3950); + BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_searchRequest); + // take query out of package again + BOOST_CHECK_EQUAL(query_in, query_out); + } +} + + +BOOST_AUTO_UNIT_TEST( test_filter_query_rewrite_1 ) +{ + try + { + yp2::filter::QueryRewrite f_query_rewrite; + } + catch ( ... ) { + BOOST_CHECK (false); + } +} + +BOOST_AUTO_UNIT_TEST( test_filter_query_rewrite2 ) +{ + try + { + yp2::RouterChain router; + + yp2::filter::QueryRewrite f_query_rewrite; + //FilterBounceZ3950 f_bounce_z3950; + + router.append(f_query_rewrite); + //router.append(f_bounce_z3950); + + check_query_rewrite_init(router); + check_query_rewrite_search(router, "@attr 1=4 the", "@attr 1=4 the"); + + } + catch ( ... ) { + BOOST_CHECK (false); + } +} + +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * c-file-style: "stroustrup" + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ -- 1.7.10.4