jylis-rb 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +221 -0
  3. data/doc/Jylis.html +1087 -6
  4. data/doc/Jylis/Connection.html +1128 -0
  5. data/doc/Jylis/Connection/HostMissingError.html +135 -0
  6. data/doc/Jylis/Connection/UnsupportedSchemaError.html +135 -0
  7. data/doc/Jylis/DataType.html +135 -0
  8. data/doc/Jylis/DataType/Base.html +346 -0
  9. data/doc/Jylis/DataType/GCOUNT.html +331 -0
  10. data/doc/Jylis/DataType/GCOUNT/Result.html +132 -0
  11. data/doc/Jylis/DataType/MVREG.html +318 -0
  12. data/doc/Jylis/DataType/PNCOUNT.html +404 -0
  13. data/doc/Jylis/DataType/TLOG.html +750 -0
  14. data/doc/Jylis/DataType/TLOG/Result.html +640 -0
  15. data/doc/Jylis/DataType/TLOG/Row.html +616 -0
  16. data/doc/Jylis/DataType/TREG.html +345 -0
  17. data/doc/Jylis/DataType/TREG/Result.html +616 -0
  18. data/doc/Jylis/DataType/UJSON.html +549 -0
  19. data/doc/_index.html +185 -4
  20. data/doc/class_list.html +1 -1
  21. data/doc/file.README.html +223 -3
  22. data/doc/file.license.html +3 -3
  23. data/doc/frames.html +1 -1
  24. data/doc/index.html +223 -3
  25. data/doc/method_list.html +512 -0
  26. data/doc/top-level-namespace.html +3 -3
  27. data/lib/jylis-rb.rb +9 -0
  28. data/lib/jylis-rb/connection.rb +121 -0
  29. data/lib/jylis-rb/data_types/base.rb +24 -0
  30. data/lib/jylis-rb/data_types/gcount.rb +24 -0
  31. data/lib/jylis-rb/data_types/mvreg.rb +23 -0
  32. data/lib/jylis-rb/data_types/pncount.rb +33 -0
  33. data/lib/jylis-rb/data_types/tlog.rb +157 -0
  34. data/lib/jylis-rb/data_types/treg.rb +57 -0
  35. data/lib/jylis-rb/data_types/ujson.rb +87 -0
  36. data/lib/jylis-rb/jylis.rb +106 -0
  37. data/lib/jylis-rb/version.rb +1 -1
  38. metadata +39 -2
@@ -6,7 +6,7 @@
6
6
  <title>
7
7
  Top Level Namespace
8
8
 
9
- &mdash; Documentation by YARD 0.9.13
9
+ &mdash; Documentation by YARD 0.9.14
10
10
 
11
11
  </title>
12
12
 
@@ -100,9 +100,9 @@
100
100
  </div>
101
101
 
102
102
  <div id="footer">
103
- Generated on Sat Jun 2 13:02:48 2018 by
103
+ Generated on Sun Jun 3 21:46:59 2018 by
104
104
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
105
- 0.9.13 (ruby-2.5.1).
105
+ 0.9.14 (ruby-2.5.1).
106
106
  </div>
107
107
 
108
108
  </div>
@@ -1,2 +1,11 @@
1
1
  require_relative "jylis-rb/version"
2
2
  require_relative "jylis-rb/jylis"
3
+ require_relative "jylis-rb/connection"
4
+
5
+ require_relative "jylis-rb/data_types/base"
6
+ require_relative "jylis-rb/data_types/treg"
7
+ require_relative "jylis-rb/data_types/tlog"
8
+ require_relative "jylis-rb/data_types/gcount"
9
+ require_relative "jylis-rb/data_types/pncount"
10
+ require_relative "jylis-rb/data_types/mvreg"
11
+ require_relative "jylis-rb/data_types/ujson"
@@ -0,0 +1,121 @@
1
+ require "hiredis"
2
+
3
+ class Jylis
4
+ # A connection to the database.
5
+ class Connection
6
+ # The server URI schema is not supported.
7
+ class UnsupportedSchemaError < StandardError; end
8
+ # The host is missing from the server URI.
9
+ class HostMissingError < StandardError; end
10
+
11
+ # @param server_uri [URI, String] uri of the server to connect to
12
+ #
13
+ # @raise [UnsupportedSchemaError] if the server URI schema is not supported
14
+ # @raise [HostMissingError] if the host is missing from the server URI
15
+ def initialize(server_uri)
16
+ server_uri = URI.parse(server_uri) unless server_uri.is_a?(URI)
17
+
18
+ unless server_uri.scheme.downcase == "jylis"
19
+ raise UnsupportedSchemaError.new(
20
+ "#{server_uri.scheme} is not a supported schema"
21
+ )
22
+ end
23
+
24
+ unless server_uri.host
25
+ raise HostMissingError.new("No host specified")
26
+ end
27
+
28
+ @server_host = server_uri.host
29
+ @server_port = server_uri.port || 6379
30
+ @connection = Hiredis::Connection.new
31
+
32
+ connect
33
+ end
34
+
35
+ # @return [Boolean] true if a connection to the server is established
36
+ def connected?
37
+ @connection.connected?
38
+ end
39
+
40
+ # Reconnect to the server.
41
+ def reconnect
42
+ disconnect if connected?
43
+
44
+ connect
45
+ end
46
+
47
+ # Disconnect from the server.
48
+ def disconnect
49
+ @connection.disconnect
50
+ end
51
+
52
+ # Make a query to the database.
53
+ #
54
+ # @param args data type function args. Can be an args list or array.
55
+ #
56
+ # @return [Array] query response
57
+ #
58
+ # @see https://jemc.github.io/jylis/docs/types/
59
+ def query(*args)
60
+ if args.count == 1 && args.first.is_a?(Array)
61
+ args = *args.first
62
+ end
63
+
64
+ @connection.write(args)
65
+ @connection.read
66
+ end
67
+
68
+ # @!group Data Types
69
+
70
+ # TREG functions
71
+ #
72
+ # @return [Jylis::DataType::TREG]
73
+ def treg
74
+ @treg ||= Jylis::DataType::TREG.new(self)
75
+ end
76
+
77
+ # TLOG functions
78
+ #
79
+ # @return [Jylis::DataType::TLOG]
80
+ def tlog
81
+ @tlog ||= Jylis::DataType::TLOG.new(self)
82
+ end
83
+
84
+ # GCOUNT functions
85
+ #
86
+ # @return [Jylis::DataType::GCOUNT]
87
+ def gcount
88
+ @gcount ||= Jylis::DataType::GCOUNT.new(self)
89
+ end
90
+
91
+ # PNCOUNT functions
92
+ #
93
+ # @return [Jylis::DataType::PNCOUNT]
94
+ def pncount
95
+ @pncount ||= Jylis::DataType::PNCOUNT.new(self)
96
+ end
97
+
98
+ # MVREG functions
99
+ #
100
+ # @return [Jylis::DataType::MVREG]
101
+ def mvreg
102
+ @mvreg ||= Jylis::DataType::MVREG.new(self)
103
+ end
104
+
105
+ # UJSON functions
106
+ #
107
+ # @return [Jylis::DataType::UJSON]
108
+ def ujson
109
+ @ujson ||= Jylis::DataType::UJSON.new(self)
110
+ end
111
+
112
+ private
113
+
114
+ # Connect to the server.
115
+ def connect
116
+ @connection.connect(@server_host, @server_port)
117
+ end
118
+
119
+ # @!endgroup
120
+ end
121
+ end
@@ -0,0 +1,24 @@
1
+ class Jylis
2
+ # Namespace for Jylis data types.
3
+ #
4
+ # @see https://jemc.github.io/jylis/docs/types/
5
+ module DataType
6
+ # A base data type for others to inherit shared logic from.
7
+ # This class can't be instantiated directly.
8
+ class Base
9
+ # The Jylis::Connection to use for queries.
10
+ attr_reader :connection
11
+
12
+ # @param connection [Jylis::Connection] connection to use for queries
13
+ def initialize(connection)
14
+ if self.class == Base
15
+ raise "Base is an abstract class and must be inherited"
16
+ end
17
+
18
+ raise ArgumentError.new("Connection can't be nil") unless connection
19
+
20
+ @connection = connection
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ class Jylis
2
+ module DataType
3
+ # A grow-only counter.
4
+ #
5
+ # @see https://jemc.github.io/jylis/docs/types/gcount/
6
+ class GCOUNT < Base
7
+ # Get the resulting `value` for the counter at `key`.
8
+ #
9
+ # @return [Integer]
10
+ def get(key)
11
+ connection.query("GCOUNT", "GET", key)
12
+ end
13
+
14
+ # Increase the counter at `key` by the amount of `value`.
15
+ def inc(key, value)
16
+ result = connection.query("GCOUNT", "INC", key, value)
17
+
18
+ unless result == "OK"
19
+ raise "Failed: GCOUNT INC #{key} #{value}"
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ class Jylis
2
+ module DataType
3
+ # A multi-value register.
4
+ #
5
+ # @see https://jemc.github.io/jylis/docs/types/mvreg/
6
+ class MVREG < Base
7
+
8
+ # Get the latest value(s) for the register at `key`.
9
+ def get(key)
10
+ connection.query("MVREG", "GET", key)
11
+ end
12
+
13
+ # Set the latest `value` for the register at `key`.
14
+ def set(key, value)
15
+ result = connection.query("MVREG", "SET", key, value)
16
+
17
+ unless result == "OK"
18
+ raise "Failed: MVREG SET #{key} #{value}"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,33 @@
1
+ class Jylis
2
+ module DataType
3
+ # A positive/negative counter.
4
+ #
5
+ # @see https://jemc.github.io/jylis/docs/types/pncount/
6
+ class PNCOUNT < Base
7
+ # Get the resulting `value` for the counter at `key`.
8
+ #
9
+ # @return [Integer]
10
+ def get(key)
11
+ connection.query("PNCOUNT", "GET", key)
12
+ end
13
+
14
+ # Increase the counter at `key` by the amount of `value`.
15
+ def inc(key, value)
16
+ result = connection.query("PNCOUNT", "INC", key, value)
17
+
18
+ unless result == "OK"
19
+ raise "Failed: PNCOUNT INC #{key} #{value}"
20
+ end
21
+ end
22
+
23
+ # Decrease the counter at `key` by the amount of `value`.
24
+ def dec(key, value)
25
+ result = connection.query("PNCOUNT", "DEC", key, value)
26
+
27
+ unless result == "OK"
28
+ raise "Failed: PNCOUNT DEC #{key} #{value}"
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,157 @@
1
+ class Jylis
2
+ module DataType
3
+ # A timestamped log.
4
+ #
5
+ # @see https://jemc.github.io/jylis/docs/types/tlog/
6
+ class TLOG < Base
7
+ # The result of a TLOG query.
8
+ class Result
9
+ include Enumerable
10
+
11
+ # Construct a Result from a raw query result.
12
+ #
13
+ # @param query_result [Array]
14
+ #
15
+ # @return [Jylis::DataType::TLOG::Result]
16
+ def self.parse(query_result)
17
+ rows = query_result.reduce([]) do |memo, row|
18
+ memo << Row.parse(row)
19
+ memo
20
+ end
21
+
22
+ new(rows)
23
+ end
24
+
25
+ def initialize(rows)
26
+ @rows = rows
27
+ end
28
+
29
+ # @return [Jylis::DataType::TLOG::Row] the row at the given index
30
+ def [](index)
31
+ @rows[index]
32
+ end
33
+
34
+ # :no doc:
35
+ def each(&block)
36
+ @rows.each(&block)
37
+ end
38
+
39
+ # @return [Integer] number of rows
40
+ def count
41
+ @rows.count
42
+ end
43
+
44
+ # Reconstruct the raw result returned by the database.
45
+ def to_a
46
+ @rows.map(&:to_a)
47
+ end
48
+ end
49
+
50
+ # A row (data point) in a TLOG::Result.
51
+ class Row
52
+ attr_reader :value
53
+ attr_reader :timestamp
54
+
55
+ # Construct a Row from a raw query row.
56
+ #
57
+ # @param query_row [Array]
58
+ #
59
+ # @return [Jylis::DataType::TLOG::Row]
60
+ def self.parse(query_row)
61
+ new(query_row[0], query_row[1])
62
+ end
63
+
64
+ def initialize(value, timestamp)
65
+ @value = value
66
+ @timestamp = timestamp
67
+ end
68
+
69
+ # :nodoc:
70
+ def ==(other)
71
+ other.value == self.value &&
72
+ other.timestamp == self.timestamp
73
+ end
74
+
75
+ # Reconstruct the raw result returned by the database.
76
+ def to_a
77
+ [value, timestamp]
78
+ end
79
+ end
80
+
81
+ # Get the latest `value` and `timestamp` for the register at `key`.
82
+ #
83
+ # @return [Jylis::DataType::TLOG::Result]
84
+ def get(key, count = nil)
85
+ params = ["TLOG", "GET", key]
86
+
87
+ params.push(count) if count
88
+
89
+ result = connection.query(*params)
90
+
91
+ Result.parse(result)
92
+ end
93
+
94
+ # # Set a `value` and `timestamp` for the register at `key`.
95
+ # def set(key, value, timestamp)
96
+ # result = connection.query("TLOG", "SET", key, value, timestamp)
97
+
98
+ # unless result == "OK"
99
+ # raise "Failed: TLOG SET #{key} #{value} #{timestamp}"
100
+ # end
101
+ # end
102
+
103
+ # Insert a `value`/`timestamp` entry into the log at `key`.
104
+ def ins(key, value, timestamp)
105
+ result = connection.query("TLOG", "INS", key, value, timestamp)
106
+
107
+ unless result == "OK"
108
+ raise "Failed: TLOG INS #{key} #{value} #{timestamp}"
109
+ end
110
+ end
111
+
112
+ # @return [Integer] the number of entries in the log at `key`
113
+ def size(key)
114
+ connection.query("TLOG", "SIZE", key)
115
+ end
116
+
117
+ # @return [Integer] the current cutoff timestamp of the log at `key`
118
+ def cutoff(key)
119
+ connection.query("TLOG", "CUTOFF", key)
120
+ end
121
+
122
+ # Raise the cutoff timestamp of the log, causing any entries to be
123
+ # discarded whose timestamp is earlier than the newly given `timestamp`.
124
+ def trimat(key, timestamp)
125
+ result = connection.query("TLOG", "TRIMAT", key, timestamp)
126
+
127
+ unless result == "OK"
128
+ raise "Failed: TLOG TRIMAT #{key} #{timestamp}"
129
+ end
130
+ end
131
+
132
+ # Raise the cutoff timestamp of the log to retain at least `count`
133
+ # entries, by setting the cutoff timestamp to the timestamp of the entry
134
+ # at index `count - 1` in the log. Any entries with an earlier timestamp
135
+ # than the entry at that index will be discarded. If `count` is zero,
136
+ # this is the same as calling #clr.
137
+ def trim(key, count)
138
+ result = connection.query("TLOG", "TRIM", key, count)
139
+
140
+ unless result == "OK"
141
+ raise "Failed: TLOG TRIM #{key} #{count}"
142
+ end
143
+ end
144
+
145
+ # Raise the cutoff timestamp to be the timestamp of the latest entry plus
146
+ # one, such that all local entries in the log will be discarded due to
147
+ # having timestamps earlier than the cutoff timestamp.
148
+ def clr(key)
149
+ result = connection.query("TLOG", "CLR", key)
150
+
151
+ unless result == "OK"
152
+ raise "Failed: TLOG CLR #{key}"
153
+ end
154
+ end
155
+ end
156
+ end
157
+ end
@@ -0,0 +1,57 @@
1
+ class Jylis
2
+ module DataType
3
+ # A timestamped register.
4
+ #
5
+ # @see https://jemc.github.io/jylis/docs/types/treg/
6
+ class TREG < Base
7
+ # The result of a TREG query.
8
+ class Result
9
+ attr_reader :value
10
+ attr_reader :timestamp
11
+
12
+ # Construct a Result from a raw query result.
13
+ #
14
+ # @param query_result [Array]
15
+ #
16
+ # @return [Jylis::DataType::TREG::Result]
17
+ def self.parse(query_result)
18
+ new(query_result[0], query_result[1])
19
+ end
20
+
21
+ def initialize(value, timestamp)
22
+ @value = value
23
+ @timestamp = timestamp
24
+ end
25
+
26
+ # :nodoc:
27
+ def ==(other)
28
+ other.value == self.value &&
29
+ other.timestamp == self.timestamp
30
+ end
31
+
32
+ # Reconstruct the raw result returned by the database.
33
+ def to_a
34
+ [value, timestamp]
35
+ end
36
+ end
37
+
38
+ # Get the latest `value` and `timestamp` for the register at `key`.
39
+ #
40
+ # @return [Jylis::DataType::TREG::Result]
41
+ def get(key)
42
+ result = connection.query("TREG", "GET", key)
43
+
44
+ Result.parse(result)
45
+ end
46
+
47
+ # Set a `value` and `timestamp` for the register at `key`.
48
+ def set(key, value, timestamp)
49
+ result = connection.query("TREG", "SET", key, value, timestamp)
50
+
51
+ unless result == "OK"
52
+ raise "Failed: TREG SET #{key} #{value} #{timestamp}"
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end