3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef TALK_BASE_DISKCACHE_H__
29 #define TALK_BASE_DISKCACHE_H__
40 class StreamInterface;
42 ///////////////////////////////////////////////////////////////////////////////
43 // DiskCache - An LRU cache of streams, stored on disk.
45 // Streams are identified by a unique resource id. Multiple streams can be
46 // associated with each resource id, distinguished by an index. When old
47 // resources are flushed from the cache, all streams associated with those
48 // resources are removed together.
49 // DiskCache is designed to persist across executions of the program. It is
50 // safe for use from an arbitrary number of users on a single thread, but not
51 // from multiple threads or other processes.
52 ///////////////////////////////////////////////////////////////////////////////
59 bool Initialize(const std::string& folder, size_t size);
62 bool LockResource(const std::string& id);
63 StreamInterface* WriteResource(const std::string& id, size_t index);
64 bool UnlockResource(const std::string& id);
66 StreamInterface* ReadResource(const std::string& id, size_t index) const;
68 bool HasResource(const std::string& id) const;
69 bool HasResourceStream(const std::string& id, size_t index) const;
70 bool DeleteResource(const std::string& id);
73 virtual bool InitializeEntries() = 0;
74 virtual bool PurgeFiles() = 0;
76 virtual bool FileExists(const std::string& filename) const = 0;
77 virtual bool DeleteFile(const std::string& filename) const = 0;
79 enum LockState { LS_UNLOCKED, LS_LOCKED, LS_UNLOCKING };
82 mutable size_t accessors;
87 typedef std::map<std::string, Entry> EntryMap;
88 friend class DiskCacheAdapter;
92 std::string IdToFilename(const std::string& id, size_t index) const;
93 bool FilenameToId(const std::string& filename, std::string* id,
96 const Entry* GetEntry(const std::string& id) const {
97 return const_cast<DiskCache*>(this)->GetOrCreateEntry(id, false);
99 Entry* GetOrCreateEntry(const std::string& id, bool create);
101 void ReleaseResource(const std::string& id, size_t index) const;
104 size_t max_cache_, total_size_;
106 mutable size_t total_accessors_;
109 ///////////////////////////////////////////////////////////////////////////////
110 // CacheLock - Automatically manage locking and unlocking, with optional
111 // rollback semantics
112 ///////////////////////////////////////////////////////////////////////////////
116 CacheLock(DiskCache* cache, const std::string& id, bool rollback = false)
117 : cache_(cache), id_(id), rollback_(rollback)
119 locked_ = cache_->LockResource(id_);
123 cache_->UnlockResource(id_);
125 cache_->DeleteResource(id_);
129 bool IsLocked() const { return locked_; }
130 void Commit() { rollback_ = false; }
135 bool rollback_, locked_;
138 ///////////////////////////////////////////////////////////////////////////////
140 } // namespace talk_base
142 #endif // TALK_BASE_DISKCACHE_H__