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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/stl_util.h"
#include "net/disk_cache/flash/format.h"
#include "net/disk_cache/flash/log_structured_store.h"
#include "net/disk_cache/flash/segment.h"
#include "net/disk_cache/flash/storage.h"
namespace disk_cache {
LogStructuredStore::LogStructuredStore(Storage* storage)
: storage_(storage),
num_segments_(storage->size() / kFlashSegmentSize),
open_segments_(num_segments_),
write_index_(0),
current_entry_id_(-1),
current_entry_num_bytes_left_to_write_(0),
init_(false),
closed_(false) {
DCHECK(storage->size() % kFlashSegmentSize == 0);
}
LogStructuredStore::~LogStructuredStore() {
DCHECK(!init_ || closed_);
STLDeleteElements(&open_segments_);
}
bool LogStructuredStore::Init() {
DCHECK(!init_);
// TODO(agayev): Once we start persisting segment metadata to disk, we will
// start from where we left off during the last shutdown.
scoped_ptr<Segment> segment(new Segment(write_index_, false, storage_));
if (!segment->Init())
return false;
segment->AddUser();
open_segments_[write_index_] = segment.release();
init_ = true;
return true;
}
bool LogStructuredStore::Close() {
DCHECK(init_ && !closed_);
open_segments_[write_index_]->ReleaseUser();
if (!open_segments_[write_index_]->Close())
return false;
closed_ = true;
return true;
// TODO(agayev): persist metadata to disk.
}
bool LogStructuredStore::CreateEntry(int32 size, int32* id) {
DCHECK(init_ && !closed_);
DCHECK(current_entry_id_ == -1 && size <= disk_cache::kFlashSegmentFreeSpace);
// TODO(agayev): Avoid large entries from leaving the segments almost empty.
if (!open_segments_[write_index_]->CanHold(size)) {
if (!open_segments_[write_index_]->Close())
return false;
open_segments_[write_index_]->ReleaseUser();
if (open_segments_[write_index_]->HasNoUsers()) {
delete open_segments_[write_index_];
open_segments_[write_index_] = NULL;
}
write_index_ = GetNextSegmentIndex();
scoped_ptr<Segment> segment(new Segment(write_index_, false, storage_));
if (!segment->Init())
return false;
segment->AddUser();
open_segments_[write_index_] = segment.release();
}
*id = open_segments_[write_index_]->write_offset();
open_segments_[write_index_]->StoreOffset(*id);
current_entry_id_ = *id;
current_entry_num_bytes_left_to_write_ = size;
open_entries_.insert(current_entry_id_);
return true;
}
bool LogStructuredStore::WriteData(const void* buffer, int32 size) {
DCHECK(init_ && !closed_);
DCHECK(current_entry_id_ != -1 &&
size <= current_entry_num_bytes_left_to_write_);
if (open_segments_[write_index_]->WriteData(buffer, size)) {
current_entry_num_bytes_left_to_write_ -= size;
return true;
}
return false;
}
bool LogStructuredStore::OpenEntry(int32 id) {
DCHECK(init_ && !closed_);
if (open_entries_.find(id) != open_entries_.end())
return false;
// Segment is already open.
int32 index = id / disk_cache::kFlashSegmentSize;
if (open_segments_[index]) {
if (!open_segments_[index]->HaveOffset(id))
return false;
open_segments_[index]->AddUser();
open_entries_.insert(id);
return true;
}
// Segment is not open.
scoped_ptr<Segment> segment(new Segment(index, true, storage_));
if (!segment->Init() || !segment->HaveOffset(id))
return false;
segment->AddUser();
open_segments_[index] = segment.release();
open_entries_.insert(id);
return true;
}
bool LogStructuredStore::ReadData(int32 id, void* buffer, int32 size,
int32 offset) const {
DCHECK(init_ && !closed_);
DCHECK(open_entries_.find(id) != open_entries_.end());
int32 index = id / disk_cache::kFlashSegmentSize;
DCHECK(open_segments_[index] && open_segments_[index]->HaveOffset(id));
return open_segments_[index]->ReadData(buffer, size, id + offset);
}
void LogStructuredStore::CloseEntry(int32 id) {
DCHECK(init_ && !closed_);
std::set<int32>::iterator entry_iter = open_entries_.find(id);
DCHECK(entry_iter != open_entries_.end());
if (current_entry_id_ != -1) {
DCHECK(id == current_entry_id_ && !current_entry_num_bytes_left_to_write_);
open_entries_.erase(entry_iter);
current_entry_id_ = -1;
return;
}
int32 index = id / disk_cache::kFlashSegmentSize;
DCHECK(open_segments_[index]);
open_entries_.erase(entry_iter);
open_segments_[index]->ReleaseUser();
if (open_segments_[index]->HasNoUsers()) {
delete open_segments_[index];
open_segments_[index] = NULL;
}
}
int32 LogStructuredStore::GetNextSegmentIndex() {
DCHECK(init_ && !closed_);
int32 next_index = (write_index_ + 1) % num_segments_;
while (InUse(next_index)) {
next_index = (next_index + 1) % num_segments_;
DCHECK_NE(next_index, write_index_);
}
return next_index;
}
bool LogStructuredStore::InUse(int32 index) const {
DCHECK(init_ && !closed_);
DCHECK(index >= 0 && index < num_segments_);
return open_segments_[index] != NULL;
}
} // namespace disk_cache
|