Class: Stat::Count::Client::StatCountClient

Inherits:
Object
  • Object
show all
Includes:
ConfigLoader, Data
Defined in:
lib/stat-count-client/hessian_client.rb

Constant Summary collapse

CONFIG =
ConfigLoader::CONFIG
HESSIAN_PACKAGE =
CONFIG['hessian.java.package'] || "com.ximalaya.stat.count.data"
@@logger =
LoggerFactory.getLogger("StatCountHessianClient")

Instance Method Summary collapse

Constructor Details

#initialize(hessian_url = nil) ⇒ StatCountClient

Returns a new instance of StatCountClient.



19
20
21
22
23
24
25
26
# File 'lib/stat-count-client/hessian_client.rb', line 19

def initialize(hessian_url=nil)
  @hessian_url = hessian_url;
  if (@hessian_url.nil?)
    @hessian_url ||= ENV['stat.count.domain']
    @hessian_url ||= CONFIG['hessian.domain']
  end
  init()
end

Instance Method Details

#decr(name, id, count = 1, date = nil) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/stat-count-client/hessian_client.rb', line 90

def decr(name, id, count=1, date=nil)
   if(date.nil?)
     @client.decrBy(name, id, count)
   else
     @client.decrByWithDate(name, id, count, date)
   end
end

#decrByCount(count) ⇒ Object



98
99
100
101
102
# File 'lib/stat-count-client/hessian_client.rb', line 98

def decrByCount(count)
  wrapper = to_hessian(count)
  count_result = @client.decrByCount(wrapper)
  CountResult.new(count_result)
end

#decrByCountWithDate(dateCount) ⇒ Object



104
105
106
107
# File 'lib/stat-count-client/hessian_client.rb', line 104

def decrByCountWithDate(dateCount)
  wrapper = to_hessian(dateCount)
  @client.decrByCountWithDate(wrapper)
end

#delByDateQuery(dateQuery) ⇒ Object



160
161
162
163
# File 'lib/stat-count-client/hessian_client.rb', line 160

def delByDateQuery(dateQuery)
  wrapper = to_hessian(dateQuery)
  @client.delByDateQuery(wrapper)
end

#delByQuery(query) ⇒ Object



155
156
157
158
# File 'lib/stat-count-client/hessian_client.rb', line 155

def delByQuery(query)
 wrapper = to_hessian(query)
 @client.delByQuery(wrapper)
end

#get(name, id, limit = 1) ⇒ Object



131
132
133
# File 'lib/stat-count-client/hessian_client.rb', line 131

def get(name, id, limit=1)
  @client.getByLimit(name, id, limit)
end

#getByDateQuery(dateQuery) ⇒ Object



149
150
151
152
153
# File 'lib/stat-count-client/hessian_client.rb', line 149

def getByDateQuery(dateQuery)
   wrapper = to_hessian(dateQuery)
   countResult = @client.getByDateQuery(wrapper)
   CountResult.new(countResult)
end

#getByIds(name, ids, limit = 1) ⇒ Object



139
140
141
# File 'lib/stat-count-client/hessian_client.rb', line 139

def getByIds(name, ids, limit=1)
  @client.getByIdsAndLimit(name, ids, limit)
end

#getByNames(name, id, limit = 1) ⇒ Object



135
136
137
# File 'lib/stat-count-client/hessian_client.rb', line 135

def getByNames(name, id, limit=1)
  @client.getByNamesAndLimit(name, id, limit)
end

#getByQuery(query) ⇒ Object



143
144
145
146
147
# File 'lib/stat-count-client/hessian_client.rb', line 143

def getByQuery(query)
    wrapper = to_hessian(query)
    countResult = @client.getByQuery(wrapper)
    CountResult.new(countResult)
end

#incr(name, id, count = 1, date = nil) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/stat-count-client/hessian_client.rb', line 82

def incr(name, id, count=1, date=nil)
  if(date.nil?)
    @client.incrBy(name,id, count)
  else
    @client.incrByWithDate(name, id, count, date)
  end
end

#incrByCount(count) ⇒ Object



76
77
78
79
80
# File 'lib/stat-count-client/hessian_client.rb', line 76

def incrByCount(count)
   wrapper = to_hessian(count)
   countResult = @client.incrByCount(wrapper)
   CountResult.new(countResult)
end

#incrByCountWithDate(dateCount) ⇒ Object



71
72
73
74
# File 'lib/stat-count-client/hessian_client.rb', line 71

def incrByCountWithDate(dateCount)
  wrapper = to_hessian(dateCount)
  @client.incrByCountWithDate(wrapper)
end

#java_class_name(java_package, clazz) ⇒ Object



54
55
56
57
58
59
# File 'lib/stat-count-client/hessian_client.rb', line 54

def java_class_name(java_package, clazz)
     index = clazz.name.rindex "::"
     index = (index.nil?) ?  0 : index+2
     simple_name = clazz.name[index..-1]
     java_package.nil? ? simple_name : java_package + "." + simple_name
end

#reset(name, id, limit = 1) ⇒ Object



127
128
129
# File 'lib/stat-count-client/hessian_client.rb', line 127

def reset(name, id, limit=1)
  @client.resetByLimit(name, id, limit)
end

#set(name, id, value, date = nil) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/stat-count-client/hessian_client.rb', line 119

def  set(name,id, value, date=nil)
  if(date.nil?)
    @client.set(name, id, value)
  else
    @client.setWithDate(name,id, value,date)
  end
end

#setByCount(count) ⇒ Object



114
115
116
117
# File 'lib/stat-count-client/hessian_client.rb', line 114

def setByCount(count)
  wrapper = to_hessian(count)
  @client.setByCount(wrapper)
end

#setByCountWithDate(dateCount) ⇒ Object



109
110
111
112
# File 'lib/stat-count-client/hessian_client.rb', line 109

def setByCountWithDate(dateCount)
  wrapper = to_hessian(dateCount)
  @client.setByCountWithDate(wrapper)
end

#to_hash_map(data) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/stat-count-client/hessian_client.rb', line 34

def to_hash_map(data)
     hash = {}
     data.instance_variables.each {|var|
       internal_hash = {}
       map = data.instance_variable_get(var)
       map.each { |key, list|
         newList = nil
         list.each { |unit|
           if (newList.nil?)
             newList = Array.new
           end
           newList << to_hessian_unit(unit)
         }
         internal_hash[key] = newList
       }
       hash[var.to_s.delete("@")] = internal_hash
     }
     hash
end

#to_hash_unit(data) ⇒ Object



28
29
30
31
32
# File 'lib/stat-count-client/hessian_client.rb', line 28

def to_hash_unit(data)
     hash = {}
     data.instance_variables.each {|var| hash[var.to_s.delete("@")] = data.instance_variable_get(var) }
     hash
end

#to_hessian(data) ⇒ Object



66
67
68
69
# File 'lib/stat-count-client/hessian_client.rb', line 66

def to_hessian(data)
  clazz = data.class
  Hessian2::TypeWrapper.new(java_class_name(HESSIAN_PACKAGE, clazz), to_hash_map(data))
end

#to_hessian_unit(data) ⇒ Object



61
62
63
64
# File 'lib/stat-count-client/hessian_client.rb', line 61

def to_hessian_unit(data)
  clazz = data.class
  Hessian2::TypeWrapper.new(java_class_name(HESSIAN_PACKAGE, clazz), to_hash_unit(data))
end