public static void purgeAttachments ( SQLDatabase db , String attachmentsDir ) { // it's easier to deal with Strings since java doesn't know how to compare byte[]s Set < String > currentKeys = new HashSet < String > ( ) ; Cursor c = null ; try { // delete attachment table entries for revs which have been purged db . delete ( "attachments" , "sequence IN " + "(SELECT sequence from revs WHERE json IS null)" , null ) ; // get all keys from attachments table c = db . rawQuery ( SQL_ATTACHMENTS_SELECT_ALL_KEYS , null ) ; while ( c . moveToNext ( ) ) { byte [ ] key = c . getBlob ( 0 ) ; currentKeys . add ( keyToString ( key ) ) ; } } catch ( SQLException e ) { logger . log ( Level . SEVERE , "SQL exception in purgeAttachments when updating attachments table" , e ) ; return ; // not safe to continue } finally { DatabaseUtils . closeCursorQuietly ( c ) ; } try { // Now iterate through the attachments_key_filename, deleting items we need to // (both db row and file on disk). File attachments = new File ( attachmentsDir ) ; c = db . rawQuery ( SQL_ATTACHMENTS_SELECT_KEYS_FILENAMES , null ) ; while ( c . moveToNext ( ) ) { String keyForFile = c . getString ( 0 ) ; if ( ! currentKeys . contains ( keyForFile ) ) { File f = new File ( attachments , c . getString ( 1 ) ) ; try { boolean deleted = f . delete ( ) ; if ( deleted ) { db . delete ( ATTACHMENTS_KEY_FILENAME , "key = ?" , new String [ ] { keyForFile } ) ; } else { logger . warning ( "Could not delete file from BLOB store: " + f . getAbsolutePath ( ) ) ; } } catch ( SecurityException e ) { String msg = String . format ( "SecurityException deleting %s from blob store" , f . getAbsolutePath ( ) ) ; logger . log ( Level . WARNING , msg , e ) ; } } } } catch ( SQLException e ) { logger . log ( Level . SEVERE , "SQL exception in purgeAttachments when removing redundant attachments" , e ) ; } finally { DatabaseUtils . closeCursorQuietly ( c ) ; } }
Called by DatabaseImpl on the execution queue this needs to have the db passed to it .