X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fyaz4j%2FResultSet.java;h=aef52f641eb8531bcf97e5839fa67d0b65d6d85c;hb=80fe126a86a610466bdc4089f10e17fd3e683d44;hp=b70a03e94076b3938aaf4e45876f2953adc2af6e;hpb=428d895d6dc31b784e953ea935d662cfc4a4bd43;p=yaz4j-moved-to-github.git diff --git a/src/main/java/org/yaz4j/ResultSet.java b/src/main/java/org/yaz4j/ResultSet.java index b70a03e..aef52f6 100644 --- a/src/main/java/org/yaz4j/ResultSet.java +++ b/src/main/java/org/yaz4j/ResultSet.java @@ -1,66 +1,125 @@ package org.yaz4j; -import org.yaz4j.jni.SWIGTYPE_p_ZOOM_connection_p; +import java.util.Iterator; +import java.util.NoSuchElementException; +import org.yaz4j.exception.ZoomException; import org.yaz4j.jni.SWIGTYPE_p_ZOOM_record_p; import org.yaz4j.jni.SWIGTYPE_p_ZOOM_resultset_p; import org.yaz4j.jni.yaz4jlib; -public class ResultSet -{ - private SWIGTYPE_p_ZOOM_resultset_p resultSet; - private SWIGTYPE_p_ZOOM_connection_p connection; - private long size = 0 ; - private Record[] records = null ; - private boolean disposed = false; - - ResultSet(SWIGTYPE_p_ZOOM_resultset_p resultSet, SWIGTYPE_p_ZOOM_connection_p connection) - { - this.resultSet = resultSet ; - this.connection = connection ; - size = yaz4jlib.ZOOM_resultset_size(this.resultSet); - records = new Record[(int)size]; - } - - public void finalize() - { - this.Dispose(); - } +/** + * This class represents a "buffered handle" to the result set created on the + * server and thus retrieving records may invlove a request to the server if + * those records are not locally cached. Details on how to configure the retrieval + * (present) process are available in the YAZ manual + * + * @see YAZ ZOOM result sets + * + * Becacuse of the server misbehaviour or errors during retrieval the + * "getRecord" method may either return null or throw exceptions, even when the + * index of retrieved records lies within the bounds of the set. Client + * code should be prepared for such situations. + * + * This class implements the iterable interface and as such can be used within + * foreach loops, it's important to note however that in this case the errors + * during retrieval will be masked with standard NoSuchElementExceptions + * + * @author jakub + */ +public class ResultSet implements Iterable { + //for GC refcount - ResultSetOptionsCollection getResultSetOptions() - { - return new ResultSetOptionsCollection(resultSet); + private Connection conn; + private SWIGTYPE_p_ZOOM_resultset_p resultSet; + private long size = 0; + private boolean disposed = false; + + ResultSet(SWIGTYPE_p_ZOOM_resultset_p resultSet, Connection conn) { + this.resultSet = resultSet; + size = yaz4jlib.ZOOM_resultset_size(this.resultSet); + this.conn = conn; + } + + @Override + public void finalize() { + this._dispose(); + } + + /** + * Read option by name. + * @param name option name + * @return option value + */ + public String option(String name) { + return yaz4jlib.ZOOM_resultset_option_get(resultSet, name); + } + + /** + * Write option with a given name. + * @param name option name + * @param value option value + * @return result set (self) for chainability + */ + public ResultSet option(String name, String value) { + yaz4jlib.ZOOM_resultset_option_set(resultSet, name, value); + return this; + } + + public Record getRecord(long index) throws ZoomException { + SWIGTYPE_p_ZOOM_record_p record = + yaz4jlib.ZOOM_resultset_record(resultSet, index); + //may be out of range or unsupported syntax + if (record == null) { + return null; + } + int errorCode = yaz4jlib.ZOOM_record_error(record, null, null, null); + if (errorCode != 0) { + throw new ZoomException("Record excpetion, code " + errorCode); } - - public Record getRecord(int index) - { - if ( records[index] == null) - { - SWIGTYPE_p_ZOOM_record_p recordTemp = yaz4jlib.ZOOM_resultset_record(resultSet, index); - records[index] = new Record(recordTemp, this); - } - - return this.records[index]; - } - - public int getSize() - { - return (int)size ; - } - - public void Dispose() - { - if (! disposed ) - { - for( int i=0 ; i iterator() { + return new Iterator() { + private long cur; + @Override + public boolean hasNext() { + return cur < size; + } + + @Override + public Record next() { + try { + return getRecord(cur++); + } catch (ZoomException ze) { + throw new NoSuchElementException(ze.getMessage()); + } + } + + @Override + public void remove() { + throw new UnsupportedOperationException("remove operation not supported"); + } + }; + } + + 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"); + return this; + } + + public long getHitCount() { + return size; + } + + void _dispose() { + if (!disposed) { + yaz4jlib.ZOOM_resultset_destroy(resultSet); + resultSet = null; + conn = null; + disposed = true; + } + } }