blob: 3c569759c0b078df3c39c63797311232b08ad33d [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
pwyckoff99b000b2008-04-03 19:30:55 +000019
20#include "FacebookBase.h"
21
22using namespace facebook::fb303;
David Reiss858519a2009-02-07 02:36:50 +000023using apache::thrift::concurrency::Guard;
pwyckoff99b000b2008-04-03 19:30:55 +000024
David Reiss0b7d6fa2009-02-07 02:36:35 +000025FacebookBase::FacebookBase(std::string name) :
pwyckoff99b000b2008-04-03 19:30:55 +000026 name_(name) {
27 aliveSince_ = (int64_t) time(NULL);
pwyckoff99b000b2008-04-03 19:30:55 +000028}
29
30inline void FacebookBase::getName(std::string& _return) {
31 _return = name_;
32}
33
34void FacebookBase::setOption(const std::string& key, const std::string& value) {
35 Guard g(optionsLock_);
36 options_[key] = value;
37}
38
39void FacebookBase::getOption(std::string& _return, const std::string& key) {
40 Guard g(optionsLock_);
41 _return = options_[key];
42}
43
44void FacebookBase::getOptions(std::map<std::string, std::string> & _return) {
45 Guard g(optionsLock_);
46 _return = options_;
47}
48
49int64_t FacebookBase::incrementCounter(const std::string& key, int64_t amount) {
50 counters_.acquireRead();
51
52 // if we didn't find the key, we need to write lock the whole map to create it
53 ReadWriteCounterMap::iterator it = counters_.find(key);
54 if (it == counters_.end()) {
55 counters_.release();
56 counters_.acquireWrite();
57
58 // we need to check again to make sure someone didn't create this key
59 // already while we released the lock
60 it = counters_.find(key);
61 if(it == counters_.end()){
62 counters_[key].value = amount;
63 counters_.release();
64 return amount;
65 }
66 }
67
68 it->second.acquireWrite();
69 int64_t count = it->second.value + amount;
70 it->second.value = count;
71 it->second.release();
72 counters_.release();
73 return count;
74}
75
76int64_t FacebookBase::setCounter(const std::string& key, int64_t value) {
77 counters_.acquireRead();
78
79 // if we didn't find the key, we need to write lock the whole map to create it
80 ReadWriteCounterMap::iterator it = counters_.find(key);
81 if (it == counters_.end()) {
82 counters_.release();
83 counters_.acquireWrite();
84 counters_[key].value = value;
85 counters_.release();
86 return value;
87 }
88
89 it->second.acquireWrite();
90 it->second.value = value;
91 it->second.release();
92 counters_.release();
93 return value;
94}
95
96void FacebookBase::getCounters(std::map<std::string, int64_t>& _return) {
97 // we need to lock the whole thing and actually build the map since we don't
98 // want our read/write structure to go over the wire
99 counters_.acquireRead();
100 for(ReadWriteCounterMap::iterator it = counters_.begin();
Roger Meier71f2d8a2015-04-26 17:00:04 +0200101 it != counters_.end(); ++it)
pwyckoff99b000b2008-04-03 19:30:55 +0000102 {
103 _return[it->first] = it->second.value;
104 }
105 counters_.release();
106}
107
108int64_t FacebookBase::getCounter(const std::string& key) {
109 int64_t rv = 0;
110 counters_.acquireRead();
111 ReadWriteCounterMap::iterator it = counters_.find(key);
112 if (it != counters_.end()) {
113 it->second.acquireRead();
114 rv = it->second.value;
115 it->second.release();
116 }
117 counters_.release();
118 return rv;
119}
120
121inline int64_t FacebookBase::aliveSince() {
122 return aliveSince_;
123}
124