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

Side by Side Diff: sql/connection_unittest.cc

Issue 1533703002: [sql] Consider fresh databases suitable for memory-mapped I/O. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years 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
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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/files/file_util.h" 6 #include "base/files/file_util.h"
7 #include "base/files/memory_mapped_file.h" 7 #include "base/files/memory_mapped_file.h"
8 #include "base/files/scoped_file.h" 8 #include "base/files/scoped_file.h"
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 1443 matching lines...) Expand 10 before | Expand all | Expand 10 after
1454 EXPECT_FALSE(db().RegisterIntentToUpload()); 1454 EXPECT_FALSE(db().RegisterIntentToUpload());
1455 1455
1456 // Old tag is still prevented. 1456 // Old tag is still prevented.
1457 db().Close(); 1457 db().Close();
1458 db().set_histogram_tag("Test"); 1458 db().set_histogram_tag("Test");
1459 ASSERT_TRUE(db().Open(db_path())); 1459 ASSERT_TRUE(db().Open(db_path()));
1460 EXPECT_FALSE(db().RegisterIntentToUpload()); 1460 EXPECT_FALSE(db().RegisterIntentToUpload());
1461 } 1461 }
1462 #endif // !defined(MOJO_APPTEST_IMPL) 1462 #endif // !defined(MOJO_APPTEST_IMPL)
1463 1463
1464 // Test that a fresh database has mmap enabled by default, if mmap'ed I/O is
1465 // enabled by SQLite.
1466 TEST_F(SQLConnectionTest, MmapInitiallyEnabled) {
1467 {
1468 sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size"));
1469
1470 // SQLite doesn't have mmap support (perhaps an early iOS release).
1471 if (!s.Step())
1472 return;
1473
1474 // If mmap I/O is not on, attempt to turn it on. If that succeeds, then
1475 // Open() should have turned it on.
1476 if (!s.ColumnInt(0)) {
1477 ASSERT_TRUE(db().Execute("PRAGMA mmap_size = 1048576"));
1478 s.Reset(true);
1479 ASSERT_TRUE(s.Step());
1480 EXPECT_EQ(0, s.ColumnInt(0));
1481 }
1482 }
1483
1484 // Test that explicit disable prevents mmap'ed I/O.
1485 db().Close();
1486 sql::Connection::Delete(db_path());
1487 db().set_mmap_disabled();
1488 ASSERT_TRUE(db().Open(db_path()));
1489 {
1490 sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size"));
1491 ASSERT_TRUE(s.Step());
1492 EXPECT_EQ(0, s.ColumnInt(0));
1493 }
1494 }
1495
1496 // Test specific operation of the GetAppropriateMmapSize() helper.
1497 TEST_F(SQLConnectionTest, GetAppropriateMmapSize) {
1498 const size_t kMmapAlot = 25 * 1024 * 1024;
1499
1500 // If there is no meta table (as for a fresh database), assume that everything
1501 // should be mapped.
1502 ASSERT_TRUE(!db().DoesTableExist("meta"));
1503 ASSERT_GT(db().GetAppropriateMmapSize(), kMmapAlot);
1504
1505 // Getting the status fails if there is an error. GetAppropriateMmapSize()
1506 // should not call GetMmapStatus() if the table does not exist, but this is an
1507 // easy error to setup for testing.
1508 int64_t mmap_status;
1509 {
1510 sql::ScopedErrorIgnorer ignore_errors;
1511 ignore_errors.IgnoreError(SQLITE_ERROR);
1512 ASSERT_FALSE(MetaTable::GetMmapStatus(&db(), &mmap_status));
1513 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
1514 }
1515
1516 // When the meta table is first created, it sets up to map everything.
1517 MetaTable().Init(&db(), 1, 1);
1518 ASSERT_TRUE(db().DoesTableExist("meta"));
1519 ASSERT_GT(db().GetAppropriateMmapSize(), kMmapAlot);
1520 ASSERT_TRUE(MetaTable::GetMmapStatus(&db(), &mmap_status));
1521 ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status);
1522
1523 // Failure status maps nothing.
1524 ASSERT_TRUE(db().Execute("REPLACE INTO meta VALUES ('mmap_status', -2)"));
1525 ASSERT_EQ(0UL, db().GetAppropriateMmapSize());
1526
1527 // Re-initializing the meta table does not re-create the key if the table
1528 // already exists.
1529 ASSERT_TRUE(db().Execute("DELETE FROM meta WHERE key = 'mmap_status'"));
1530 MetaTable().Init(&db(), 1, 1);
1531 ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status);
1532 ASSERT_TRUE(MetaTable::GetMmapStatus(&db(), &mmap_status));
1533 ASSERT_EQ(0, mmap_status);
1534
1535 // With no key, map everything and create the key.
1536 // TODO(shess): This really should be "maps everything after validating it",
1537 // but that is more complicated to structure.
1538 ASSERT_GT(db().GetAppropriateMmapSize(), kMmapAlot);
1539 ASSERT_TRUE(MetaTable::GetMmapStatus(&db(), &mmap_status));
1540 ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status);
1541 }
1542
1464 } // namespace sql 1543 } // namespace sql
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698