Page Speed Optimization Libraries  1.9.32.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
property_cache.h
Go to the documentation of this file.
1 /*
2  * Copyright 2012 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
91 
92 #ifndef NET_INSTAWEB_UTIL_PUBLIC_PROPERTY_CACHE_H_
93 #define NET_INSTAWEB_UTIL_PUBLIC_PROPERTY_CACHE_H_
94 
95 #include <map>
96 #include <vector>
97 
100 #include "net/instaweb/util/public/cache_interface.h"
101 #include "net/instaweb/util/public/ref_counted_ptr.h"
105 
106 namespace net_instaweb {
107 
108 class AbstractLogRecord;
109 class AbstractMutex;
110 class AbstractPropertyStoreGetCallback;
111 class PropertyCacheValues;
112 class PropertyValueProtobuf;
113 class PropertyPage;
114 class PropertyStore;
115 class Statistics;
116 class ThreadSystem;
117 class Timer;
118 
119 typedef std::vector<PropertyPage*> PropertyPageStarVector;
120 
123  public:
124  StringPiece value() const;
125  bool has_value() const { return valid_; }
126 
129  int64 write_timestamp_ms() const;
130 
133  bool was_read() { return was_read_; }
134 
138  bool IsStable(int stable_hit_per_thousand_threshold) const;
139 
142  bool IsRecentlyConstant(int num_writes_unchanged) const;
143 
146  static bool IsIndexOfLeastSetBitSmaller(uint64 value, int index);
147 
148  private:
149  friend class PropertyCache;
150  friend class PropertyPage;
151 
153  PropertyValue();
154  ~PropertyValue();
155 
156  void set_was_read(bool was_read) { was_read_ = was_read; }
157 
159  void InitFromProtobuf(const PropertyValueProtobuf& value);
160 
167  void SetValue(const StringPiece& value, int64 now_ms);
168 
169  PropertyValueProtobuf* protobuf() { return proto_.get(); }
170 
171  scoped_ptr<PropertyValueProtobuf> proto_;
172  bool changed_;
173  bool valid_;
174  bool was_read_;
175 
176  DISALLOW_COPY_AND_ASSIGN(PropertyValue);
177 };
178 
181  public:
186  class Cohort {
187  public:
188  explicit Cohort(StringPiece name) {
189  name.CopyToString(&name_);
190  }
191  const GoogleString& name() const { return name_; }
192 
193  private:
194  GoogleString name_;
195 
196  DISALLOW_COPY_AND_ASSIGN(Cohort);
197  };
198 
199  typedef std::vector<const Cohort*> CohortVector;
200 
203  PropertyCache(PropertyStore* property_store,
204  Timer* timer,
205  Statistics* stats,
206  ThreadSystem* threads);
207  ~PropertyCache();
208 
212  void Read(PropertyPage* property_page) const;
213 
216  void ReadWithCohorts(const CohortVector& cohort_list,
217  PropertyPage* property_page) const;
218 
220  const CohortVector GetAllCohorts() const { return cohort_list_; }
221 
223  bool IsStable(const PropertyValue* property) const {
224  return property->IsStable(mutations_per_1000_writes_threshold_);
225  }
226 
235  bool IsExpired(const PropertyValue* property_value, int64 ttl_ms) const;
236 
237  void set_mutations_per_1000_writes_threshold(int x) {
238  mutations_per_1000_writes_threshold_ = x;
239  }
240 
243  const Cohort* AddCohort(const StringPiece& cohort_name);
244 
248  const Cohort* GetCohort(const StringPiece& cohort_name) const;
249 
253  void set_enabled(bool x) { enabled_ = x; }
254 
256  bool enabled() const { return enabled_; }
257 
259  static void InitCohortStats(const GoogleString& cohort,
260  Statistics* statistics);
261 
263  static GoogleString GetStatsPrefix(const GoogleString& cohort_name);
264 
266  Timer* timer() const { return timer_; }
267 
268  ThreadSystem* thread_system() const { return thread_system_; }
269 
270  PropertyStore* property_store() { return property_store_; }
271 
273 
274  private:
275  PropertyStore* property_store_;
276  Timer* timer_;
277  Statistics* stats_;
278  ThreadSystem* thread_system_;
279 
280  int mutations_per_1000_writes_threshold_;
281  typedef std::map<GoogleString, Cohort*> CohortMap;
282  CohortMap cohorts_;
284  CohortVector cohort_list_;
285  bool enabled_;
286 
287  DISALLOW_COPY_AND_ASSIGN(PropertyCache);
288 };
289 
292  public:
293  virtual ~AbstractPropertyPage();
296  virtual PropertyValue* GetProperty(
297  const PropertyCache::Cohort* cohort,
298  const StringPiece& property_name) = 0;
299 
302  virtual void UpdateValue(
303  const PropertyCache::Cohort* cohort, const StringPiece& property_name,
304  const StringPiece& value) = 0;
305 
309  virtual void WriteCohort(const PropertyCache::Cohort* cohort) = 0;
310 
312  virtual CacheInterface::KeyState GetCacheState(
313  const PropertyCache::Cohort* cohort) = 0;
314 
316  virtual void DeleteProperty(const PropertyCache::Cohort* cohort,
317  const StringPiece& property_name) = 0;
318 };
319 
320 
324  public:
326  enum PageType {
327  kPropertyCachePage,
328  kPropertyCacheFallbackPage,
329  kDevicePropertyCachePage,
330  };
331 
332  virtual ~PropertyPage();
333 
350  virtual PropertyValue* GetProperty(const PropertyCache::Cohort* cohort,
351  const StringPiece& property_name);
352 
355  virtual void UpdateValue(
356  const PropertyCache::Cohort* cohort, const StringPiece& property_name,
357  const StringPiece& value);
358 
365  virtual void WriteCohort(const PropertyCache::Cohort* cohort);
366 
371  CacheInterface::KeyState GetCacheState(const PropertyCache::Cohort* cohort);
372 
375  void SetCacheState(const PropertyCache::Cohort* cohort,
376  CacheInterface::KeyState x);
377 
387  void DeleteProperty(const PropertyCache::Cohort* cohort,
388  const StringPiece& property_name);
389 
390  AbstractLogRecord* log_record() {
391  return request_context_->log_record();
392  }
393 
395  void Read(const PropertyCache::CohortVector& cohort_list);
396 
398  void Abort();
399 
402  virtual bool IsCacheValid(int64 write_timestamp_ms) const { return true; }
403 
405  void AddValueFromProtobuf(const PropertyCache::Cohort* cohort,
406  const PropertyValueProtobuf& proto);
407 
409  PageType page_type() { return page_type_; }
410 
412  bool IsCohortPresent(const PropertyCache::Cohort* cohort);
413 
416  void FastFinishLookup();
417 
423  PropertyCacheValues* values);
424 
425  protected:
430  StringPiece url,
431  StringPiece options_signature_hash,
432  StringPiece cache_key_suffix,
433  const RequestContextPtr& request_context,
434  AbstractMutex* mutex,
435  PropertyCache* property_cache);
436 
438  virtual void Done(bool success) = 0;
439 
440  private:
441  void SetupCohorts(const PropertyCache::CohortVector& cohort_list);
442 
444  bool HasPropertyValueDeleted(const PropertyCache::Cohort* cohort);
445 
446  void CallDone(bool success) {
447  was_read_ = true;
448  Done(success);
449  }
450 
451  typedef std::map<GoogleString, PropertyValue*> PropertyMap;
452 
453  struct PropertyMapStruct {
454  explicit PropertyMapStruct(AbstractLogRecord* log)
455  : has_deleted_property(false),
456  log_record(log),
457  has_value(false) {}
458  PropertyMap pmap;
459  bool has_deleted_property;
460  AbstractLogRecord* log_record;
461  CacheInterface::KeyState cache_state;
462  bool has_value;
463  };
464  typedef std::map<const PropertyCache::Cohort*, PropertyMapStruct*>
465  CohortDataMap;
466  CohortDataMap cohort_data_map_;
467  scoped_ptr<AbstractMutex> mutex_;
468  GoogleString url_;
469  GoogleString options_signature_hash_;
470  GoogleString cache_key_suffix_;
471  RequestContextPtr request_context_;
472  bool was_read_;
473  PropertyCache* property_cache_;
474  AbstractPropertyStoreGetCallback* property_store_callback_;
479  PageType page_type_;
480 
481  DISALLOW_COPY_AND_ASSIGN(PropertyPage);
482 };
483 
484 }
485 
486 #endif
Abstract interface for implementing a PropertyPage.
Definition: property_cache.h:291
Holds the value & stability-metadata for a property.
Definition: property_cache.h:122
const Cohort * GetCohort(const StringPiece &cohort_name) const
Definition: property_cache.h:186
void Abort()
Abort the reading of PropertyPage.
PropertyCache(PropertyStore *property_store, Timer *timer, Statistics *stats, ThreadSystem *threads)
Adds property-semantics to a raw cache API.
Definition: property_cache.h:180
Definition: property_store.h:41
bool IsRecentlyConstant(int num_writes_unchanged) const
bool IsCohortPresent(const PropertyCache::Cohort *cohort)
Returns true if cohort present in the PropertyPage.
const CohortVector GetAllCohorts() const
Returns all the cohorts from cache.
Definition: property_cache.h:220
int64 write_timestamp_ms() const
PropertyPage(PageType page_type, StringPiece url, StringPiece options_signature_hash, StringPiece cache_key_suffix, const RequestContextPtr &request_context, AbstractMutex *mutex, PropertyCache *property_cache)
void Read(const PropertyCache::CohortVector &cohort_list)
Read the property page from cache.
Definition: log_record.h:61
virtual PropertyValue * GetProperty(const PropertyCache::Cohort *cohort, const StringPiece &property_name)=0
Definition: property_cache.h:323
bool IsStable(int stable_hit_per_thousand_threshold) const
bool IsStable(const PropertyValue *property) const
Determines whether a value that was read is reasonably stable.
Definition: property_cache.h:223
PageType
The cache type associated with this callback.
Definition: property_cache.h:326
void SetCacheState(const PropertyCache::Cohort *cohort, CacheInterface::KeyState x)
void Read(PropertyPage *property_page) const
void DeleteProperty(const PropertyCache::Cohort *cohort, const StringPiece &property_name)
virtual void Done(bool success)=0
Called as a result of PropertyCache::Read when the data is available.
bool IsExpired(const PropertyValue *property_value, int64 ttl_ms) const
PageType page_type()
Returns the type of the page.
Definition: property_cache.h:409
Timer * timer() const
Returns timer pointer.
Definition: property_cache.h:266
bool EncodePropertyCacheValues(const PropertyCache::Cohort *cohort, PropertyCacheValues *values)
const Cohort * AddCohort(const StringPiece &cohort_name)
static void InitCohortStats(const GoogleString &cohort, Statistics *statistics)
Initialize stats for the specified cohort.
static bool IsIndexOfLeastSetBitSmaller(uint64 value, int index)
bool enabled() const
Indicates if the property cache is enabled.
Definition: property_cache.h:256
virtual CacheInterface::KeyState GetCacheState(const PropertyCache::Cohort *cohort)=0
This function returns the cache state for a given cohort.
CacheInterface::KeyState GetCacheState(const PropertyCache::Cohort *cohort)
virtual void UpdateValue(const PropertyCache::Cohort *cohort, const StringPiece &property_name, const StringPiece &value)=0
void set_enabled(bool x)
Definition: property_cache.h:253
bool was_read()
Definition: property_cache.h:133
virtual void WriteCohort(const PropertyCache::Cohort *cohort)=0
void ReadWithCohorts(const CohortVector &cohort_list, PropertyPage *property_page) const
virtual void WriteCohort(const PropertyCache::Cohort *cohort)
virtual bool IsCacheValid(int64 write_timestamp_ms) const
Definition: property_cache.h:402
void AddValueFromProtobuf(const PropertyCache::Cohort *cohort, const PropertyValueProtobuf &proto)
Populate PropertyCacheValues to the respective cohort in PropertyPage.
virtual void DeleteProperty(const PropertyCache::Cohort *cohort, const StringPiece &property_name)=0
Deletes a property given the property name.
virtual void UpdateValue(const PropertyCache::Cohort *cohort, const StringPiece &property_name, const StringPiece &value)
virtual PropertyValue * GetProperty(const PropertyCache::Cohort *cohort, const StringPiece &property_name)
static GoogleString GetStatsPrefix(const GoogleString &cohort_name)
Creates stats prefix for the given cohort.