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

Unified Diff: net/disk_cache/simple/simple_index.h

Issue 14263005: Refactor our SimpleIndex file format and serialization to use Pickle instead of the previously bugg… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: net/disk_cache/simple/simple_index.h
diff --git a/net/disk_cache/simple/simple_index.h b/net/disk_cache/simple/simple_index.h
index 71f5bb473a2355d5ca22b6d0400c7a878264a1f1..886894ae5b36a918fa754e63e10113469d29099e 100644
--- a/net/disk_cache/simple/simple_index.h
+++ b/net/disk_cache/simple/simple_index.h
@@ -9,13 +9,14 @@
#include <string>
#include "base/basictypes.h"
-#include "base/file_util.h"
+#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/hash_tables.h"
+#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
-#include "net/disk_cache/disk_cache.h"
-#include "net/disk_cache/simple/simple_disk_format.h"
+#include "base/time.h"
+#include "net/base/net_export.h"
namespace base {
class TaskRunner;
@@ -23,8 +24,46 @@ class TaskRunner;
namespace disk_cache {
+class NET_EXPORT_PRIVATE EntryMetadata {
+ public:
+ EntryMetadata();
+ EntryMetadata(uint64 hash_key,
+ base::Time last_used_time,
+ uint64 entry_size);
+
+ uint64 GetHashKey() const { return hash_key_; }
+ base::Time GetLastUsedTime() const;
+ void SetLastUsedTime(const base::Time& last_used_time);
+
+ uint64 GetEntrySize() const { return entry_size_; }
+ void SetEntrySize(uint64 entry_size) { entry_size_ = entry_size; }
+
+ // Serialize the data into the provided pickle.
+ void Serialize(Pickle* pickle) const;
gavinp 2013/04/16 11:22:13 These need a forward def or an #include. I'm amaze
felipeg 2013/04/16 11:25:35 Done.
+ bool Deserialize(PickleIterator* it);
+
+ // Merge two EntryMetadata instances.
+ // The existing current valid data in |this| will prevail.
+ void MergeWith(const EntryMetadata& entry_metadata);
+
+ private:
+ friend class SimpleIndexFileTest;
+
+ // When adding new members here, you should update the Serialize() and
+ // Deserialize() methods.
+ uint64 hash_key_;
+
+ // This is the serialized format from Time::ToInternalValue().
+ // If you want to make calculations/comparisons, you should use the
+ // base::Time() class. Use the GetLastUsedTime() method above.
+ // TODO(felipeg): Use Time() here.
+ int64 last_used_time_;
+
+ uint64 entry_size_; // Storage size in bytes.
+};
+
// This class is not Thread-safe.
-class SimpleIndex
+class NET_EXPORT_PRIVATE SimpleIndex
: public base::SupportsWeakPtr<SimpleIndex> {
public:
SimpleIndex(
@@ -52,44 +91,32 @@ class SimpleIndex
// entry.
bool UpdateEntrySize(const std::string& key, uint64 entry_size);
- private:
- // TODO(felipeg): This way we are storing the hash_key twice (as the
+ // TODO(felipeg): This way we are storing the hash_key twice, as the
// hash_map::key and as a member of EntryMetadata. We could save space if we
// use a hash_set.
- typedef base::hash_map<uint64, SimpleIndexFile::EntryMetadata> EntrySet;
+ typedef base::hash_map<uint64, EntryMetadata> EntrySet;
- typedef base::Callback<void(scoped_ptr<EntrySet>)> MergeCallback;
+ static void InsertInEntrySet(const EntryMetadata& entry_metadata,
+ EntrySet* entry_set);
- static void InsertInternal(
- EntrySet* entry_set,
- const SimpleIndexFile::EntryMetadata& entry_metadata);
+ private:
+ typedef base::Callback<void(scoped_ptr<EntrySet>)> IndexCompletionCallback;
- // Load index from disk. If it is corrupted, call RestoreFromDisk().
static void LoadFromDisk(
const base::FilePath& index_filename,
const scoped_refptr<base::TaskRunner>& io_thread,
- const MergeCallback& merge_callback);
+ const IndexCompletionCallback& completion_callback);
// Enumerates all entries' files on disk and regenerates the index.
- static void RestoreFromDisk(
- const base::FilePath& index_filename,
- const scoped_refptr<base::TaskRunner>& io_thread,
- const MergeCallback& merge_callback);
+ static scoped_ptr<SimpleIndex::EntrySet> RestoreFromDisk(
+ const base::FilePath& index_filename);
+
+ static void WriteToDiskInternal(const base::FilePath& index_filename,
+ scoped_ptr<Pickle> pickle);
// Must run on IO Thread.
void MergeInitializingSet(scoped_ptr<EntrySet> index_file_entries);
- // |out_buffer| needs to be pre-allocated. The serialized index is stored in
- // |out_buffer|.
- void Serialize(std::string* out_buffer);
-
- bool OpenIndexFile();
- bool CloseIndexFile();
-
- static void UpdateFile(const base::FilePath& index_filename,
- const base::FilePath& temp_filename,
- scoped_ptr<std::string> buffer);
-
EntrySet entries_set_;
uint64 cache_size_; // Total cache storage size in bytes.

Powered by Google App Engine
This is Rietveld 408576698