Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_DISK_FORMAT_H_ | 5 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_DISK_FORMAT_H_ |
| 6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_DISK_FORMAT_H_ | 6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_DISK_FORMAT_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/port.h" | |
| 12 | 11 |
| 13 namespace base { | 12 namespace base { |
| 14 class Time; | 13 class Time; |
| 15 } | 14 } |
| 16 | 15 |
| 17 namespace disk_cache { | 16 namespace disk_cache { |
| 18 | 17 |
| 19 const uint64 kSimpleInitialMagicNumber = GG_UINT64_C(0xfcfb6d1ba7725c30); | 18 const uint64 kSimpleInitialMagicNumber = GG_UINT64_C(0xfcfb6d1ba7725c30); |
|
gavinp
2013/04/15 15:40:54
Good to rename this file to simple_entry_format.h
| |
| 20 const uint64 kSimpleIndexInitialMagicNumber = GG_UINT64_C(0x656e74657220796f); | |
| 21 | 19 |
| 22 // A file in the Simple cache consists of a SimpleFileHeader followed | 20 // A file in the Simple cache consists of a SimpleFileHeader followed |
| 23 // by data. | 21 // by data. |
| 24 | 22 |
| 25 const uint32 kSimpleVersion = 1; | 23 const uint32 kSimpleVersion = 1; |
| 26 | 24 |
| 27 static const int kSimpleEntryFileCount = 3; | 25 static const int kSimpleEntryFileCount = 3; |
| 28 | 26 |
| 29 struct SimpleFileHeader { | 27 struct SimpleFileHeader { |
| 30 uint64 initial_magic_number; | 28 uint64 initial_magic_number; |
| 31 uint32 version; | 29 uint32 version; |
| 32 uint32 key_length; | 30 uint32 key_length; |
| 33 uint32 key_hash; | 31 uint32 key_hash; |
| 34 }; | 32 }; |
| 35 | 33 |
| 36 // Simple Index File sketch: | |
| 37 // This is based on the struct Header and Footer as seem below, and the struct | |
| 38 // alignment is platform dependent. | |
| 39 // The CRC check is a guarantee that we don't read incorrect values. | |
| 40 // ------------------------- | |
| 41 // struct Header; | |
| 42 // ------------------------- | |
| 43 // Repeated |size| times { | |
| 44 // struct EntryMetadata; | |
| 45 // } | |
| 46 // ------------------------- | |
| 47 // struct Footer; | |
| 48 // ------------------------- | |
| 49 namespace SimpleIndexFile { | |
| 50 // Simple Index File metadata is defined here. | |
| 51 struct Header { | |
| 52 Header(); | |
| 53 uint64 initial_magic_number; | |
| 54 uint32 version; | |
| 55 uint64 number_of_entries; | |
| 56 uint64 cache_size; // Total cache storage size in bytes. | |
| 57 }; | |
| 58 | |
| 59 // TODO(felipeg): At some point we should consider using a protobuffer. See | |
| 60 // that we are re-implementing most of protobuffer's functionality such as | |
| 61 // Serialize and Merge. | |
| 62 // We must keep this struct a POD. | |
| 63 struct EntryMetadata { | |
| 64 EntryMetadata(); | |
| 65 EntryMetadata(uint64 hash_key_p, | |
| 66 base::Time last_used_time_p, | |
| 67 uint64 entry_size_p); | |
| 68 | |
| 69 base::Time GetLastUsedTime() const; | |
| 70 uint64 GetHashKey() const; | |
| 71 void SetLastUsedTime(const base::Time& last_used_time_p); | |
| 72 | |
| 73 // Serialize the data from |in_entry_metadata| and appends the bytes in | |
| 74 // |out_buffer|. The serialization is platform dependent since it simply | |
| 75 // writes the whole struct from memory into the given buffer. | |
| 76 static void Serialize(const EntryMetadata& in_entry_metadata, | |
| 77 std::string* out_buffer); | |
| 78 | |
| 79 static void DeSerialize(const char* in_buffer, | |
| 80 EntryMetadata* out_entry_metadata); | |
| 81 | |
| 82 // Merge two EntryMetadata instances. | |
| 83 // The existing valid data in |out_entry_metadata| will prevail. | |
| 84 static void Merge(const EntryMetadata& entry_metadata, | |
| 85 EntryMetadata* out_entry_metadata); | |
| 86 | |
| 87 uint64 hash_key; | |
| 88 | |
| 89 // This is the serialized format from Time::ToInternalValue(). | |
| 90 // If you want to make calculations/comparisons, you should use the | |
| 91 // base::Time() class. Use the GetLastUsedTime() method above. | |
| 92 int64 last_used_time; | |
| 93 | |
| 94 uint64 entry_size; // Storage size in bytes. | |
| 95 }; | |
| 96 | |
| 97 const size_t kEntryMetadataSize = sizeof(EntryMetadata); | |
| 98 | |
| 99 struct Footer { | |
| 100 Footer(); | |
| 101 uint32 crc; | |
| 102 }; | |
| 103 | |
| 104 } // namespace SimpleIndexFile | |
| 105 | |
| 106 // Size of the uint64 hash_key number in Hex format in a string. | |
| 107 const size_t kEntryHashKeyAsHexStringSize = 2 * sizeof( | |
| 108 SimpleIndexFile::EntryMetadata::hash_key); | |
| 109 | |
| 110 std::string ConvertEntryHashKeyToHexString(uint64 hash_key); | |
| 111 | |
| 112 // |key| is the regular HTTP Cache key, which is a URL. | |
| 113 // Returns the Hex ascii representation of the uint64 hash_key. | |
| 114 std::string GetEntryHashKeyAsHexString(const std::string& key); | |
| 115 | |
| 116 // |key| is the regular HTTP Cache key, which is a URL. | |
| 117 // Returns the hash of the key as uint64. | |
| 118 uint64 GetEntryHashKey(const std::string& key); | |
| 119 | |
| 120 // Parses the |hash_key| string into a uint64 buffer. | |
| 121 // |hash_key| string must be of the form: FFFFFFFFFFFFFFFF . | |
| 122 bool GetEntryHashKeyFromHexString(const std::string& hash_key, | |
| 123 uint64* hash_key_out); | |
| 124 | |
| 125 } // namespace disk_cache | 34 } // namespace disk_cache |
| 126 | 35 |
| 127 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_DISK_FORMAT_H_ | 36 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_DISK_FORMAT_H_ |
| OLD | NEW |