From 9d6d4f4d56764fcac2603fe623bdf90b3ac33ba8 Mon Sep 17 00:00:00 2001 From: Jakub Skoczen Date: Wed, 13 Feb 2013 14:45:58 +0100 Subject: [PATCH] Expose ZOOM query object This allows to specify sort criteria prior to the search and adds support for CQL scan queries. --- src/main/java/org/yaz4j/CQLQuery.java | 21 +++++++++++ src/main/java/org/yaz4j/Connection.java | 56 +++++++++++++++++++++++++++-- src/main/java/org/yaz4j/Package.java | 2 +- src/main/java/org/yaz4j/PrefixQuery.java | 21 +++++++++++ src/main/java/org/yaz4j/Query.java | 43 ++++++++++++++++++++++ src/main/java/org/yaz4j/ResultSet.java | 7 ++++ src/main/java/org/yaz4j/package-info.java | 13 +++++++ src/test/org/yaz4j/ConnectionTest.java | 47 +++++++++++++++++++++++- 8 files changed, 206 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/yaz4j/CQLQuery.java create mode 100644 src/main/java/org/yaz4j/PrefixQuery.java create mode 100644 src/main/java/org/yaz4j/Query.java create mode 100644 src/main/java/org/yaz4j/package-info.java diff --git a/src/main/java/org/yaz4j/CQLQuery.java b/src/main/java/org/yaz4j/CQLQuery.java new file mode 100644 index 0000000..58cc0ff --- /dev/null +++ b/src/main/java/org/yaz4j/CQLQuery.java @@ -0,0 +1,21 @@ +/* + * Copyright (c) 1995-2013, Index Datassss + * All rights reserved. + * See the file LICENSE for details. + */ +package org.yaz4j; + +import org.yaz4j.jni.yaz4jlib; + +/** + * + * @author jakub + */ +public class CQLQuery extends Query { + + public CQLQuery(String cqlQuery) { + super(); + yaz4jlib.ZOOM_query_cql(query, cqlQuery); + } + +} diff --git a/src/main/java/org/yaz4j/Connection.java b/src/main/java/org/yaz4j/Connection.java index 3331052..a47ad3a 100644 --- a/src/main/java/org/yaz4j/Connection.java +++ b/src/main/java/org/yaz4j/Connection.java @@ -32,6 +32,7 @@ import org.yaz4j.jni.yaz4jlib; * } * * + * @see YAZ ZOOM Connection * @author jakub */ public class Connection { @@ -75,11 +76,14 @@ public class Connection { * Performs a search operation (submits the query to the server, waits for * response and creates a new result set that allows to retrieve particular * results) + * @deprecated Does not allow specifying sort criteria prior to search + * use {@link #search(org.yaz4j.Query) search(Query)} instead. * @param query search query * @param queryType type of the query (e.g RPN. CQL) * @return result set containing records (hits) * @throws ZoomException protocol or network-level error */ + @Deprecated public ResultSet search(String query, QueryType queryType) throws ZoomException { if (closed) { @@ -104,15 +108,40 @@ public class Connection { } return new ResultSet(yazResultSet, this); } + + /** + * Performs a search operation (submits the query to the server, waits for + * response and creates a new result set that allows to retrieve particular + * results). Sort criteria may be specified prior to the search, directly + * on the query object. + * @param query search query of any type supported by YAZ. + * @return result set containing records (hits) + * @throws ZoomException protocol or network-level error + */ + public ResultSet search(Query query) throws ZoomException { + if (closed) { + throw new IllegalStateException("Connection is closed."); + } + SWIGTYPE_p_ZOOM_resultset_p yazResultSet = yaz4jlib.ZOOM_connection_search( + zoomConnection, query.query); + ZoomException err = ExceptionUtil.getError(zoomConnection, host, + port); + if (err != null) { + yaz4jlib.ZOOM_resultset_destroy(yazResultSet); + throw err; + } + return new ResultSet(yazResultSet, this); + } /** * Performs a scan operation (obtains a list of candidate search terms against - * a particular access point) - * @see ZOOM-API Scan + * a particular access point). + * @deprecated Only allows PQF scan queries, use {@link #scan(org.yaz4j.Query) scan(Query)} instead * @param query query for scanning * @return a scan set with the terms * @throws ZoomException a protocol or network-level error */ + @Deprecated public ScanSet scan(String query) throws ZoomException { if (closed) { throw new IllegalStateException("Connection is closed."); @@ -127,6 +156,29 @@ public class Connection { ScanSet scanSet = new ScanSet(yazScanSet, this); return scanSet; } + + /** + * Performs a scan operation (obtains a list of candidate search terms against + * a particular access point). Allows to use both CQL and PQF for Scan. + * @see ZOOM-API Scan + * @param query scan query of type supported by YAZ + * @return a scan set with the terms + * @throws ZoomException a protocol or network-level error + */ + public ScanSet scan(Query query) throws ZoomException { + if (closed) { + throw new IllegalStateException("Connection is closed."); + } + SWIGTYPE_p_ZOOM_scanset_p yazScanSet = yaz4jlib.ZOOM_connection_scan1( + zoomConnection, query.query); + ZoomException err = ExceptionUtil.getError(zoomConnection, host, port); + if (err != null) { + yaz4jlib.ZOOM_scanset_destroy(yazScanSet); + throw err; + } + ScanSet scanSet = new ScanSet(yazScanSet, this); + return scanSet; + } /** * Establishes a connection to the remote server. diff --git a/src/main/java/org/yaz4j/Package.java b/src/main/java/org/yaz4j/Package.java index 83aa74b..bf7280a 100644 --- a/src/main/java/org/yaz4j/Package.java +++ b/src/main/java/org/yaz4j/Package.java @@ -9,7 +9,7 @@ import org.yaz4j.jni.yaz4jlib; * Once created, a package is configured by means of options, then the package * is send and the result is inspected (again, by means of options). * - * @see org.yaz4j.ConnectionExtended#getPackage(java.lang.String) + * @see org.yaz4j.ConnectionExtended#getPackage(java.lang.String) * * @author jakub */ diff --git a/src/main/java/org/yaz4j/PrefixQuery.java b/src/main/java/org/yaz4j/PrefixQuery.java new file mode 100644 index 0000000..80522e0 --- /dev/null +++ b/src/main/java/org/yaz4j/PrefixQuery.java @@ -0,0 +1,21 @@ +/* + * Copyright (c) 1995-2013, Index Datassss + * All rights reserved. + * See the file LICENSE for details. + */ +package org.yaz4j; + +import org.yaz4j.jni.yaz4jlib; + +/** + * + * @author jakub + */ +public class PrefixQuery extends Query { + + public PrefixQuery(String prefixQuery) { + super(); + yaz4jlib.ZOOM_query_prefix(query, prefixQuery); + } + +} diff --git a/src/main/java/org/yaz4j/Query.java b/src/main/java/org/yaz4j/Query.java new file mode 100644 index 0000000..03a5f09 --- /dev/null +++ b/src/main/java/org/yaz4j/Query.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 1995-2013, Index Datassss + * All rights reserved. + * See the file LICENSE for details. + */ +package org.yaz4j; + +import org.yaz4j.exception.ZoomException; +import org.yaz4j.jni.SWIGTYPE_p_ZOOM_query_p; +import org.yaz4j.jni.yaz4jlib; + +/** + * @see YAZ ZOOM Query + * @author jakub + */ +public abstract class Query { + + SWIGTYPE_p_ZOOM_query_p query; + private boolean disposed = false; + + protected Query() { + query = yaz4jlib.ZOOM_query_create(); + } + + public void sortBy(String strategy, String criteria) throws ZoomException { + int ret = yaz4jlib.ZOOM_query_sortby2(query, strategy, criteria); + if (ret != 0) { + throw new ZoomException("query sortBy failed"); + } + } + + protected void finalize() { + _dispose(); + } + + void _dispose() { + if (!disposed) { + yaz4jlib.ZOOM_query_destroy(query); + disposed = true; + } + } + +} diff --git a/src/main/java/org/yaz4j/ResultSet.java b/src/main/java/org/yaz4j/ResultSet.java index 2dc3e50..1949507 100644 --- a/src/main/java/org/yaz4j/ResultSet.java +++ b/src/main/java/org/yaz4j/ResultSet.java @@ -134,6 +134,13 @@ public class ResultSet implements Iterable { }; } + /** + * + * @param type + * @param spec + * @return + * @throws ZoomException + */ public ResultSet sort(String type, String spec) throws ZoomException { int ret = yaz4jlib.ZOOM_resultset_sort1(resultSet, type, spec); if (ret != 0) throw new ZoomException("Sorting resultset failed"); diff --git a/src/main/java/org/yaz4j/package-info.java b/src/main/java/org/yaz4j/package-info.java new file mode 100644 index 0000000..6bc6d48 --- /dev/null +++ b/src/main/java/org/yaz4j/package-info.java @@ -0,0 +1,13 @@ +/* + * Copyright (c) 1995-2013, Index Data + * All rights reserved. + * See the file LICENSE for details. + */ +/** + * Classes within this package expose the Z39.50 Object Orientation Model (ZOOM) API + * and additional extensions implemented by YAZ. + * + * @see ZOOM-API + * @see YAZ ZOOM-API + */ +package org.yaz4j; diff --git a/src/test/org/yaz4j/ConnectionTest.java b/src/test/org/yaz4j/ConnectionTest.java index 056039b..38a4f3e 100644 --- a/src/test/org/yaz4j/ConnectionTest.java +++ b/src/test/org/yaz4j/ConnectionTest.java @@ -6,7 +6,7 @@ import org.yaz4j.exception.*; import java.util.List; public class ConnectionTest { - + @Test public void testConnection() { Connection con = new Connection("z3950.indexdata.dk:210/gils", 0); @@ -15,6 +15,51 @@ public class ConnectionTest { con.setSyntax("sutrs"); System.out.println("Open connection to z3950.indexdata.dk:210/gils..."); con.connect(); + ResultSet s = con.search(new PrefixQuery("@attr 1=4 utah")); + System.out.println("Search for 'utah'..."); + assertNotNull(s); + assertEquals(s.getHitCount(), 9); + Record rec = s.getRecord(0); + assertNotNull(rec); + byte[] content = rec.getContent(); + // first SUTRS record + assertEquals(content.length, 1940); + assertEquals(content[0], 103); + assertEquals(rec.getSyntax(), "SUTRS"); + assertEquals(rec.getDatabase(), "gils"); + System.out.println("Read all records.."); + // read all records + int i = 0; + for (Record r : s) { + assertNotNull(r); + System.out.println("Got "+i+" record of type "+r.getSyntax()); + i++; + } + System.out.println("Try sorting them..."); + s.sort("yaz", "1=4 >i 1=21 >s"); + System.out.println("Try fetching them all at once..."); + i = 0; + List all = s.getRecords(0, (int) s.getHitCount()); + for (Record r : all) { + System.out.println("getRecords, rec '"+i+"'"+r.getSyntax()); + System.out.println(r.render()); + i++; + } + } catch (ZoomException ze) { + fail(ze.getMessage()); + } finally { + con.close(); + } + } + + @Test + public void testConnectionDeprecated() { + Connection con = new Connection("z3950.indexdata.dk:210/gils", 0); + assertNotNull(con); + try { + con.setSyntax("sutrs"); + System.out.println("Open connection to z3950.indexdata.dk:210/gils..."); + con.connect(); ResultSet s = con.search("@attr 1=4 utah", Connection.QueryType.PrefixQuery); System.out.println("Search for 'utah'..."); -- 1.7.10.4