proxygen
folly::settings::Snapshot Class Referencefinal

#include <Settings.h>

Inheritance diagram for folly::settings::Snapshot:

Public Member Functions

template<class T , std::atomic< uint64_t > * P>
detail::SnapshotSettingWrapper< Toperator() (detail::SettingWrapper< T, P > &&setting)
 
 Snapshot ()=default
 
void publish () override
 
bool setFromString (StringPiece settingName, StringPiece newValue, StringPiece reason) override
 
Optional< SettingsInfo > getAsString (StringPiece settingName) const override
 
bool resetToDefault (StringPiece settingName) override
 
void forEachSetting (const std::function< void(const SettingMetadata &, StringPiece, StringPiece)> &func) const override
 

Friends

template<typename T >
class detail::SnapshotSettingWrapper
 

Detailed Description

Captures the current state of all setting values and allows updating multiple settings at once, with verification and rollback.

A single snapshot cannot be used concurrently from different threads. Multiple concurrent snapshots are safe. Passing a single snapshot from one thread to another is safe as long as the user properly synchronizes the handoff.

Example usage:

folly::settings::Snapshot snapshot; // FOLLY_SETTING(project, name) refers to the globally visible value // snapshot(FOLLY_SETTING(project, name)) refers to the value saved in the // snapshot FOLLY_SETTING(project, name).set(new_value); assert(*FOLLY_SETTING(project, name) == new_value); assert(*snapshot(FOLLY_SETTING(project, name)) == old_value);

snapshot(FOLLY_SETTING(project, name)).set(new_snapshot_value); assert(*FOLLY_SETTING(project, name) == new_value); assert(*snapshot(FOLLY_SETTING(project, name)) == new_snapshot_value);

// At this point we can discard the snapshot and forget new_snapshot_value, // or choose to publish: snapshot.publish(); assert(*FOLLY_SETTING(project, name) == new_snapshot_value);

Definition at line 267 of file Settings.h.

Constructor & Destructor Documentation

folly::settings::Snapshot::Snapshot ( )
default

Returns a snapshot of all current setting values. Global settings changes will not be visible in the snapshot, and vice versa.

Member Function Documentation

void folly::settings::Snapshot::forEachSetting ( const std::function< void(const SettingMetadata &, StringPiece, StringPiece)> &  func) const
override

Iterates over all known settings and calls func(meta, to<string>(value), reason) for each.

Definition at line 95 of file Settings.cpp.

References map(), and folly::value().

Referenced by TEST().

97  {
98  detail::SettingsMap map;
99  /* Note that this won't hold the lock over the callback, which is
100  what we want since the user might call other settings:: APIs */
101  map = *detail::settingsMap().rlock();
102  for (const auto& kv : map) {
103  auto value = kv.second->getAsString(this);
104  func(kv.second->meta(), value.first, value.second);
105  }
106 }
static Map map(mapCap)
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
Optional< Snapshot::SettingsInfo > folly::settings::Snapshot::getAsString ( StringPiece  settingName) const
override
Returns
If the setting exists, the current setting information. Empty Optional otherwise.

Definition at line 75 of file Settings.cpp.

References folly::none, and folly::Range< Iter >::str().

Referenced by TEST().

76  {
77  auto mapPtr = detail::settingsMap().rlock();
78  auto it = mapPtr->find(settingName.str());
79  if (it == mapPtr->end()) {
80  return none;
81  }
82  return it->second->getAsString(this);
83 }
constexpr None none
Definition: Optional.h:87
template<class T , std::atomic< uint64_t > * P>
detail::SnapshotSettingWrapper<T> folly::settings::Snapshot::operator() ( detail::SettingWrapper< T, P > &&  setting)
inline

Wraps a global FOLLY_SETTING(a, b) and returns a snapshot-local wrapper.

Definition at line 273 of file Settings.h.

274  {
275  return detail::SnapshotSettingWrapper<T>(*this, setting.core_);
276  }
void folly::settings::Snapshot::publish ( )
override

Apply all settings updates from this snapshot to the global state unconditionally.

Definition at line 176 of file Settings.cpp.

Referenced by TEST().

176  {
177  for (auto& it : snapshotValues_) {
178  it.second.publish();
179  }
180 }
bool folly::settings::Snapshot::resetToDefault ( StringPiece  settingName)
override

Reset the value of the setting identified by name to its default value. The reason will be set to "default".

Returns
True if the setting was reset, false if the setting is not found.

Definition at line 85 of file Settings.cpp.

References folly::Range< Iter >::str().

Referenced by TEST().

85  {
86  auto mapPtr = detail::settingsMap().rlock();
87  auto it = mapPtr->find(settingName.str());
88  if (it == mapPtr->end()) {
89  return false;
90  }
91  it->second->resetToDefault(this);
92  return true;
93 }
bool folly::settings::Snapshot::setFromString ( StringPiece  settingName,
StringPiece  newValue,
StringPiece  reason 
)
override

Look up a setting by name, and update the value from a string representation.

Returns
True if the setting was successfully updated, false if no setting with that name was found.
Exceptions
std::runtime_errorIf there's a conversion error.

Definition at line 62 of file Settings.cpp.

References folly::Range< Iter >::str().

Referenced by TEST().

65  {
66  auto mapPtr = detail::settingsMap().rlock();
67  auto it = mapPtr->find(settingName.str());
68  if (it == mapPtr->end()) {
69  return false;
70  }
71  it->second->setFromString(newValue, reason, this);
72  return true;
73 }

Friends And Related Function Documentation

template<typename T >
friend class detail::SnapshotSettingWrapper
friend

Definition at line 328 of file Settings.h.


The documentation for this class was generated from the following files: