skydb 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. data/README.md +4 -0
  2. data/lib/ext/string.rb +11 -0
  3. data/lib/skydb.rb +88 -0
  4. data/lib/skydb/action.rb +57 -0
  5. data/lib/skydb/client.rb +216 -0
  6. data/lib/skydb/event.rb +114 -0
  7. data/lib/skydb/message.rb +134 -0
  8. data/lib/skydb/message/add_action.rb +53 -0
  9. data/lib/skydb/message/add_event.rb +72 -0
  10. data/lib/skydb/message/add_property.rb +55 -0
  11. data/lib/skydb/message/get_action.rb +55 -0
  12. data/lib/skydb/message/get_actions.rb +34 -0
  13. data/lib/skydb/message/get_properties.rb +34 -0
  14. data/lib/skydb/message/get_property.rb +55 -0
  15. data/lib/skydb/message/lua/map_reduce.rb +59 -0
  16. data/lib/skydb/message/multi.rb +57 -0
  17. data/lib/skydb/message/next_actions.rb +55 -0
  18. data/lib/skydb/property.rb +93 -0
  19. data/lib/skydb/property/type.rb +40 -0
  20. data/lib/skydb/timestamp.rb +22 -0
  21. data/lib/skydb/version.rb +3 -0
  22. data/test/client_test.rb +71 -0
  23. data/test/event_test.rb +37 -0
  24. data/test/message/add_action_message_test.rb +34 -0
  25. data/test/message/add_event_message_test.rb +35 -0
  26. data/test/message/add_property_message_test.rb +41 -0
  27. data/test/message/get_action_message_test.rb +34 -0
  28. data/test/message/get_actions_message_test.rb +18 -0
  29. data/test/message/get_properties_message_test.rb +18 -0
  30. data/test/message/get_property_message_test.rb +34 -0
  31. data/test/message/lua_map_reduce_message_test.rb +19 -0
  32. data/test/message/multi_message_test.rb +22 -0
  33. data/test/message/next_action_message_test.rb +34 -0
  34. data/test/message_test.rb +15 -0
  35. data/test/skydb_test.rb +20 -0
  36. data/test/test_helper.rb +12 -0
  37. metadata +166 -0
@@ -0,0 +1,59 @@
1
+ class SkyDB
2
+ class Message
3
+ class Lua
4
+ class MapReduce < SkyDB::Message
5
+ ########################################################################
6
+ #
7
+ # Constructor
8
+ #
9
+ ########################################################################
10
+
11
+ # Initializes the 'lua::map_reduce' message.
12
+ #
13
+ # @param [String] source the Lua source to execute.
14
+ def initialize(source=nil, options={})
15
+ super('lua::map_reduce')
16
+ self.source = source
17
+ end
18
+
19
+
20
+ ##########################################################################
21
+ #
22
+ # Attributes
23
+ #
24
+ ##########################################################################
25
+
26
+ ##################################
27
+ # Source
28
+ ##################################
29
+
30
+ # The Lua source code.
31
+ attr_reader :source
32
+
33
+ def source=(value)
34
+ @source = value.to_s
35
+ end
36
+
37
+
38
+ ##########################################################################
39
+ #
40
+ # Methods
41
+ #
42
+ ##########################################################################
43
+
44
+ ####################################
45
+ # Encoding
46
+ ####################################
47
+
48
+ # Encodes the message body.
49
+ #
50
+ # @param [IO] buffer the buffer to write the header to.
51
+ def encode_body(buffer)
52
+ buffer << {
53
+ :source => source
54
+ }.to_msgpack
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,57 @@
1
+ class SkyDB
2
+ class Message
3
+ class Multi < SkyDB::Message
4
+ ########################################################################
5
+ #
6
+ # Constructor
7
+ #
8
+ ########################################################################
9
+
10
+ # Initializes the 'multi' message.
11
+ def initialize(options={})
12
+ super('multi')
13
+ self.messages = []
14
+ end
15
+
16
+
17
+ ##########################################################################
18
+ #
19
+ # Attributes
20
+ #
21
+ ##########################################################################
22
+
23
+ ##################################
24
+ # Messages
25
+ ##################################
26
+
27
+ # A list of message to send to the server.
28
+ attr_accessor :messages
29
+
30
+
31
+ ##########################################################################
32
+ #
33
+ # Methods
34
+ #
35
+ ##########################################################################
36
+
37
+ ####################################
38
+ # Encoding
39
+ ####################################
40
+
41
+ # Encodes the message body.
42
+ #
43
+ # @param [IO] buffer the buffer to write to.
44
+ def encode_body(buffer)
45
+ # Encode the message count.
46
+ buffer << {
47
+ :count => messages.length
48
+ }.to_msgpack
49
+
50
+ # Encode all the messages.
51
+ messages.each do |message|
52
+ message.encode(buffer)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,55 @@
1
+ class SkyDB
2
+ class Message
3
+ class NextActions < SkyDB::Message
4
+ ########################################################################
5
+ #
6
+ # Constructor
7
+ #
8
+ ########################################################################
9
+
10
+ # Initializes the 'next action' message.
11
+ #
12
+ # @param [Array] prior_action_ids the prior action ids.
13
+ def initialize(prior_action_ids=nil, options={})
14
+ super('next_actions')
15
+ self.prior_action_ids = prior_action_ids
16
+ end
17
+
18
+
19
+ ##########################################################################
20
+ #
21
+ # Attributes
22
+ #
23
+ ##########################################################################
24
+
25
+ ##################################
26
+ # Prior Action IDs
27
+ ##################################
28
+
29
+ # The prior action ids.
30
+ attr_reader :prior_action_ids
31
+
32
+ def prior_action_ids=(value)
33
+ @prior_action_ids = value.is_a?(Array) ? value : []
34
+ end
35
+
36
+
37
+ ##########################################################################
38
+ #
39
+ # Methods
40
+ #
41
+ ##########################################################################
42
+
43
+ ####################################
44
+ # Encoding
45
+ ####################################
46
+
47
+ # Encodes the message body.
48
+ #
49
+ # @param [IO] buffer the buffer to write the header to.
50
+ def encode_body(buffer)
51
+ buffer << prior_action_ids.to_msgpack
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,93 @@
1
+ require 'skydb/property/type'
2
+
3
+ class SkyDB
4
+ class Property
5
+ ##########################################################################
6
+ #
7
+ # Constructor
8
+ #
9
+ ##########################################################################
10
+
11
+ # Initializes the property.
12
+ def initialize(id=0, type=nil, data_type="String", name=nil)
13
+ self.id = id
14
+ self.type = type
15
+ self.data_type = data_type
16
+ self.name = name
17
+ end
18
+
19
+
20
+ ##########################################################################
21
+ #
22
+ # Attributes
23
+ #
24
+ ##########################################################################
25
+
26
+ ##################################
27
+ # ID
28
+ ##################################
29
+
30
+ # The property identifier.
31
+ attr_reader :id
32
+
33
+ def id=(value)
34
+ @id = value.to_i
35
+ end
36
+
37
+
38
+ ##################################
39
+ # Type
40
+ ##################################
41
+
42
+ # The property type. This can be either :object or :action.
43
+ attr_reader :type
44
+
45
+ def type=(value)
46
+ value = :object unless [:object, :action].index(value)
47
+ @type = value
48
+ end
49
+
50
+
51
+ ##################################
52
+ # Data Type
53
+ ##################################
54
+
55
+ # The property data type. This can be either 'String', 'Int', 'Float',
56
+ # or 'Boolean'.
57
+ attr_reader :data_type
58
+
59
+ def data_type=(value)
60
+ value = nil unless ['String', 'Int', 'Float', 'Boolean'].index(value)
61
+ @data_type = value
62
+ end
63
+
64
+
65
+ ##################################
66
+ # Name
67
+ ##################################
68
+
69
+ # The name of the property.
70
+ attr_reader :name
71
+
72
+ def name=(value)
73
+ @name = value.to_s
74
+ end
75
+
76
+
77
+ ##########################################################################
78
+ #
79
+ # Methods
80
+ #
81
+ ##########################################################################
82
+
83
+ # Encodes the property into MsgPack format.
84
+ def to_msgpack
85
+ return {
86
+ id:id,
87
+ type:SkyDB::Property::Type.encode(type),
88
+ dataType:data_type,
89
+ name:name
90
+ }.to_msgpack
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,40 @@
1
+ class SkyDB
2
+ class Property
3
+ class Type
4
+ ########################################################################
5
+ #
6
+ # Constants
7
+ #
8
+ ########################################################################
9
+
10
+ OBJECT = 1
11
+
12
+ ACTION = 2
13
+
14
+
15
+ ########################################################################
16
+ #
17
+ # Static Methods
18
+ #
19
+ ########################################################################
20
+
21
+ # Encodes the type into it's numeric enum value.
22
+ #
23
+ # @param type [Symbol] the type.
24
+ # @return [Fixnum] the enum value for the property type.
25
+ def self.encode(type)
26
+ return nil unless [:object, :action].index(type)
27
+ return type == :object ? OBJECT : ACTION
28
+ end
29
+
30
+ # Decodes the numeric enum value into its symbol.
31
+ #
32
+ # @param value [Fixnum] the numeric enum value.
33
+ # @return [Symbol] the type symbol.
34
+ def self.decode(value)
35
+ return nil unless [OBJECT, ACTION].index(value)
36
+ return value == OBJECT ? :object : :action
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,22 @@
1
+ class SkyDB
2
+ class Timestamp
3
+ ##########################################################################
4
+ #
5
+ # Static Methods
6
+ #
7
+ ##########################################################################
8
+
9
+ # Converts a Time object to a Sky timestamp value.
10
+ #
11
+ # @param [Time] time the time object.
12
+ #
13
+ # @return [Fixnum] the number of microseconds since the epoch (1970-01-01T00:00:00.000000Z).
14
+ def self.to_timestamp(time)
15
+ if time.nil?
16
+ return nil
17
+ else
18
+ return time.to_time.utc.to_i * 1_000_000
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ class SkyDB
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,71 @@
1
+ # encoding: binary
2
+ require 'test_helper'
3
+
4
+ class TestClient < MiniTest::Unit::TestCase
5
+ def setup
6
+ @client = SkyDB::Client.new(host:'127.0.0.1', port:9000)
7
+ @message = mock('message')
8
+ end
9
+
10
+ ######################################
11
+ # Action Messages
12
+ ######################################
13
+
14
+ def test_add_action
15
+ action = SkyDB::Action.new
16
+ @client.expects(:send_message).with(is_a(SkyDB::Message::AddAction))
17
+ @client.add_action(action)
18
+ end
19
+
20
+ def test_get_action
21
+ @client.expects(:send_message).with(is_a(SkyDB::Message::GetAction))
22
+ @client.get_action(12)
23
+ end
24
+
25
+ def test_get_actions
26
+ @client.expects(:send_message).with(is_a(SkyDB::Message::GetActions))
27
+ @client.get_actions
28
+ end
29
+
30
+
31
+ ######################################
32
+ # Property Messages
33
+ ######################################
34
+
35
+ def test_add_property
36
+ property = SkyDB::Property.new
37
+ @client.expects(:send_message).with(is_a(SkyDB::Message::AddProperty))
38
+ @client.add_property(property)
39
+ end
40
+
41
+ def test_get_property
42
+ @client.expects(:send_message).with(is_a(SkyDB::Message::GetProperty))
43
+ @client.get_property(12)
44
+ end
45
+
46
+ def test_get_properties
47
+ @client.expects(:send_message).with(is_a(SkyDB::Message::GetProperties))
48
+ @client.get_properties
49
+ end
50
+
51
+
52
+ ######################################
53
+ # Event Messages
54
+ ######################################
55
+
56
+ def test_add_event
57
+ event = SkyDB::Event.new
58
+ @client.expects(:send_message).with(is_a(SkyDB::Message::AddEvent))
59
+ @client.add_event(event)
60
+ end
61
+
62
+
63
+ ######################################
64
+ # Query Messages
65
+ ######################################
66
+
67
+ def test_next_actions
68
+ @client.expects(:send_message).with(is_a(SkyDB::Message::NextActions))
69
+ @client.next_actions([1, 2])
70
+ end
71
+ end
@@ -0,0 +1,37 @@
1
+ # encoding: binary
2
+ require 'test_helper'
3
+
4
+ class TestEvent < MiniTest::Unit::TestCase
5
+ def setup
6
+ @event = SkyDB::Event.new()
7
+ end
8
+
9
+ ######################################
10
+ # Object ID
11
+ ######################################
12
+
13
+ def test_object_id
14
+ @event.object_id = 12
15
+ assert_equal 12, @event.object_id
16
+ end
17
+
18
+ def test_invalid_object_id
19
+ @event.object_id = "foo"
20
+ assert_equal 0, @event.object_id
21
+ end
22
+
23
+
24
+ ######################################
25
+ # Timestamp
26
+ ######################################
27
+
28
+ def test_timestamp_with_time
29
+ @event.timestamp = Time.now
30
+ refute_nil @event.timestamp
31
+ end
32
+
33
+ def test_invalid_timestamp
34
+ @event.object_id = "foo"
35
+ assert_nil @event.timestamp
36
+ end
37
+ end