idx
int64
0
165k
question
stringlengths
73
5.81k
target
stringlengths
5
918
11,600
public static boolean isBlank ( final String source ) { if ( isEmpty ( source ) ) { return true ; } int strLen = source . length ( ) ; for ( int i = 0 ; i < strLen ; i ++ ) { if ( ! Character . isWhitespace ( source . charAt ( i ) ) ) { return false ; } } return true ; }
Returns true if the source string is null or all characters in this string is space ; false otherwise .
11,601
public static int indexOfIgnoreCase ( final String source , final String target ) { int targetIndex = source . indexOf ( target ) ; if ( targetIndex == INDEX_OF_NOT_FOUND ) { String sourceLowerCase = source . toLowerCase ( ) ; String targetLowerCase = target . toLowerCase ( ) ; targetIndex = sourceLowerCase . indexOf (...
Returns the index within given source string of the first occurrence of the specified target string with ignore case sensitive .
11,602
public static boolean containsWord ( final String text , final String word ) { if ( text == null || word == null ) { return false ; } if ( text . contains ( word ) ) { Matcher matcher = matchText ( text ) ; for ( ; matcher . find ( ) ; ) { String matchedWord = matcher . group ( 0 ) ; if ( matchedWord . equals ( word ) ...
Returns true if given text contains given word ; false otherwise .
11,603
public static boolean containsWordIgnoreCase ( final String text , final String word ) { if ( text == null || word == null ) { return false ; } return containsWord ( text . toLowerCase ( ) , word . toLowerCase ( ) ) ; }
Returns true if given text contains given word ignore case ; false otherwise .
11,604
public static boolean startsWithIgnoreCase ( final String source , final String target ) { if ( source . startsWith ( target ) ) { return true ; } if ( source . length ( ) < target . length ( ) ) { return false ; } return source . substring ( 0 , target . length ( ) ) . equalsIgnoreCase ( target ) ; }
Returns true if given source string start with target string ignore case sensitive ; false otherwise .
11,605
public static boolean endsWithIgnoreCase ( final String source , final String target ) { if ( source . endsWith ( target ) ) { return true ; } if ( source . length ( ) < target . length ( ) ) { return false ; } return source . substring ( source . length ( ) - target . length ( ) ) . equalsIgnoreCase ( target ) ; }
Returns true if given source string end with target string ignore case sensitive ; false otherwise .
11,606
public static String getRandomString ( final int stringLength ) { StringBuilder stringBuilder = getStringBuild ( ) ; for ( int i = 0 ; i < stringLength ; i ++ ) { stringBuilder . append ( getRandomAlphabetic ( ) ) ; } return stringBuilder . toString ( ) ; }
Returns a random given length size alphabetic string .
11,607
public static String encoding ( final String source , final String sourceCharset , final String encodingCharset ) throws IllegalArgumentException { byte [ ] sourceBytes ; String encodeString = null ; try { sourceBytes = source . getBytes ( sourceCharset ) ; encodeString = new String ( sourceBytes , encodingCharset ) ; ...
Return encoded string by given charset .
11,608
public static double [ ] getBorders ( int numberOfBins , List < Double > counts ) { Collections . sort ( counts ) ; if ( ! counts . isEmpty ( ) ) { List < Integer > borderInds = findBordersNaive ( numberOfBins , counts ) ; if ( borderInds == null ) { borderInds = findBorderIndsByRepeatedDividing ( numberOfBins , counts...
Carefull this method sorts the counts list!
11,609
public Map < String , Set < String > > getDependencies ( ) { Map < String , Set < String > > new_deps = new HashMap < String , Set < String > > ( ) ; if ( explicitPackages == null ) return new_deps ; for ( Name pkg : explicitPackages ) { Set < Name > set = deps . get ( pkg ) ; if ( set != null ) { Set < String > new_se...
Fetch the set of dependencies that are relevant to the compile that has just been performed . I . e . we are only interested in dependencies for classes that were explicitly compiled .
11,610
public void visitPubapi ( Element e ) { Name n = ( ( ClassSymbol ) e ) . fullname ; Name p = ( ( ClassSymbol ) e ) . packge ( ) . fullname ; StringBuffer sb = publicApiPerClass . get ( n ) ; assert ( sb == null ) ; sb = new StringBuffer ( ) ; PubapiVisitor v = new PubapiVisitor ( sb ) ; v . visit ( e ) ; if ( sb . leng...
Visit the api of a class and construct a pubapi string and store it into the pubapi_perclass map .
11,611
public void collect ( Name currPkg , Name depPkg ) { if ( ! currPkg . equals ( depPkg ) ) { Set < Name > theset = deps . get ( currPkg ) ; if ( theset == null ) { theset = new HashSet < Name > ( ) ; deps . put ( currPkg , theset ) ; } theset . add ( depPkg ) ; } }
Collect a dependency . curr_pkg is marked as depending on dep_pkg .
11,612
@ SuppressWarnings ( "unchecked" ) public void preload ( int quantity ) { Object [ ] objects = new Object [ quantity ] ; for ( int i = 0 ; i < quantity ; i ++ ) { objects [ i ] = this . instantiate ( ) ; } for ( int i = 0 ; i < quantity ; i ++ ) { this . release ( ( T ) objects [ i ] ) ; } }
Instantiate quantity numbers of elements from the Pool and set them in the pool right away
11,613
private void blockMe ( int cyclesToBlock ) { long unblock = cycles + cyclesToBlock ; BlockedEntry newEntry = new BlockedEntry ( unblock ) ; blockedCollection . add ( newEntry ) ; while ( unblock > cycles ) { try { newEntry . getSync ( ) . acquire ( ) ; } catch ( InterruptedException exc ) { log . error ( "[temporaryBlo...
Blocks the calling thread until enough turns have elapsed
11,614
public void tick ( ) { log . trace ( "Tick {}" , cycles ) ; cycles ++ ; for ( BlockedEntry entry : blockedCollection ) { if ( entry . getTimeout ( ) >= cycles ) { entry . getSync ( ) . release ( ) ; } } }
Signals one cycles has elapsed
11,615
public void waitFor ( Integer clientId , int turns , String reason ) { if ( ! registered . contains ( clientId ) ) { throw new IllegalArgumentException ( "Unknown robot. All robots must first register with clock" ) ; } synchronized ( waitingList ) { if ( waitingList . contains ( clientId ) ) { throw new IllegalArgument...
Blocks the calling thread for the specified number of cycles
11,616
public String produce ( int size ) { byte bytes [ ] = new byte [ size ] ; _random . nextBytes ( bytes ) ; return Strings . toHexString ( bytes ) ; }
Produces a nonce of an arbitrary size in bytes which is not to be proved later .
11,617
public String produce ( String state , long time ) { Nonce nonce = new Nonce ( INSTANCE , time > 0 ? time : TTL , SIZE , _random ) ; _map . put ( state + ':' + nonce . value , nonce ) ; return nonce . value ; }
Produces a nonce that is associated with the given state and is to be proved within a time limit .
11,618
public boolean renew ( String value , String state , long time ) { Nonce nonce = _map . get ( state + ':' + value ) ; if ( nonce != null && ! nonce . hasExpired ( ) ) { nonce . renew ( time > 0 ? time : TTL ) ; return true ; } else { return false ; } }
Renews a nonce . The nonce must be valid and must have not expired .
11,619
public boolean prove ( String value , String state ) { Nonce nonce = _map . remove ( state + ':' + value ) ; return nonce != null && ! nonce . hasExpired ( ) ; }
Proves a nonce and its associated state . A nonce can only be successfully proved once .
11,620
public boolean hasCategory ( final String conceptURI ) { return Iterables . any ( getCategories ( ) , new Predicate < TopicAnnotation > ( ) { public boolean apply ( TopicAnnotation ta ) { return ta . getTopicReference ( ) . equals ( conceptURI ) ; } } ) ; }
Returns true if the contents has been categorized with a category identified by the URI passed by parameter
11,621
public static SQLiteDatabase create ( CursorFactory factory ) { return openDatabase ( com . couchbase . lite . internal . database . sqlite . SQLiteDatabaseConfiguration . MEMORY_DB_PATH , factory , CREATE_IF_NECESSARY ) ; }
Create a memory backed SQLite database . Its contents will be destroyed when the database is closed .
11,622
public long replace ( String table , String nullColumnHack , ContentValues initialValues ) { try { return insertWithOnConflict ( table , nullColumnHack , initialValues , CONFLICT_REPLACE ) ; } catch ( SQLException e ) { DLog . e ( TAG , "Error inserting " + initialValues , e ) ; return - 1 ; } }
Convenience method for replacing a row in the database .
11,623
private static byte [ ] decodeBase64Digest ( String base64Digest ) { String expectedPrefix = "sha1-" ; if ( ! base64Digest . startsWith ( expectedPrefix ) ) { throw new IllegalArgumentException ( base64Digest + " did not start with " + expectedPrefix ) ; } base64Digest = base64Digest . replaceFirst ( expectedPrefix , "...
Decode base64 d getDigest into a byte array that is suitable for use as a blob key .
11,624
protected void fireTrigger ( final ReplicationTrigger trigger ) { Log . d ( Log . TAG_SYNC , "%s [fireTrigger()] => " + trigger , this ) ; synchronized ( executor ) { if ( ! executor . isShutdown ( ) ) { executor . submit ( new Runnable ( ) { public void run ( ) { try { Log . d ( Log . TAG_SYNC , "firing trigger: %s" ,...
Fire a trigger to the state machine
11,625
protected void start ( ) { try { if ( ! db . isOpen ( ) ) { String msg = String . format ( Locale . ENGLISH , "Db: %s is not open, abort replication" , db ) ; parentReplication . setLastError ( new Exception ( msg ) ) ; fireTrigger ( ReplicationTrigger . STOP_IMMEDIATE ) ; return ; } db . addActiveReplication ( parentR...
Start the replication process .
11,626
protected void close ( ) { this . authenticating = false ; for ( Future future : pendingFutures ) { future . cancel ( false ) ; CancellableRunnable runnable = cancellables . get ( future ) ; if ( runnable != null ) { runnable . cancel ( ) ; cancellables . remove ( future ) ; } } if ( remoteRequestExecutor != null && ! ...
Close all resources associated with this replicator .
11,627
protected void checkSession ( ) { if ( getAuthenticator ( ) != null ) { Authorizer auth = ( Authorizer ) getAuthenticator ( ) ; auth . setRemoteURL ( remote ) ; auth . setLocalUUID ( db . publicUUID ( ) ) ; } if ( getAuthenticator ( ) != null && getAuthenticator ( ) instanceof SessionCookieAuthorizer ) { checkSessionAt...
Before doing anything else determine whether we have an active login session .
11,628
private void refreshRemoteCheckpointDoc ( ) { Log . i ( Log . TAG_SYNC , "%s: Refreshing remote checkpoint to get its _rev..." , this ) ; Future future = sendAsyncRequest ( "GET" , "_local/" + remoteCheckpointDocID ( ) , null , new RemoteRequestCompletion ( ) { public void onCompletion ( RemoteRequest remoteRequest , R...
Variant of - fetchRemoveCheckpointDoc that s used while replication is running to reload the checkpoint to get its current revision number if there was an error saving it .
11,629
protected void stop ( ) { this . authenticating = false ; batcher . clear ( ) ; setLifecycle ( Replication . Lifecycle . ONESHOT ) ; cancelRetryFuture ( ) ; while ( ! pendingFutures . isEmpty ( ) ) { Future future = pendingFutures . poll ( ) ; if ( future != null && ! future . isCancelled ( ) && ! future . isDone ( ) )...
Actual work of stopping the replication process .
11,630
private void notifyChangeListeners ( final Replication . ChangeEvent changeEvent ) { if ( changeListenerNotifyStyle == ChangeListenerNotifyStyle . SYNC ) { for ( ChangeListener changeListener : changeListeners ) { try { changeListener . changed ( changeEvent ) ; } catch ( Exception e ) { Log . e ( Log . TAG_SYNC , "Unk...
Notify all change listeners of a ChangeEvent
11,631
private void scheduleRetryFuture ( ) { Log . v ( Log . TAG_SYNC , "%s: Failed to xfer; will retry in %d sec" , this , RETRY_DELAY_SECONDS ) ; synchronized ( executor ) { if ( ! executor . isShutdown ( ) ) { this . retryFuture = executor . schedule ( new Runnable ( ) { public void run ( ) { retryIfReady ( ) ; } } , RETR...
helper function to schedule retry future . no in iOS code .
11,632
private void cancelRetryFuture ( ) { if ( retryFuture != null && ! retryFuture . isDone ( ) ) { retryFuture . cancel ( true ) ; } retryFuture = null ; }
helper function to cancel retry future . not in iOS code .
11,633
protected void retryReplicationIfError ( ) { Log . d ( TAG , "retryReplicationIfError() state=" + stateMachine . getState ( ) + ", error=" + this . error + ", isContinuous()=" + isContinuous ( ) + ", isTransientError()=" + Utils . isTransientError ( this . error ) ) ; if ( ! stateMachine . getState ( ) . equals ( Repli...
Retry replication if previous attempt ends with error
11,634
void dumpUnsafe ( Printer printer , boolean verbose ) { printer . println ( "Connection #" + mConnectionId + ":" ) ; if ( verbose ) { printer . println ( " connectionPtr: 0x" + Long . toHexString ( mConnectionPtr ) ) ; } printer . println ( " isPrimaryConnection: " + mIsPrimaryConnection ) ; printer . println ( " on...
Dumps debugging information about this connection in the case where the caller might not actually own the connection .
11,635
void collectDbStatsUnsafe ( ArrayList < com . couchbase . lite . internal . database . sqlite . SQLiteDebug . DbStats > dbStatsList ) { dbStatsList . add ( getMainDbStatsUnsafe ( 0 , 0 , 0 ) ) ; }
Collects statistics about database connection memory usage in the case where the caller might not actually own the connection .
11,636
protected void execute ( ) { Log . v ( Log . TAG_SYNC , "%s: RemoteRequest execute() called, url: %s" , this , url ) ; executeRequest ( factory . getOkHttpClient ( ) , request ( ) ) ; Log . v ( Log . TAG_SYNC , "%s: RemoteRequest execute() finished, url: %s" , this , url ) ; }
Execute remote request
11,637
protected RequestBody setCompressedBody ( byte [ ] bodyBytes ) { if ( bodyBytes . length < MIN_JSON_LENGTH_TO_COMPRESS ) return null ; byte [ ] encodedBytes = Utils . compressByGzip ( bodyBytes ) ; if ( encodedBytes == null || encodedBytes . length >= bodyBytes . length ) return null ; return RequestBody . create ( JSO...
Generate gzipped body
11,638
int indexOf ( byte [ ] data , int dataLength , byte [ ] pattern , int dataOffset ) { int [ ] failure = computeFailure ( pattern ) ; int j = 0 ; if ( data . length == 0 ) return - 1 ; final int patternLength = pattern . length ; for ( int i = dataOffset ; i < dataLength ; i ++ ) { while ( j > 0 && pattern [ j ] != data ...
Finds the first occurrence of the pattern in the text .
11,639
private static int [ ] computeFailure ( byte [ ] pattern ) { int [ ] failure = new int [ pattern . length ] ; int j = 0 ; for ( int i = 1 ; i < pattern . length ; i ++ ) { while ( j > 0 && pattern [ j ] != pattern [ i ] ) j = failure [ j - 1 ] ; if ( pattern [ j ] == pattern [ i ] ) j ++ ; failure [ i ] = j ; } return ...
Computes the failure function using a boot - strapping process where the pattern is matched against itself .
11,640
public List < String > getAllDatabaseNames ( ) { String [ ] databaseFiles = directoryFile . list ( new FilenameFilter ( ) { public boolean accept ( File dir , String filename ) { if ( filename . endsWith ( Manager . kDBExtension ) ) { return true ; } return false ; } } ) ; List < String > result = new ArrayList < Strin...
An array of the names of all existing databases .
11,641
public void close ( ) { synchronized ( lockDatabases ) { Log . d ( Database . TAG , "Closing " + this ) ; Database [ ] openDbs = databases . values ( ) . toArray ( new Database [ databases . size ( ) ] ) ; for ( Database database : openDbs ) database . close ( ) ; databases . clear ( ) ; context . getNetworkReachabilit...
Releases all resources used by the Manager instance and closes all its databases .
11,642
public boolean replaceDatabase ( String databaseName , String databaseDir ) { Database db = getDatabase ( databaseName , false ) ; if ( db == null ) return false ; File dir = new File ( databaseDir ) ; if ( ! dir . exists ( ) ) { Log . w ( Database . TAG , "Database file doesn't exist at path : %s" , databaseDir ) ; re...
Replaces or installs a database from a file .
11,643
public Future runAsync ( String databaseName , final AsyncTask function ) throws CouchbaseLiteException { final Database database = getDatabase ( databaseName ) ; return runAsync ( new Runnable ( ) { public void run ( ) { function . run ( database ) ; } } ) ; }
Asynchronously dispatches a callback to run on a background thread . The callback will be passed Database instance . There is not currently a known reason to use it it may not make sense on the Android API but it was added for the purpose of having a consistent API with iOS .
11,644
public void startedPart ( Map headers ) { if ( _docReader != null ) throw new IllegalStateException ( "_docReader is already defined" ) ; Log . v ( TAG , "%s: Starting new document; headers =%s" , this , headers ) ; _docReader = new MultipartDocumentReader ( db ) ; _docReader . setHeaders ( headers ) ; _docReader . sta...
This method is called when a part s headers have been parsed before its data is parsed .
11,645
public void finishedPart ( ) { if ( _docReader == null ) throw new IllegalStateException ( "_docReader is not defined" ) ; _docReader . finish ( ) ; _onDocument . onDocument ( _docReader . getDocumentProperties ( ) , _docReader . getDocumentSize ( ) ) ; _docReader = null ; Log . v ( TAG , "%s: Finished document" , this...
This method is called when a part is complete .
11,646
public Document getDocument ( ) { if ( getDocumentId ( ) == null ) { return null ; } assert ( database != null ) ; Document document = database . getDocument ( getDocumentId ( ) ) ; document . loadCurrentRevisionFrom ( this ) ; return document ; }
The document this row was mapped from . This will be nil if a grouping was enabled in the query because then the result rows don t correspond to individual documents .
11,647
public String getDocumentId ( ) { String docID = null ; if ( documentRevision != null ) docID = documentRevision . getDocID ( ) ; if ( docID == null ) { if ( value != null ) { if ( value instanceof Map ) { Map < String , Object > props = ( Map < String , Object > ) value ; docID = ( String ) props . get ( "_id" ) ; } }...
The ID of the document described by this view row . This is not necessarily the same as the document that caused this row to be emitted ; see the discussion of the . sourceDocumentID property for details .
11,648
public String getDocumentRevisionId ( ) { String rev = null ; if ( documentRevision != null ) rev = documentRevision . getRevID ( ) ; if ( rev == null ) { if ( value instanceof Map ) { Map < String , Object > mapValue = ( Map < String , Object > ) value ; rev = ( String ) mapValue . get ( "_rev" ) ; if ( rev == null ) ...
The revision ID of the document this row was mapped from .
11,649
public void add ( final AtomicAction action ) { if ( action instanceof Action ) { Action a = ( Action ) action ; peforms . addAll ( a . peforms ) ; backouts . addAll ( a . backouts ) ; cleanUps . addAll ( a . cleanUps ) ; } else { add ( new ActionBlock ( ) { public void execute ( ) throws ActionException { action . per...
Adds an action as a step of thid one .
11,650
public void add ( ActionBlock perform , ActionBlock backout , ActionBlock cleanup ) { peforms . add ( perform != null ? perform : nullAction ) ; backouts . add ( backout != null ? backout : nullAction ) ; cleanUps . add ( cleanup != null ? cleanup : nullAction ) ; }
Adds an action as a step of this one . The action has three components each optional .
11,651
public void run ( ) throws ActionException { try { perform ( ) ; try { cleanup ( ) ; } catch ( ActionException e ) { } lastError = null ; } catch ( ActionException e ) { lastError = e ; throw e ; } }
Performs all the actions in order . If any action fails backs out the previously performed actions in reverse order . If the actions succeeded cleans them up in reverse order . The lastError property is set to the exception thrown by the failed perform block . The failedStep property is set to the index of the failed p...
11,652
private void doAction ( List < ActionBlock > actions ) throws ActionException { try { actions . get ( nextStep ) . execute ( ) ; } catch ( ActionException e ) { throw e ; } catch ( Exception e ) { throw new ActionException ( "Exception raised by step: " + nextStep , e ) ; } }
Subroutine that calls an action block from either performs backOuts or cleanUps .
11,653
public boolean setVersion ( String version ) { SQLiteStorageEngine storage = store . getStorageEngine ( ) ; boolean hasView ; Cursor cursor = null ; try { String sql = "SELECT name, version FROM views WHERE name=?" ; String [ ] args = { name } ; cursor = storage . rawQuery ( sql , args ) ; hasView = cursor . moveToNext...
Updates the version of the view . A change in version means the delegate s map block has changed its semantics so the _index should be deleted .
11,654
public long getLastSequenceIndexed ( ) { String sql = "SELECT lastSequence FROM views WHERE name=?" ; String [ ] args = { name } ; Cursor cursor = null ; long result = - 1 ; try { cursor = store . getStorageEngine ( ) . rawQuery ( sql , args ) ; if ( cursor . moveToNext ( ) ) { result = cursor . getLong ( 0 ) ; } } cat...
The last sequence number that has been indexed .
11,655
private static boolean groupTogether ( Object key1 , Object key2 , int groupLevel ) { if ( groupLevel == 0 || ! ( key1 instanceof List ) || ! ( key2 instanceof List ) ) { return key1 . equals ( key2 ) ; } @ SuppressWarnings ( "unchecked" ) List < Object > key1List = ( List < Object > ) key1 ; @ SuppressWarnings ( "unch...
Are key1 and key2 grouped together at this groupLevel?
11,656
public static Object groupKey ( Object key , int groupLevel ) { if ( groupLevel > 0 && ( key instanceof List ) && ( ( ( List < Object > ) key ) . size ( ) > groupLevel ) ) { return ( ( List < Object > ) key ) . subList ( 0 , groupLevel ) ; } else { return key ; } }
Returns the prefix of the key to use in the result row at this groupLevel
11,657
public int getTotalRows ( ) { try { updateIndex ( ) ; } catch ( CouchbaseLiteException e ) { Log . e ( Log . TAG_VIEW , "Update index failed when getting the total rows" , e ) ; } return getCurrentTotalRows ( ) ; }
Get total number of rows in the view . The view s index will be updated if needed before returning the value .
11,658
public static double totalValues ( List < Object > values ) { double total = 0 ; for ( Object object : values ) { if ( object instanceof Number ) { Number number = ( Number ) object ; total += number . doubleValue ( ) ; } else { Log . w ( Log . TAG_VIEW , "Warning non-numeric value found in totalValues: %s" , object ) ...
Utility function to use in reduce blocks . Totals an array of Numbers .
11,659
protected Status updateIndexes ( List < View > views ) throws CouchbaseLiteException { List < ViewStore > storages = new ArrayList < ViewStore > ( ) ; for ( View view : views ) { storages . add ( view . viewStore ) ; } return viewStore . updateIndexes ( storages ) ; }
Update multiple view indexes at once .
11,660
public List < QueryRow > query ( QueryOptions options ) throws CouchbaseLiteException { if ( options == null ) options = new QueryOptions ( ) ; if ( groupOrReduce ( options ) ) return viewStore . reducedQuery ( options ) ; else return viewStore . regularQuery ( options ) ; }
Queries the view . Does NOT first update the index .
11,661
public static Printer create ( Printer printer , String prefix ) { if ( prefix == null || prefix . equals ( "" ) ) { return printer ; } return new PrefixPrinter ( printer , prefix ) ; }
Creates a new PrefixPrinter .
11,662
public void deleteCookie ( Cookie cookie ) { cookies . remove ( cookie . name ( ) ) ; deletePersistedCookie ( cookie . name ( ) ) ; }
Non - standard helper method to delete cookie
11,663
public static int getDefaultPageSize ( ) { synchronized ( sLock ) { if ( sDefaultPageSize == 0 ) { try { Class clazz = Class . forName ( "android.os.StatFs" ) ; Method m = clazz . getMethod ( "getBlockSize" ) ; Object statFsObj = clazz . getConstructor ( String . class ) . newInstance ( "/data" ) ; Integer value = ( In...
Gets the default page size to use when creating a database .
11,664
private static String byteArrayToHexString ( byte [ ] bytes ) { StringBuilder sb = new StringBuilder ( bytes . length * 2 ) ; for ( byte element : bytes ) { int v = element & 0xff ; if ( v < 16 ) { sb . append ( '0' ) ; } sb . append ( Integer . toHexString ( v ) ) ; } return sb . toString ( ) ; }
Using some super basic byte array &lt ; - &gt ; hex conversions so we don t have to rely on any large Base64 libraries . Can be overridden if you like!
11,665
public SavedRevision createRevision ( Map < String , Object > properties ) throws CouchbaseLiteException { boolean allowConflict = false ; return document . putProperties ( properties , revisionInternal . getRevID ( ) , allowConflict ) ; }
Creates and saves a new revision with the given properties . This will fail with a 412 error if the receiver is not the current revision of the document .
11,666
public Map < String , Object > getProperties ( ) { Map < String , Object > properties = revisionInternal . getProperties ( ) ; if ( ! checkedProperties ) { if ( properties == null ) { if ( loadProperties ( ) == true ) { properties = revisionInternal . getProperties ( ) ; } } checkedProperties = true ; } return properti...
The contents of this revision of the document . Any keys in the dictionary that begin with _ such as _id and _rev contain CouchbaseLite metadata .
11,667
public Object jsonObject ( ) { if ( json == null ) { return null ; } if ( cached == null ) { Object tmp = null ; if ( json [ 0 ] == '{' ) { tmp = new LazyJsonObject < String , Object > ( json ) ; } else if ( json [ 0 ] == '[' ) { tmp = new LazyJsonArray < Object > ( json ) ; } else { try { if ( json . length > 0 && jso...
values are requested
11,668
public void queueObjects ( List < T > objects ) { if ( objects == null || objects . size ( ) == 0 ) return ; boolean readyToProcess = false ; synchronized ( mutex ) { Log . v ( Log . TAG_BATCHER , "%s: queueObjects called with %d objects (current inbox size = %d)" , this , objects . size ( ) , inbox . size ( ) ) ; inbo...
Adds multiple objects to the queue .
11,669
public void flushAll ( boolean waitForAllToFinish ) { Log . v ( Log . TAG_BATCHER , "%s: flushing all objects (wait=%b)" , this , waitForAllToFinish ) ; synchronized ( mutex ) { isFlushing = true ; unschedule ( ) ; } while ( true ) { ScheduledFuture future = null ; synchronized ( mutex ) { if ( inbox . size ( ) == 0 ) ...
Sends _all_ the queued objects at once to the processor block . After this method returns all inbox objects will be processed .
11,670
private void scheduleBatchProcess ( boolean immediate ) { synchronized ( mutex ) { if ( inbox . size ( ) == 0 ) return ; long suggestedDelay = 0 ; if ( ! immediate && inbox . size ( ) < capacity ) { if ( System . currentTimeMillis ( ) - lastProcessedTime < delay ) suggestedDelay = delay ; else { suggestedDelay = Math ....
Schedule batch process based on capacity inbox size and last processed time .
11,671
private void scheduleWithDelay ( long delay ) { synchronized ( mutex ) { if ( scheduled && delay < scheduledDelay ) { if ( isPendingFutureReadyOrInProcessing ( ) ) { Log . v ( Log . TAG_BATCHER , "%s: scheduleWithDelay: %d ms, ignored as current batch " + "is ready or in process" , this , delay ) ; return ; } unschedul...
Schedule the batch processing with the delay . If there is one batch currently in processing the schedule will be ignored as after the processing is done the next batch will be rescheduled .
11,672
private void unschedule ( ) { synchronized ( mutex ) { if ( pendingFuture != null && ! pendingFuture . isDone ( ) && ! pendingFuture . isCancelled ( ) ) { Log . v ( Log . TAG_BATCHER , "%s: cancelling the pending future ..." , this ) ; pendingFuture . cancel ( false ) ; } scheduled = false ; } }
Unschedule the scheduled batch processing .
11,673
private boolean isPendingFutureReadyOrInProcessing ( ) { synchronized ( mutex ) { if ( pendingFuture != null && ! pendingFuture . isDone ( ) && ! pendingFuture . isCancelled ( ) ) { return pendingFuture . getDelay ( TimeUnit . MILLISECONDS ) <= 0 ; } return false ; } }
Check if the current pending future is ready to be processed or in processing .
11,674
private void processNow ( ) { List < T > toProcess ; boolean scheduleNextBatchImmediately = false ; synchronized ( mutex ) { int count = inbox . size ( ) ; Log . v ( Log . TAG_BATCHER , "%s: processNow() called, inbox size: %d" , this , count ) ; if ( count == 0 ) return ; else if ( count <= capacity ) { toProcess = ne...
This method is called by the work executor to do the batch process . The inbox items up to the batcher capacity will be taken out to process . The next batch will be rescheduled if there are still some items left in the inbox .
11,675
private String getRequestHeaderContentType ( ) { String contentType = getRequestHeaderValue ( "Content-Type" ) ; if ( contentType != null ) { int index = contentType . indexOf ( ';' ) ; if ( index > 0 ) contentType = contentType . substring ( 0 , index ) ; contentType = contentType . trim ( ) ; } return contentType ; }
get Content - Type from URLConnection
11,676
private void setResponseLocation ( URL url ) { String location = url . getPath ( ) ; String query = url . getQuery ( ) ; if ( query != null ) { int startOfQuery = location . indexOf ( query ) ; if ( startOfQuery > 0 ) { location = location . substring ( 0 , startOfQuery ) ; } } connection . getResHeader ( ) . add ( "Lo...
Router + Handlers
11,677
private static void convertCBLQueryRowsToMaps ( Map < String , Object > allDocsResult ) { List < Map < String , Object > > rowsAsMaps = new ArrayList < Map < String , Object > > ( ) ; List < QueryRow > rows = ( List < QueryRow > ) allDocsResult . get ( "rows" ) ; if ( rows != null ) { for ( QueryRow row : rows ) { rows...
This is a hack to deal with the fact that there is currently no custom serializer for QueryRow . Instead just convert everything to generic Maps .
11,678
public void changed ( Database . ChangeEvent event ) { synchronized ( changesLock ) { if ( isTimeout ) return ; lastChangesTimestamp = System . currentTimeMillis ( ) ; stopTimeout ( ) ; if ( ! filled ) { filled = true ; RevisionList changes = db . changesSince ( changesSince , changesOptions , changesFilter , changesFi...
Implementation of ChangeListener
11,679
public void cancel ( ) { final OnCancelListener listener ; synchronized ( this ) { if ( mIsCanceled ) { return ; } mIsCanceled = true ; mCancelInProgress = true ; listener = mOnCancelListener ; } try { if ( listener != null ) { listener . onCancel ( ) ; } } finally { synchronized ( this ) { mCancelInProgress = false ; ...
Cancels the operation and signals the cancellation listener . If the operation has not yet started then it will be canceled as soon as it does .
11,680
public void setOnCancelListener ( OnCancelListener listener ) { synchronized ( this ) { waitForCancelFinishedLocked ( ) ; if ( mOnCancelListener == listener ) { return ; } mOnCancelListener = listener ; if ( ! mIsCanceled || listener == null ) { return ; } } listener . onCancel ( ) ; }
Sets the cancellation listener to be called when canceled .
11,681
public void waitForRows ( ) throws CouchbaseLiteException { start ( ) ; while ( true ) { try { queryFuture . get ( ) ; break ; } catch ( InterruptedException e ) { continue ; } catch ( Exception e ) { lastError = e ; throw new CouchbaseLiteException ( e , Status . INTERNAL_SERVER_ERROR ) ; } } }
Blocks until the intial async query finishes . After this call either . rows or . error will be non - nil .
11,682
public QueryEnumerator getRows ( ) { start ( ) ; if ( rows == null ) { return null ; } else { return new QueryEnumerator ( rows ) ; } }
Gets the results of the Query . The value will be null until the initial Query completes .
11,683
public SavedRevision getCurrentRevision ( ) { if ( currentRevision == null ) currentRevision = getRevision ( null ) ; return currentRevision ; }
Get the current revision
11,684
public Map < String , Object > getProperties ( ) { return getCurrentRevision ( ) == null ? null : getCurrentRevision ( ) . getProperties ( ) ; }
The contents of the current revision of the document . This is shorthand for self . currentRevision . properties . Any keys in the dictionary that begin with _ such as _id and _rev contain CouchbaseLite metadata .
11,685
public boolean delete ( ) throws CouchbaseLiteException { return getCurrentRevision ( ) == null ? false : getCurrentRevision ( ) . deleteDocument ( ) != null ; }
Deletes this document by adding a deletion revision . This will be replicated to other databases .
11,686
public void purge ( ) throws CouchbaseLiteException { Map < String , List < String > > docsToRevs = new HashMap < String , List < String > > ( ) ; List < String > revs = new ArrayList < String > ( ) ; revs . add ( "*" ) ; docsToRevs . put ( documentId , revs ) ; database . purgeRevisions ( docsToRevs ) ; database . rem...
Purges this document from the database ; this is more than deletion it forgets entirely about it . The purge will NOT be replicated to other databases .
11,687
public SavedRevision getRevision ( String revID ) { if ( revID != null && currentRevision != null && revID . equals ( currentRevision . getId ( ) ) ) return currentRevision ; RevisionInternal revisionInternal = database . getDocument ( getId ( ) , revID , true ) ; return getRevisionFromRev ( revisionInternal ) ; }
The revision with the specified ID .
11,688
public long getLength ( ) { Number length = ( Number ) metadata . get ( "length" ) ; if ( length != null ) { return length . longValue ( ) ; } else { return 0 ; } }
Get the length in bytes of the contents .
11,689
protected static Map < String , Object > installAttachmentBodies ( Map < String , Object > attachments , Database database ) throws CouchbaseLiteException { Map < String , Object > updatedAttachments = new HashMap < String , Object > ( ) ; for ( String name : attachments . keySet ( ) ) { Object value = attachments . ge...
Goes through an _attachments dictionary and replaces any values that are Attachment objects with proper JSON metadata dicts . It registers the attachment bodies with the blob store and sets the metadata getDigest and follows properties accordingly .
11,690
private void initSupportExecutor ( ) { if ( supportExecutor == null || supportExecutor . isShutdown ( ) ) { supportExecutor = Executors . newSingleThreadExecutor ( new ThreadFactory ( ) { public Thread newThread ( Runnable r ) { String maskedRemote = URLUtils . sanitizeURL ( remote ) ; return new Thread ( r , "CBLPushe...
create single thread supportExecutor for push replication
11,691
public List < String > getAttachmentNames ( ) { Map < String , Object > attachmentMetadata = getAttachmentMetadata ( ) ; if ( attachmentMetadata == null ) { return new ArrayList < String > ( ) ; } return new ArrayList < String > ( attachmentMetadata . keySet ( ) ) ; }
The names of all attachments
11,692
public List < Attachment > getAttachments ( ) { Map < String , Object > attachmentMetadata = getAttachmentMetadata ( ) ; if ( attachmentMetadata == null ) { return new ArrayList < Attachment > ( ) ; } List < Attachment > result = new ArrayList < Attachment > ( attachmentMetadata . size ( ) ) ; for ( Map . Entry < Strin...
All attachments as Attachment objects .
11,693
public void setUserProperties ( Map < String , Object > userProperties ) { Map < String , Object > newProps = new HashMap < String , Object > ( ) ; newProps . putAll ( userProperties ) ; for ( String key : properties . keySet ( ) ) { if ( key . startsWith ( "_" ) ) { newProps . put ( key , properties . get ( key ) ) ; ...
Sets the userProperties of the Revision . Set replaces all properties except for those with keys prefixed with _ .
11,694
protected void addAttachment ( Attachment attachment , String name ) { Map < String , Object > attachments = ( Map < String , Object > ) properties . get ( "_attachments" ) ; if ( attachments == null ) { attachments = new HashMap < String , Object > ( ) ; } attachments . put ( name , attachment ) ; properties . put ( "...
Creates or updates an attachment . The attachment data will be written to the database when the revision is saved .
11,695
protected void beginReplicating ( ) { Log . v ( TAG , "submit startReplicating()" ) ; executor . submit ( new Runnable ( ) { public void run ( ) { if ( isRunning ( ) ) { Log . v ( TAG , "start startReplicating()" ) ; initPendingSequences ( ) ; initDownloadsToInsert ( ) ; startChangeTracker ( ) ; } } } ) ; }
Actual work of starting the replication process .
11,696
protected void processInbox ( RevisionList inbox ) { Log . d ( TAG , "processInbox called" ) ; if ( db == null || ! db . isOpen ( ) ) { Log . w ( Log . TAG_SYNC , "%s: Database is null or closed. Unable to continue. db name is %s." , this , db . getName ( ) ) ; return ; } if ( canBulkGet == null ) { canBulkGet = server...
Process a bunch of remote revisions from the _changes feed at once
11,697
protected void pullBulkRevisions ( List < RevisionInternal > bulkRevs ) { int nRevs = bulkRevs . size ( ) ; if ( nRevs == 0 ) { return ; } Log . d ( TAG , "%s bulk-fetching %d remote revisions..." , this , nRevs ) ; Log . d ( TAG , "%s bulk-fetching remote revisions: %s" , this , bulkRevs ) ; if ( ! canBulkGet ) { pull...
Get a bunch of revisions in one bulk request . Will use _bulk_get if possible .
11,698
private void queueDownloadedRevision ( RevisionInternal rev ) { if ( revisionBodyTransformationBlock != null ) { for ( Map . Entry < String , Map < String , Object > > entry : ( ( Map < String , Map < String , Object > > ) rev . getProperties ( ) . get ( "_attachments" ) ) . entrySet ( ) ) { String name = entry . getKe...
This invokes the tranformation block if one is installed and queues the resulting CBL_Revision
11,699
protected void pullBulkWithAllDocs ( final List < RevisionInternal > bulkRevs ) { ++ httpConnectionCount ; final RevisionList remainingRevs = new RevisionList ( bulkRevs ) ; Collection < String > keys = CollectionUtils . transform ( bulkRevs , new CollectionUtils . Functor < RevisionInternal , String > ( ) { public Str...
This is compatible with CouchDB but it only works for revs of generation 1 without attachments .