Home | History | Annotate | Download | only in flash
      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_STORE_H_
      6 #define NET_DISK_CACHE_FLASH_LOG_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 #include "net/disk_cache/flash/storage.h"
     15 
     16 namespace disk_cache {
     17 
     18 class Segment;
     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 LogStore {
     26  public:
     27   LogStore(const base::FilePath& path, int32 size);
     28   ~LogStore();
     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   // Deletes |entry_id|; the client should keep track of |size| and provide it
     42   // here.  Only inactive (i.e. not currently open or being created) entries can
     43   // be deleted.
     44   void DeleteEntry(int32 entry_id, int32 size);
     45 
     46   // Appends data to the end of the last created entry.
     47   bool WriteData(const void* buffer, int32 size);
     48 
     49   // Opens an entry with id |entry_id|.
     50   bool OpenEntry(int32 entry_id);
     51 
     52   // Reads |size| bytes starting from |offset| into |buffer|, where |offset| is
     53   // relative to the entry's content, from an entry identified by |entry_id|.
     54   bool ReadData(int32 entry_id, void* buffer, int32 size, int32 offset) const;
     55 
     56   // Closes an entry that was either opened with OpenEntry or created with
     57   // CreateEntry.
     58   void CloseEntry(int32 id);
     59 
     60  private:
     61   FRIEND_TEST_ALL_PREFIXES(FlashCacheTest, LogStoreReadFromClosedSegment);
     62   FRIEND_TEST_ALL_PREFIXES(FlashCacheTest, LogStoreSegmentSelectionIsFifo);
     63   FRIEND_TEST_ALL_PREFIXES(FlashCacheTest, LogStoreInUseSegmentIsSkipped);
     64   FRIEND_TEST_ALL_PREFIXES(FlashCacheTest, LogStoreReadFromCurrentAfterClose);
     65 
     66   int32 GetNextSegmentIndex();
     67   bool InUse(int32 segment_index) const;
     68 
     69   Storage storage_;
     70 
     71   int32 num_segments_;
     72 
     73   // Currently open segments, either for reading or writing.  There can only be
     74   // one segment open for writing, and multiple open for reading.
     75   std::vector<Segment*> open_segments_;
     76 
     77   // The index of the segment currently being written to.  It's an index to
     78   // |open_segments_| vector.
     79   int32 write_index_;
     80 
     81   // Ids of entries currently open, either CreatEntry'ed or OpenEntry'ed.
     82   std::set<int32> open_entries_;
     83 
     84   // Id of the entry that is currently being written to, -1 if there is no entry
     85   // currently being written to.
     86   int32 current_entry_id_;
     87 
     88   // Number of bytes left to be written to the entry identified by
     89   // |current_entry_id_|.  Its value makes sense iff |current_entry_id_| is not
     90   // -1.
     91   int32 current_entry_num_bytes_left_to_write_;
     92 
     93   bool init_;  // Init was called.
     94   bool closed_;  // Close was called.
     95 
     96   DISALLOW_COPY_AND_ASSIGN(LogStore);
     97 };
     98 
     99 }  // namespace disk_cache
    100 
    101 #endif  // NET_DISK_CACHE_FLASH_LOG_STORE_H_
    102