r48453 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r48452‎ | r48453 | r48454 >
Date:19:48, 16 March 2009
Author:demon
Status:reverted
Tags:
Comment:
AndCache 0.2:
* Allow contexts to lock the cache to their private uses
* Use db.delete() instead of raw SQL
* Standardize exceptions a bit
Modified paths:
  • /trunk/android-client/src/org/mediawiki/android/AndCache.java (modified) (history)

Diff [purge]

Index: trunk/android-client/src/org/mediawiki/android/AndCache.java
@@ -28,7 +28,7 @@
2929 * set.
3030 *
3131 * @author Chad Horohoe
32 - * @version 0.1
 32+ * @version 0.2
3333 * @license GNU GPL v2 (or later)
3434 *
3535 * This program is free software; you can redistribute it and/or modify
@@ -54,6 +54,9 @@
5555 // For debugging
5656 private static final String LOG = "org.mediawiki.android.Cache";
5757
 58+ // Call lockCache() to get a lock on the cache
 59+ private static boolean locked = false;
 60+
5861 // Database info
5962 private static final String DB_NAME = "cache.db";
6063 private static final int DB_VERSION = 1;
@@ -165,15 +168,27 @@
166169 private static void delete( String key ) throws CacheException {
167170 SQLiteDatabase db = mWrapper.getWritableDatabase();
168171 try {
169 - db.execSQL( "DELETE FROM " + TBL_NAME + " WHERE " + TBL_SCHEMA.cols.KEY + " = '" + key + "'" );
 172+ db.delete( TBL_NAME, TBL_SCHEMA.cols.KEY + " = '" + key + "'", null );
170173 } catch ( SQLiteException e ) {
171 - throw new CacheException( "SQL Error in AndCache.delete() :" + e.getMessage() );
 174+ throw new CacheException( "SQL", "AndCache.delete", e.getMessage() );
172175 } finally {
173176 Log.i( "org.mediawiki.android.Cache", "Deleting key: " + key );
174177 }
175178 }
176179
177180 /**
 181+ * Flush the entire cache. Helpful if you've got stuff caught in it
 182+ */
 183+ public static void flush() throws CacheException {
 184+ SQLiteDatabase db = mWrapper.getWritableDatabase();
 185+ try {
 186+ db.delete( TBL_NAME , "1", null );
 187+ } catch( SQLiteException e ) {
 188+ throw new CacheException( "Query", "flush()", e.getMessage() );
 189+ }
 190+ }
 191+
 192+ /**
178193 * Is the entry expired?
179194 * @param
180195 * @return boolean
@@ -198,11 +213,11 @@
199214 oOut.writeObject(obj);
200215 bytes = bOut.toByteArray();
201216 } catch ( IOException e ) {
202 - throw new CacheException( "Failed to serialize, IOException: " + e.getMessage() );
 217+ throw new CacheException( "IOException", "AndCache.serialize", e.getMessage() );
203218 }
204219 return bytes;
205220 }
206 -
 221+
207222 /**
208223 * Unserialize an object
209224 * @param byte[] bytes A byte array to be deserialized into an object
@@ -217,9 +232,9 @@
218233 bIn.close();
219234 return obj;
220235 } catch( IOException e ) {
221 - throw new CacheException( "Failed to unserialize, IOException: " + e.getMessage() );
 236+ throw new CacheException( "IOException", "AndCache.unserialize", e.getMessage() );
222237 } catch (ClassNotFoundException e) {
223 - throw new CacheException( "Failed to unserialize: " + e.getMessage() );
 238+ throw new CacheException( "ClassNotFoundException", "AndCache.unserialize" ,e.getMessage() );
224239 }
225240 }
226241
@@ -266,18 +281,36 @@
267282 * Need to be able to set our context
268283 * @param Context c the new Context to use
269284 */
270 - public static void setContext( Context c ) throws CacheException {
271 - try {
272 - AndCache.ctx = c;
273 - mWrapper = new DbWrapper( AndCache.ctx );
 285+ public static void setContext( Context c, boolean lock ) throws CacheException {
 286+ String curPackage = AndCache.ctx.getPackageName();
 287+ if ( c.getPackageName() == curPackage ) {
 288+ return;
274289 }
275 - catch ( SQLiteException e ) {
276 - throw new CacheException( "Could not setup db for ctx " +
277 - c.getClass().getName() + ". SQL error was" + e.getMessage() );
 290+ else if ( AndCache.locked ) {
 291+ throw new CacheException( "Context locked", "AndCache.setContext",
 292+ "locked to " + curPackage );
278293 }
 294+ else {
 295+ try {
 296+ AndCache.ctx = c;
 297+ mWrapper = new DbWrapper( AndCache.ctx );
 298+ AndCache.locked = lock;
 299+ }
 300+ catch ( SQLiteException e ) {
 301+ throw new CacheException( "Create DB", "AndCache.setContext", e.getMessage() );
 302+ }
 303+ }
279304 }
280305
281306 /**
 307+ * Unlock the cache. Call this if you've gotten a lock and you're
 308+ * done with the cache.
 309+ */
 310+ public static void unlockCache() {
 311+ AndCache.locked = false;
 312+ }
 313+
 314+ /**
282315 * Get the current context and re-init the DbWrapper
283316 * @return Context
284317 */
@@ -291,8 +324,19 @@
292325 public static class CacheException extends Exception {
293326 // Auto-gen
294327 private static final long serialVersionUID = 675324595007281374L;
295 - public CacheException( String msg ) {
296 - super(msg);
 328+ /**
 329+ * Cache exceptions. We generally throw these when catching higher
 330+ * exceptions so we can bail out nicer.
 331+ * @param error The specific type of error or exception being caught
 332+ * @param method The method that is throwing the error (eg: AndCache.get)
 333+ * @param eMsg Any extra info to return, perhaps from a caught exception
 334+ */
 335+ public CacheException( String error, String caller, String eMsg ) {
 336+ // Java is stupid and wants the parent call first, so I can't
 337+ // just make a string and use it twice :\
 338+ super( error + " error in " + caller + "()" + ( eMsg != null ? ": " + eMsg : "" ) );
 339+ Log.e( AndCache.LOG, error + " error in " + caller + "()" +
 340+ ( eMsg != null ? ": " + eMsg : "" ) );
297341 }
298342 }
299343 }

Status & tagging log