OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/sync_file_system/drive_backend/metadata_database.h" | 5 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <stack> | 8 #include <stack> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 1406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1417 // static | 1417 // static |
1418 void MetadataDatabase::CreateOnWorkerTaskRunner( | 1418 void MetadataDatabase::CreateOnWorkerTaskRunner( |
1419 scoped_ptr<CreateParam> create_param, | 1419 scoped_ptr<CreateParam> create_param, |
1420 const CreateCallback& callback) { | 1420 const CreateCallback& callback) { |
1421 DCHECK(create_param->worker_task_runner->RunsTasksOnCurrentThread()); | 1421 DCHECK(create_param->worker_task_runner->RunsTasksOnCurrentThread()); |
1422 | 1422 |
1423 scoped_ptr<MetadataDatabase> metadata_database( | 1423 scoped_ptr<MetadataDatabase> metadata_database( |
1424 new MetadataDatabase(create_param->worker_task_runner, | 1424 new MetadataDatabase(create_param->worker_task_runner, |
1425 create_param->database_path, | 1425 create_param->database_path, |
1426 create_param->env_override)); | 1426 create_param->env_override)); |
| 1427 |
1427 SyncStatusCode status = metadata_database->Initialize(); | 1428 SyncStatusCode status = metadata_database->Initialize(); |
| 1429 if (status == SYNC_DATABASE_ERROR_FAILED) { |
| 1430 // Delete the previous instance to avoid creating a LevelDB instance for |
| 1431 // the same path. |
| 1432 metadata_database.reset(); |
| 1433 |
| 1434 metadata_database.reset( |
| 1435 new MetadataDatabase(create_param->worker_task_runner, |
| 1436 create_param->database_path, |
| 1437 create_param->env_override)); |
| 1438 status = metadata_database->Initialize(); |
| 1439 } |
| 1440 |
1428 if (status != SYNC_STATUS_OK) | 1441 if (status != SYNC_STATUS_OK) |
1429 metadata_database.reset(); | 1442 metadata_database.reset(); |
1430 | 1443 |
1431 metadata_database->DetachFromSequence(); | 1444 if (metadata_database) |
| 1445 metadata_database->DetachFromSequence(); |
1432 create_param->worker_task_runner->PostTask( | 1446 create_param->worker_task_runner->PostTask( |
1433 FROM_HERE, | 1447 FROM_HERE, |
1434 base::Bind( | 1448 base::Bind( |
1435 callback, status, base::Passed(&metadata_database))); | 1449 callback, status, base::Passed(&metadata_database))); |
1436 } | 1450 } |
1437 | 1451 |
1438 SyncStatusCode MetadataDatabase::Initialize() { | 1452 SyncStatusCode MetadataDatabase::Initialize() { |
1439 base::ThreadRestrictions::AssertIOAllowed(); | 1453 base::ThreadRestrictions::AssertIOAllowed(); |
1440 DCHECK(worker_sequence_checker_.CalledOnValidSequencedThread()); | 1454 DCHECK(worker_sequence_checker_.CalledOnValidSequencedThread()); |
1441 | 1455 |
(...skipping 11 matching lines...) Expand all Loading... |
1453 if (status != SYNC_STATUS_OK) | 1467 if (status != SYNC_STATUS_OK) |
1454 return status; | 1468 return status; |
1455 } | 1469 } |
1456 | 1470 |
1457 if (CommandLine::ForCurrentProcess()->HasSwitch( | 1471 if (CommandLine::ForCurrentProcess()->HasSwitch( |
1458 kEnableMetadataDatabaseOnDisk)) { | 1472 kEnableMetadataDatabaseOnDisk)) { |
1459 index_ = MetadataDatabaseIndexOnDisk::Create(db_.get()); | 1473 index_ = MetadataDatabaseIndexOnDisk::Create(db_.get()); |
1460 } else { | 1474 } else { |
1461 index_ = MetadataDatabaseIndex::Create(db_.get()); | 1475 index_ = MetadataDatabaseIndex::Create(db_.get()); |
1462 } | 1476 } |
| 1477 if (!index_) { |
| 1478 // Delete all entries in |db_| to reset it. |
| 1479 // TODO(peria): Make LevelDBWrapper::DestroyDB() to avoid a full scan. |
| 1480 scoped_ptr<LevelDBWrapper::Iterator> itr = db_->NewIterator(); |
| 1481 for (itr->SeekToFirst(); itr->Valid();) |
| 1482 itr->Delete(); |
| 1483 db_->Commit(); |
| 1484 |
| 1485 return SYNC_DATABASE_ERROR_FAILED; |
| 1486 } |
1463 | 1487 |
1464 status = LevelDBStatusToSyncStatusCode(db_->Commit()); | 1488 status = LevelDBStatusToSyncStatusCode(db_->Commit()); |
1465 if (status != SYNC_STATUS_OK) | 1489 if (status != SYNC_STATUS_OK) |
1466 return status; | 1490 return status; |
1467 | 1491 |
1468 UpdateLargestKnownChangeID(index_->GetLargestChangeID()); | 1492 UpdateLargestKnownChangeID(index_->GetLargestChangeID()); |
1469 | 1493 |
1470 return status; | 1494 return status; |
1471 } | 1495 } |
1472 | 1496 |
(...skipping 481 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1954 return false; | 1978 return false; |
1955 | 1979 |
1956 if (!parents.empty()) | 1980 if (!parents.empty()) |
1957 return false; | 1981 return false; |
1958 | 1982 |
1959 return true; | 1983 return true; |
1960 } | 1984 } |
1961 | 1985 |
1962 } // namespace drive_backend | 1986 } // namespace drive_backend |
1963 } // namespace sync_file_system | 1987 } // namespace sync_file_system |
OLD | NEW |