1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef NET_DISK_CACHE_ENTRY_IMPL_H__
#define NET_DISK_CACHE_ENTRY_IMPL_H__
#include "net/disk_cache/disk_cache.h"
#include "net/disk_cache/storage_block.h"
#include "net/disk_cache/storage_block-inl.h"
namespace disk_cache {
class BackendImpl;
// This class implements the Entry interface. An object of this
// class represents a single entry on the cache.
class EntryImpl : public Entry, public base::RefCounted<EntryImpl> {
friend class base::RefCounted<EntryImpl>;
public:
EntryImpl(BackendImpl* backend, Addr address);
// Entry interface.
virtual void Doom();
virtual void Close();
virtual std::string GetKey() const;
virtual Time GetLastUsed() const;
virtual Time GetLastModified() const;
virtual int32 GetDataSize(int index) const;
virtual int ReadData(int index, int offset, char* buf, int buf_len,
net::CompletionCallback* completion_callback);
virtual int WriteData(int index, int offset, const char* buf, int buf_len,
net::CompletionCallback* completion_callback,
bool truncate);
inline CacheEntryBlock* entry() {
return &entry_;
}
inline CacheRankingsBlock* rankings() {
return &node_;
}
uint32 GetHash();
// Performs the initialization of a EntryImpl that will be added to the
// cache.
bool CreateEntry(Addr node_address, const std::string& key,
uint32 hash);
// Returns true if this entry matches the lookup arguments.
bool IsSameEntry(const std::string& key, uint32 hash);
// Permamently destroys this entry
void InternalDoom();
// Returns the address of the next entry on the list of entries with the same
// hash.
CacheAddr GetNextAddress();
// Sets the address of the next entry on the list of entries with the same
// hash.
void SetNextAddress(Addr address);
// Reloads the rankings node information.
bool LoadNodeAddress();
// Reloads the data for this entry. If there is already an object in memory
// for the entry, the returned value is a pointer to that entry, otherwise
// it is the passed in entry. On failure returns NULL.
static EntryImpl* Update(EntryImpl* entry);
// Returns true if this entry is marked as dirty on disk.
bool IsDirty(int32 current_id);
void ClearDirtyFlag();
// Fixes this entry so it can be treated as valid (to delete it).
void SetPointerForInvalidEntry(int32 new_id);
// Returns false if the entry is clearly invalid.
bool SanityCheck();
// Handle the pending asynchronous IO count.
void IncrementIoCount();
void DecrementIoCount();
private:
~EntryImpl();
// Index for the file used to store the key, if any (files_[kKeyFileIndex]).
static const int kKeyFileIndex = 2;
// Initializes the storage for an internal or external data block.
bool CreateDataBlock(int index, int size);
// Initializes the storage for an internal or external generic block.
bool CreateBlock(int size, Addr* address);
// Deletes the data pointed by address, maybe backed by files_[index].
void DeleteData(Addr address, int index);
// Updates ranking information.
void UpdateRank(bool modified);
// Returns a pointer to the file that stores the given address.
File* GetBackingFile(Addr address, int index);
// Returns a pointer to the file that stores external data.
File* GetExternalFile(Addr address, int index);
// Prepares the target file or buffer for a write of buf_len bytes at the
// given offset.
bool PrepareTarget(int index, int offset, int buf_len, bool truncate);
// Grows the size of the storage used to store user data, if needed.
bool GrowUserBuffer(int index, int offset, int buf_len, bool truncate);
// Reads from a block data file to this object's memory buffer.
bool MoveToLocalBuffer(int index);
// Loads the external file to this object's memory buffer.
bool ImportSeparateFile(int index, int offset, int buf_len);
// Flush the in-memory data to the backing storage.
bool Flush(int index, int size, bool async);
// Logs this entry to the internal trace buffer.
void Log(const char* msg);
CacheEntryBlock entry_; // Key related information for this entry.
CacheRankingsBlock node_; // Rankings related information for this entry.
BackendImpl* backend_; // Back pointer to the cache.
scoped_ptr<char> user_buffers_[2]; // Store user data.
scoped_refptr<File> files_[3]; // Files to store external user data and key.
int unreported_size_[2]; // Bytes not reported yet to the backend.
bool doomed_; // True if this entry was removed from the cache.
DISALLOW_EVIL_CONSTRUCTORS(EntryImpl);
};
} // namespace disk_cache
#endif // NET_DISK_CACHE_ENTRY_IMPL_H__
|