kbaum-mongo_ext 0.18.3p

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile ADDED
@@ -0,0 +1,138 @@
1
+ # -*- mode: ruby; -*-
2
+ require 'rubygems'
3
+ require 'rubygems/specification'
4
+ require 'fileutils'
5
+ require 'rake'
6
+ require 'rake/testtask'
7
+ require 'rake/gempackagetask'
8
+ begin
9
+ require 'rake/contrib/rubyforgepublisher'
10
+ rescue LoadError
11
+ end
12
+ require 'rbconfig'
13
+ include Config
14
+ ENV['TEST_MODE'] = 'TRUE'
15
+
16
+ desc "Test the MongoDB Ruby driver."
17
+ task :test do
18
+ puts "\nThis option has changed."
19
+ puts "\nTo test the driver with the c-extensions:\nrake test:c\n"
20
+ puts "To test the pure ruby driver: \nrake test:ruby"
21
+ end
22
+
23
+ namespace :test do
24
+
25
+ desc "Test the driver with the c extension enabled."
26
+ task :c do
27
+ ENV['C_EXT'] = 'TRUE'
28
+ Rake::Task['test:unit'].invoke
29
+ Rake::Task['test:functional'].invoke
30
+ Rake::Task['test:pooled_threading'].invoke
31
+ Rake::Task['test:drop_databases'].invoke
32
+ ENV['C_EXT'] = nil
33
+ end
34
+
35
+ desc "Test the driver using pure ruby (no c extension)"
36
+ task :ruby do
37
+ ENV['C_EXT'] = nil
38
+ Rake::Task['test:unit'].invoke
39
+ Rake::Task['test:functional'].invoke
40
+ Rake::Task['test:pooled_threading'].invoke
41
+ Rake::Task['test:drop_databases'].invoke
42
+ end
43
+
44
+ Rake::TestTask.new(:unit) do |t|
45
+ t.test_files = FileList['test/unit/*_test.rb']
46
+ t.verbose = true
47
+ end
48
+
49
+ Rake::TestTask.new(:functional) do |t|
50
+ t.test_files = FileList['test/*_test.rb']
51
+ t.verbose = true
52
+ end
53
+
54
+ Rake::TestTask.new(:pooled_threading) do |t|
55
+ t.test_files = FileList['test/threading/*.rb']
56
+ t.verbose = true
57
+ end
58
+
59
+ Rake::TestTask.new(:pair_count) do |t|
60
+ t.test_files = FileList['test/replica/count_test.rb']
61
+ t.verbose = true
62
+ end
63
+
64
+ Rake::TestTask.new(:pair_insert) do |t|
65
+ t.test_files = FileList['test/replica/insert_test.rb']
66
+ t.verbose = true
67
+ end
68
+
69
+ Rake::TestTask.new(:pooled_pair_insert) do |t|
70
+ t.test_files = FileList['test/replica/pooled_insert_test.rb']
71
+ t.verbose = true
72
+ end
73
+
74
+ Rake::TestTask.new(:pair_query) do |t|
75
+ t.test_files = FileList['test/replica/query_test.rb']
76
+ t.verbose = true
77
+ end
78
+
79
+ Rake::TestTask.new(:auto_reconnect) do |t|
80
+ t.test_files = FileList['test/auxillary/autoreconnect_test.rb']
81
+ t.verbose = true
82
+ end
83
+
84
+ task :drop_databases do |t|
85
+ puts "Dropping test database..."
86
+ require File.join(File.dirname(__FILE__), 'lib', 'mongo')
87
+ include Mongo
88
+ con = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
89
+ ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT)
90
+ con.drop_database('ruby-mongo-test')
91
+ end
92
+ end
93
+
94
+ desc "Generate documentation"
95
+ task :rdoc do
96
+ version = eval(File.read("mongo-ruby-driver.gemspec")).version
97
+ out = File.join('html', version.to_s)
98
+ FileUtils.rm_rf('html')
99
+ system "rdoc --main README.rdoc --op #{out} --inline-source --quiet README.rdoc `find lib -name '*.rb'`"
100
+ end
101
+
102
+ desc "Generate YARD documentation"
103
+ task :ydoc do
104
+ require File.join(File.dirname(__FILE__), 'lib', 'mongo')
105
+ out = File.join('ydoc', Mongo::VERSION)
106
+ FileUtils.rm_rf('ydoc')
107
+ system "yardoc lib/**/*.rb lib/mongo/**/*.rb -e docs/yard_ext.rb -p docs/templates -o #{out} --title MongoRuby-#{Mongo::VERSION}"
108
+ end
109
+
110
+ desc "Publish documentation to mongo.rubyforge.org"
111
+ task :publish => [:rdoc] do
112
+ # Assumes docs are in ./html
113
+ Rake::RubyForgePublisher.new(GEM, RUBYFORGE_USER).upload
114
+ end
115
+
116
+ namespace :gem do
117
+
118
+ desc "Install the gem locally"
119
+ task :install do
120
+ sh "gem build mongo-ruby-driver.gemspec"
121
+ sh "gem install mongo-*.gem"
122
+ sh "rm mongo-*.gem"
123
+ end
124
+
125
+ desc "Install the optional c extensions"
126
+ task :install_extensions do
127
+ sh "gem build mongo-extensions.gemspec"
128
+ sh "gem install mongo_ext-*.gem"
129
+ sh "rm mongo_ext-*.gem"
130
+ end
131
+
132
+ end
133
+
134
+ task :default => :list
135
+
136
+ task :list do
137
+ system 'rake -T'
138
+ end
@@ -0,0 +1,135 @@
1
+ /*
2
+ * Copyright 2009 10gen, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ #include <stdlib.h>
18
+ #include <string.h>
19
+
20
+ #include "buffer.h"
21
+
22
+ #define INITIAL_BUFFER_SIZE 256
23
+
24
+ struct buffer {
25
+ char* buffer;
26
+ int size;
27
+ int position;
28
+ };
29
+
30
+ /* Allocate and return a new buffer.
31
+ * Return NULL on allocation failure. */
32
+ buffer_t buffer_new(void) {
33
+ buffer_t buffer;
34
+ buffer = (buffer_t)malloc(sizeof(struct buffer));
35
+ if (buffer == NULL) {
36
+ return NULL;
37
+ }
38
+
39
+ buffer->size = INITIAL_BUFFER_SIZE;
40
+ buffer->position = 0;
41
+ buffer->buffer = (char*)malloc(sizeof(char) * INITIAL_BUFFER_SIZE);
42
+ if (buffer->buffer == NULL) {
43
+ free(buffer);
44
+ return NULL;
45
+ }
46
+
47
+ return buffer;
48
+ }
49
+
50
+ /* Free the memory allocated for `buffer`.
51
+ * Return non-zero on failure. */
52
+ int buffer_free(buffer_t buffer) {
53
+ if (buffer == NULL) {
54
+ return 1;
55
+ }
56
+ free(buffer->buffer);
57
+ free(buffer);
58
+ return 0;
59
+ }
60
+
61
+ /* Grow `buffer` to at least `min_length`.
62
+ * Return non-zero on allocation failure. */
63
+ static int buffer_grow(buffer_t buffer, int min_length) {
64
+ int size = buffer->size;
65
+ char* old_buffer = buffer->buffer;
66
+ if (size >= min_length) {
67
+ return 0;
68
+ }
69
+ while (size < min_length) {
70
+ size *= 2;
71
+ }
72
+ buffer->buffer = (char*)realloc(buffer->buffer, sizeof(char) * size);
73
+ if (buffer->buffer == NULL) {
74
+ free(old_buffer);
75
+ free(buffer);
76
+ return 1;
77
+ }
78
+ buffer->size = size;
79
+ return 0;
80
+ }
81
+
82
+ /* Assure that `buffer` has at least `size` free bytes (and grow if needed).
83
+ * Return non-zero on allocation failure. */
84
+ static int buffer_assure_space(buffer_t buffer, int size) {
85
+ if (buffer->position + size <= buffer->size) {
86
+ return 0;
87
+ }
88
+ return buffer_grow(buffer, buffer->position + size);
89
+ }
90
+
91
+ /* Save `size` bytes from the current position in `buffer` (and grow if needed).
92
+ * Return offset for writing, or -1 on allocation failure. */
93
+ buffer_position buffer_save_space(buffer_t buffer, int size) {
94
+ int position = buffer->position;
95
+ if (buffer_assure_space(buffer, size) != 0) {
96
+ return -1;
97
+ }
98
+ buffer->position += size;
99
+ return position;
100
+ }
101
+
102
+ /* Write `size` bytes from `data` to `buffer` (and grow if needed).
103
+ * Return non-zero on allocation failure. */
104
+ int buffer_write(buffer_t buffer, const char* data, int size) {
105
+ if (buffer_assure_space(buffer, size) != 0) {
106
+ return 1;
107
+ }
108
+
109
+ memcpy(buffer->buffer + buffer->position, data, size);
110
+ buffer->position += size;
111
+ return 0;
112
+ }
113
+
114
+ /* Write `size` bytes from `data` to `buffer` at position `position`.
115
+ * Does not change the internal position of `buffer`.
116
+ * Return non-zero if buffer isn't large enough for write. */
117
+ int buffer_write_at_position(buffer_t buffer, buffer_position position,
118
+ const char* data, int size) {
119
+ if (position + size > buffer->size) {
120
+ buffer_free(buffer);
121
+ return 1;
122
+ }
123
+
124
+ memcpy(buffer->buffer + position, data, size);
125
+ return 0;
126
+ }
127
+
128
+
129
+ int buffer_get_position(buffer_t buffer) {
130
+ return buffer->position;
131
+ }
132
+
133
+ char* buffer_get_buffer(buffer_t buffer) {
134
+ return buffer->buffer;
135
+ }
@@ -0,0 +1,55 @@
1
+ /*
2
+ * Copyright 2009 10gen, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ #ifndef BUFFER_H
18
+ #define BUFFER_H
19
+
20
+ /* Note: if any of these functions return a failure condition then the buffer
21
+ * has already been freed. */
22
+
23
+ /* A buffer */
24
+ typedef struct buffer* buffer_t;
25
+ /* A position in the buffer */
26
+ typedef int buffer_position;
27
+
28
+ /* Allocate and return a new buffer.
29
+ * Return NULL on allocation failure. */
30
+ buffer_t buffer_new(void);
31
+
32
+ /* Free the memory allocated for `buffer`.
33
+ * Return non-zero on failure. */
34
+ int buffer_free(buffer_t buffer);
35
+
36
+ /* Save `size` bytes from the current position in `buffer` (and grow if needed).
37
+ * Return offset for writing, or -1 on allocation failure. */
38
+ buffer_position buffer_save_space(buffer_t buffer, int size);
39
+
40
+ /* Write `size` bytes from `data` to `buffer` (and grow if needed).
41
+ * Return non-zero on allocation failure. */
42
+ int buffer_write(buffer_t buffer, const char* data, int size);
43
+
44
+ /* Write `size` bytes from `data` to `buffer` at position `position`.
45
+ * Does not change the internal position of `buffer`.
46
+ * Return non-zero if buffer isn't large enough for write. */
47
+ int buffer_write_at_position(buffer_t buffer, buffer_position position, const char* data, int size);
48
+
49
+ /* Getters for the internals of a buffer_t.
50
+ * Should try to avoid using these as much as possible
51
+ * since they break the abstraction. */
52
+ buffer_position buffer_get_position(buffer_t buffer);
53
+ char* buffer_get_buffer(buffer_t buffer);
54
+
55
+ #endif