SQLiteCursor.java revision 1a2344b8b2c374cb32ff4af020666e6910ff2a8f
1/* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.database.sqlite; 18 19import android.database.AbstractWindowedCursor; 20import android.database.CursorWindow; 21import android.database.DataSetObserver; 22import android.database.SQLException; 23 24import android.os.Handler; 25import android.os.Message; 26import android.os.Process; 27import android.os.StrictMode; 28import android.text.TextUtils; 29import android.util.Config; 30import android.util.Log; 31 32import java.util.HashMap; 33import java.util.Iterator; 34import java.util.Map; 35import java.util.concurrent.locks.ReentrantLock; 36 37/** 38 * A Cursor implementation that exposes results from a query on a 39 * {@link SQLiteDatabase}. 40 * 41 * SQLiteCursor is not internally synchronized so code using a SQLiteCursor from multiple 42 * threads should perform its own synchronization when using the SQLiteCursor. 43 */ 44public class SQLiteCursor extends AbstractWindowedCursor { 45 static final String TAG = "Cursor"; 46 static final int NO_COUNT = -1; 47 48 /** The name of the table to edit */ 49 private String mEditTable; 50 51 /** The names of the columns in the rows */ 52 private String[] mColumns; 53 54 /** The query object for the cursor */ 55 private SQLiteQuery mQuery; 56 57 /** The database the cursor was created from */ 58 private SQLiteDatabase mDatabase; 59 60 /** The compiled query this cursor came from */ 61 private SQLiteCursorDriver mDriver; 62 63 /** The number of rows in the cursor */ 64 private int mCount = NO_COUNT; 65 66 /** A mapping of column names to column indices, to speed up lookups */ 67 private Map<String, Integer> mColumnNameMap; 68 69 /** Used to find out where a cursor was allocated in case it never got released. */ 70 private Throwable mStackTrace; 71 72 /** 73 * mMaxRead is the max items that each cursor window reads 74 * default to a very high value 75 */ 76 private int mMaxRead = Integer.MAX_VALUE; 77 private int mInitialRead = Integer.MAX_VALUE; 78 private int mCursorState = 0; 79 private ReentrantLock mLock = null; 80 private boolean mPendingData = false; 81 82 /** 83 * support for a cursor variant that doesn't always read all results 84 * initialRead is the initial number of items that cursor window reads 85 * if query contains more than this number of items, a thread will be 86 * created and handle the left over items so that caller can show 87 * results as soon as possible 88 * @param initialRead initial number of items that cursor read 89 * @param maxRead leftover items read at maxRead items per time 90 * @hide 91 */ 92 public void setLoadStyle(int initialRead, int maxRead) { 93 mMaxRead = maxRead; 94 mInitialRead = initialRead; 95 mLock = new ReentrantLock(true); 96 } 97 98 private void queryThreadLock() { 99 if (mLock != null) { 100 mLock.lock(); 101 } 102 } 103 104 private void queryThreadUnlock() { 105 if (mLock != null) { 106 mLock.unlock(); 107 } 108 } 109 110 111 /** 112 * @hide 113 */ 114 final private class QueryThread implements Runnable { 115 private final int mThreadState; 116 QueryThread(int version) { 117 mThreadState = version; 118 } 119 private void sendMessage() { 120 if (mNotificationHandler != null) { 121 mNotificationHandler.sendEmptyMessage(1); 122 mPendingData = false; 123 } else { 124 mPendingData = true; 125 } 126 127 } 128 public void run() { 129 // use cached mWindow, to avoid get null mWindow 130 CursorWindow cw = mWindow; 131 Process.setThreadPriority(Process.myTid(), Process.THREAD_PRIORITY_BACKGROUND); 132 // the cursor's state doesn't change 133 while (true) { 134 mLock.lock(); 135 try { 136 if (mCursorState != mThreadState) { 137 break; 138 } 139 140 int count = mQuery.fillWindow(cw, mMaxRead, mCount); 141 // return -1 means not finished 142 if (count != 0) { 143 if (count == NO_COUNT){ 144 mCount += mMaxRead; 145 sendMessage(); 146 } else { 147 mCount = count; 148 sendMessage(); 149 break; 150 } 151 } else { 152 break; 153 } 154 } catch (Exception e) { 155 // end the tread when the cursor is close 156 break; 157 } finally { 158 mLock.unlock(); 159 } 160 } 161 } 162 } 163 164 /** 165 * @hide 166 */ 167 protected class MainThreadNotificationHandler extends Handler { 168 public void handleMessage(Message msg) { 169 notifyDataSetChange(); 170 } 171 } 172 173 /** 174 * @hide 175 */ 176 protected MainThreadNotificationHandler mNotificationHandler; 177 178 public void registerDataSetObserver(DataSetObserver observer) { 179 super.registerDataSetObserver(observer); 180 if ((Integer.MAX_VALUE != mMaxRead || Integer.MAX_VALUE != mInitialRead) && 181 mNotificationHandler == null) { 182 queryThreadLock(); 183 try { 184 mNotificationHandler = new MainThreadNotificationHandler(); 185 if (mPendingData) { 186 notifyDataSetChange(); 187 mPendingData = false; 188 } 189 } finally { 190 queryThreadUnlock(); 191 } 192 } 193 194 } 195 196 /** 197 * Execute a query and provide access to its result set through a Cursor 198 * interface. For a query such as: {@code SELECT name, birth, phone FROM 199 * myTable WHERE ... LIMIT 1,20 ORDER BY...} the column names (name, birth, 200 * phone) would be in the projection argument and everything from 201 * {@code FROM} onward would be in the params argument. This constructor 202 * has package scope. 203 * 204 * @param db a reference to a Database object that is already constructed 205 * and opened 206 * @param editTable the name of the table used for this query 207 * @param query the rest of the query terms 208 * cursor is finalized 209 */ 210 public SQLiteCursor(SQLiteDatabase db, SQLiteCursorDriver driver, 211 String editTable, SQLiteQuery query) { 212 // The AbstractCursor constructor needs to do some setup. 213 super(); 214 mStackTrace = new DatabaseObjectNotClosedException().fillInStackTrace(); 215 mDatabase = db; 216 mDriver = driver; 217 mEditTable = editTable; 218 mColumnNameMap = null; 219 mQuery = query; 220 221 db.lock(); 222 try { 223 // Setup the list of columns 224 int columnCount = mQuery.columnCountLocked(); 225 mColumns = new String[columnCount]; 226 227 // Read in all column names 228 for (int i = 0; i < columnCount; i++) { 229 String columnName = mQuery.columnNameLocked(i); 230 mColumns[i] = columnName; 231 if (Config.LOGV) { 232 Log.v("DatabaseWindow", "mColumns[" + i + "] is " 233 + mColumns[i]); 234 } 235 236 // Make note of the row ID column index for quick access to it 237 if ("_id".equals(columnName)) { 238 mRowIdColumnIndex = i; 239 } 240 } 241 } finally { 242 db.unlock(); 243 } 244 } 245 246 /** 247 * @return the SQLiteDatabase that this cursor is associated with. 248 */ 249 public SQLiteDatabase getDatabase() { 250 return mDatabase; 251 } 252 253 @Override 254 public boolean onMove(int oldPosition, int newPosition) { 255 // Make sure the row at newPosition is present in the window 256 if (mWindow == null || newPosition < mWindow.getStartPosition() || 257 newPosition >= (mWindow.getStartPosition() + mWindow.getNumRows())) { 258 fillWindow(newPosition); 259 } 260 261 return true; 262 } 263 264 @Override 265 public int getCount() { 266 if (mCount == NO_COUNT) { 267 fillWindow(0); 268 } 269 return mCount; 270 } 271 272 private void fillWindow (int startPos) { 273 if (mWindow == null) { 274 // If there isn't a window set already it will only be accessed locally 275 mWindow = new CursorWindow(true /* the window is local only */); 276 } else { 277 mCursorState++; 278 queryThreadLock(); 279 try { 280 mWindow.clear(); 281 } finally { 282 queryThreadUnlock(); 283 } 284 } 285 mWindow.setStartPosition(startPos); 286 mCount = mQuery.fillWindow(mWindow, mInitialRead, 0); 287 // return -1 means not finished 288 if (mCount == NO_COUNT){ 289 mCount = startPos + mInitialRead; 290 Thread t = new Thread(new QueryThread(mCursorState), "query thread"); 291 t.start(); 292 } 293 } 294 295 @Override 296 public int getColumnIndex(String columnName) { 297 // Create mColumnNameMap on demand 298 if (mColumnNameMap == null) { 299 String[] columns = mColumns; 300 int columnCount = columns.length; 301 HashMap<String, Integer> map = new HashMap<String, Integer>(columnCount, 1); 302 for (int i = 0; i < columnCount; i++) { 303 map.put(columns[i], i); 304 } 305 mColumnNameMap = map; 306 } 307 308 // Hack according to bug 903852 309 final int periodIndex = columnName.lastIndexOf('.'); 310 if (periodIndex != -1) { 311 Exception e = new Exception(); 312 Log.e(TAG, "requesting column name with table name -- " + columnName, e); 313 columnName = columnName.substring(periodIndex + 1); 314 } 315 316 Integer i = mColumnNameMap.get(columnName); 317 if (i != null) { 318 return i.intValue(); 319 } else { 320 return -1; 321 } 322 } 323 324 /** 325 * @hide 326 * @deprecated 327 */ 328 @Override 329 public boolean deleteRow() { 330 checkPosition(); 331 332 // Only allow deletes if there is an ID column, and the ID has been read from it 333 if (mRowIdColumnIndex == -1 || mCurrentRowID == null) { 334 Log.e(TAG, 335 "Could not delete row because either the row ID column is not available or it" + 336 "has not been read."); 337 return false; 338 } 339 340 boolean success; 341 342 /* 343 * Ensure we don't change the state of the database when another 344 * thread is holding the database lock. requery() and moveTo() are also 345 * synchronized here to make sure they get the state of the database 346 * immediately following the DELETE. 347 */ 348 mDatabase.lock(); 349 try { 350 try { 351 mDatabase.delete(mEditTable, mColumns[mRowIdColumnIndex] + "=?", 352 new String[] {mCurrentRowID.toString()}); 353 success = true; 354 } catch (SQLException e) { 355 success = false; 356 } 357 358 int pos = mPos; 359 requery(); 360 361 /* 362 * Ensure proper cursor state. Note that mCurrentRowID changes 363 * in this call. 364 */ 365 moveToPosition(pos); 366 } finally { 367 mDatabase.unlock(); 368 } 369 370 if (success) { 371 onChange(true); 372 return true; 373 } else { 374 return false; 375 } 376 } 377 378 @Override 379 public String[] getColumnNames() { 380 return mColumns; 381 } 382 383 /** 384 * @hide 385 * @deprecated 386 */ 387 @Override 388 public boolean supportsUpdates() { 389 return super.supportsUpdates() && !TextUtils.isEmpty(mEditTable); 390 } 391 392 /** 393 * @hide 394 * @deprecated 395 */ 396 @Override 397 public boolean commitUpdates(Map<? extends Long, 398 ? extends Map<String, Object>> additionalValues) { 399 if (!supportsUpdates()) { 400 Log.e(TAG, "commitUpdates not supported on this cursor, did you " 401 + "include the _id column?"); 402 return false; 403 } 404 405 /* 406 * Prevent other threads from changing the updated rows while they're 407 * being processed here. 408 */ 409 synchronized (mUpdatedRows) { 410 if (additionalValues != null) { 411 mUpdatedRows.putAll(additionalValues); 412 } 413 414 if (mUpdatedRows.size() == 0) { 415 return true; 416 } 417 418 /* 419 * Prevent other threads from changing the database state while 420 * we process the updated rows, and prevents us from changing the 421 * database behind the back of another thread. 422 */ 423 mDatabase.beginTransaction(); 424 try { 425 StringBuilder sql = new StringBuilder(128); 426 427 // For each row that has been updated 428 for (Map.Entry<Long, Map<String, Object>> rowEntry : 429 mUpdatedRows.entrySet()) { 430 Map<String, Object> values = rowEntry.getValue(); 431 Long rowIdObj = rowEntry.getKey(); 432 433 if (rowIdObj == null || values == null) { 434 throw new IllegalStateException("null rowId or values found! rowId = " 435 + rowIdObj + ", values = " + values); 436 } 437 438 if (values.size() == 0) { 439 continue; 440 } 441 442 long rowId = rowIdObj.longValue(); 443 444 Iterator<Map.Entry<String, Object>> valuesIter = 445 values.entrySet().iterator(); 446 447 sql.setLength(0); 448 sql.append("UPDATE " + mEditTable + " SET "); 449 450 // For each column value that has been updated 451 Object[] bindings = new Object[values.size()]; 452 int i = 0; 453 while (valuesIter.hasNext()) { 454 Map.Entry<String, Object> entry = valuesIter.next(); 455 sql.append(entry.getKey()); 456 sql.append("=?"); 457 bindings[i] = entry.getValue(); 458 if (valuesIter.hasNext()) { 459 sql.append(", "); 460 } 461 i++; 462 } 463 464 sql.append(" WHERE " + mColumns[mRowIdColumnIndex] 465 + '=' + rowId); 466 sql.append(';'); 467 mDatabase.execSQL(sql.toString(), bindings); 468 mDatabase.rowUpdated(mEditTable, rowId); 469 } 470 mDatabase.setTransactionSuccessful(); 471 } finally { 472 mDatabase.endTransaction(); 473 } 474 475 mUpdatedRows.clear(); 476 } 477 478 // Let any change observers know about the update 479 onChange(true); 480 481 return true; 482 } 483 484 private void deactivateCommon() { 485 if (Config.LOGV) Log.v(TAG, "<<< Releasing cursor " + this); 486 mCursorState = 0; 487 if (mWindow != null) { 488 mWindow.close(); 489 mWindow = null; 490 } 491 if (Config.LOGV) Log.v("DatabaseWindow", "closing window in release()"); 492 } 493 494 @Override 495 public void deactivate() { 496 super.deactivate(); 497 deactivateCommon(); 498 mDriver.cursorDeactivated(); 499 } 500 501 @Override 502 public void close() { 503 super.close(); 504 deactivateCommon(); 505 mQuery.close(); 506 mDriver.cursorClosed(); 507 } 508 509 @Override 510 public boolean requery() { 511 if (isClosed()) { 512 return false; 513 } 514 long timeStart = 0; 515 if (Config.LOGV) { 516 timeStart = System.currentTimeMillis(); 517 } 518 /* 519 * Synchronize on the database lock to ensure that mCount matches the 520 * results of mQuery.requery(). 521 */ 522 mDatabase.lock(); 523 try { 524 if (mWindow != null) { 525 mWindow.clear(); 526 } 527 mPos = -1; 528 // This one will recreate the temp table, and get its count 529 mDriver.cursorRequeried(this); 530 mCount = NO_COUNT; 531 mCursorState++; 532 queryThreadLock(); 533 try { 534 mQuery.requery(); 535 } finally { 536 queryThreadUnlock(); 537 } 538 } finally { 539 mDatabase.unlock(); 540 } 541 542 if (Config.LOGV) { 543 Log.v("DatabaseWindow", "closing window in requery()"); 544 Log.v(TAG, "--- Requery()ed cursor " + this + ": " + mQuery); 545 } 546 547 boolean result = super.requery(); 548 if (Config.LOGV) { 549 long timeEnd = System.currentTimeMillis(); 550 Log.v(TAG, "requery (" + (timeEnd - timeStart) + " ms): " + mDriver.toString()); 551 } 552 return result; 553 } 554 555 @Override 556 public void setWindow(CursorWindow window) { 557 if (mWindow != null) { 558 mCursorState++; 559 queryThreadLock(); 560 try { 561 mWindow.close(); 562 } finally { 563 queryThreadUnlock(); 564 } 565 mCount = NO_COUNT; 566 } 567 mWindow = window; 568 } 569 570 /** 571 * Changes the selection arguments. The new values take effect after a call to requery(). 572 */ 573 public void setSelectionArguments(String[] selectionArgs) { 574 mDriver.setBindArguments(selectionArgs); 575 } 576 577 /** 578 * Release the native resources, if they haven't been released yet. 579 */ 580 @Override 581 protected void finalize() { 582 try { 583 // if the cursor hasn't been closed yet, close it first 584 if (mWindow != null) { 585 if (StrictMode.vmSqliteObjectLeaksEnabled()) { 586 int len = mQuery.mSql.length(); 587 StrictMode.onSqliteObjectLeaked( 588 "Finalizing a Cursor that has not been deactivated or closed. " + 589 "database = " + mDatabase.getPath() + ", table = " + mEditTable + 590 ", query = " + mQuery.mSql.substring(0, (len > 100) ? 100 : len), 591 mStackTrace); 592 } 593 close(); 594 SQLiteDebug.notifyActiveCursorFinalized(); 595 } else { 596 if (Config.LOGV) { 597 Log.v(TAG, "Finalizing cursor on database = " + mDatabase.getPath() + 598 ", table = " + mEditTable + ", query = " + mQuery.mSql); 599 } 600 } 601 } finally { 602 super.finalize(); 603 } 604 } 605} 606