source: gs3-extensions/audioDB/trunk/src/src/java/org/greenstone/gsdl3/service/GS2AudioDBSearch.java@ 30411

Last change on this file since 30411 was 30411, checked in by davidb, 8 years ago

Changes after debugging on MacOS El Capitan

  • Property svn:executable set to *
File size: 6.8 KB
Line 
1/*
2 * GS2AudioDBSearch.java
3 * Copyright (C) 2011 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18package org.greenstone.gsdl3.service;
19
20
21// Greenstone classes
22import org.greenstone.gsdl3.util.*;
23
24// XML classes
25import org.w3c.dom.Document;
26import org.w3c.dom.Element;
27import org.w3c.dom.NodeList;
28
29// java
30import java.util.Vector;
31import java.util.ArrayList;
32import java.util.HashMap;
33import java.util.Map;
34import java.util.Set;
35import java.util.Iterator;
36import java.io.File;
37
38import org.apache.log4j.*;
39
40public class GS2AudioDBSearch extends AbstractGS2AudioSearch {
41
42 static Logger logger = Logger.getLogger (org.greenstone.gsdl3.service.GS2AudioDBSearch.class.getName ());
43
44 protected static final String OFFSET_PARAM = "offset";
45 protected static final String LENGTH_PARAM = "length";
46 protected static final String RADIUS_PARAM = "radius";
47 protected static final String MAXDOCS_PARAM = "maxDocs";
48
49 protected static final String AUDIODB_DEFAULT_DIRECTORY = "audioDB";
50 protected static final String ADB_FILENAME = "lsh-features.adb";
51
52 protected AudioDBWrapper audiodb_src = null;
53
54 /** constructor */
55 public GS2AudioDBSearch () {
56 if(this.audiodb_src == null){
57 this.audiodb_src = new AudioDBWrapper ();
58 }
59 }
60
61 /** do the actual query */
62 protected Element processAudioQuery (Element request) {
63 // MG needs to be synchronized (this inspiration for this class)
64 // Since it is not known how concurrent audioDB can be, play it safe for
65 // now and also require synchronization
66
67 synchronized(this.audiodb_src) {
68 // Create a new (empty) result message ('doc' is in ServiceRack.java)
69 Document result_doc = XMLConverter.newDOM();
70 Element result = result_doc.createElement (GSXML.RESPONSE_ELEM);
71
72 // Rather than QUERY_SERVICE use "TextQuery"
73 // => makes the result looks the same as a text query
74 result.setAttribute (GSXML.FROM_ATT, "TextQuery");
75 result.setAttribute (GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
76
77 // Get the parameters of the request
78 Element param_list = (Element) GSXML.getChildByTagName (request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
79 if (param_list == null) {
80 logger.error ("AudioDB AudioQuery request had no paramList.");
81 return result; // Return the empty result
82 }
83
84 // Process the request parameters
85 HashMap params = GSXML.extractParams (param_list, false);
86
87 // Make sure a query has been specified
88 String query = (String) params.get (QUERY_PARAM);
89 if (query == null || query.equals ("")) {
90 return result; // Return the empty result
91 }
92
93 // If an index hasn't been specified, use the default
94 String index = (String) params.get (INDEX_PARAM);
95 if (index == null) {
96 index = AUDIODB_DEFAULT_DIRECTORY;
97 }
98
99 // The location of the audioDB index
100 String toplevel_index_dir = GSFile.collectionIndexDir (this.site_home, this.cluster_name);
101 String audioDB_index_dir = toplevel_index_dir + File.separatorChar + index;
102 String assoc_index_dir = toplevel_index_dir + File.separatorChar + "assoc";
103
104 // set the audioDB query parameters to the values the user has specified
105 setStandardQueryParams (params);
106
107 this.audiodb_src.runQuery (audioDB_index_dir, ADB_FILENAME, assoc_index_dir, query);
108 Vector docs = this.audiodb_src.getQueryResult ();
109
110 if (docs.isEmpty()) {
111 // something has gone wrong
112 GSXML.addError (result, "Couldn't query the audioDB database", GSXML.ERROR_TYPE_SYSTEM);
113 return result;
114 }
115 long totalDocs = docs.size();
116
117 // Get the docnums out, and convert to HASH ids
118 if (docs.size () == 0) {
119 logger.error ("No results found...\n");
120 }
121
122 // Create a metadata list to store information about the query results
123 Element metadata_list = result_doc.createElement (GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
124 result.appendChild (metadata_list);
125
126 // Add a metadata element specifying the number of matching documents
127 // because the total number is just the number returned, use numDocsReturned, not numDocsMatched
128 GSXML.addMetadata (metadata_list, "numDocsReturned", ""+totalDocs);
129 // add a metadata item to specify what actual query was done - eg if stuff was stripped out etc. and then we can use the query later, cos we don't know which parameter was the query
130 GSXML.addMetadata (metadata_list, "query", query);
131
132 if (docs.size () > 0) {
133 // Create a document list to store the matching documents, and add them
134 Element document_list = result_doc.createElement (GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
135 result.appendChild (document_list);
136 for (int d = 0; d < docs.size (); d++) {
137 AudioDBDocInfo adb_doc = (AudioDBDocInfo) docs.elementAt(d);
138
139 String doc_id = adb_doc.getDocID();
140 double rank = adb_doc.getTopRank();
141 String offsets = adb_doc.getOffsetList();
142
143 Element doc_node = createDocNode (result_doc, doc_id, Double.toString (rank));
144 doc_node.setAttribute("frameOffset", offsets);
145
146 document_list.appendChild (doc_node);
147 }
148 }
149
150 // Create an empty term list as a place holder for the term information
151 Element term_list = result_doc.createElement (GSXML.TERM_ELEM+GSXML.LIST_MODIFIER);
152 result.appendChild (term_list);
153
154 return result;
155 }//end of synchronized
156 }
157
158 // should probably use a list rather than map
159 protected boolean setStandardQueryParams(HashMap params)
160 {
161 Set entries = params.entrySet();
162 Iterator i = entries.iterator();
163 while (i.hasNext()) {
164 Map.Entry m = (Map.Entry)i.next();
165 String name = (String)m.getKey();
166 String value = (String)m.getValue();
167
168 if (name.equals(OFFSET_PARAM)) {
169 int offset = Integer.parseInt(value);
170 this.audiodb_src.setOffset(offset);
171 }
172 else if (name.equals(LENGTH_PARAM)) {
173 int length = Integer.parseInt(value);
174 this.audiodb_src.setLength(length);
175 }
176 else if (name.equals(RADIUS_PARAM)) {
177 double radius = Double.parseDouble(value);
178 this.audiodb_src.setRadius(radius);
179 }
180 else if (name.equals(MAXDOCS_PARAM)) {
181 int docs = Integer.parseInt(value);
182 this.audiodb_src.setMaxDocs(docs);
183 } // ignore any others
184 }
185 return true;
186 }
187
188
189}
190
191
Note: See TracBrowser for help on using the repository browser.