mitchellh-lightcloud 0.7 → 0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/lightcloud.rb +1 -1
  2. data/lib/tyrant_client.rb +117 -0
  3. metadata +2 -1
data/lib/lightcloud.rb CHANGED
@@ -35,7 +35,7 @@ require File.join(File.dirname(__FILE__), 'tyrant_client')
35
35
  # print LightCloud.get("hello") # => nil
36
36
  #
37
37
  class LightCloud
38
- VERSION = '0.7'
38
+ VERSION = '0.8'
39
39
  DEFAULT_SYSTEM = 'default'
40
40
  @@instance = nil
41
41
 
@@ -0,0 +1,117 @@
1
+ ######################################
2
+ # Tyrant Client
3
+ # Code ported from Python version written by Amir Salihefendic
4
+ ######################################
5
+ # Copyright (c) 2009, Mitchell Hashimoto, mitchell.hashimoto@gmail.com
6
+ #
7
+
8
+ require 'rubygems'
9
+ require 'hash_ring'
10
+ require 'rufus/tokyo'
11
+ require 'rufus/tokyo/tyrant'
12
+
13
+ # = Tyrant Client
14
+ #
15
+ # Manages many tyrant clients and exposes basic get/set/delete
16
+ # functionality for them.
17
+ #
18
+ class TyrantClient
19
+ @@connections = {}
20
+
21
+ #
22
+ # Initializes a TyrantClient. servers is expected to be an array
23
+ # of servers in form of "host:port"
24
+ def initialize(servers)
25
+ @servers = servers.collect do |server|
26
+ parts = server.split(':')
27
+ [parts[0], parts[1].to_i]
28
+ end
29
+ end
30
+
31
+ #--
32
+ # Get/Set/Delete
33
+ #++
34
+ def get(key)
35
+ db = self.get_db(key)
36
+
37
+ db[key]
38
+ end
39
+
40
+ def set(key, value)
41
+ db = self.get_db(key)
42
+
43
+ db[key] = value
44
+ self
45
+ end
46
+
47
+ def delete(key)
48
+ db = self.get_db(key)
49
+
50
+ begin
51
+ db.delete(key)
52
+ return true
53
+ rescue
54
+ return false
55
+ end
56
+ end
57
+
58
+ #--
59
+ # Helpers
60
+ #++
61
+ def get_db(key)
62
+ index = self.hash(key) % @servers.length
63
+ first_host, first_port = @servers[index]
64
+
65
+ begin
66
+ return self.get_connection(first_host, first_port)
67
+ rescue
68
+ # Didn't work, try out other servers
69
+ @servers.each do |server|
70
+ host, port = server
71
+
72
+ # The python code "continues" on error code 61. Not sure
73
+ # what that is so I'll ignore it here but TODO to go back
74
+ # and look at it
75
+ return self.get_connection(host, port)
76
+ end
77
+ end
78
+ end
79
+
80
+ def get_connection(host, port)
81
+ key = "#{host}#{port}"
82
+
83
+ return @@connections[key] unless @@connections[key].nil?
84
+
85
+ @@connections[key] = Rufus::Tokyo::Tyrant.new(host, port)
86
+ return @@connections[key]
87
+ end
88
+
89
+ def hash(key)
90
+ m = Digest::MD5.new
91
+ m.update(key)
92
+ b_key = m.digest
93
+
94
+ return ((b_key[7] << 24) |
95
+ (b_key[6] << 16) |
96
+ (b_key[5] << 8) |
97
+ (b_key[4]))
98
+ end
99
+ end
100
+
101
+ # = TyrantNode
102
+ #
103
+ # Extends TyrantClient with proper to_s functionality
104
+ class TyrantNode < TyrantClient
105
+ #
106
+ # Creates a TyrantNode given a name and a list of servers. The list
107
+ # of servers should be in the format for the constructor of
108
+ # TyrantClient
109
+ def initialize(name, nodes)
110
+ @name = name
111
+ super(nodes)
112
+ end
113
+
114
+ def to_s
115
+ @name
116
+ end
117
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mitchellh-lightcloud
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.7"
4
+ version: "0.8"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mitchell Hashimoto
@@ -47,6 +47,7 @@ files:
47
47
  - README.rdoc
48
48
  - Rakefile
49
49
  - lib/lightcloud.rb
50
+ - lib/tyrant_client.rb
50
51
  - spec/spec_base.rb
51
52
  - spec/lightcloud_spec.rb
52
53
  has_rdoc: true