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