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