OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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_FLASH_LOG_STRUCTURED_STORE_H_ | |
6 #define NET_DISK_CACHE_FLASH_LOG_STRUCTURED_STORE_H_ | |
7 | |
8 #include <set> | |
9 #include <vector> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/gtest_prod_util.h" | |
13 #include "net/base/net_export.h" | |
14 | |
15 namespace disk_cache { | |
16 | |
17 class Segment; | |
18 class Storage; | |
19 | |
20 // This class implements a general purpose store for storing and retrieving | |
21 // entries consisting of arbitrary binary data. The store has log semantics, | |
22 // i.e. it's not possible to overwrite data in place. In order to update an | |
23 // entry, a new version must be written. Only one entry can be written to at | |
24 // any given time, while concurrent reading of multiple entries is supported. | |
25 class NET_EXPORT_PRIVATE LogStructuredStore { | |
26 public: | |
27 explicit LogStructuredStore(Storage* storage); | |
28 ~LogStructuredStore(); | |
29 | |
30 // Performs initialization. Must be the first function called and further | |
31 // calls should be made only if it is successful. | |
32 bool Init(); | |
33 | |
34 // Closes the store. Should be the last function called before destruction. | |
35 bool Close(); | |
36 | |
37 // Creates an entry of |size| bytes. The id of the created entry is stored in | |
38 // |entry_id|. | |
39 bool CreateEntry(int32 size, int32* entry_id); | |
40 | |
41 // TODO(agayev): Add DeleteEntry. | |
42 | |
43 // Appends data to the end of the last created entry. | |
44 bool WriteData(const void* buffer, int32 size); | |
45 | |
46 // Opens an entry with id |entry_id|. | |
47 bool OpenEntry(int32 entry_id); | |
48 | |
49 // Reads |size| bytes starting from |offset| into |buffer|, where |offset| is | |
50 // relative to the entry's content, from an entry identified by |entry_id|. | |
51 bool ReadData(int32 entry_id, void* buffer, int32 size, int32 offset) const; | |
52 | |
53 // Closes an entry that was either opened with OpenEntry or created with | |
54 // CreateEntry. | |
55 void CloseEntry(int32 id); | |
56 | |
57 private: | |
58 FRIEND_TEST_ALL_PREFIXES(FlashCacheTest, | |
59 LogStructuredStoreReadFromClosedSegment); | |
60 FRIEND_TEST_ALL_PREFIXES(FlashCacheTest, | |
61 LogStructuredStoreSegmentSelectionIsFifo); | |
62 FRIEND_TEST_ALL_PREFIXES(FlashCacheTest, | |
63 LogStructuredStoreInUseSegmentIsSkipped); | |
64 FRIEND_TEST_ALL_PREFIXES(FlashCacheTest, | |
65 LogStructuredStoreReadFromCurrentAfterClose); | |
66 | |
67 int32 GetNextSegmentIndex(); | |
68 bool InUse(int32 segment_index) const; | |
69 | |
70 Storage* storage_; | |
71 | |
72 int32 num_segments_; | |
73 | |
74 // Currently open segments, either for reading or writing. There can only be | |
75 // one segment open for writing, and multiple open for reading. | |
76 std::vector<Segment*> open_segments_; | |
77 | |
78 // The index of the segment currently being written to. It's an index to | |
79 // |open_segments_| vector. | |
80 int32 write_index_; | |
81 | |
82 // Ids of entries currently open, either CreatEntry'ed or OpenEntry'ed. | |
83 std::set<int32> open_entries_; | |
84 | |
85 // Id of the entry that is currently being written to, -1 if there is no entry | |
86 // currently being written to. | |
87 int32 current_entry_id_; | |
88 | |
89 // Number of bytes left to be written to the entry identified by | |
90 // |current_entry_id_|. Its value makes sense iff |current_entry_id_| is not | |
91 // -1. | |
92 int32 current_entry_num_bytes_left_to_write_; | |
93 | |
94 bool init_; // Init was called. | |
95 bool closed_; // Close was called. | |
96 | |
97 DISALLOW_COPY_AND_ASSIGN(LogStructuredStore); | |
98 }; | |
99 | |
100 } // namespace disk_cache | |
101 | |
102 #endif // NET_DISK_CACHE_FLASH_LOG_STRUCTURED_STORE_H_ | |
OLD | NEW |