public boolean updateSearchFunctionIfNecessary ( CouchDbConnector db , String viewName , String searchFunction , String javascriptIndexFunctionBody ) { boolean updatePerformed = false ; String designDocName = viewName . startsWith ( DesignDocument . ID_PREFIX ) ? viewName : ( DesignDocument . ID_PREFIX + viewName ) ; Checksum chk = new CRC32 ( ) ; byte [ ] bytes = javascriptIndexFunctionBody . getBytes ( ) ; chk . update ( bytes , 0 , bytes . length ) ; long actualChk = chk . getValue ( ) ; if ( ! db . contains ( designDocName ) ) { // it doesn't exist, let's put one in DesignDocument doc = new DesignDocument ( designDocName ) ; db . create ( doc ) ; updateSearchFunction ( db , doc , searchFunction , javascriptIndexFunctionBody , actualChk ) ; updatePerformed = true ; } else { // make sure it's up to date DesignDocument doc = db . get ( DesignDocument . class , designDocName ) ; Number docChk = ( Number ) doc . getAnonymous ( ) . get ( getChecksumFieldName ( searchFunction ) ) ; if ( docChk == null || ! ( docChk . longValue ( ) == actualChk ) ) { log . info ( "Updating the index function" ) ; updateSearchFunction ( db , doc , searchFunction , javascriptIndexFunctionBody , actualChk ) ; updatePerformed = true ; } } return updatePerformed ; }
Ensures taht the given body of the index function matches that in the database otherwise update it .