hirlite 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/LICENSE +28 -0
- data/Rakefile +51 -0
- data/ext/hirlite_ext/extconf.rb +33 -0
- data/ext/hirlite_ext/hirlite_ext.c +14 -0
- data/ext/hirlite_ext/hirlite_ext.h +38 -0
- data/ext/hirlite_ext/rlite.c +351 -0
- data/lib/hirlite/rlite.rb +1 -0
- data/lib/hirlite/version.rb +3 -0
- data/lib/hirlite.rb +2 -0
- data/vendor/rlite/Makefile +6 -0
- data/vendor/rlite/deps/crc64.c +191 -0
- data/vendor/rlite/deps/crc64.h +3 -0
- data/vendor/rlite/deps/endianconv.h +73 -0
- data/vendor/rlite/deps/hyperloglog.c +1547 -0
- data/vendor/rlite/deps/hyperloglog.h +14 -0
- data/vendor/rlite/deps/lzf.h +100 -0
- data/vendor/rlite/deps/lzfP.h +159 -0
- data/vendor/rlite/deps/lzf_c.c +295 -0
- data/vendor/rlite/deps/lzf_d.c +150 -0
- data/vendor/rlite/deps/sha1.c +227 -0
- data/vendor/rlite/deps/sha1.h +19 -0
- data/vendor/rlite/deps/utilfromredis.c +397 -0
- data/vendor/rlite/deps/utilfromredis.h +11 -0
- data/vendor/rlite/src/Makefile +79 -0
- data/vendor/rlite/src/constants.h +15 -0
- data/vendor/rlite/src/dump.c +191 -0
- data/vendor/rlite/src/dump.h +3 -0
- data/vendor/rlite/src/hirlite.c +3985 -0
- data/vendor/rlite/src/hirlite.h +186 -0
- data/vendor/rlite/src/page_btree.c +1556 -0
- data/vendor/rlite/src/page_btree.h +133 -0
- data/vendor/rlite/src/page_key.c +283 -0
- data/vendor/rlite/src/page_key.h +25 -0
- data/vendor/rlite/src/page_list.c +718 -0
- data/vendor/rlite/src/page_list.h +70 -0
- data/vendor/rlite/src/page_long.c +61 -0
- data/vendor/rlite/src/page_long.h +14 -0
- data/vendor/rlite/src/page_multi_string.c +538 -0
- data/vendor/rlite/src/page_multi_string.h +18 -0
- data/vendor/rlite/src/page_skiplist.c +689 -0
- data/vendor/rlite/src/page_skiplist.h +70 -0
- data/vendor/rlite/src/page_string.c +55 -0
- data/vendor/rlite/src/page_string.h +12 -0
- data/vendor/rlite/src/pqsort.c +185 -0
- data/vendor/rlite/src/pqsort.h +40 -0
- data/vendor/rlite/src/restore.c +401 -0
- data/vendor/rlite/src/restore.h +3 -0
- data/vendor/rlite/src/rlite.c +1309 -0
- data/vendor/rlite/src/rlite.h +159 -0
- data/vendor/rlite/src/sort.c +530 -0
- data/vendor/rlite/src/sort.h +18 -0
- data/vendor/rlite/src/status.h +19 -0
- data/vendor/rlite/src/type_hash.c +607 -0
- data/vendor/rlite/src/type_hash.h +29 -0
- data/vendor/rlite/src/type_list.c +477 -0
- data/vendor/rlite/src/type_list.h +23 -0
- data/vendor/rlite/src/type_set.c +796 -0
- data/vendor/rlite/src/type_set.h +34 -0
- data/vendor/rlite/src/type_string.c +613 -0
- data/vendor/rlite/src/type_string.h +34 -0
- data/vendor/rlite/src/type_zset.c +1147 -0
- data/vendor/rlite/src/type_zset.h +50 -0
- data/vendor/rlite/src/util.c +334 -0
- data/vendor/rlite/src/util.h +71 -0
- metadata +151 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YmNjMGQ3Njg2NTFjYWM5Njk4ODI5MDA2ZjA4MDFiZmZjNDU5ZWMyYw==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZGE1OGU5OTdlNmIzMmZhMWMzNDI1ZjhjZDg1MjJlODUwOGIzZDQwZA==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
M2MwNTM5NWEzMTZkYjMwMTk4ZWFhMDIzZWE5ZjUwNTM0MGQwZDBjMzk3NjJj
|
10
|
+
NWVhZDkwZmJjMTA3NWJiYTVkNDExNjM4YTIwNDU3YjRkMDE5Nzc5Njg5ZTg3
|
11
|
+
MzE1M2E1ZDc0NzI1N2Y5N2ZlMGViYzQ1NGYwYTJjMjBkY2FhOWE=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MjkwYTFhMzM2MjRiMzMyMDA2Y2UwOGU4MjZkZGQ5MDgwMGRjMzVhNmQ0NGQw
|
14
|
+
NWMxNWRjODYyOTBlNWE1ZTgzYTFlYjE3ZDRjNTBiMDE2NmM5YzVlYWMzMWE0
|
15
|
+
YjMxYzBkZjFjZGNiMjA3NzEzZmZiZDM4ZmY0ODgyZjc1NzA1MmY=
|
data/LICENSE
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
Copyright (c) 2015, Sebastian Waisbrot
|
2
|
+
|
3
|
+
All rights reserved.
|
4
|
+
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
7
|
+
|
8
|
+
* Redistributions of source code must retain the above copyright notice, this
|
9
|
+
list of conditions and the following disclaimer.
|
10
|
+
|
11
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
13
|
+
and/or other materials provided with the distribution.
|
14
|
+
|
15
|
+
* Neither the name of Redis nor the names of its contributors may be used to
|
16
|
+
endorse or promote products derived from this software without specific prior
|
17
|
+
written permission.
|
18
|
+
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
20
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
21
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
23
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
24
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
25
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
26
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
27
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
28
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/Rakefile
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require "bundler"
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require "rbconfig"
|
5
|
+
require "rake/testtask"
|
6
|
+
require "rake/extensiontask"
|
7
|
+
|
8
|
+
unless defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
|
9
|
+
|
10
|
+
Rake::ExtensionTask.new('hirlite_ext') do |task|
|
11
|
+
# Pass --with-foo-config args to extconf.rb
|
12
|
+
task.config_options = ARGV[1..-1] || []
|
13
|
+
task.lib_dir = File.join(*['lib', 'hirlite', 'ext'])
|
14
|
+
end
|
15
|
+
|
16
|
+
namespace :hirlite do
|
17
|
+
task :clean do
|
18
|
+
# Fetch hirlite if not present
|
19
|
+
if !File.directory?("vendor/rlite/.git")
|
20
|
+
system("git submodule update --init")
|
21
|
+
end
|
22
|
+
RbConfig::CONFIG['configure_args'] =~ /with-make-prog\=(\w+)/
|
23
|
+
make_program = $1 || ENV['make']
|
24
|
+
unless make_program then
|
25
|
+
make_program = (/mswin/ =~ RUBY_PLATFORM) ? 'nmake' : 'make'
|
26
|
+
end
|
27
|
+
system("cd vendor/rlite && #{make_program} clean")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# "rake clean" should also clean bundled hirlite
|
32
|
+
Rake::Task[:clean].enhance(['hirlite:clean'])
|
33
|
+
|
34
|
+
# Build from scratch
|
35
|
+
task :rebuild => [:clean, :compile]
|
36
|
+
|
37
|
+
else
|
38
|
+
|
39
|
+
task :rebuild do
|
40
|
+
# no-op
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
task :default => [:rebuild, :test]
|
46
|
+
|
47
|
+
desc "Run tests"
|
48
|
+
Rake::TestTask.new(:test) do |t|
|
49
|
+
t.pattern = 'test/**/*_test.rb'
|
50
|
+
t.verbose = true
|
51
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'mkmf'
|
2
|
+
|
3
|
+
RbConfig::MAKEFILE_CONFIG['CC'] = ENV['CC'] if ENV['CC']
|
4
|
+
|
5
|
+
hirlite_dir = File.join(File.dirname(__FILE__), %w{.. .. vendor rlite})
|
6
|
+
unless File.directory?(hirlite_dir)
|
7
|
+
STDERR.puts "vendor/rlite missing, please checkout its submodule..."
|
8
|
+
exit 1
|
9
|
+
end
|
10
|
+
|
11
|
+
RbConfig::CONFIG['configure_args'] =~ /with-make-prog\=(\w+)/
|
12
|
+
make_program = $1 || ENV['make']
|
13
|
+
make_program ||= case RUBY_PLATFORM
|
14
|
+
when /mswin/
|
15
|
+
'nmake'
|
16
|
+
when /(bsd|solaris)/
|
17
|
+
'gmake'
|
18
|
+
else
|
19
|
+
'make'
|
20
|
+
end
|
21
|
+
|
22
|
+
# Make sure hirlite is built...
|
23
|
+
Dir.chdir(hirlite_dir) do
|
24
|
+
success = system("#{make_program} libhirlite.a")
|
25
|
+
raise "Building hirlite failed" if !success
|
26
|
+
end
|
27
|
+
|
28
|
+
# Statically link to hirlite (mkmf can't do this for us)
|
29
|
+
$CFLAGS << " -I#{hirlite_dir}/src"
|
30
|
+
$LDFLAGS << " #{hirlite_dir}/src/libhirlite.a"
|
31
|
+
|
32
|
+
have_func("rb_thread_fd_select")
|
33
|
+
create_makefile('hirlite/ext/hirlite_ext')
|
@@ -0,0 +1,14 @@
|
|
1
|
+
#include <stdlib.h>
|
2
|
+
#include <string.h>
|
3
|
+
#include <assert.h>
|
4
|
+
#include "hirlite_ext.h"
|
5
|
+
|
6
|
+
VALUE mod_hirlite;
|
7
|
+
VALUE mod_ext;
|
8
|
+
void Init_hirlite_ext() {
|
9
|
+
mod_hirlite = rb_define_module("Hirlite");
|
10
|
+
mod_ext = rb_define_module_under(mod_hirlite,"Ext");
|
11
|
+
rb_global_variable(&mod_hirlite);
|
12
|
+
rb_global_variable(&mod_ext);
|
13
|
+
InitRlite(mod_ext);
|
14
|
+
}
|
@@ -0,0 +1,38 @@
|
|
1
|
+
#ifndef __HIRLITE_EXT_H
|
2
|
+
#define __HIRLITE_EXT_H
|
3
|
+
|
4
|
+
/* Defined for Rubinius. This indicates a char* obtained
|
5
|
+
* through RSTRING_PTR is never modified in place. With this
|
6
|
+
* define Rubinius can disable the slow copy back mechanisms
|
7
|
+
* to make sure strings are updated at the Ruby side.
|
8
|
+
*/
|
9
|
+
#define RSTRING_NOT_MODIFIED
|
10
|
+
|
11
|
+
#include "hirlite.h"
|
12
|
+
#include "ruby.h"
|
13
|
+
|
14
|
+
/* Defined in hirlite_ext.c */
|
15
|
+
extern VALUE mod_hirlite;
|
16
|
+
|
17
|
+
/* Defined in rlite.c */
|
18
|
+
extern VALUE klass_rlite;
|
19
|
+
extern void InitRlite(VALUE module);
|
20
|
+
|
21
|
+
/* Borrowed from Nokogiri */
|
22
|
+
#ifndef RSTRING_PTR
|
23
|
+
#define RSTRING_PTR(s) (RSTRING(s)->ptr)
|
24
|
+
#endif
|
25
|
+
|
26
|
+
#ifndef RSTRING_LEN
|
27
|
+
#define RSTRING_LEN(s) (RSTRING(s)->len)
|
28
|
+
#endif
|
29
|
+
|
30
|
+
#ifndef RARRAY_PTR
|
31
|
+
#define RARRAY_PTR(a) RARRAY(a)->ptr
|
32
|
+
#endif
|
33
|
+
|
34
|
+
#ifndef RARRAY_LEN
|
35
|
+
#define RARRAY_LEN(a) RARRAY(a)->len
|
36
|
+
#endif
|
37
|
+
|
38
|
+
#endif
|
@@ -0,0 +1,351 @@
|
|
1
|
+
/**
|
2
|
+
*
|
3
|
+
* Copyright (c) 2010-2012, Pieter Noordhuis
|
4
|
+
*
|
5
|
+
* All rights reserved.
|
6
|
+
*
|
7
|
+
* Redistribution and use in source and binary forms, with or without
|
8
|
+
* modification, are permitted provided that the following conditions are met:
|
9
|
+
*
|
10
|
+
* Redistributions of source code must retain the above copyright notice, this
|
11
|
+
* list of conditions and the following disclaimer.
|
12
|
+
*
|
13
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
14
|
+
* this list of conditions and the following disclaimer in the documentation
|
15
|
+
* and/or other materials provided with the distribution.
|
16
|
+
*
|
17
|
+
* Neither the name of Redis nor the names of its contributors may be used to
|
18
|
+
* endorse or promote products derived from this software without specific prior
|
19
|
+
* written permission.
|
20
|
+
*
|
21
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
22
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
23
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
24
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
25
|
+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
26
|
+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
27
|
+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
28
|
+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
29
|
+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
30
|
+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
*
|
32
|
+
* This file is based on these files:
|
33
|
+
* https://github.com/redis/hiredis-rb/blob/master/ext/hiredis_ext/connection.c
|
34
|
+
* https://github.com/redis/hiredis-rb/blob/master/ext/hiredis_ext/reader.c
|
35
|
+
*
|
36
|
+
*/
|
37
|
+
#include <errno.h>
|
38
|
+
#include "hirlite_ext.h"
|
39
|
+
|
40
|
+
/* Force encoding on new strings? */
|
41
|
+
static VALUE enc_klass;
|
42
|
+
static ID enc_default_external = 0;
|
43
|
+
static ID str_force_encoding = 0;
|
44
|
+
|
45
|
+
typedef struct rliteParentContext {
|
46
|
+
rliteContext *context;
|
47
|
+
} rliteParentContext;
|
48
|
+
|
49
|
+
static void parent_context_try_free_context(rliteParentContext *pc) {
|
50
|
+
if (pc->context) {
|
51
|
+
rliteFree(pc->context);
|
52
|
+
pc->context = NULL;
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
static void parent_context_try_free(rliteParentContext *pc) {
|
57
|
+
parent_context_try_free_context(pc);
|
58
|
+
}
|
59
|
+
|
60
|
+
static void parent_context_mark(rliteParentContext *pc) {
|
61
|
+
}
|
62
|
+
|
63
|
+
static void parent_context_free(rliteParentContext *pc) {
|
64
|
+
parent_context_try_free(pc);
|
65
|
+
free(pc);
|
66
|
+
}
|
67
|
+
|
68
|
+
static void parent_context_raise(rliteParentContext *pc) {
|
69
|
+
int err;
|
70
|
+
char errstr[1024];
|
71
|
+
|
72
|
+
/* Copy error and free context */
|
73
|
+
err = pc->context->err;
|
74
|
+
snprintf(errstr,sizeof(errstr),"%s",pc->context->errstr);
|
75
|
+
parent_context_try_free(pc);
|
76
|
+
|
77
|
+
switch(err) {
|
78
|
+
case RLITE_ERR_IO:
|
79
|
+
/* Raise native Ruby I/O error */
|
80
|
+
rb_sys_fail(0);
|
81
|
+
break;
|
82
|
+
case RLITE_ERR_EOF:
|
83
|
+
/* Raise native Ruby EOFError */
|
84
|
+
rb_raise(rb_eEOFError,"%s",errstr);
|
85
|
+
break;
|
86
|
+
default:
|
87
|
+
/* Raise something else */
|
88
|
+
rb_raise(rb_eRuntimeError,"%s",errstr);
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
static VALUE rlite_parent_context_alloc(VALUE klass) {
|
93
|
+
rliteParentContext *pc = malloc(sizeof(*pc));
|
94
|
+
pc->context = NULL;
|
95
|
+
return Data_Wrap_Struct(klass, parent_context_mark, parent_context_free, pc);
|
96
|
+
}
|
97
|
+
|
98
|
+
static VALUE rlite_generic_connect(VALUE self, rliteContext *c, VALUE arg_timeout) {
|
99
|
+
rliteParentContext *pc;
|
100
|
+
|
101
|
+
Data_Get_Struct(self,rliteParentContext,pc);
|
102
|
+
|
103
|
+
if (c->err) {
|
104
|
+
char buf[1024];
|
105
|
+
int err;
|
106
|
+
|
107
|
+
/* Copy error and free context */
|
108
|
+
err = c->err;
|
109
|
+
snprintf(buf,sizeof(buf),"%s",c->errstr);
|
110
|
+
rliteFree(c);
|
111
|
+
|
112
|
+
if (err == RLITE_ERR_IO) {
|
113
|
+
/* Raise native Ruby I/O error */
|
114
|
+
rb_sys_fail(0);
|
115
|
+
} else {
|
116
|
+
/* Raise something else */
|
117
|
+
rb_raise(rb_eRuntimeError,"%s",buf);
|
118
|
+
}
|
119
|
+
}
|
120
|
+
|
121
|
+
parent_context_try_free_context(pc);
|
122
|
+
pc->context = c;
|
123
|
+
return Qnil;
|
124
|
+
}
|
125
|
+
|
126
|
+
static VALUE rlite_connect(int argc, VALUE *argv, VALUE self) {
|
127
|
+
rliteContext *c;
|
128
|
+
VALUE arg_host = Qnil;
|
129
|
+
VALUE arg_port = Qnil;
|
130
|
+
VALUE arg_timeout = Qnil;
|
131
|
+
|
132
|
+
if (argc == 2 || argc == 3) {
|
133
|
+
arg_host = argv[0];
|
134
|
+
arg_port = argv[1];
|
135
|
+
|
136
|
+
if (argc == 3) {
|
137
|
+
arg_timeout = argv[2];
|
138
|
+
|
139
|
+
/* Sanity check */
|
140
|
+
if (NUM2INT(arg_timeout) <= 0) {
|
141
|
+
rb_raise(rb_eArgError, "timeout should be positive");
|
142
|
+
}
|
143
|
+
}
|
144
|
+
} else {
|
145
|
+
rb_raise(rb_eArgError, "invalid number of arguments");
|
146
|
+
}
|
147
|
+
|
148
|
+
c = rliteConnectNonBlock(StringValuePtr(arg_host), NUM2INT(arg_port));
|
149
|
+
return rlite_generic_connect(self,c,arg_timeout);
|
150
|
+
}
|
151
|
+
|
152
|
+
static VALUE rlite_connect_unix(int argc, VALUE *argv, VALUE self) {
|
153
|
+
rliteContext *c;
|
154
|
+
VALUE arg_path = Qnil;
|
155
|
+
VALUE arg_timeout = Qnil;
|
156
|
+
|
157
|
+
if (argc == 1 || argc == 2) {
|
158
|
+
arg_path = argv[0];
|
159
|
+
|
160
|
+
if (argc == 2) {
|
161
|
+
arg_timeout = argv[1];
|
162
|
+
|
163
|
+
/* Sanity check */
|
164
|
+
if (NUM2INT(arg_timeout) <= 0) {
|
165
|
+
rb_raise(rb_eArgError, "timeout should be positive");
|
166
|
+
}
|
167
|
+
}
|
168
|
+
} else {
|
169
|
+
rb_raise(rb_eArgError, "invalid number of arguments");
|
170
|
+
}
|
171
|
+
|
172
|
+
c = rliteConnectUnixNonBlock(StringValuePtr(arg_path));
|
173
|
+
return rlite_generic_connect(self,c,arg_timeout);
|
174
|
+
}
|
175
|
+
|
176
|
+
static VALUE rlite_is_connected(VALUE self) {
|
177
|
+
rliteParentContext *pc;
|
178
|
+
Data_Get_Struct(self,rliteParentContext,pc);
|
179
|
+
if (pc->context && !pc->context->err)
|
180
|
+
return Qtrue;
|
181
|
+
else
|
182
|
+
return Qfalse;
|
183
|
+
}
|
184
|
+
|
185
|
+
static VALUE rlite_disconnect(VALUE self) {
|
186
|
+
rliteParentContext *pc;
|
187
|
+
Data_Get_Struct(self,rliteParentContext,pc);
|
188
|
+
if (!pc->context)
|
189
|
+
rb_raise(rb_eRuntimeError,"%s","not connected");
|
190
|
+
parent_context_try_free(pc);
|
191
|
+
return Qnil;
|
192
|
+
}
|
193
|
+
|
194
|
+
static VALUE rlite_write(VALUE self, VALUE command) {
|
195
|
+
rliteParentContext *pc;
|
196
|
+
int argc;
|
197
|
+
char **argv = NULL;
|
198
|
+
size_t *alen = NULL;
|
199
|
+
int i;
|
200
|
+
|
201
|
+
/* Commands should be an array of commands, where each command
|
202
|
+
* is an array of string arguments. */
|
203
|
+
if (TYPE(command) != T_ARRAY)
|
204
|
+
rb_raise(rb_eArgError,"%s","not an array");
|
205
|
+
|
206
|
+
Data_Get_Struct(self,rliteParentContext,pc);
|
207
|
+
if (!pc->context)
|
208
|
+
rb_raise(rb_eRuntimeError,"%s","not connected");
|
209
|
+
|
210
|
+
argc = (int)RARRAY_LEN(command);
|
211
|
+
argv = malloc(argc*sizeof(char*));
|
212
|
+
alen = malloc(argc*sizeof(size_t));
|
213
|
+
for (i = 0; i < argc; i++) {
|
214
|
+
/* Replace arguments in the arguments array to prevent their string
|
215
|
+
* equivalents to be garbage collected before this loop is done. */
|
216
|
+
VALUE entry = rb_obj_as_string(rb_ary_entry(command, i));
|
217
|
+
rb_ary_store(command, i, entry);
|
218
|
+
argv[i] = RSTRING_PTR(entry);
|
219
|
+
alen[i] = RSTRING_LEN(entry);
|
220
|
+
}
|
221
|
+
rliteAppendCommandArgv(pc->context,argc,argv,alen);
|
222
|
+
free(argv);
|
223
|
+
free(alen);
|
224
|
+
return Qnil;
|
225
|
+
}
|
226
|
+
|
227
|
+
static VALUE rlite_flush(VALUE self) {
|
228
|
+
return Qnil;
|
229
|
+
}
|
230
|
+
|
231
|
+
static VALUE createDecodedString(char *str, size_t len) {
|
232
|
+
VALUE v, enc;
|
233
|
+
v = rb_str_new(str,len);
|
234
|
+
|
235
|
+
/* Force default external encoding if possible. */
|
236
|
+
if (enc_default_external) {
|
237
|
+
enc = rb_funcall(enc_klass,enc_default_external,0);
|
238
|
+
v = rb_funcall(v,str_force_encoding,1,enc);
|
239
|
+
}
|
240
|
+
|
241
|
+
return v;
|
242
|
+
}
|
243
|
+
|
244
|
+
static VALUE reply_to_value(rliteReply *reply) {
|
245
|
+
if (reply->type == RLITE_REPLY_STATUS || reply->type == RLITE_REPLY_STRING) {
|
246
|
+
if (reply->type == RLITE_REPLY_STATUS && reply->len == 2 && memcmp(reply->str, "OK", 2) == 0) {
|
247
|
+
return Qtrue;
|
248
|
+
}
|
249
|
+
return createDecodedString(reply->str, reply->len);
|
250
|
+
}
|
251
|
+
if (reply->type == RLITE_REPLY_NIL) {
|
252
|
+
return Qnil;
|
253
|
+
}
|
254
|
+
if (reply->type == RLITE_REPLY_INTEGER) {
|
255
|
+
return LL2NUM(reply->integer);
|
256
|
+
}
|
257
|
+
if (reply->type == RLITE_REPLY_ERROR) {
|
258
|
+
VALUE obj = createDecodedString(reply->str, reply->len);
|
259
|
+
return rb_funcall(rb_eRuntimeError,rb_intern("new"),1,obj);
|
260
|
+
}
|
261
|
+
if (reply->type == RLITE_REPLY_ARRAY) {
|
262
|
+
VALUE v, element;
|
263
|
+
size_t i;
|
264
|
+
v = rb_ary_new2(reply->elements);
|
265
|
+
for (i = 0; i < reply->elements; i++) {
|
266
|
+
element = reply_to_value(reply->element[i]);
|
267
|
+
rb_ary_store(v, i, element);
|
268
|
+
}
|
269
|
+
return v;
|
270
|
+
}
|
271
|
+
return Qnil;
|
272
|
+
}
|
273
|
+
|
274
|
+
static int __get_reply(rliteParentContext *pc, VALUE *reply) {
|
275
|
+
rliteContext *c = pc->context;
|
276
|
+
void *aux = NULL;
|
277
|
+
|
278
|
+
/* Try to read pending replies */
|
279
|
+
if (rliteGetReply(c,&aux) == RLITE_ERR) {
|
280
|
+
/* Protocol error */
|
281
|
+
return -1;
|
282
|
+
}
|
283
|
+
|
284
|
+
/* Set reply object */
|
285
|
+
if (reply && aux) {
|
286
|
+
*reply = reply_to_value(aux);
|
287
|
+
}
|
288
|
+
|
289
|
+
return 0;
|
290
|
+
}
|
291
|
+
|
292
|
+
static VALUE rlite_read(VALUE self) {
|
293
|
+
rliteParentContext *pc;
|
294
|
+
VALUE reply = Qnil;
|
295
|
+
|
296
|
+
Data_Get_Struct(self,rliteParentContext,pc);
|
297
|
+
if (!pc->context)
|
298
|
+
rb_raise(rb_eRuntimeError, "not connected");
|
299
|
+
|
300
|
+
if (__get_reply(pc,&reply) == -1)
|
301
|
+
parent_context_raise(pc);
|
302
|
+
|
303
|
+
return reply;
|
304
|
+
}
|
305
|
+
|
306
|
+
static VALUE rlite_set_timeout(VALUE self, VALUE usecs) {
|
307
|
+
if (NUM2INT(usecs) < 0) {
|
308
|
+
rb_raise(rb_eArgError, "timeout cannot be negative");
|
309
|
+
}
|
310
|
+
|
311
|
+
return Qnil;
|
312
|
+
}
|
313
|
+
|
314
|
+
static VALUE rlite_fileno(VALUE self) {
|
315
|
+
rliteParentContext *pc;
|
316
|
+
|
317
|
+
Data_Get_Struct(self,rliteParentContext,pc);
|
318
|
+
|
319
|
+
if (!pc->context)
|
320
|
+
rb_raise(rb_eRuntimeError, "not connected");
|
321
|
+
|
322
|
+
return INT2NUM(0);
|
323
|
+
}
|
324
|
+
|
325
|
+
VALUE klass_rlite;
|
326
|
+
void InitRlite(VALUE mod) {
|
327
|
+
klass_rlite = rb_define_class_under(mod, "Rlite", rb_cObject);
|
328
|
+
rb_global_variable(&klass_rlite);
|
329
|
+
rb_define_alloc_func(klass_rlite, rlite_parent_context_alloc);
|
330
|
+
rb_define_method(klass_rlite, "connect", rlite_connect, -1);
|
331
|
+
rb_define_method(klass_rlite, "connect_unix", rlite_connect_unix, -1);
|
332
|
+
rb_define_method(klass_rlite, "connected?", rlite_is_connected, 0);
|
333
|
+
rb_define_method(klass_rlite, "disconnect", rlite_disconnect, 0);
|
334
|
+
rb_define_method(klass_rlite, "timeout=", rlite_set_timeout, 1);
|
335
|
+
rb_define_method(klass_rlite, "fileno", rlite_fileno, 0);
|
336
|
+
rb_define_method(klass_rlite, "write", rlite_write, 1);
|
337
|
+
rb_define_method(klass_rlite, "flush", rlite_flush, 0);
|
338
|
+
rb_define_method(klass_rlite, "read", rlite_read, 0);
|
339
|
+
|
340
|
+
/* If the Encoding class is present, #default_external should be used to
|
341
|
+
* determine the encoding for new strings. The "enc_default_external"
|
342
|
+
* ID is non-zero when encoding should be set on new strings. */
|
343
|
+
if (rb_const_defined(rb_cObject, rb_intern("Encoding"))) {
|
344
|
+
enc_klass = rb_const_get(rb_cObject, rb_intern("Encoding"));
|
345
|
+
enc_default_external = rb_intern("default_external");
|
346
|
+
str_force_encoding = rb_intern("force_encoding");
|
347
|
+
rb_global_variable(&enc_klass);
|
348
|
+
} else {
|
349
|
+
enc_default_external = 0;
|
350
|
+
}
|
351
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
require "hirlite/ext/hirlite_ext"
|
data/lib/hirlite.rb
ADDED