Page Speed Optimization Libraries  1.13.35.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 PAGESPEED_OPT_HTTP_PROPERTY_CACHE_H_
93 #define PAGESPEED_OPT_HTTP_PROPERTY_CACHE_H_
94 
95 #include <map>
96 #include <vector>
97 
103 #include "pagespeed/kernel/cache/cache_interface.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 
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 
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 
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 
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  kPropertyCachePerOriginPage,
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 
372 
375  void SetCacheState(const PropertyCache::Cohort* cohort,
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 
426  static StringPiece PageTypeSuffix(PageType type);
427 
428  protected:
433  StringPiece url,
434  StringPiece options_signature_hash,
435  StringPiece cache_key_suffix,
436  const RequestContextPtr& request_context,
437  AbstractMutex* mutex,
438  PropertyCache* property_cache);
439 
441  virtual void Done(bool success) = 0;
442 
443  private:
444  void SetupCohorts(const PropertyCache::CohortVector& cohort_list);
445 
447  bool HasPropertyValueDeleted(const PropertyCache::Cohort* cohort);
448 
449  void CallDone(bool success) {
450  was_read_ = true;
451  Done(success);
452  }
453 
454  typedef std::map<GoogleString, PropertyValue*> PropertyMap;
455 
456  struct PropertyMapStruct {
457  explicit PropertyMapStruct(AbstractLogRecord* log)
458  : has_deleted_property(false),
459  log_record(log),
460  has_value(false) {}
461  PropertyMap pmap;
462  bool has_deleted_property;
463  AbstractLogRecord* log_record;
464  CacheInterface::KeyState cache_state;
465  bool has_value;
466  };
467  typedef std::map<const PropertyCache::Cohort*, PropertyMapStruct*>
468  CohortDataMap;
469  CohortDataMap cohort_data_map_;
470  scoped_ptr<AbstractMutex> mutex_;
471  GoogleString url_;
472  GoogleString options_signature_hash_;
473  GoogleString cache_key_suffix_;
474  RequestContextPtr request_context_;
475  bool was_read_;
476  PropertyCache* property_cache_;
477  AbstractPropertyStoreGetCallback* property_store_callback_;
482  PageType page_type_;
483 
484 
485 };
486 
487 }
488 
489 #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.
Base class for implementations of monitoring statistics.
Definition: statistics.h:342
Abstract interface for implementing a mutex.
Definition: abstract_mutex.h:28
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:59
virtual PropertyValue * GetProperty(const PropertyCache::Cohort *cohort, const StringPiece &property_name)=0
Definition: property_cache.h:323
KeyState
Definition: cache_interface.h:34
const CohortVector & GetAllCohorts() const
Returns all the cohorts from cache.
Definition: property_cache.h:220
bool IsStable(int stable_hit_per_thousand_threshold) const
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
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
virtual AbstractLogRecord * log_record()
The log record for the this request, created when the request context is.
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
Definition: thread_system.h:40
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
static StringPiece PageTypeSuffix(PageType type)
Suffix for property cache keys for given page type.
Timer interface, made virtual so it can be mocked for tests.
Definition: timer.h:27
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.