OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/history_report/usage_reports_buffer_backend.h" |
| 6 |
| 7 #include <inttypes.h> |
| 8 |
| 9 #include "base/files/file_util.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "chrome/browser/android/history_report/usage_report_util.h" |
| 13 #include "chrome/browser/android/proto/delta_file.pb.h" |
| 14 #include "third_party/leveldatabase/src/include/leveldb/comparator.h" |
| 15 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 16 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" |
| 17 #include "third_party/leveldatabase/src/include/leveldb/options.h" |
| 18 #include "third_party/leveldatabase/src/include/leveldb/slice.h" |
| 19 #include "third_party/leveldatabase/src/include/leveldb/status.h" |
| 20 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
| 21 |
| 22 namespace { |
| 23 const base::FilePath::CharType kBufferFileName[] = |
| 24 FILE_PATH_LITERAL("UsageReportsBuffer"); |
| 25 } // namespace |
| 26 |
| 27 namespace history_report { |
| 28 |
| 29 UsageReportsBufferBackend::UsageReportsBufferBackend(const base::FilePath& dir) |
| 30 : db_file_name_(dir.Append(kBufferFileName)) {} |
| 31 |
| 32 UsageReportsBufferBackend::~UsageReportsBufferBackend() {} |
| 33 |
| 34 bool UsageReportsBufferBackend::Init() { |
| 35 leveldb::Options options; |
| 36 options.create_if_missing = true; |
| 37 options.max_open_files = 0; // Use minimum number of files. |
| 38 std::string path = db_file_name_.value(); |
| 39 leveldb::DB* db = NULL; |
| 40 leveldb::Status status = leveldb::DB::Open(options, path, &db); |
| 41 if (status.IsCorruption()) { |
| 42 LOG(WARNING) << "Deleting possibly-corrupt database"; |
| 43 base::DeleteFile(db_file_name_, true); |
| 44 status = leveldb::DB::Open(options, path, &db); |
| 45 } |
| 46 if (status.ok()) { |
| 47 CHECK(db); |
| 48 db_.reset(db); |
| 49 return true; |
| 50 } |
| 51 LOG(WARNING) << "Unable to open " << path << ": " |
| 52 << status.ToString(); |
| 53 return false; |
| 54 } |
| 55 |
| 56 void UsageReportsBufferBackend::AddVisit(const std::string& id, |
| 57 int64 timestamp_ms, |
| 58 bool typed_visit) { |
| 59 if (!db_.get()) { |
| 60 LOG(WARNING) << "AddVisit db not initilized."; |
| 61 return; |
| 62 } |
| 63 history_report::UsageReport report; |
| 64 report.set_id(id); |
| 65 report.set_timestamp_ms(timestamp_ms); |
| 66 report.set_typed_visit(typed_visit); |
| 67 leveldb::WriteOptions writeOptions; |
| 68 leveldb::Status status = db_->Put( |
| 69 writeOptions, |
| 70 leveldb::Slice(usage_report_util::ReportToKey(report)), |
| 71 leveldb::Slice(report.SerializeAsString())); |
| 72 if (!status.ok()) |
| 73 LOG(WARNING) << "AddVisit failed " << status.ToString(); |
| 74 } |
| 75 |
| 76 scoped_ptr<std::vector<UsageReport> > |
| 77 UsageReportsBufferBackend::GetUsageReportsBatch(int batch_size) { |
| 78 scoped_ptr<std::vector<UsageReport> > reports(new std::vector<UsageReport>()); |
| 79 if (!db_.get()) { |
| 80 return reports.Pass(); |
| 81 } |
| 82 reports->reserve(batch_size); |
| 83 leveldb::ReadOptions options; |
| 84 scoped_ptr<leveldb::Iterator> db_iter(db_->NewIterator(options)); |
| 85 db_iter->SeekToFirst(); |
| 86 while (batch_size > 0 && db_iter->Valid()) { |
| 87 history_report::UsageReport last_report; |
| 88 leveldb::Slice value_slice = db_iter->value(); |
| 89 if (last_report.ParseFromArray(value_slice.data(), value_slice.size())) { |
| 90 reports->push_back(last_report); |
| 91 --batch_size; |
| 92 } |
| 93 db_iter->Next(); |
| 94 } |
| 95 return reports.Pass(); |
| 96 } |
| 97 |
| 98 void UsageReportsBufferBackend::Remove( |
| 99 const std::vector<std::string>& reports) { |
| 100 if (!db_.get()) { |
| 101 return; |
| 102 } |
| 103 // TODO(haaawk): investigate if it's worth sorting the keys here to improve |
| 104 // performance. |
| 105 leveldb::WriteBatch updates; |
| 106 for (std::vector<std::string>::const_iterator it = reports.begin(); |
| 107 it != reports.end(); |
| 108 ++it) { |
| 109 updates.Delete(leveldb::Slice(*it)); |
| 110 } |
| 111 |
| 112 leveldb::WriteOptions write_options; |
| 113 leveldb::Status status = db_->Write(write_options, &updates); |
| 114 if (!status.ok()) { |
| 115 LOG(WARNING) << "Remove failed: " << status.ToString(); |
| 116 } |
| 117 } |
| 118 |
| 119 void UsageReportsBufferBackend::Clear() { |
| 120 db_.reset(); |
| 121 base::DeleteFile(db_file_name_, true); |
| 122 Init(); |
| 123 } |
| 124 |
| 125 std::string UsageReportsBufferBackend::Dump() { |
| 126 std::string dump("\n UsageReportsBuffer ["); |
| 127 if (!db_.get()) { |
| 128 dump.append("not initialized]"); |
| 129 return dump; |
| 130 } |
| 131 dump.append("num pending entries="); |
| 132 leveldb::ReadOptions options; |
| 133 int num_entries = 0; |
| 134 scoped_ptr<leveldb::Iterator> db_it(db_->NewIterator(options)); |
| 135 for (db_it->SeekToFirst(); db_it->Valid(); db_it->Next()) num_entries++; |
| 136 dump.append(base::IntToString(num_entries)); |
| 137 dump.append("]"); |
| 138 return dump; |
| 139 } |
| 140 |
| 141 } // namespace history_report |
| 142 |
OLD | NEW |