Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: sql/sqlite_features_unittest.cc

Issue 2732553002: [sql] SQLite patch to implement "smart" auto-vacuum. (Closed)
Patch Set: sigh, keep clang happy on windows Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | third_party/sqlite/amalgamation/sqlite3.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <string> 8 #include <string>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 EXPECT_TRUE(CSBackupIsItemExcluded(dbURL, &excluded_by_path)); 354 EXPECT_TRUE(CSBackupIsItemExcluded(dbURL, &excluded_by_path));
355 EXPECT_FALSE(excluded_by_path); 355 EXPECT_FALSE(excluded_by_path);
356 EXPECT_TRUE(CSBackupIsItemExcluded(journalURL, &excluded_by_path)); 356 EXPECT_TRUE(CSBackupIsItemExcluded(journalURL, &excluded_by_path));
357 EXPECT_FALSE(excluded_by_path); 357 EXPECT_FALSE(excluded_by_path);
358 358
359 // TODO(shess): In WAL mode this will touch -wal and -shm files. -shm files 359 // TODO(shess): In WAL mode this will touch -wal and -shm files. -shm files
360 // could be always excluded. 360 // could be always excluded.
361 } 361 }
362 #endif 362 #endif
363 363
364 #if !defined(USE_SYSTEM_SQLITE)
365 // Test that Chromium's patch to make auto_vacuum integrate with
366 // SQLITE_FCNTL_CHUNK_SIZE is working.
367 TEST_F(SQLiteFeaturesTest, SmartAutoVacuum) {
368 // Turn on auto_vacuum, and set the page size low to make results obvious.
369 // These settings require re-writing the database, which VACUUM does.
370 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum = FULL"));
371 ASSERT_TRUE(db().Execute("PRAGMA page_size = 1024"));
372 ASSERT_TRUE(db().Execute("VACUUM"));
373
374 // Code-coverage of the PRAGMA set/get implementation.
375 const char kPragmaSql[] = "PRAGMA auto_vacuum_slack_pages";
376 ASSERT_EQ("0", sql::test::ExecuteWithResult(&db(), kPragmaSql));
377 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum_slack_pages = 4"));
378 ASSERT_EQ("4", sql::test::ExecuteWithResult(&db(), kPragmaSql));
379 // Max out at 255.
380 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum_slack_pages = 1000"));
381 ASSERT_EQ("255", sql::test::ExecuteWithResult(&db(), kPragmaSql));
382 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum_slack_pages = 0"));
383
384 // With page_size=1024, the following will insert rows which take up an
385 // overflow page, plus a small header in a b-tree node. An empty table takes
386 // a single page, so for small row counts each insert will add one page, and
387 // each delete will remove one page.
388 const char kCreateSql[] = "CREATE TABLE t (id INTEGER PRIMARY KEY, value)";
389 const char kInsertSql[] = "INSERT INTO t (value) VALUES (randomblob(980))";
390 #if !defined(OS_WIN)
391 const char kDeleteSql[] = "DELETE FROM t WHERE id = (SELECT MIN(id) FROM t)";
392 #endif
393
394 // This database will be 34 overflow pages plus the table's root page plus the
395 // SQLite header page plus the freelist page.
396 ASSERT_TRUE(db().Execute(kCreateSql));
397 {
398 sql::Statement s(db().GetUniqueStatement(kInsertSql));
399 for (int i = 0; i < 34; ++i) {
400 s.Reset(true);
401 ASSERT_TRUE(s.Run());
402 }
403 }
404 ASSERT_EQ("37", sql::test::ExecuteWithResult(&db(), "PRAGMA page_count"));
405
406 // http://sqlite.org/mmap.html indicates that Windows will silently fail when
407 // truncating a memory-mapped file. That pretty much invalidates these tests
408 // against the actual file size.
409 #if !defined(OS_WIN)
410 // Each delete will delete a single page, including crossing a
411 // multiple-of-four boundary.
412 {
413 sql::Statement s(db().GetUniqueStatement(kDeleteSql));
414 for (int i = 0; i < 5; ++i) {
415 int64_t file_size_before, file_size_after;
416 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_before));
417
418 s.Reset(true);
419 ASSERT_TRUE(s.Run());
420
421 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_after));
422 ASSERT_EQ(file_size_after, file_size_before - 1024);
423 }
424 }
425
426 // Turn on "smart" auto-vacuum to remove 4 pages at a time.
427 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum_slack_pages = 4"));
428
429 // No pages removed, then four deleted at once.
430 {
431 sql::Statement s(db().GetUniqueStatement(kDeleteSql));
432 for (int i = 0; i < 3; ++i) {
433 int64_t file_size_before, file_size_after;
434 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_before));
435
436 s.Reset(true);
437 ASSERT_TRUE(s.Run());
438
439 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_after));
440 ASSERT_EQ(file_size_after, file_size_before);
441 }
442
443 int64_t file_size_before, file_size_after;
444 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_before));
445
446 s.Reset(true);
447 ASSERT_TRUE(s.Run());
448
449 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_after));
450 ASSERT_EQ(file_size_after, file_size_before - 4096);
451 }
452 #endif
453 }
454 #endif // !defined(USE_SYSTEM_SQLITE)
455
364 } // namespace 456 } // namespace
OLDNEW
« no previous file with comments | « no previous file | third_party/sqlite/amalgamation/sqlite3.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698