2
* Copyright 2013 Google Inc.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
// Author: pulkitg@google.com (Pulkit Goyal)
19
// Retrieves property values stored in the storage system and populate them
20
// in PropertyPage after validation of the properties.
21
#ifndef NET_INSTAWEB_UTIL_PUBLIC_PROPERTY_STORE_H_
22
#define NET_INSTAWEB_UTIL_PUBLIC_PROPERTY_STORE_H_
24
#include "net/instaweb/util/public/abstract_property_store_get_callback.h"
25
#include "net/instaweb/util/public/property_cache.h"
26
#include "net/instaweb/util/public/string.h"
27
#include "pagespeed/kernel/base/basictypes.h"
28
#include "pagespeed/kernel/base/callback.h"
29
#include "pagespeed/kernel/base/scoped_ptr.h"
31
namespace net_instaweb {
34
class PropertyCacheValues;
35
class PropertyValueProtobuf;
39
// Abstract interface for implementing PropertyStore which helps to
40
// retrieve and put properties into the storage system.
43
typedef Callback1<bool> BoolCallback;
45
virtual ~PropertyStore();
47
// Populates the values field for all the cohorts present in the cohort_list
48
// and call the BoolCallback after lookup of all the cohorts are done.
49
// BoolCallback is called with true if at least one of the cohorts lookup is
51
// PropertyPage object is used to validate the entries looked up from cache.
52
// AbstractPropertyStoreGetCallback is set in callback parameter and can be
53
// used to fast finish the lookup. Client must call DeleteWhenDone() on this
54
// callback after that it is no more usable. This parameter can be set to
57
const GoogleString& url,
58
const GoogleString& options_signature_hash,
59
const GoogleString& cache_key_suffix,
60
const PropertyCache::CohortVector& cohort_list,
63
AbstractPropertyStoreGetCallback** callback) = 0;
65
// Write to storage system for the given key.
66
// Callback done can be NULL. BoolCallback done will be called with true if
67
// Insert operation is successful.
68
// TODO(pulkitg): Remove UserAgentMatcher dependency.
70
const GoogleString& url,
71
const GoogleString& options_signature_hash,
72
const GoogleString& cache_key_suffix,
73
const PropertyCache::Cohort* cohort,
74
const PropertyCacheValues* values,
75
BoolCallback* done) = 0;
77
// PropertyStore::Get can be cancelled if enable_get_cancellation is true
78
// i.e. input done callback will be called as soon as FastFinishLookup() is
79
// called on the AbstractPropertyStoreGetCallback callback.
80
bool enable_get_cancellation() { return enable_get_cancellation_; }
81
void set_enable_get_cancellation(bool x) { enable_get_cancellation_ = x; }
83
// The name of this PropertyStore -- used for logging and debugging.
85
// It is strongly recommended that you provide a static GoogleString
86
// FormatName(...) method for use in formatting the Name() return,
87
// and in testing, e.g. in net/instaweb/system/system_caches_test.cc.
88
virtual GoogleString Name() const = 0;
91
bool enable_get_cancellation_;
92
DISALLOW_COPY_AND_ASSIGN(PropertyStore);
95
// This class manages the lookup for the properties in PropertyStore. It works
96
// in two mode: Cancellable mode and Non-Cancellable mode.
97
// Non-Cancellable Mode:
98
// - FastFinishLookup() has no-op in this mode.
99
// - Done() will be called whenever lookup finishes and calls the
100
// done callback based on sucess of the lookup.
101
// - DeleteWhenDone() will delete the callback if Done() is already called or
102
// set the bit so that callback delete itself after executing Done().
104
// - FastFinishLookup() will call the done callback if its not yet called.
105
// - Done() is same as that in non-cancellable mode but if FastFinishLookup()
106
// is called then it will not call the done callback.
107
// - DeleteWhenDone() works same as it works in non-cancellable mode.
108
class PropertyStoreGetCallback : public AbstractPropertyStoreGetCallback {
110
typedef Callback1<bool> BoolCallback;
111
PropertyStoreGetCallback(
112
AbstractMutex* mutex,
117
virtual ~PropertyStoreGetCallback();
119
static void InitStats(Statistics* statistics);
121
// Try to finish all the pending lookups if possible and call Done as soon as
123
virtual void FastFinishLookup();
124
// Deletes the callback after done finishes.
125
virtual void DeleteWhenDone();
126
// Add the given property cache value to the PropertyPage if PropertyPage is
128
// Returns true if PropertyValueProtobuf is successfully added to
130
bool AddPropertyValueProtobufToPropertyPage(
131
const PropertyCache::Cohort* cohort,
132
const PropertyValueProtobuf& pcache_value,
133
int64 min_write_timestamp_ms);
135
// Done is called when lookup is finished. This method is made public so that
136
// PropertyStore implementations may call it.
137
void Done(bool success);
140
AbstractMutex* mutex() { return mutex_.get(); }
141
PropertyPage* page() { return page_; }
144
scoped_ptr<AbstractMutex> mutex_;
146
const bool is_cancellable_;
148
bool delete_when_done_;
151
int64 fast_finish_time_ms_;
153
DISALLOW_COPY_AND_ASSIGN(PropertyStoreGetCallback);
156
} // namespace net_instaweb
157
#endif // NET_INSTAWEB_UTIL_PUBLIC_PROPERTY_STORE_H_