Subversion Repositories SmartDukaan

Rev

Rev 30 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
/*
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
 */
19
 
20
#include "FacebookBase.h"
21
 
22
using namespace facebook::fb303;
23
using apache::thrift::concurrency::Guard;
24
 
25
FacebookBase::FacebookBase(std::string name) :
26
  name_(name) {
27
  aliveSince_ = (int64_t) time(NULL);
28
}
29
 
30
inline void FacebookBase::getName(std::string& _return) {
31
  _return = name_;
32
}
33
 
34
void FacebookBase::setOption(const std::string& key, const std::string& value) {
35
  Guard g(optionsLock_);
36
  options_[key] = value;
37
}
38
 
39
void FacebookBase::getOption(std::string& _return, const std::string& key) {
40
  Guard g(optionsLock_);
41
  _return = options_[key];
42
}
43
 
44
void FacebookBase::getOptions(std::map<std::string, std::string> & _return) {
45
  Guard g(optionsLock_);
46
  _return = options_;
47
}
48
 
49
int64_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
 
76
int64_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
 
96
void 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();
101
      it != counters_.end(); it++)
102
  {
103
    _return[it->first] = it->second.value;
104
  }
105
  counters_.release();
106
}
107
 
108
int64_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
 
121
inline int64_t FacebookBase::aliveSince() {
122
  return aliveSince_;
123
}
124