Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: clowder/libzdb
base: 83e256e0e5
...
head fork: clowder/libzdb
compare: a9e9a710ad
  • 3 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
Commits on Mar 26, 2012
Chris Lowder Remove all setters on [ZDB::ConnectionPool].
All options can now be configured through arguments hash passed to
initialiser.
f4cc81a
Commits on Mar 27, 2012
Chris Lowder Remove all setters on [ZDB::Connection].
All options can now be configured through arguments hash passed to
initialiser.
8379c15
Chris Lowder Updated [ZDB::ConnectionPool#get_connection] to take the same args as
[ZDB::Connection.new].
a9e9a71
39 ext/zdb/rb_connection.c
View
@@ -28,13 +28,30 @@ static Connection_T *get_connection_pointer(VALUE self)
*
* Returns [ZDB::Connection]
*/
-static VALUE initialize(VALUE self, VALUE connection_pool)
+static VALUE initialize(int argc, VALUE *argv, VALUE self)
{
+ VALUE connection_pool, options_hash, max_rows, query_timeout;
+
+ rb_scan_args(argc, argv, "11", &connection_pool, &options_hash);
+
ConnectionPool_T *pool = get_pool_pointer(connection_pool);
Connection_T *connection = get_connection_pointer(self);
*connection = ConnectionPool_getConnection(*pool);
+ if (!NIL_P(options_hash)) {
+ max_rows = rb_hash_aref(options_hash, STRING2SYM("max_rows"));
+ query_timeout = rb_hash_aref(options_hash, STRING2SYM("query_timeout"));
+
+ if (!NIL_P(max_rows)) {
+ Connection_setMaxRows(*connection, NUM2INT(max_rows));
+ }
+
+ if (!NIL_P(query_timeout)) {
+ Connection_setQueryTimeout(*connection, NUM2INT(query_timeout));
+ }
+ }
+
rb_iv_set(self, "@connection_pool", connection_pool);
return self;
@@ -122,22 +139,6 @@ static VALUE max_rows(VALUE self)
return INT2NUM(max);
}
-static void set_max_rows(VALUE self, VALUE rb_int)
-{
- int max = NUM2INT(rb_int);
- Connection_T *connection = get_connection_pointer(self);
-
- Connection_setMaxRows(*connection, max);
-}
-
-static void set_query_timeout(VALUE self, VALUE rb_int)
-{
- int timeout = NUM2INT(rb_int);
- Connection_T *connection = get_connection_pointer(self);
-
- Connection_setQueryTimeout(*connection, timeout);
-}
-
static VALUE ping(VALUE self)
{
Connection_T *connection = get_connection_pointer(self);
@@ -189,15 +190,13 @@ void init_connection()
cZDBConnection = rb_define_class_under(mZDB, "Connection", rb_cObject);
rb_define_alloc_func(cZDBConnection, allocate);
- rb_define_method(cZDBConnection, "initialize", initialize, 1);
+ rb_define_method(cZDBConnection, "initialize", initialize, -1);
rb_define_method(cZDBConnection, "execute", execute, 1);
rb_define_method(cZDBConnection, "execute_query", execute_query, 1);
rb_define_method(cZDBConnection, "url", url, 0);
rb_define_method(cZDBConnection, "connection_pool", connection_pool, 0);
rb_define_method(cZDBConnection, "query_timeout", query_timeout, 0);
rb_define_method(cZDBConnection, "max_rows", max_rows, 0);
- rb_define_method(cZDBConnection, "max_rows=", set_max_rows, 1);
- rb_define_method(cZDBConnection, "query_timeout=", set_query_timeout, 1);
rb_define_method(cZDBConnection, "ping", ping, 0);
rb_define_method(cZDBConnection, "transaction", transaction, 0);
}
64 ext/zdb/rb_connection_pool.c
View
@@ -27,15 +27,37 @@ ConnectionPool_T *get_pool_pointer(VALUE self)
return pool;
}
-static VALUE initialize(VALUE self, VALUE rb_string)
+static VALUE initialize(int argc, VALUE *argv, VALUE self)
{
- Check_Type(rb_string, T_STRING);
+ VALUE rb_url_string, options_hash, rb_initial_connections, rb_max_connections;
+
+ // (int arg count, VALUE* args, char* "(no. required args)(no. optional args)", VALUE* individual args ...
+ rb_scan_args(argc, argv, "11", &rb_url_string, &options_hash);
+
+ Check_Type(rb_url_string, T_STRING);
ConnectionPool_T *pool = get_pool_pointer(self);
- char *url_string = StringValueCStr(rb_string);
+ char *url_string = StringValueCStr(rb_url_string);
URL_T url = URL_new(url_string);
*pool = ConnectionPool_new(url);
+
+ if (!NIL_P(options_hash)) {
+ // Configure based on our options hash
+ Check_Type(options_hash, T_HASH);
+
+ rb_initial_connections = rb_hash_aref(options_hash, STRING2SYM("initial_connections"));
+ rb_max_connections = rb_hash_aref(options_hash, STRING2SYM("max_connections"));
+
+ if (!NIL_P(rb_initial_connections)) {
+ ConnectionPool_setInitialConnections(*pool, NUM2INT(rb_initial_connections));
+ }
+
+ if (!NIL_P(rb_max_connections)) {
+ ConnectionPool_setMaxConnections(*pool, NUM2INT(rb_max_connections));
+ }
+ }
+
ConnectionPool_setReaper(*pool, 60);
ConnectionPool_start(*pool);
@@ -89,12 +111,18 @@ static VALUE url(VALUE self)
*
* Returns a connection from the pool
*/
-static VALUE get_connection(VALUE self)
+static VALUE get_connection(int argc, VALUE *argv, VALUE self)
{
- VALUE args[1];
- args[0] = self;
+ VALUE options[argc+1];
+ int index;
+
+ options[0] = self;
+
+ for (index = 0; index < argc; index++) {
+ options[index+1] = argv[index];
+ }
- return rb_class_new_instance(1, args, cZDBConnection);
+ return rb_class_new_instance(sizeof(options)/SIZEOF_VALUE, options, cZDBConnection);
}
static VALUE initial_connections(VALUE self)
@@ -109,22 +137,6 @@ static VALUE max_connections(VALUE self)
return INT2NUM(ConnectionPool_getMaxConnections(*pool));
}
-static void set_initial_connections(VALUE self, VALUE rb_int)
-{
- ConnectionPool_T *pool = get_pool_pointer(self);
- int initial_connections = NUM2INT(rb_int);
-
- ConnectionPool_setInitialConnections(*pool, initial_connections);
-}
-
-static void set_max_connections(VALUE self, VALUE rb_int)
-{
- ConnectionPool_T *pool = get_pool_pointer(self);
- int max_connections = NUM2INT(rb_int);
-
- ConnectionPool_setMaxConnections(*pool, max_connections);
-}
-
VALUE cZDBConnectionPool;
void init_connection_pool()
{
@@ -134,13 +146,11 @@ void init_connection_pool()
cZDBConnectionPool = klass;
rb_define_alloc_func(cZDBConnectionPool, allocate);
- rb_define_method(cZDBConnectionPool, "initialize", initialize, 1);
+ rb_define_method(cZDBConnectionPool, "initialize", initialize, -1);
rb_define_method(cZDBConnectionPool, "active", active, 0);
rb_define_method(cZDBConnectionPool, "size", size, 0);
rb_define_method(cZDBConnectionPool, "url", url, 0);
- rb_define_method(cZDBConnectionPool, "get_connection", get_connection, 0);
+ rb_define_method(cZDBConnectionPool, "get_connection", get_connection, -1);
rb_define_method(cZDBConnectionPool, "initial_connections", initial_connections, 0);
rb_define_method(cZDBConnectionPool, "max_connections", max_connections, 0);
- rb_define_method(cZDBConnectionPool, "initial_connections=", set_initial_connections, 1);
- rb_define_method(cZDBConnectionPool, "max_connections=", set_max_connections, 1);
}
2  ext/zdb/zdb.h
View
@@ -13,6 +13,8 @@
#include <zdb/zdb.h>
+#define STRING2SYM(str) ID2SYM(rb_intern(str))
+
#include <rb_connection_pool.h>
#include <rb_connection.h>
79 spec/zdb/connection_pool_spec.rb
View
@@ -1,18 +1,41 @@
require 'spec_helper'
describe ZDB::ConnectionPool do
- subject { ZDB::ConnectionPool.new("sqlite://#{ tmp_db_path.to_s }") }
+ subject { ZDB::ConnectionPool.new(uri) }
+
+ let(:uri) { "sqlite://#{ tmp_db_path.to_s }" }
describe ".new" do
- it "works" do
- connection_pool = ZDB::ConnectionPool.new("sqlite://#{ tmp_db_path.to_s }")
- tmp_db_path.should be_file
+ context "no args" do
+ it "configures the instance properly" do
+ pool = ZDB::ConnectionPool.new(uri)
+
+ tmp_db_path.should be_file
+
+ pool.url.should == uri
+ pool.initial_connections.should == 5
+ pool.max_connections.should == 20
+ end
+ end
+
+ context "with args" do
+ let(:args) { Hash[:initial_connections => 2, :max_connections => 10] }
+
+ it "configures the instance properly" do
+ pool = ZDB::ConnectionPool.new(uri, args)
+
+ tmp_db_path.should be_file
+
+ pool.initial_connections.should == 2
+ pool.max_connections.should == 10
+ pool.url.should == uri
+ end
end
end
describe "#size" do
it "returns the number count of all connections in the pool" do
- subject.size.should == 5
+ subject.size.should == 5 # brand new object so === initial_connections
end
end
@@ -22,44 +45,26 @@
end
end
- describe "#url" do
- it "returns the db url that the connection pool was configured with" do
- subject.url.should == "sqlite://#{ tmp_db_path.to_s }"
- end
- end
-
describe "#get_connection" do
- it "returns a new connection that belongs to the current pool" do
- connection = subject.get_connection
+ let(:args) { Hash[:max_rows => 100, :query_timeout => 10] }
- connection.url.should == subject.url
- connection.connection_pool.should == subject
- end
- end
+ context "no args" do
+ it "returns a new connection that belongs to the current pool" do
+ connection = subject.get_connection
- describe "#initial_connections" do
- it "returns the number of connection that the pool is started with" do
- subject.initial_connections.should == 5
+ connection.url.should == subject.url
+ connection.connection_pool.should == subject
+ end
end
- end
- describe "#max_connections" do
- it "returns the maximum number of connections that can exist in a pool" do
- subject.max_connections.should == 20
- end
- end
-
- describe "#initial_connections=" do
- it "sets the number of connections that the pool is started with" do
- subject.initial_connections = 2
- subject.initial_connections.should == 2
- end
- end
+ context "with args" do
+ it "returns a properly configured connection in the current pool" do
+ connection = subject.get_connection(args)
- describe "#max_connections=" do
- it "sets the maximum number of connections for the pool" do
- subject.max_connections = 10
- subject.max_connections.should == 10
+ connection.connection_pool.should == subject
+ connection.max_rows.should == 100
+ connection.query_timeout == 10
+ end
end
end
end
73 spec/zdb/connection_spec.rb
View
@@ -15,9 +15,26 @@
EOSQL
describe ".new" do
- it "returns a connection in the given connection pool" do
- connection = ZDB::Connection.new(connection_pool)
- connection.url.should == connection_pool.url
+ context "no args" do
+ it "configures the instance properly" do
+ connection = ZDB::Connection.new(connection_pool)
+
+ connection.connection_pool.should == connection_pool
+ connection.url.should == connection_pool.url
+ connection.query_timeout.should == 3000
+ connection.max_rows.should == 0
+ end
+ end
+
+ context "with args" do
+ let(:args) { Hash[:max_rows => 100, :query_timeout => 10] }
+
+ it "configures the instance properly" do
+ connection = ZDB::Connection.new(connection_pool, args)
+
+ connection.max_rows.should == 100
+ connection.query_timeout.should == 10
+ end
end
end
@@ -41,56 +58,6 @@
end
end
- describe "#url" do
- it "returns the url the connection was started on" do
- subject.url.should == "sqlite://#{ tmp_db_path.to_s }"
- end
- end
-
- describe "#connection_pool" do
- it "returns the pool that the connection was initialized with" do
- subject.connection_pool.should == connection_pool
- end
- end
-
- describe "#query_timeout" do
- it "returns the query timeout in milliseconds" do
- subject.query_timeout.should == 3000
- end
- end
-
- describe "#max_rows" do
- it "returns the maximum number of rows that any query can return" do
- subject.max_rows.should == 0
- end
- end
-
- describe "#max_rows=" do
- it "sets the maximum number of rows, 0 for no limit" do
- subject.max_rows = 100
- subject.max_rows.should == 100
- end
-
- it "type checks the incoming value" do
- expect {
- subject.max_rows = "foo"
- }.to raise_error(TypeError)
- end
- end
-
- describe "#query_timeout=" do
- it "sets the query timeout, 0 for no limit" do
- subject.query_timeout = 0
- subject.query_timeout.should == 0
- end
-
- it "type checks the incoming value" do
- expect {
- subject.query_timeout = "foo"
- }.to raise_error(TypeError)
- end
- end
-
describe "#ping" do
context "the connection is alive" do
it "is true" do

No commit comments for this range

Something went wrong with that request. Please try again.