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
169
170
171
172
173
174
175
176
177
178
|
/*
* Copyright (C) 2014-2015 Apple 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:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS 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 APPLE INC. OR ITS 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 NetworkCacheStorage_h
#define NetworkCacheStorage_h
#if ENABLE(NETWORK_CACHE)
#include "NetworkCacheBlobStorage.h"
#include "NetworkCacheData.h"
#include "NetworkCacheKey.h"
#include <WebCore/Timer.h>
#include <wtf/BloomFilter.h>
#include <wtf/Deque.h>
#include <wtf/HashSet.h>
#include <wtf/Optional.h>
#include <wtf/WorkQueue.h>
#include <wtf/text/WTFString.h>
namespace WebKit {
namespace NetworkCache {
class IOChannel;
class Storage {
WTF_MAKE_NONCOPYABLE(Storage);
public:
static std::unique_ptr<Storage> open(const String& cachePath);
struct Record {
WTF_MAKE_FAST_ALLOCATED;
public:
Key key;
std::chrono::system_clock::time_point timeStamp;
Data header;
Data body;
};
// This may call completion handler synchronously on failure.
typedef std::function<bool (std::unique_ptr<Record>)> RetrieveCompletionHandler;
void retrieve(const Key&, unsigned priority, RetrieveCompletionHandler&&);
typedef std::function<void (const Data& mappedBody)> MappedBodyHandler;
void store(const Record&, MappedBodyHandler&&);
void remove(const Key&);
void clear(const String& type, std::chrono::system_clock::time_point modifiedSinceTime, std::function<void ()>&& completionHandler);
struct RecordInfo {
size_t bodySize;
double worth; // 0-1 where 1 is the most valuable.
unsigned bodyShareCount;
String bodyHash;
};
enum TraverseFlag {
ComputeWorth = 1 << 0,
ShareCount = 1 << 1,
};
typedef unsigned TraverseFlags;
typedef std::function<void (const Record*, const RecordInfo&)> TraverseHandler;
// Null record signals end.
void traverse(const String& type, TraverseFlags, TraverseHandler&&);
void setCapacity(size_t);
size_t capacity() const { return m_capacity; }
size_t approximateSize() const;
static const unsigned version = 5;
String basePath() const;
String versionPath() const;
String recordsPath() const;
~Storage();
private:
Storage(const String& directoryPath);
String recordDirectoryPathForKey(const Key&) const;
String recordPathForKey(const Key&) const;
String blobPathForKey(const Key&) const;
void synchronize();
void deleteOldVersions();
void shrinkIfNeeded();
void shrink();
struct ReadOperation;
void dispatchReadOperation(std::unique_ptr<ReadOperation>);
void dispatchPendingReadOperations();
void finishReadOperation(ReadOperation&);
void cancelAllReadOperations();
struct WriteOperation;
void dispatchWriteOperation(std::unique_ptr<WriteOperation>);
void dispatchPendingWriteOperations();
void finishWriteOperation(WriteOperation&);
Optional<BlobStorage::Blob> storeBodyAsBlob(WriteOperation&);
Data encodeRecord(const Record&, Optional<BlobStorage::Blob>);
void readRecord(ReadOperation&, const Data&);
void updateFileModificationTime(const String& path);
void removeFromPendingWriteOperations(const Key&);
WorkQueue& ioQueue() { return m_ioQueue.get(); }
WorkQueue& backgroundIOQueue() { return m_backgroundIOQueue.get(); }
WorkQueue& serialBackgroundIOQueue() { return m_serialBackgroundIOQueue.get(); }
bool mayContain(const Key&) const;
bool mayContainBlob(const Key&) const;
void addToRecordFilter(const Key&);
const String m_basePath;
const String m_recordsPath;
size_t m_capacity { std::numeric_limits<size_t>::max() };
size_t m_approximateRecordsSize { 0 };
// 2^18 bit filter can support up to 26000 entries with false positive rate < 1%.
using ContentsFilter = BloomFilter<18>;
std::unique_ptr<ContentsFilter> m_recordFilter;
std::unique_ptr<ContentsFilter> m_blobFilter;
bool m_synchronizationInProgress { false };
bool m_shrinkInProgress { false };
Vector<Key::HashType> m_recordFilterHashesAddedDuringSynchronization;
Vector<Key::HashType> m_blobFilterHashesAddedDuringSynchronization;
static const int maximumRetrievePriority = 4;
Deque<std::unique_ptr<ReadOperation>> m_pendingReadOperationsByPriority[maximumRetrievePriority + 1];
HashSet<std::unique_ptr<ReadOperation>> m_activeReadOperations;
WebCore::Timer m_readOperationTimeoutTimer;
Deque<std::unique_ptr<WriteOperation>> m_pendingWriteOperations;
HashSet<std::unique_ptr<WriteOperation>> m_activeWriteOperations;
WebCore::Timer m_writeOperationDispatchTimer;
struct TraverseOperation;
HashSet<std::unique_ptr<TraverseOperation>> m_activeTraverseOperations;
Ref<WorkQueue> m_ioQueue;
Ref<WorkQueue> m_backgroundIOQueue;
Ref<WorkQueue> m_serialBackgroundIOQueue;
BlobStorage m_blobStorage;
};
// FIXME: Remove, used by NetworkCacheStatistics only.
void traverseRecordsFiles(const String& recordsPath, const String& type, const std::function<void (const String& fileName, const String& hashString, const String& type, bool isBodyBlob, const String& recordDirectoryPath)>&);
}
}
#endif
#endif
|