Line | Hits | Source |
---|---|---|
1 | /* | |
2 | * Copyright (C) 2006 TopCoder Inc., All Rights Reserved. | |
3 | */ | |
4 | package com.topcoder.document.index.persistence.impl.db; | |
5 | ||
6 | import com.topcoder.document.index.CollectionIndex; | |
7 | import com.topcoder.document.index.DocumentIndex; | |
8 | import com.topcoder.document.index.persistence.IndexPersistenceException; | |
9 | import com.topcoder.document.index.persistence.impl.Utils; | |
10 | import com.topcoder.document.index.wordsource.WordSourceId; | |
11 | ||
12 | import java.sql.Connection; | |
13 | import java.text.CollationKey; | |
14 | import java.util.HashMap; | |
15 | import java.util.HashSet; | |
16 | import java.util.Iterator; | |
17 | import java.util.List; | |
18 | import java.util.Locale; | |
19 | import java.util.Map; | |
20 | import java.util.Set; | |
21 | ||
22 | ||
23 | /** | |
24 | * This class contains the database operations that are common to both database persistence implementations. These | |
25 | * methods have been factored out to reduce code redundancy. As these operation are quite specific to the persistence | |
26 | * layer they have been factored into a different utility class instead of adding the operations to {@link | |
27 | * DatabaseUtils} which does contain the more general and generic JDBC handling code. | |
28 | * | |
29 | * @author TCSDEVELOPER | |
30 | * @version 1.0 | |
31 | */ | |
32 | class CommonDatabaseOperations { | |
33 | /** | |
34 | * As this class is a utility class, a private constructor is added to avoid class instantiation. | |
35 | */ | |
36 | 0 | private CommonDatabaseOperations() { |
37 | //an empty private default constructor | |
38 | 0 | } |
39 | ||
40 | /** | |
41 | * This method does query the database for a WordSourceId matching the given documentId String as described in CS | |
42 | * 1.4.1.1. | |
43 | * | |
44 | * @param persistence the AbstractDBIndexPersistence instance that can be used to perform rollback operations on the | |
45 | * given connection in case an error is encountered | |
46 | * @param connection the connection to perform the necessary queries on | |
47 | * @param documentId the document record id from which to create the WordSourceId | |
48 | * | |
49 | * @return the WordSourceId created for the given document id or <tt>null</tt> if no matching record existed in the | |
50 | * database | |
51 | * | |
52 | * @throws IndexPersistenceException in case an error is encountered during database operations | |
53 | * @throws IllegalArgumentException in case any arg is null or documentId is an empty (trim'd) String | |
54 | */ | |
55 | static WordSourceId getWordSourceId(final AbstractDBIndexPersistence persistence, | |
56 | final Connection connection, final String documentId) | |
57 | throws IndexPersistenceException { | |
58 | 56 | if (documentId == null) { |
59 | 2 | throw new IllegalArgumentException("The parameter named [documentId] was null."); |
60 | } | |
61 | 54 | if (documentId.trim().length() == 0) { |
62 | 2 | throw new IllegalArgumentException("The parameter named [documentId] was an empty String."); |
63 | } | |
64 | ||
65 | 52 | final Object[] queryArgsDocumentId = new Object[]{documentId}; |
66 | ||
67 | 52 | final List result1 = DatabaseUtils.doQuery(connection, persistence, |
68 | "SELECT LOCALE,SOURCE_IDENTITY,CLASS_NAME FROM DOCUMENT WHERE DOCUMENT_ID=?", | |
69 | queryArgsDocumentId, | |
70 | new DatabaseUtils.DataType[]{ | |
71 | DatabaseUtils.STRING_TYPE, | |
72 | DatabaseUtils.BYTE_ARRAY_TYPE, | |
73 | DatabaseUtils.STRING_TYPE} | |
74 | ); | |
75 | ||
76 | 48 | if (result1.isEmpty()) { |
77 | // no record matched the id, so we return null here | |
78 | 2 | return null; |
79 | } | |
80 | ||
81 | 46 | final Object[] documentData = (Object[]) result1.get(0); |
82 | 46 | final Object sourceIdentity = Utils.deSerializeObject((byte[]) documentData[1]); |
83 | ||
84 | // build the locale from the locale string | |
85 | 46 | final String[] localeElements = ((String) documentData[0]).split("_"); |
86 | final Locale locale; | |
87 | 46 | switch (localeElements.length) { |
88 | case 1: | |
89 | 0 | locale = new Locale(localeElements[0]); |
90 | 0 | break; |
91 | case 2: | |
92 | 46 | locale = new Locale(localeElements[0], localeElements[1]); |
93 | 46 | break; |
94 | case 3: | |
95 | 0 | locale = new Locale(localeElements[0], localeElements[1], localeElements[2]); |
96 | 0 | break; |
97 | default: | |
98 | 0 | persistence.rollbackTransaction(connection); |
99 | 0 | throw new IndexPersistenceException("Unexpected locale String [" + documentData[0] |
100 | + "] encountered, was not parseable into a valid locale."); | |
101 | } | |
102 | ||
103 | // fetch all delimiters | |
104 | 46 | final List result2 = DatabaseUtils.doSingleColumnQuery(connection, persistence, |
105 | "SELECT DELIMITER_ENTRY FROM DELIMITER WHERE DOCUMENT_ID=?", | |
106 | queryArgsDocumentId, | |
107 | DatabaseUtils.STRING_TYPE | |
108 | ); | |
109 | 46 | final String[] delimiters = (String[]) result2.toArray(new String[result2.size()]); |
110 | ||
111 | //build WordSourceId | |
112 | 46 | return new WordSourceId(sourceIdentity, (String) documentData[2], delimiters, locale); |
113 | } | |
114 | ||
115 | /** | |
116 | * This method does store the given document collection index in the persistence. | |
117 | * <p/> | |
118 | * CS section 1.4.3.1 describes the algorithm of this method. | |
119 | * | |
120 | * @param collectionIndex CollectionIndex to store | |
121 | * @param persistence the AbstractDBIndexPersistence instance that can be used to obtain a database connection, | |
122 | * perform rollback operations on the connection in case an error is encountered or commit | |
123 | * the connection in case the operation has succeeded | |
124 | * | |
125 | * @throws IllegalArgumentException if any arg is <tt>null</tt> | |
126 | * @throws IndexPersistenceException if fails to store document collection index or given index is already persisted | |
127 | * or contains an unpersisted document index | |
128 | */ | |
129 | static void addCollectionIndex(final AbstractDBIndexPersistence persistence, final CollectionIndex collectionIndex) | |
130 | throws | |
131 | IndexPersistenceException { | |
132 | 38 | if (collectionIndex == null) { |
133 | 4 | throw new IllegalArgumentException("The parameter named [collectionIndex] was null."); |
134 | } | |
135 | 34 | if (persistence == null) { |
136 | 2 | throw new IllegalArgumentException("The parameter named [persistence] was null."); |
137 | } | |
138 | 32 | final String collectionId = collectionIndex.getId(); |
139 | 32 | final Object[] collectionIdQueryArgs = new Object[]{collectionId}; |
140 | 32 | final Connection connection = persistence.getConnection(); |
141 | 32 | persistence.beginTransaction(connection); |
142 | ||
143 | 32 | final Integer count = (Integer) DatabaseUtils.doSingleValueQuery(connection, persistence, |
144 | "SELECT COUNT(*) FROM DOCUMENT_COLLECTION WHERE COLLECTION_ID=?", collectionIdQueryArgs, | |
145 | DatabaseUtils.INTEGER_TYPE); | |
146 | 32 | if (count.intValue() > 0) { |
147 | 4 | DatabaseUtils.closeSilently(connection); |
148 | 4 | throw new IndexPersistenceException( |
149 | "The given collection with the id [" + collectionId + "] does already exist in the persistence."); | |
150 | } | |
151 | 28 | final Set allDocumentIds = collectionIndex.getAllDocumentIds(); |
152 | 28 | for (Iterator iterator = allDocumentIds.iterator(); iterator.hasNext();) { |
153 | 32 | final WordSourceId sourceId = (WordSourceId) iterator.next(); |
154 | 32 | final String documentId = Utils.createIdString(sourceId); |
155 | 32 | final Integer docCount = (Integer) DatabaseUtils.doSingleValueQuery(connection, persistence, |
156 | "SELECT COUNT(*) from DOCUMENT WHERE DOCUMENT_ID=?", | |
157 | new Object[]{documentId}, DatabaseUtils.INTEGER_TYPE); | |
158 | 32 | if (docCount.intValue() == 0) { |
159 | 4 | throw new IndexPersistenceException( |
160 | "The given document index collection contains unpersisted documents."); | |
161 | } | |
162 | } | |
163 | ||
164 | 24 | DatabaseUtils.doDMLQuery(connection, persistence, |
165 | "INSERT INTO DOCUMENT_COLLECTION (COLLECTION_ID) VALUES (?)", | |
166 | collectionIdQueryArgs); | |
167 | ||
168 | 24 | for (Iterator iterator = allDocumentIds.iterator(); iterator.hasNext();) { |
169 | 28 | final WordSourceId sourceId = (WordSourceId) iterator.next(); |
170 | 28 | final String documentId = Utils.createIdString(sourceId); |
171 | 28 | DatabaseUtils.doDMLQuery(connection, persistence, |
172 | "INSERT INTO DOCUMENT_COLLECTION_DOCUMENT_XREF (COLLECTION_ID,DOCUMENT_ID) VALUES (?,?)", | |
173 | new Object[]{collectionId, documentId}); | |
174 | // Use count is maintained externally | |
175 | } | |
176 | ||
177 | 24 | persistence.commitTransaction(connection); |
178 | 24 | } |
179 | ||
180 | /** | |
181 | * This method retrieves a document collection index with specified identifier. It does return <tt>null</tt> when | |
182 | * collection with specified identifier is not found. | |
183 | * <p/> | |
184 | * CS section 1.4.3.2 describes the algorithm of this method. | |
185 | * | |
186 | * @param persistence the AbstractDBIndexPersistence instance that can be used to obtain a database connection, | |
187 | * perform rollback operations on the connection in case an error is encountered or commit the | |
188 | * connection in case the operation has succeeded. Furthermore this instance does retrieve the | |
189 | * document instances from the persistent storage | |
190 | * @param collectionId identifier of document collection index to retrieve | |
191 | * | |
192 | * @return CollectionIndex with specified identifier or <tt>null</tt> if the collection index with the given | |
193 | * identifier was not found in persistence | |
194 | * | |
195 | * @throws IllegalArgumentException when collectionId is <tt>null</tt> or empty (trim'd) string or persistence is | |
196 | * <tt>null</tt> | |
197 | * @throws IndexPersistenceException when collection index can not be retrieved | |
198 | */ | |
199 | static CollectionIndex getCollectionIndex(final AbstractDBIndexPersistence persistence, final String collectionId) | |
200 | throws IndexPersistenceException { | |
201 | 42 | if (collectionId == null) { |
202 | 4 | throw new IllegalArgumentException("The parameter named [collectionId] was null."); |
203 | } | |
204 | 38 | if (collectionId.trim().length() == 0) { |
205 | 4 | throw new IllegalArgumentException("The parameter named [collectionId] was an empty String."); |
206 | } | |
207 | 34 | if (persistence == null) { |
208 | 2 | throw new IllegalArgumentException("The parameter named [persistence] was null."); |
209 | } | |
210 | ||
211 | 32 | final Object[] collectionIdQueryArgs = new Object[]{collectionId}; |
212 | 32 | final Connection connection = persistence.getConnection(); |
213 | ||
214 | //check whether collection exists, return null if not | |
215 | 32 | final Integer count = (Integer) DatabaseUtils.doSingleValueQuery(connection, persistence, |
216 | "SELECT COUNT(*) FROM DOCUMENT_COLLECTION WHERE COLLECTION_ID=?", collectionIdQueryArgs, | |
217 | DatabaseUtils.INTEGER_TYPE); | |
218 | 32 | if (count.intValue() == 0) { |
219 | 8 | DatabaseUtils.closeSilently(connection); |
220 | 8 | return null; |
221 | } | |
222 | ||
223 | 24 | final Map wordsOfCollection = new HashMap(); |
224 | 24 | final Set documentsOfCollection = new HashSet(); |
225 | ||
226 | 24 | final List documentIds = DatabaseUtils.doSingleColumnQuery(connection, persistence, |
227 | "SELECT DOCUMENT_ID FROM DOCUMENT_COLLECTION_DOCUMENT_XREF where COLLECTION_ID=?", | |
228 | collectionIdQueryArgs, DatabaseUtils.STRING_TYPE); | |
229 | 24 | for (Iterator iterator = documentIds.iterator(); iterator.hasNext();) { |
230 | 32 | final String documentId = (String) iterator.next(); |
231 | 32 | final WordSourceId sourceId = getWordSourceId(persistence, connection, documentId); |
232 | 32 | final DocumentIndex index = persistence.getDocumentIndex(sourceId); |
233 | ||
234 | // add all words of the index to the collection's word-index | |
235 | // map and add a reference to the current index | |
236 | 32 | final Set wordsOfIndex = index.getWords().keySet(); |
237 | 32 | for (Iterator iterator2 = wordsOfIndex.iterator(); iterator2.hasNext();) { |
238 | 96 | final CollationKey collationKey = (CollationKey) iterator2.next(); |
239 | 96 | Set documentsCotainingWord = (Set) wordsOfCollection.get(collationKey); |
240 | 96 | if (documentsCotainingWord == null) { |
241 | 72 | documentsCotainingWord = new HashSet(); |
242 | 72 | wordsOfCollection.put(collationKey, documentsCotainingWord); |
243 | } | |
244 | 96 | documentsCotainingWord.add(sourceId); |
245 | } | |
246 | // add document to collection's contained documents set | |
247 | 32 | documentsOfCollection.add(sourceId); |
248 | } | |
249 | // build the collection object and return it | |
250 | 24 | DatabaseUtils.closeSilently(connection); |
251 | 24 | return new CollectionIndex(null, wordsOfCollection, documentsOfCollection, collectionId); |
252 | } | |
253 | ||
254 | /** | |
255 | * This method does remove the document collection index with specified identifier from the persistence. | |
256 | * <p/> | |
257 | * CS section 1.4.3.4 describes the algorithm of this method. | |
258 | * | |
259 | * @param collectionId Identifier of document collection index to remove | |
260 | * @param persistence the AbstractDBIndexPersistence instance that can be used to obtain a database connection, | |
261 | * perform rollback operations on the connection in case an error is encountered or commit the | |
262 | * connection in case the operation has succeeded | |
263 | * | |
264 | * @throws IllegalArgumentException when collectionId is <tt>null</tt> or empty (trim'd) string or persistence is | |
265 | * <tt>null</tt> | |
266 | * @throws IndexPersistenceException when fails to remove CollectionIndex with given identifier from database, or | |
267 | * when collection with that specific identifier did not exist in database | |
268 | */ | |
269 | static void removeCollectionIndex(final AbstractDBIndexPersistence persistence, final String collectionId) | |
270 | throws IndexPersistenceException { | |
271 | 18 | if (collectionId == null) { |
272 | 4 | throw new IllegalArgumentException("The parameter named [collectionId] was null."); |
273 | } | |
274 | 14 | if (collectionId.trim().length() == 0) { |
275 | 4 | throw new IllegalArgumentException("The parameter named [collectionId] was an empty String."); |
276 | } | |
277 | 10 | if (persistence == null) { |
278 | 2 | throw new IllegalArgumentException("The parameter named [persistence] was null."); |
279 | } | |
280 | ||
281 | 8 | final Connection connection = persistence.getConnection(); |
282 | 8 | persistence.beginTransaction(connection); |
283 | ||
284 | 8 | final Object[] queryArgsCollectionId = new Object[]{collectionId}; |
285 | 8 | DatabaseUtils.doDMLQuery(connection, persistence, |
286 | "DELETE FROM DOCUMENT_COLLECTION_DOCUMENT_XREF WHERE COLLECTION_ID=?", queryArgsCollectionId); | |
287 | 8 | final int updateCount = DatabaseUtils.doDMLQuery(connection, persistence, |
288 | "DELETE FROM DOCUMENT_COLLECTION WHERE COLLECTION_ID=?", queryArgsCollectionId); | |
289 | 8 | if (updateCount == 0) { |
290 | 4 | persistence.rollbackTransaction(connection); |
291 | 4 | throw new IndexPersistenceException( |
292 | "The collection with the given id [" + collectionId + "] did not exist in the database."); | |
293 | } | |
294 | 4 | persistence.commitTransaction(connection); |
295 | //counts are not updated as indexer implementation is responsible for that | |
296 | 4 | } |
297 | ||
298 | /** | |
299 | * This method does update specified CollectionIndex in the persistence. | |
300 | * <p/> | |
301 | * CS section 1.4.3.3 describes the algorithm of this method. | |
302 | * | |
303 | * @param collectionIndex CollectionIndex to update | |
304 | * @param persistence the AbstractDBIndexPersistence instance that can be used to obtain a database connection, | |
305 | * perform rollback operations on the connection in case an error is encountered or commit | |
306 | * the connection in case the operation has succeeded | |
307 | * | |
308 | * @throws IllegalArgumentException if any parameter is <tt>null</tt> | |
309 | * @throws IndexPersistenceException if any error happens when updating the collection index in the persistence, | |
310 | * this includes the case, when the specified collectionIndex is not found in the | |
311 | * persistence | |
312 | */ | |
313 | static void updateCollectionIndex(final AbstractDBIndexPersistence persistence, | |
314 | final CollectionIndex collectionIndex) throws | |
315 | IndexPersistenceException { | |
316 | 14 | if (collectionIndex == null) { |
317 | 4 | throw new IllegalArgumentException("The parameter named [collectionIndex] was null."); |
318 | } | |
319 | 10 | if (persistence == null) { |
320 | 2 | throw new IllegalArgumentException("The parameter named [persistence] was null."); |
321 | } | |
322 | ||
323 | 8 | final String collectionId = collectionIndex.getId(); |
324 | 8 | final Object[] collectionIdQueryArgs = new Object[]{collectionId}; |
325 | 8 | final Connection connection = persistence.getConnection(); |
326 | ||
327 | //check whether collection exists, throw exception if not | |
328 | 8 | final Integer count = (Integer) DatabaseUtils.doSingleValueQuery(connection, persistence, |
329 | "SELECT COUNT(*) FROM DOCUMENT_COLLECTION WHERE COLLECTION_ID=?", collectionIdQueryArgs, | |
330 | DatabaseUtils.INTEGER_TYPE); | |
331 | 8 | if (count.intValue() == 0) { |
332 | 4 | DatabaseUtils.closeSilently(connection); |
333 | 4 | throw new IndexPersistenceException( |
334 | "The given collection with the id [" + collectionId + "] did not exist in the persistence."); | |
335 | } | |
336 | ||
337 | //delete all currently existent records | |
338 | 4 | DatabaseUtils.doDMLQuery(connection, persistence, |
339 | "DELETE FROM DOCUMENT_COLLECTION_DOCUMENT_XREF WHERE COLLECTION_ID=?", collectionIdQueryArgs); | |
340 | ||
341 | // newly insert all records | |
342 | 4 | final Set allDocumentIds = collectionIndex.getAllDocumentIds(); |
343 | 4 | for (Iterator iterator = allDocumentIds.iterator(); iterator.hasNext();) { |
344 | 8 | final WordSourceId sourceId = (WordSourceId) iterator.next(); |
345 | 8 | final String documentId = Utils.createIdString(sourceId); |
346 | 8 | DatabaseUtils.doDMLQuery(connection, persistence, |
347 | "INSERT INTO DOCUMENT_COLLECTION_DOCUMENT_XREF (COLLECTION_ID,DOCUMENT_ID) VALUES (?,?)", | |
348 | new Object[]{collectionId, documentId}); | |
349 | // Use count is maintained externally | |
350 | } | |
351 | 4 | persistence.commitTransaction(connection); |
352 | 4 | } |
353 | ||
354 | /** | |
355 | * This method increases the use count value for document index with specified WordSourceId by the given amount. | |
356 | * | |
357 | * @param persistence the AbstractDBIndexPersistence instance that can be used to obtain a database connection, | |
358 | * perform rollback operations on the connection in case an error is encountered or commit the | |
359 | * connection in case the operation has succeeded | |
360 | * @param wordSourceId WordSourceId of document index of which to update use count | |
361 | * @param amount the amount by which to increase the document use count, may also be a negative integer, then | |
362 | * the result is a decreased use count | |
363 | * | |
364 | * @throws IllegalArgumentException if wordSourceId is <tt>null</tt> | |
365 | * @throws IndexPersistenceException when fails to increase document index use count in the persistence or the | |
366 | * document with the given wordSourceId does not exist in persistence | |
367 | */ | |
368 | static void increaseDocumentUseCount(final AbstractDBIndexPersistence persistence, final WordSourceId wordSourceId, | |
369 | final int amount) throws IndexPersistenceException { | |
370 | 48 | if (wordSourceId == null) { |
371 | 6 | throw new IllegalArgumentException("The parameter named [wordSourceId] was null."); |
372 | } | |
373 | 42 | if (persistence == null) { |
374 | 2 | throw new IllegalArgumentException("The parameter named [persistence] was null."); |
375 | } | |
376 | ||
377 | 40 | final String documentId = Utils.createIdString(wordSourceId); |
378 | 40 | final Connection connection = persistence.getConnection(); |
379 | 40 | persistence.beginTransaction(connection); |
380 | 40 | final int useCount = getUseCount(persistence, connection, documentId); |
381 | 34 | setUseCount(persistence, connection, useCount + amount, documentId); |
382 | 34 | persistence.commitTransaction(connection); |
383 | 34 | } |
384 | ||
385 | /** | |
386 | * This method does return the set of WordSourceId of documents that exist in this persistence instance. | |
387 | * <p/> | |
388 | * CS section 1.4.1.1 describes the algorithm of this method. | |
389 | * | |
390 | * @param persistence the AbstractDBIndexPersistence instance that can be used to obtain a database connection, | |
391 | * perform rollback operations on the connection in case an error is encountered or commit the | |
392 | * connection in case the operation has succeeded | |
393 | * | |
394 | * @return set of WordSourceId of documents that exist in this persistence | |
395 | * | |
396 | * @throws IllegalArgumentException in case the given persistence is <tt>null</tt> | |
397 | * @throws IndexPersistenceException when the retrieval fails | |
398 | */ | |
399 | static Set getIndexedDocuments(final AbstractDBIndexPersistence persistence) throws IndexPersistenceException { | |
400 | 14 | if (persistence == null) { |
401 | 2 | throw new IllegalArgumentException("The parameter named [persistence] was null."); |
402 | } | |
403 | ||
404 | 12 | final Connection connection = persistence.getConnection(); |
405 | 12 | final List result = DatabaseUtils.doSingleColumnQuery(connection, persistence, |
406 | "SELECT DOCUMENT_ID FROM DOCUMENT", new Object[0], DatabaseUtils.STRING_TYPE); | |
407 | ||
408 | 12 | final Set ret = new HashSet(result.size()); |
409 | 12 | for (Iterator iterator = result.iterator(); iterator.hasNext();) { |
410 | 12 | final String documentId = (String) iterator.next(); |
411 | 12 | ret.add(getWordSourceId(persistence, connection, documentId)); |
412 | } | |
413 | 12 | DatabaseUtils.closeSilently(connection); |
414 | 12 | return ret; |
415 | } | |
416 | ||
417 | /** | |
418 | * This method does return the use count of the document index with the given id. | |
419 | * | |
420 | * @param persistence the AbstractDBIndexPersistence instance that can be used to obtain a database connection, | |
421 | * perform rollback operations on the connection in case an error is encountered or commit the | |
422 | * connection in case the operation has succeeded | |
423 | * @param wordSourceId WordSourceId of document index of which to retrieve use count | |
424 | * | |
425 | * @return the use count of the document identified by the given id | |
426 | * | |
427 | * @throws IllegalArgumentException if any arg is <tt>null</tt> | |
428 | * @throws IndexPersistenceException when fails to retrieve document index use count in the persistence or the | |
429 | * document with the given id does not exist in the persistence | |
430 | */ | |
431 | static int getDocumentUseCount(final AbstractDBIndexPersistence persistence, final WordSourceId wordSourceId) | |
432 | throws IndexPersistenceException { | |
433 | 68 | if (wordSourceId == null) { |
434 | 4 | throw new IllegalArgumentException("The parameter named [wordSourceId] was null."); |
435 | } | |
436 | 64 | if (persistence == null) { |
437 | 2 | throw new IllegalArgumentException("The parameter named [persistence] was null."); |
438 | } | |
439 | ||
440 | 62 | final String documentId = Utils.createIdString(wordSourceId); |
441 | 62 | final Connection connection = persistence.getConnection(); |
442 | 62 | final int useCount = getUseCount(persistence, connection, documentId); |
443 | 56 | DatabaseUtils.closeSilently(connection); |
444 | 56 | return useCount; |
445 | } | |
446 | ||
447 | /** | |
448 | * This method does persist the document header data stored in the given WordSourceId into a new record in the | |
449 | * DOCUMENT table. | |
450 | * <p/> | |
451 | * CS section 1.4.1.3 (steps 2 and 3) describes the algorithm of this method. | |
452 | * | |
453 | * @param persistence the AbstractDBIndexPersistence instance that can be used to perform rollback operations on the | |
454 | * connection in case an error is encountered | |
455 | * @param sourceId the source identity representing the document to be persisted | |
456 | * @param connection the connection on which to perform the database operation | |
457 | * | |
458 | * @return the calculated id string representing the given wordSourceId as database key | |
459 | * | |
460 | * @throws IllegalArgumentException in case any arg is <tt>null</tt> | |
461 | * @throws com.topcoder.document.index.persistence.IndexPersistenceException | |
462 | * when fails to add DocumentIndex to the persistence or given index is already | |
463 | * persisted | |
464 | */ | |
465 | ||
466 | static String insertDocumentHeaderData(final AbstractDBIndexPersistence persistence, final WordSourceId sourceId, | |
467 | final Connection connection) throws | |
468 | IndexPersistenceException { | |
469 | 80 | if (persistence == null) { |
470 | 2 | throw new IllegalArgumentException("The parameter named [persistence] was null."); |
471 | } | |
472 | 78 | if (sourceId == null) { |
473 | 2 | throw new IllegalArgumentException("The parameter named [sourceId] was null."); |
474 | } | |
475 | 76 | if (connection == null) { |
476 | 2 | throw new IllegalArgumentException("The parameter named [connection] was null."); |
477 | } | |
478 | ||
479 | // the validity of these values (CS 1.4.1.3) is guaranteed by the final class | |
480 | // WordSourceId and its constructor logic | |
481 | 74 | final Locale locale = sourceId.getSourceLocale(); |
482 | 74 | final String[] delimiters = sourceId.getDelimiters(); |
483 | ||
484 | 74 | final String documentId = Utils.createIdString(sourceId); |
485 | 74 | final byte[] sourceIdentityBinary = Utils.serializeObject(sourceId.getSourceIdentity()); |
486 | 74 | final Integer count = (Integer) DatabaseUtils.doSingleValueQuery(connection, persistence, |
487 | "SELECT COUNT(*) from DOCUMENT WHERE DOCUMENT_ID=?", | |
488 | new Object[]{documentId}, DatabaseUtils.INTEGER_TYPE); | |
489 | 74 | if (count.intValue() > 0) { |
490 | 4 | throw new IndexPersistenceException("The given document index does already exist in the database."); |
491 | } | |
492 | // create the DOCUMENT record | |
493 | 70 | DatabaseUtils.doDMLQuery(connection, persistence, |
494 | "INSERT INTO DOCUMENT (DOCUMENT_ID,LOCALE,SOURCE_IDENTITY,CLASS_NAME,USE_COUNT) VALUES (?,?,?,?,?)", | |
495 | new Object[]{ | |
496 | documentId, | |
497 | locale.toString(), | |
498 | sourceIdentityBinary, | |
499 | sourceId.getSourceClassName(), | |
500 | new Integer(0) | |
501 | }); | |
502 | ||
503 | // insert the DELIMITERs | |
504 | 292 | for (int i = 0; i < delimiters.length; i++) { |
505 | ||
506 | 222 | DatabaseUtils.doDMLQuery(connection, persistence, |
507 | "INSERT INTO DELIMITER (DOCUMENT_ID,DELIMITER_ENTRY) VALUES (?,?)", | |
508 | new Object[]{ | |
509 | documentId, | |
510 | delimiters[i] | |
511 | }); | |
512 | } | |
513 | 70 | return documentId; |
514 | } | |
515 | ||
516 | /** | |
517 | * This method sets the use count value for document index with specified documentId to the given value. | |
518 | * | |
519 | * @param persistence the AbstractDBIndexPersistence instance that can be used to obtain a database connection, | |
520 | * perform rollback operations on the connection in case an error is encountered or commit the | |
521 | * connection in case the operation has succeeded | |
522 | * @param connection the connection on which to operate | |
523 | * @param documentId id of document index of which to update use count | |
524 | * @param newUseCount new use count value | |
525 | * | |
526 | * @throws IllegalArgumentException if wordSourceId is <tt>null</tt> | |
527 | * @throws IndexPersistenceException when fails to increase document index use count in the persistence or the | |
528 | * document with the given wordSourceId does not exist in persistence | |
529 | */ | |
530 | private static void setUseCount(final AbstractDBIndexPersistence persistence, final Connection connection, | |
531 | final int newUseCount, | |
532 | final String documentId) throws IndexPersistenceException { | |
533 | 34 | DatabaseUtils.doDMLQuery(connection, persistence, "UPDATE DOCUMENT SET USE_COUNT=? WHERE DOCUMENT_ID=?", |
534 | new Object[]{new Integer(newUseCount), documentId}); | |
535 | 34 | } |
536 | ||
537 | /** | |
538 | * This method retrieves the use count value for document index with specified WordSourceId. | |
539 | * | |
540 | * @param persistence the AbstractDBIndexPersistence instance that can be used to obtain a database connection, | |
541 | * perform rollback operations on the connection in case an error is encountered or commit the | |
542 | * connection in case the operation has succeeded | |
543 | * @param connection the connection on which to operate | |
544 | * @param documentId id of document index of which to retrieve use count | |
545 | * | |
546 | * @return the use count of the document with the given id | |
547 | * | |
548 | * @throws IllegalArgumentException if wordSourceId is <tt>null</tt> | |
549 | * @throws IndexPersistenceException when fails to increase document index use count in the persistence or the | |
550 | * document with the given wordSourceId does not exist in persistence | |
551 | */ | |
552 | private static int getUseCount(final AbstractDBIndexPersistence persistence, final Connection connection, | |
553 | final String documentId) | |
554 | throws IndexPersistenceException { | |
555 | //check whether document exists, throw exception if not | |
556 | 102 | final List result = DatabaseUtils.doQuery(connection, persistence, |
557 | "SELECT USE_COUNT FROM DOCUMENT WHERE DOCUMENT_ID=?", | |
558 | new Object[]{documentId}, | |
559 | new DatabaseUtils.DataType[]{DatabaseUtils.INTEGER_TYPE}); | |
560 | 102 | if (result.isEmpty()) { |
561 | 12 | DatabaseUtils.closeSilently(connection); |
562 | 12 | throw new IndexPersistenceException( |
563 | "The given document with the database id [" + documentId + "] did not exist in the persistence."); | |
564 | } | |
565 | ||
566 | 90 | return ((Integer) ((Object[]) result.get(0))[0]).intValue(); |
567 | } | |
568 | ||
569 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |