relaxo 1.0.1 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore DELETED
@@ -1,18 +0,0 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
18
- spec/relaxo/test
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --color
2
- --format documentation
3
- --warnings
data/.simplecov DELETED
@@ -1,9 +0,0 @@
1
-
2
- SimpleCov.start do
3
- add_filter "/spec/"
4
- end
5
-
6
- if ENV['TRAVIS']
7
- require 'coveralls'
8
- Coveralls.wear!
9
- end
@@ -1,17 +0,0 @@
1
- language: ruby
2
- sudo: false
3
- before_install:
4
- # For testing purposes:
5
- - git config --global user.name "Samuel Williams"
6
- - git config --global user.email "samuel@oriontransfer.net"
7
- rvm:
8
- - 2.1.8
9
- - 2.2.4
10
- - 2.3.1
11
- - 2.4.0
12
- - rbx-2
13
- env: COVERAGE=true BENCHMARK=true
14
- matrix:
15
- allow_failures:
16
- - rvm: ruby-head
17
- - rvm: "rbx-2"
data/Gemfile DELETED
@@ -1,20 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in relaxo.gemspec
4
- gemspec
5
-
6
- gem 'rugged', git: 'git://github.com/libgit2/rugged.git', submodules: true
7
-
8
- group :development do
9
- gem "pry"
10
- gem "msgpack"
11
- end
12
-
13
- group :test do
14
- gem 'benchmark-ips'
15
- gem 'ruby-prof'
16
-
17
- gem 'rack-test'
18
- gem 'simplecov'
19
- gem 'coveralls', require: false
20
- end
data/README.md DELETED
@@ -1,150 +0,0 @@
1
- # Relaxo
2
-
3
- Relaxo is a transactional database built on top of git. It's aim is to provide a robust interface for document storage and sorted indexes.
4
-
5
- [![Build Status](https://secure.travis-ci.org/ioquatix/relaxo.svg)](http://travis-ci.org/ioquatix/relaxo)
6
- [![Code Climate](https://codeclimate.com/github/ioquatix/relaxo.svg)](https://codeclimate.com/github/ioquatix/relaxo)
7
- [![Coverage Status](https://coveralls.io/repos/ioquatix/relaxo/badge.svg)](https://coveralls.io/r/ioquatix/relaxo)
8
-
9
- ## Installation
10
-
11
- Add this line to your application's Gemfile:
12
-
13
- gem 'relaxo'
14
-
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install relaxo
22
-
23
- ## Usage
24
-
25
- Connect to a local database and manipulate some documents.
26
-
27
- require 'relaxo'
28
- require 'msgpack'
29
-
30
- DB = Relaxo.connect("test")
31
-
32
- DB.commit(message: "Create test data") do |dataset|
33
- object = dataset.append(MessagePack.dump({bob: 'dole'}))
34
- dataset.write("doc1.json", object)
35
- end
36
-
37
- DB.commit(message: "Update test data") do |dataset|
38
- doc = MessagePack.load dataset.read('doc1.json').data
39
- doc[:foo] = 'bar'
40
-
41
- object = dataset.append(MessagePack.dump(doc))
42
- dataset.write("doc2.json", object)
43
- end
44
-
45
- doc = MessagePack.load DB.current['doc2.json'].data
46
- puts doc
47
- # => {"bob"=>"dole", "foo"=>"bar"}
48
-
49
- ### Document Storage
50
-
51
- Relaxo uses the git persistent data structure for storing documents. This data structure exposes a file-system like interface, which stores any kind of data. This means that you are free to use JSON, or BSON, or MessagePack, or JPEG, or XML, or any combination of those.
52
-
53
- Relaxo has a transactional model for both reading and writing.
54
-
55
- #### Reading Files
56
-
57
- path = "path/to/document"
58
-
59
- DB.current do |dataset|
60
- object = dataset.read(path)
61
-
62
- puts "The object id: #{object.oid}"
63
- puts "The object data size: #{object.size}"
64
- puts "The object data: #{object.data.inspect}"
65
- end
66
-
67
- #### Writing Files
68
-
69
- path = "path/to/document"
70
- data = MessagePack.dump(document)
71
-
72
- DB.commit(message: "Adding document") do |changeset|
73
- object = changeset.append(data)
74
- changeset.write(path, object)
75
- end
76
-
77
- ### Datasets and Transactions
78
-
79
- `Dataset`s and `Changeset`s are important concepts. Relaxo doesn't allow arbitrary access to data, but instead exposes the git persistent model for both reading and writing. The implications of this are that when reading or writing, you always see a consistent snapshot of the data store.
80
-
81
- ### Suitability
82
-
83
- Relaxo is designed to scale to the hundreds of thousands of documents. It's designed around the git persistent data store, and therefore has some performance and concurrency limitations due to the underlying implementation.
84
-
85
- Because it maintains a full history of all changes, the repository would continue to grow over time by default, but there are mechanisms to deal with that.
86
-
87
- #### Performance
88
-
89
- Relaxo can do anywhere from 1000-10,000 inserts per second depending on how you structure the workload.
90
-
91
- Relaxo Performance
92
- Warming up --------------------------------------
93
- single 129.000 i/100ms
94
- Calculating -------------------------------------
95
- single 6.224k (±14.7%) i/s - 114.036k in 20.000025s
96
- single transaction should be fast
97
- Warming up --------------------------------------
98
- multiple 152.000 i/100ms
99
- Calculating -------------------------------------
100
- multiple 1.452k (±15.2%) i/s - 28.120k in 20.101831s
101
- multiple transactions should be fast
102
-
103
- Reading data is lighting fast as it's loaded directly from disk and cached.
104
-
105
- ### Loading Data
106
-
107
- As Relaxo is unapologetically based on git, you can use git directly with a non-bare working directory to add any files you like. You can even point Relaxo at an existing git repository.
108
-
109
- ### Durability
110
-
111
- Relaxo is based on `libgit2` and asserts that it is a transactional database. We base this assertion on:
112
-
113
- - All writes into the object store using `libgit2` are atomic and synchronized to disk.
114
- - All updates to refs are atomic and synchronized to disk.
115
-
116
- Provided these two invariants are maintained, the operation of Relaxo will be safe, even if there are unexpected interruptions to the program.
117
-
118
- The durability guarantees of Relaxo depend on [`libgit2` calling `fsync`](https://github.com/libgit2/libgit2/pull/4030), and [this being respected by the underlying hardware](http://www.evanjones.ca/intel-ssd-durability.html). Otherwise, durability cannot be guaranteed.
119
-
120
- ## Contributing
121
-
122
- 1. Fork it
123
- 2. Create your feature branch (`git checkout -b my-new-feature`)
124
- 3. Commit your changes (`git commit -am 'Add some feature'`)
125
- 4. Push to the branch (`git push origin my-new-feature`)
126
- 5. Create new Pull Request
127
-
128
- ## License
129
-
130
- Released under the MIT license.
131
-
132
- Copyright, 2015, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
133
-
134
- Permission is hereby granted, free of charge, to any person obtaining a copy
135
- of this software and associated documentation files (the "Software"), to deal
136
- in the Software without restriction, including without limitation the rights
137
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
138
- copies of the Software, and to permit persons to whom the Software is
139
- furnished to do so, subject to the following conditions:
140
-
141
- The above copyright notice and this permission notice shall be included in
142
- all copies or substantial portions of the Software.
143
-
144
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
145
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
146
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
147
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
148
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
149
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
150
- THE SOFTWARE.
data/Rakefile DELETED
@@ -1,20 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec) do |task|
5
- task.rspec_opts = ["--require", "simplecov"] if ENV['COVERAGE']
6
- end
7
-
8
- task :default => :spec
9
-
10
- task :console do
11
- require 'pry'
12
- require 'msgpack'
13
- require 'securerandom'
14
-
15
- require_relative 'lib/relaxo'
16
-
17
- DB = Relaxo.connect(File.join(__dir__, '/tmp/relaxo-test-db'))
18
-
19
- Pry.start
20
- end
data/bin/relaxo DELETED
@@ -1,142 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'relaxo'
4
- require 'relaxo/version'
5
- require 'optparse'
6
- require 'yaml'
7
- require 'csv'
8
-
9
- OPTIONS = {
10
- :existing => :update, # :merge, :replace
11
- :format => :yaml,
12
- :transaction => true,
13
- }
14
-
15
- module Formats
16
- def self.yaml(io, &block)
17
- documents = YAML::load(io)
18
-
19
- unless Array === documents
20
- documents = [documents]
21
- end
22
-
23
- documents.each &block
24
- end
25
-
26
- def self.csv(io, &block)
27
- csv = CSV.new(io, :headers => :first_row)
28
-
29
- csv.each do |row|
30
- yield row.to_hash
31
- end
32
- end
33
- end
34
-
35
- ARGV.options do |o|
36
- script_name = File.basename($0)
37
-
38
- o.banner = "Usage: #{script_name} [options] [server-url] [files]"
39
- o.define_head "This script can be used to import data to CouchDB."
40
-
41
- o.separator ""
42
- o.separator "Document creation:"
43
-
44
- o.on("--existing [mode]", "Control whether to 'update (new document attributes takes priority), 'merge' (existing document attributes takes priority) or replace (old document attributes discarded) existing documents.") do |mode|
45
- OPTIONS[:existing] = mode.to_sym
46
- end
47
-
48
- o.on("--format [type]", "Control the input format. 'yaml' files are imported as a single document or array of documents. 'csv' files are imported as records using the first row as attribute keys.") do |format|
49
- OPTIONS[:format] = format.to_sym
50
- end
51
-
52
- o.on("--[no-]transaction", "Controls whether data is saved using the batch save operation. Not suitable for huge amounts of data.")
53
-
54
- o.separator ""
55
- o.separator "Help and Copyright information:"
56
-
57
- o.on_tail("--copy", "Display copyright and warranty information") do
58
- $stderr.puts "#{script_name} v#{Relaxo::VERSION}. Copyright (c) 2012 Samuel Williams."
59
- $stderr.puts "This software is released under the MIT license and comes with ABSOLUTELY NO WARRANTY."
60
- exit
61
- end
62
-
63
- o.on_tail("-h", "--help", "Show this help message.") do
64
- $stderr.puts o
65
- exit
66
- end
67
- end.parse!
68
-
69
- url = ARGV.shift
70
-
71
- $stderr.puts "Connecting to #{url}..."
72
- database = Relaxo.connect(url)
73
-
74
- begin
75
- info = database.info
76
-
77
- $stderr.puts "Connected to #{info['db_name']} which contains #{info['doc_count']} document(s)."
78
- rescue StandardError => error
79
- $stderr.puts "Could not fetch info from database server!"
80
-
81
- throw
82
- end
83
-
84
-
85
- format = Formats.method(OPTIONS[:format])
86
-
87
- @stats = {
88
- :saved => 0,
89
- :errors => 0,
90
- :total => 0
91
- }
92
-
93
- def process_document(database, document)
94
- begin
95
- existing_document = nil
96
-
97
- if document && document[Relaxo::ID]
98
- $stderr.puts "Loading #{document[Relaxo::ID]}"
99
- existing_document = database.get(document[Relaxo::ID]) rescue nil
100
- end
101
-
102
- if existing_document
103
- if OPTIONS[:existing] == :replace
104
- $stderr.puts "Replacing existing document..."
105
- # The document is replaced entirely with the incoming data.
106
- document[Relaxo::REV] = existing_document[Relaxo::REV]
107
- elsif OPTIONS[:existing] == :update
108
- $stderr.puts "Updating existing document..."
109
- # Values in `existing_document` take priority
110
- document.update(existing_document)
111
- elsif OPTIONS[:existing] == :merge
112
- $stderr.puts "Merging existing document..."
113
- # Values in `document` take priority, except for `_rev`.
114
- document.update(existing_document) do |key, oldval, newval|
115
- key == Relaxo::REV ? newval : oldval
116
- end
117
- end
118
- end
119
-
120
- $stderr.puts "Saving #{document.inspect}"
121
- result = database.save(document)
122
-
123
- @stats[:saved] += 1
124
- rescue RestClient::BadRequest => ex
125
- $stderr.puts ex.inspect
126
-
127
- @stats[:errors] += 1
128
- end
129
-
130
- @stats[:total] += 1
131
- end
132
-
133
- begin
134
- format.call(ARGF) do |document|
135
- process_document(database, document)
136
- end
137
- ensure
138
- $stderr.puts "#{@stats[:saved]} document(s) saved out of #{@stats[:total]}."
139
- if @stats[:errors] > 0
140
- $stderr.puts "#{@stats[:errors]} errors occurred!"
141
- end
142
- end
@@ -1,61 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'relaxo'
4
- require 'relaxo/version'
5
- require 'optparse'
6
- require 'digest/sha1'
7
-
8
- $url = ARGV.shift unless ARGV[0] =~ /^-/
9
- $connection = Relaxo::Connection.new($url)
10
-
11
- OPTIONS = {
12
- :roles => [],
13
- :password => nil
14
- }
15
-
16
- ARGV.options do |o|
17
- script_name = File.basename($0)
18
-
19
- o.banner = "Usage: #{script_name} [server-url] [options]"
20
- o.define_head "This script can be used to import data to CouchDB."
21
-
22
- o.on("--password password", "Specify the password for a new user") do |password|
23
- OPTIONS[:password] = password
24
- end
25
-
26
- o.on("--roles reader,writer", Array, "Specify the roles for a new user") do |roles|
27
- OPTIONS[:roles] = roles
28
- end
29
-
30
- o.on("--create-user name", "Create a new (non-admin) user") do |name|
31
- password = OPTIONS[:password]
32
-
33
- salt = Digest::SHA1.hexdigest(rand.to_s + name + Time.now.to_s + password)
34
-
35
- user = {
36
- Relaxo::ID => "org.couchdb.user:#{name}",
37
- :type => "user",
38
- :name => name,
39
- :roles => OPTIONS[:roles],
40
- :salt => salt,
41
- :password_sha => Digest::SHA1.hexdigest(password + salt)
42
- }
43
-
44
- database = Relaxo::Database.new($connection, "_users")
45
- puts database.save(user).inspect
46
- end
47
-
48
- o.separator ""
49
- o.separator "Help and Copyright information:"
50
-
51
- o.on_tail("--copy", "Display copyright and warranty information") do
52
- $stderr.puts "#{script_name} v#{Relaxo::VERSION}. Copyright (c) 2012 Samuel Williams."
53
- $stderr.puts "This software is released under the MIT license and comes with ABSOLUTELY NO WARRANTY."
54
- exit
55
- end
56
-
57
- o.on_tail("-h", "--help", "Show this help message.") do
58
- $stderr.puts o
59
- exit
60
- end
61
- end.parse!