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

Side by Side Diff: chrome/browser/android/history_report/delta_file_backend_leveldb.cc

Issue 1542413002: Switch to standard integer types in chrome/browser/, part 1 of 4. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 12 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 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/android/history_report/delta_file_backend_leveldb.h" 5 #include "chrome/browser/android/history_report/delta_file_backend_leveldb.h"
6 6
7 #include <inttypes.h> 7 #include <inttypes.h>
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "chrome/browser/android/history_report/delta_file_commons.h" 12 #include "chrome/browser/android/history_report/delta_file_commons.h"
13 #include "third_party/leveldatabase/src/include/leveldb/comparator.h" 13 #include "third_party/leveldatabase/src/include/leveldb/comparator.h"
14 #include "third_party/leveldatabase/src/include/leveldb/db.h" 14 #include "third_party/leveldatabase/src/include/leveldb/db.h"
15 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" 15 #include "third_party/leveldatabase/src/include/leveldb/iterator.h"
16 #include "third_party/leveldatabase/src/include/leveldb/options.h" 16 #include "third_party/leveldatabase/src/include/leveldb/options.h"
17 #include "third_party/leveldatabase/src/include/leveldb/slice.h" 17 #include "third_party/leveldatabase/src/include/leveldb/slice.h"
18 #include "third_party/leveldatabase/src/include/leveldb/status.h" 18 #include "third_party/leveldatabase/src/include/leveldb/status.h"
19 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" 19 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
20 #include "url/gurl.h" 20 #include "url/gurl.h"
21 21
22 namespace { 22 namespace {
23 const base::FilePath::CharType kDbFileName[] = 23 const base::FilePath::CharType kDbFileName[] =
24 FILE_PATH_LITERAL("DeltaFileLevelDb"); 24 FILE_PATH_LITERAL("DeltaFileLevelDb");
25 25
26 int64 GetLastSeqNo(leveldb::DB* db) { 26 int64_t GetLastSeqNo(leveldb::DB* db) {
27 leveldb::ReadOptions options; 27 leveldb::ReadOptions options;
28 scoped_ptr<leveldb::Iterator> db_iter(db->NewIterator(options)); 28 scoped_ptr<leveldb::Iterator> db_iter(db->NewIterator(options));
29 db_iter->SeekToLast(); 29 db_iter->SeekToLast();
30 int64 seq_no = 0; 30 int64_t seq_no = 0;
31 if (db_iter->Valid()) { 31 if (db_iter->Valid()) {
32 history_report::DeltaFileEntry last_entry; 32 history_report::DeltaFileEntry last_entry;
33 leveldb::Slice value_slice = db_iter->value(); 33 leveldb::Slice value_slice = db_iter->value();
34 if (last_entry.ParseFromArray(value_slice.data(), value_slice.size())) 34 if (last_entry.ParseFromArray(value_slice.data(), value_slice.size()))
35 seq_no = last_entry.seq_no(); 35 seq_no = last_entry.seq_no();
36 } 36 }
37 return seq_no; 37 return seq_no;
38 } 38 }
39 39
40 void SaveChange(leveldb::DB* db, 40 void SaveChange(leveldb::DB* db,
41 const std::string& url, 41 const std::string& url,
42 const std::string& type) { 42 const std::string& type) {
43 int64 seq_no = GetLastSeqNo(db) + 1; 43 int64_t seq_no = GetLastSeqNo(db) + 1;
44 history_report::DeltaFileEntry entry; 44 history_report::DeltaFileEntry entry;
45 entry.set_seq_no(seq_no); 45 entry.set_seq_no(seq_no);
46 entry.set_type(type); 46 entry.set_type(type);
47 entry.set_url(url); 47 entry.set_url(url);
48 leveldb::WriteOptions writeOptions; 48 leveldb::WriteOptions writeOptions;
49 std::string key; 49 std::string key;
50 base::SStringPrintf(&key, "%" PRId64, seq_no); 50 base::SStringPrintf(&key, "%" PRId64, seq_no);
51 leveldb::Status status = db->Put( 51 leveldb::Status status = db->Put(
52 writeOptions, 52 writeOptions,
53 leveldb::Slice(key), 53 leveldb::Slice(key),
54 leveldb::Slice(entry.SerializeAsString())); 54 leveldb::Slice(entry.SerializeAsString()));
55 if (!status.ok()) 55 if (!status.ok())
56 LOG(WARNING) << "Save Change failed " << status.ToString(); 56 LOG(WARNING) << "Save Change failed " << status.ToString();
57 } 57 }
58 58
59 } // namespace 59 } // namespace
60 60
61 namespace history_report { 61 namespace history_report {
62 62
63 // Comparator used in leveldb. 63 // Comparator used in leveldb.
64 class DeltaFileBackend::DigitsComparator : public leveldb::Comparator { 64 class DeltaFileBackend::DigitsComparator : public leveldb::Comparator {
65 public: 65 public:
66 int Compare(const leveldb::Slice& a, 66 int Compare(const leveldb::Slice& a,
67 const leveldb::Slice& b) const override { 67 const leveldb::Slice& b) const override {
68 int64 first; 68 int64_t first;
69 int64 second; 69 int64_t second;
70 // Keys which can't be parsed go to the end. 70 // Keys which can't be parsed go to the end.
71 if (!base::StringToInt64(a.ToString(), &first)) return 1; 71 if (!base::StringToInt64(a.ToString(), &first)) return 1;
72 if (!base::StringToInt64(b.ToString(), &second)) return -1; 72 if (!base::StringToInt64(b.ToString(), &second)) return -1;
73 if (first < second) return -1; 73 if (first < second) return -1;
74 if (first > second) return 1; 74 if (first > second) return 1;
75 return 0; 75 return 0;
76 } 76 }
77 const char* Name() const override { return "DigitsComparator"; } 77 const char* Name() const override { return "DigitsComparator"; }
78 void FindShortestSeparator(std::string*, 78 void FindShortestSeparator(std::string*,
79 const leveldb::Slice&) const override { } 79 const leveldb::Slice&) const override { }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 void DeltaFileBackend::PageAdded(const GURL& url) { 118 void DeltaFileBackend::PageAdded(const GURL& url) {
119 if (!EnsureInitialized()) return; 119 if (!EnsureInitialized()) return;
120 SaveChange(db_.get(), url.spec().c_str(), "add"); 120 SaveChange(db_.get(), url.spec().c_str(), "add");
121 } 121 }
122 122
123 void DeltaFileBackend::PageDeleted(const GURL& url) { 123 void DeltaFileBackend::PageDeleted(const GURL& url) {
124 if (!EnsureInitialized()) return; 124 if (!EnsureInitialized()) return;
125 SaveChange(db_.get(), url.spec().c_str(), "del"); 125 SaveChange(db_.get(), url.spec().c_str(), "del");
126 } 126 }
127 127
128 int64 DeltaFileBackend::Trim(int64 lower_bound) { 128 int64_t DeltaFileBackend::Trim(int64_t lower_bound) {
129 if (!EnsureInitialized()) return -1; 129 if (!EnsureInitialized()) return -1;
130 leveldb::ReadOptions read_options; 130 leveldb::ReadOptions read_options;
131 scoped_ptr<leveldb::Iterator> db_iter(db_->NewIterator(read_options)); 131 scoped_ptr<leveldb::Iterator> db_iter(db_->NewIterator(read_options));
132 db_iter->SeekToFirst(); 132 db_iter->SeekToFirst();
133 if (!db_iter->Valid()) 133 if (!db_iter->Valid())
134 return -1; 134 return -1;
135 history_report::DeltaFileEntry first_entry; 135 history_report::DeltaFileEntry first_entry;
136 leveldb::Slice value_slice = db_iter->value(); 136 leveldb::Slice value_slice = db_iter->value();
137 if (!first_entry.ParseFromArray(value_slice.data(), value_slice.size())) 137 if (!first_entry.ParseFromArray(value_slice.data(), value_slice.size()))
138 return -1; 138 return -1;
139 int64 min_seq_no = first_entry.seq_no(); 139 int64_t min_seq_no = first_entry.seq_no();
140 db_iter->SeekToLast(); 140 db_iter->SeekToLast();
141 if (!db_iter->Valid()) 141 if (!db_iter->Valid())
142 return -1; 142 return -1;
143 history_report::DeltaFileEntry last_entry; 143 history_report::DeltaFileEntry last_entry;
144 value_slice = db_iter->value(); 144 value_slice = db_iter->value();
145 if (!last_entry.ParseFromArray(value_slice.data(), value_slice.size())) 145 if (!last_entry.ParseFromArray(value_slice.data(), value_slice.size()))
146 return -1; 146 return -1;
147 int64 max_seq_no = last_entry.seq_no(); 147 int64_t max_seq_no = last_entry.seq_no();
148 // We want to have at least one entry in delta file left to know 148 // We want to have at least one entry in delta file left to know
149 // last sequence number in SaveChange. 149 // last sequence number in SaveChange.
150 if (max_seq_no <= lower_bound) 150 if (max_seq_no <= lower_bound)
151 lower_bound = max_seq_no - 1; 151 lower_bound = max_seq_no - 1;
152 leveldb::WriteBatch updates; 152 leveldb::WriteBatch updates;
153 for (int64 seq_no = min_seq_no; seq_no <= lower_bound; ++seq_no) { 153 for (int64_t seq_no = min_seq_no; seq_no <= lower_bound; ++seq_no) {
154 std::string key; 154 std::string key;
155 base::SStringPrintf(&key, "%" PRId64, seq_no); 155 base::SStringPrintf(&key, "%" PRId64, seq_no);
156 updates.Delete(leveldb::Slice(key)); 156 updates.Delete(leveldb::Slice(key));
157 } 157 }
158 158
159 leveldb::WriteOptions write_options; 159 leveldb::WriteOptions write_options;
160 leveldb::Status status = db_->Write(write_options, &updates); 160 leveldb::Status status = db_->Write(write_options, &updates);
161 if (status.ok()) 161 if (status.ok())
162 return max_seq_no; 162 return max_seq_no;
163 LOG(WARNING) << "Trim failed: " << status.ToString(); 163 LOG(WARNING) << "Trim failed: " << status.ToString();
164 return -1; 164 return -1;
165 } 165 }
166 166
167 bool DeltaFileBackend::Recreate(const std::vector<std::string>& urls) { 167 bool DeltaFileBackend::Recreate(const std::vector<std::string>& urls) {
168 if (!EnsureInitialized()) return false; 168 if (!EnsureInitialized()) return false;
169 Clear(); 169 Clear();
170 int64 seq_no = 1; 170 int64_t seq_no = 1;
171 leveldb::WriteBatch updates; 171 leveldb::WriteBatch updates;
172 for (std::vector<std::string>::const_iterator it = urls.begin(); 172 for (std::vector<std::string>::const_iterator it = urls.begin();
173 it != urls.end(); 173 it != urls.end();
174 ++it) { 174 ++it) {
175 DeltaFileEntry entry; 175 DeltaFileEntry entry;
176 entry.set_seq_no(seq_no); 176 entry.set_seq_no(seq_no);
177 entry.set_url(*it); 177 entry.set_url(*it);
178 entry.set_type("add"); 178 entry.set_type("add");
179 std::string key; 179 std::string key;
180 base::SStringPrintf(&key, "%" PRId64, seq_no); 180 base::SStringPrintf(&key, "%" PRId64, seq_no);
181 updates.Put(leveldb::Slice(key), 181 updates.Put(leveldb::Slice(key),
182 leveldb::Slice(entry.SerializeAsString())); 182 leveldb::Slice(entry.SerializeAsString()));
183 ++seq_no; 183 ++seq_no;
184 } 184 }
185 leveldb::WriteOptions options; 185 leveldb::WriteOptions options;
186 leveldb::Status status = db_->Write(options, &updates); 186 leveldb::Status status = db_->Write(options, &updates);
187 if (status.ok()) 187 if (status.ok())
188 return true; 188 return true;
189 LOG(WARNING) << "Recreate failed: " << status.ToString(); 189 LOG(WARNING) << "Recreate failed: " << status.ToString();
190 return false; 190 return false;
191 } 191 }
192 192
193 scoped_ptr<std::vector<DeltaFileEntryWithData> > DeltaFileBackend::Query( 193 scoped_ptr<std::vector<DeltaFileEntryWithData>> DeltaFileBackend::Query(
194 int64 last_seq_no, 194 int64_t last_seq_no,
195 int32 limit) { 195 int32_t limit) {
196 if (!EnsureInitialized()) 196 if (!EnsureInitialized())
197 return make_scoped_ptr(new std::vector<DeltaFileEntryWithData>()); 197 return make_scoped_ptr(new std::vector<DeltaFileEntryWithData>());
198 std::string start; 198 std::string start;
199 base::SStringPrintf(&start, "%" PRId64, last_seq_no + 1); 199 base::SStringPrintf(&start, "%" PRId64, last_seq_no + 1);
200 leveldb::ReadOptions options; 200 leveldb::ReadOptions options;
201 scoped_ptr<leveldb::Iterator> db_it(db_->NewIterator(options)); 201 scoped_ptr<leveldb::Iterator> db_it(db_->NewIterator(options));
202 scoped_ptr<std::vector<DeltaFileEntryWithData> > result( 202 scoped_ptr<std::vector<DeltaFileEntryWithData> > result(
203 new std::vector<DeltaFileEntryWithData>()); 203 new std::vector<DeltaFileEntryWithData>());
204 int32 count = 0; 204 int32_t count = 0;
205 for (db_it->Seek(start); db_it->Valid() && count < limit; db_it->Next()) { 205 for (db_it->Seek(start); db_it->Valid() && count < limit; db_it->Next()) {
206 DeltaFileEntry entry; 206 DeltaFileEntry entry;
207 leveldb::Slice value_slice = db_it->value(); 207 leveldb::Slice value_slice = db_it->value();
208 if (!entry.ParseFromArray(value_slice.data(), value_slice.size())) 208 if (!entry.ParseFromArray(value_slice.data(), value_slice.size()))
209 continue; 209 continue;
210 result->push_back(DeltaFileEntryWithData(entry)); 210 result->push_back(DeltaFileEntryWithData(entry));
211 ++count; 211 ++count;
212 } 212 }
213 return result.Pass(); 213 return result.Pass();
214 } 214 }
(...skipping 15 matching lines...) Expand all
230 leveldb::ReadOptions options; 230 leveldb::ReadOptions options;
231 scoped_ptr<leveldb::Iterator> db_it(db_->NewIterator(options)); 231 scoped_ptr<leveldb::Iterator> db_it(db_->NewIterator(options));
232 int num_entries = 0; 232 int num_entries = 0;
233 for (db_it->SeekToFirst(); db_it->Valid(); db_it->Next()) num_entries++; 233 for (db_it->SeekToFirst(); db_it->Valid(); db_it->Next()) num_entries++;
234 dump.append(base::IntToString(num_entries)); 234 dump.append(base::IntToString(num_entries));
235 dump.append("]"); 235 dump.append("]");
236 return dump; 236 return dump;
237 } 237 }
238 238
239 } // namespace history_report 239 } // namespace history_report
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698