unqlite 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NDBhZTE1YWRhMTE2ZDA5ZmFkY2IxNjdhNTk3MDFmZDFhNzFjNWZkNw==
5
+ data.tar.gz: !binary |-
6
+ MWVjY2FhZmIwNzAyNjIwNjY1YTc2ODY1ZTliMjY5MWRmNmJiYTM1Mg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YzliODY1OWNlOThlNmU4YWUxZjAzYmY4YWIxYzdmZGVjNzI4YTc1ZDM2MDJl
10
+ ZjE4MDUzMWNkOTUzYTFlM2U3YmEwZjdlNTYwZWY0MzQxYThiNWM1Mzg3ZThl
11
+ Mjg0ZGNjZTdhZjczN2M3ZDBiZTNmNDlhNzljYTJiNjZmYmZlNjI=
12
+ data.tar.gz: !binary |-
13
+ MjUxYzhhZTZiNzkwYzgyNzY0ZjM3NDRkMDg1NjE4OTIwZTM4Mjc5NmIyOTcw
14
+ ODllMWZjODlmOTVkZTI4ZjA1NzhmMTY5YzRkNDEyMTUyYzM3ZjlhNGM1NzQ1
15
+ MWQ1ZDk3MzBkZTg5NWE2ZjI3ZTMxODAyNmFlNmZiZmQwYTY1YWQ=
@@ -0,0 +1,9 @@
1
+ Autotest.add_hook :initialize do |at|
2
+ at.add_mapping(/ext\/.*\.c/) do |f, _|
3
+ at.files_matching(/(lib|ext)\/test_.*rb$/)
4
+ end
5
+ end
6
+
7
+ Autotest.add_hook :run_command do |at|
8
+ system "bundle exec rake clean compile"
9
+ end
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ *.so
9
+ *.bundle
10
+ *.o
11
+ _yardoc
12
+ coverage
13
+ doc/
14
+ lib/bundler/man
15
+ pkg
16
+ rdoc
17
+ spec/reports
18
+ test/tmp
19
+ test/version_tmp
20
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in unqlite.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Daniel Teixeira
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,38 @@
1
+ # UnQLite for Ruby
2
+
3
+ [UnQLite](http://www.unqlite.org/) interface for ruby programs.
4
+
5
+ Note: This is an alpha version and many features are missing! But,
6
+ I would love to merge some pull-requests to make it better (:
7
+
8
+ ## Installation
9
+
10
+ You have to install UnQLite into your system and compile it as a shared library. Unfortunately,
11
+ UnQLite doesn't have a Makefile (or something like that) to automate that step. If you are on
12
+ linux, you can check [this gist](https://gist.github.com/danieltdt/5693070) and compile it using gcc.
13
+
14
+ After installing UnQLite, add this line to your application's Gemfile:
15
+
16
+ gem 'unqlite'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install unqlite
25
+
26
+ ## Usage
27
+
28
+ db = UnQLite::Database.new(":mem:")
29
+ db.store("key", "wabba")
30
+ db.fetch("key") # => "wabba"
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/extensiontask'
3
+
4
+ Rake::ExtensionTask.new('unqlite_native') do |ext|
5
+ ext.ext_dir = 'ext/unqlite'
6
+ ext.lib_dir = 'lib/unqlite'
7
+ end
@@ -0,0 +1,6 @@
1
+ require 'mkmf'
2
+
3
+ abort "unqlite.h is missing. Please, install unqlite." unless find_header 'unqlite.h'
4
+ abort "unqlite is missing. Please, install unqlite" unless find_library 'unqlite', 'unqlite_open'
5
+
6
+ create_makefile('unqlite/unqlite_native')
@@ -0,0 +1,11 @@
1
+ #include <unqlite_ruby.h>
2
+
3
+ VALUE mUnQLite;
4
+
5
+ void Init_unqlite_native()
6
+ {
7
+ mUnQLite = rb_define_module("UnQLite");
8
+
9
+ Init_unqlite_database();
10
+ Init_unqlite_codes();
11
+ }
@@ -0,0 +1,36 @@
1
+ #include <unqlite_codes.h>
2
+
3
+ VALUE mUnQLiteCodes;
4
+
5
+ void Init_unqlite_codes()
6
+ {
7
+ #if 0
8
+ VALUE mUnqlite = rb_define_module("UnQLite");
9
+ #endif
10
+
11
+ mUnQLiteCodes = rb_define_module_under(mUnQLite, "Codes");
12
+ rb_define_const(mUnQLiteCodes, "OK", INT2FIX(UNQLITE_OK));
13
+ rb_define_const(mUnQLiteCodes, "NOMEM", INT2FIX(UNQLITE_NOMEM));
14
+ rb_define_const(mUnQLiteCodes, "ABORT", INT2FIX(UNQLITE_ABORT));
15
+ rb_define_const(mUnQLiteCodes, "IOERR", INT2FIX(UNQLITE_IOERR));
16
+ rb_define_const(mUnQLiteCodes, "CORRUPT", INT2FIX(UNQLITE_CORRUPT));
17
+ rb_define_const(mUnQLiteCodes, "LOCKED", INT2FIX(UNQLITE_LOCKED));
18
+ rb_define_const(mUnQLiteCodes, "BUSY", INT2FIX(UNQLITE_BUSY));
19
+ rb_define_const(mUnQLiteCodes, "DONE", INT2FIX(UNQLITE_DONE));
20
+ rb_define_const(mUnQLiteCodes, "PERM", INT2FIX(UNQLITE_PERM));
21
+ rb_define_const(mUnQLiteCodes, "NOTIMPLEMENTED", INT2FIX(UNQLITE_NOTIMPLEMENTED));
22
+ rb_define_const(mUnQLiteCodes, "NOTFOUND", INT2FIX(UNQLITE_NOTFOUND));
23
+ rb_define_const(mUnQLiteCodes, "NOOP", INT2FIX(UNQLITE_NOOP));
24
+ rb_define_const(mUnQLiteCodes, "INVALID", INT2FIX(UNQLITE_INVALID));
25
+ rb_define_const(mUnQLiteCodes, "EOF", INT2FIX(UNQLITE_EOF));
26
+ rb_define_const(mUnQLiteCodes, "UNKNOWN", INT2FIX(UNQLITE_UNKNOWN));
27
+ rb_define_const(mUnQLiteCodes, "LIMIT", INT2FIX(UNQLITE_LIMIT));
28
+ rb_define_const(mUnQLiteCodes, "EXISTS", INT2FIX(UNQLITE_EXISTS));
29
+ rb_define_const(mUnQLiteCodes, "EMPTY", INT2FIX(UNQLITE_EMPTY));
30
+ rb_define_const(mUnQLiteCodes, "COMPILE_ERR", INT2FIX(UNQLITE_COMPILE_ERR));
31
+ rb_define_const(mUnQLiteCodes, "VM_ERR", INT2FIX(UNQLITE_VM_ERR));
32
+ rb_define_const(mUnQLiteCodes, "FULL", INT2FIX(UNQLITE_FULL));
33
+ rb_define_const(mUnQLiteCodes, "CANTOPEN", INT2FIX(UNQLITE_CANTOPEN));
34
+ rb_define_const(mUnQLiteCodes, "READ_ONLY", INT2FIX(UNQLITE_READ_ONLY));
35
+ rb_define_const(mUnQLiteCodes, "LOCKERR", INT2FIX(UNQLITE_LOCKERR));
36
+ }
@@ -0,0 +1,10 @@
1
+ #ifndef UNQLITE_RUBY_CODES
2
+ #define UNQLITE_RUBY_CODES
3
+
4
+ #include <unqlite_ruby.h>
5
+
6
+ extern VALUE mUnQLiteCodes;
7
+
8
+ void Init_unqlite_codes();
9
+
10
+ #endif
@@ -0,0 +1,130 @@
1
+ #include <unqlite_database.h>
2
+
3
+ VALUE cUnQLiteDatabase;
4
+
5
+ static void deallocate(void *ctx)
6
+ {
7
+ unqliteRubyPtr c = (unqliteRubyPtr) ctx;
8
+ unqlite *pDb = c->pDb;
9
+
10
+ if (pDb) unqlite_close(pDb);
11
+ xfree(c);
12
+ }
13
+
14
+ static VALUE allocate(VALUE klass)
15
+ {
16
+ unqliteRubyPtr ctx = malloc(sizeof(unqliteRuby));
17
+ ctx->pDb = NULL;
18
+
19
+ return Data_Wrap_Struct(klass, NULL, deallocate, ctx);
20
+ }
21
+
22
+ static VALUE initialize(VALUE self, VALUE rb_string)
23
+ {
24
+ void *c_string;
25
+ int rc;
26
+ unqliteRubyPtr ctx;
27
+
28
+ // Ensure the given argument is a ruby string
29
+ Check_Type(rb_string, T_STRING);
30
+
31
+ // Get class context
32
+ Data_Get_Struct(self, unqliteRuby, ctx);
33
+
34
+ // Transform Ruby string into C string
35
+ c_string = calloc(RSTRING_LEN(rb_string), sizeof(char));
36
+ memcpy(c_string, StringValuePtr(rb_string), RSTRING_LEN(rb_string));
37
+
38
+ // Open database
39
+ // TODO: Always opening as a memory database. It should be a parameter.
40
+ rc = unqlite_open(&ctx->pDb, c_string, UNQLITE_OPEN_IN_MEMORY);
41
+
42
+ // Verify if is everything ok
43
+ if( rc != UNQLITE_OK ){
44
+ // TODO: Return error code with a better message raising a UnQLite exception.
45
+ rb_raise(rb_eRuntimeError, "Couldn't open database");
46
+
47
+ return self;
48
+ }
49
+
50
+ return self;
51
+ }
52
+
53
+ static VALUE unqlite_database_store(VALUE self, VALUE key, VALUE value)
54
+ {
55
+ void *c_key;
56
+ void *c_value;
57
+ int rc;
58
+ unqliteRubyPtr ctx;
59
+
60
+ // Ensure the given argument is a ruby string
61
+ Check_Type(key, T_STRING);
62
+ Check_Type(value, T_STRING);
63
+
64
+ // Get class context
65
+ Data_Get_Struct(self, unqliteRuby, ctx);
66
+
67
+ // Transform Ruby string into C string
68
+ c_key = calloc(RSTRING_LEN(key), sizeof(char));
69
+ memcpy(c_key, StringValuePtr(key), RSTRING_LEN(key));
70
+
71
+ c_value = calloc(RSTRING_LEN(value), sizeof(char));
72
+ memcpy(c_value, StringValuePtr(value), RSTRING_LEN(value));
73
+
74
+ rc = unqlite_kv_store(ctx->pDb, c_key, -1, c_value, sizeof(c_value));
75
+
76
+ if( rc != UNQLITE_OK ) {
77
+ if( rc != UNQLITE_BUSY && rc != UNQLITE_NOTIMPLEMENTED ) {
78
+ unqlite_rollback(ctx->pDb);
79
+ }
80
+ }
81
+
82
+ return INT2FIX(rc);
83
+ }
84
+
85
+ static VALUE unqlite_database_fetch(VALUE self, VALUE collection_name)
86
+ {
87
+ void *c_collection_name;
88
+ void *fetched_data;
89
+ unqlite_int64 n_bytes;
90
+ int rc;
91
+ unqliteRubyPtr ctx;
92
+
93
+ // Ensure the given argument is a ruby string
94
+ Check_Type(collection_name, T_STRING);
95
+
96
+ // Get class context
97
+ Data_Get_Struct(self, unqliteRuby, ctx);
98
+
99
+ // Transform Ruby string into C string
100
+ c_collection_name = calloc(RSTRING_LEN(collection_name), sizeof(char));
101
+ memcpy(c_collection_name, StringValuePtr(collection_name), RSTRING_LEN(collection_name));
102
+
103
+ rc = unqlite_kv_fetch(ctx->pDb, c_collection_name, -1, NULL, &n_bytes);
104
+ if( rc != UNQLITE_OK ) { return INT2FIX(rc); }
105
+
106
+ fetched_data = (char *)malloc(n_bytes);
107
+ if( fetched_data == NULL ) { return INT2FIX(UNQLITE_EMPTY); }
108
+
109
+ rc = unqlite_kv_fetch(ctx->pDb, c_collection_name, -1, fetched_data, &n_bytes);
110
+ if( rc == UNQLITE_OK ) { return rb_str_new2((char *)fetched_data); }
111
+
112
+ return INT2FIX(rc);
113
+ }
114
+
115
+ void Init_unqlite_database()
116
+ {
117
+ #if 0
118
+ VALUE mUnqlite = rb_define_module("UnQLite");
119
+ #endif
120
+
121
+ /* defining UnQLite::Database class and appending its methods */
122
+ cUnQLiteDatabase = rb_define_class_under(mUnQLite, "Database", rb_cObject);
123
+
124
+ rb_define_alloc_func(cUnQLiteDatabase, allocate);
125
+
126
+ rb_define_method(cUnQLiteDatabase, "store", unqlite_database_store, 2);
127
+ rb_define_method(cUnQLiteDatabase, "fetch", unqlite_database_fetch, 1);
128
+
129
+ rb_define_method(cUnQLiteDatabase, "initialize", initialize, 1);
130
+ }
@@ -0,0 +1,17 @@
1
+ #ifndef UNQLITE_RUBY_DATABASE
2
+ #define UNQLITE_RUBY_DATABASE
3
+
4
+ #include <unqlite_ruby.h>
5
+
6
+ struct _unqliteRuby {
7
+ unqlite *pDb;
8
+ };
9
+
10
+ typedef struct _unqliteRuby unqliteRuby;
11
+ typedef unqliteRuby * unqliteRubyPtr;
12
+
13
+ extern VALUE cUnQLiteDatabase;
14
+
15
+ void Init_unqlite_database();
16
+
17
+ #endif
@@ -0,0 +1,12 @@
1
+ #ifndef UNQLITE_RUBY
2
+ #define UNQLITE_RUBY
3
+
4
+ #include <ruby.h>
5
+ #include <unqlite.h>
6
+
7
+ #include <unqlite_database.h>
8
+ #include <unqlite_codes.h>
9
+
10
+ extern VALUE mUnQLite;
11
+
12
+ #endif
@@ -0,0 +1,6 @@
1
+ require 'unqlite/unqlite_native'
2
+ require "unqlite/version"
3
+
4
+ module UnQLite
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,3 @@
1
+ module UnQLite
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,3 @@
1
+ require 'unqlite'
2
+ require 'minitest/autorun'
3
+
@@ -0,0 +1,21 @@
1
+ require 'helper'
2
+
3
+ module UnQLite
4
+ class TestDatabase < Minitest::Test
5
+ attr_reader :db
6
+
7
+ def setup
8
+ @db = UnQLite::Database.new(":mem:")
9
+ end
10
+
11
+ def test_store
12
+ assert_equal UnQLite::Codes::OK, @db.store("key", "stored content")
13
+ end
14
+
15
+ def test_fetch
16
+ @db.store("key", "wabba")
17
+
18
+ assert_equal "wabba", @db.fetch("key")
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'unqlite/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "unqlite"
8
+ spec.version = UnQLite::VERSION
9
+ spec.authors = ["Daniel Teixeira"]
10
+ spec.email = ["daniel.t.dt@gmail.com"]
11
+ spec.description = %q{UnQLite lib for ruby, using C extension.}
12
+ spec.summary = %q{UnQLite for ruby (using C extension)}
13
+ spec.homepage = "https://github.com/danieltdt/unqlite-ruby"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.extensions = ['ext/unqlite/extconf.rb']
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "ZenTest"
25
+ spec.add_development_dependency "rake-compiler"
26
+ spec.add_development_dependency "minitest", "~> 5.0.3"
27
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unqlite
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Teixeira
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ZenTest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake-compiler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 5.0.3
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 5.0.3
83
+ description: UnQLite lib for ruby, using C extension.
84
+ email:
85
+ - daniel.t.dt@gmail.com
86
+ executables: []
87
+ extensions:
88
+ - ext/unqlite/extconf.rb
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .autotest
92
+ - .gitignore
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - ext/unqlite/extconf.rb
98
+ - ext/unqlite/unqlite.c
99
+ - ext/unqlite/unqlite_codes.c
100
+ - ext/unqlite/unqlite_codes.h
101
+ - ext/unqlite/unqlite_database.c
102
+ - ext/unqlite/unqlite_database.h
103
+ - ext/unqlite/unqlite_ruby.h
104
+ - lib/unqlite.rb
105
+ - lib/unqlite/version.rb
106
+ - test/helper.rb
107
+ - test/test_database.rb
108
+ - unqlite.gemspec
109
+ homepage: https://github.com/danieltdt/unqlite-ruby
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.0.3
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: UnQLite for ruby (using C extension)
133
+ test_files:
134
+ - test/helper.rb
135
+ - test/test_database.rb