heliodor 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f68d88cabd00c5927c58c03e62c9ef4c763ca93c
4
+ data.tar.gz: 4b467b876e1d87bfe3185b770f4995159a7fe14c
5
+ SHA512:
6
+ metadata.gz: cd9e3c8bd52897efcb53224e430952e1e53e3c7e9ec3a49bcfe9b700b9e6f6b41c6d574a35738b032eb257a8c364fb0f663487c49e42e1984d5e7346d5f811e8
7
+ data.tar.gz: efc1731366889d3e9b23ebc9b8ad04ac9975f6aeb46f11d11616f2e1929d7044487c896855c5bef1ca937704f4276b8afeb6347a1284834ea9a30f7f1abbdbed
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.db
@@ -0,0 +1,23 @@
1
+ Metrics/MethodLength:
2
+ Enabled: false
3
+
4
+ Metrics/ModuleLength:
5
+ Enabled: false
6
+
7
+ Metrics/ClassLength:
8
+ Enabled: false
9
+
10
+ Metrics/BlockLength:
11
+ Enabled: false
12
+
13
+ Metrics/AbcSize:
14
+ Enabled: false
15
+
16
+ Metrics/CyclomaticComplexity:
17
+ Enabled: false
18
+
19
+ Style/Documentation:
20
+ Enabled: false
21
+
22
+ Style/ClassAndModuleChildren:
23
+ Enabled: false
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at nickolay02@inbox.ru. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in heliodor.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Nickolay
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,53 @@
1
+ # Heliodor
2
+
3
+ Heliodor is server-less database management tool
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'heliodor'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install heliodor
20
+
21
+ ## Usage
22
+
23
+ Next example creates new database file, table inside of it and inserts value
24
+
25
+ ```ruby
26
+ require 'heliodor'
27
+ db = Heliodor::DB.new './example.db'
28
+ db
29
+ .query('Users') # Perform query on table 'users'
30
+ .ensure # Ensure that this table exists - if it does not, create it
31
+ .insert(first_name: 'Vasya', last_name: 'Pupkin', age: 20) # Insert data into table
32
+ .write # Write data
33
+ .finish # Finish query - executes all statements
34
+
35
+ # Resulting table will be returned after finishing query
36
+
37
+ ```
38
+
39
+ ## Development
40
+
41
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
42
+
43
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/handicraftsman/heliodor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
48
+
49
+
50
+ ## License
51
+
52
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
53
+
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+ task default: :spec
3
+ task :check do
4
+ sh 'rubocop'
5
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'heliodor'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'heliodor/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'heliodor'
8
+ spec.version = Heliodor::VERSION
9
+ spec.authors = ['Nickolay']
10
+ spec.email = ['nickolay02@inbox.ru']
11
+
12
+ spec.summary = 'NoSQL Server-Less DB-library'
13
+ spec.description = 'NoSQL Server-Less DB-library'
14
+ spec.homepage = 'https://github.com/handicraftsman/heliodor/'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.13'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rubocop', '~> 0.47'
27
+
28
+ spec.add_runtime_dependency 'bson', '~> 4.0'
29
+ end
@@ -0,0 +1,16 @@
1
+ require 'bson'
2
+
3
+ module Heliodor
4
+ end
5
+
6
+ require 'heliodor/version'
7
+ require 'heliodor/db'
8
+ require 'heliodor/query'
9
+ require 'heliodor/query_internal'
10
+
11
+ class Hash
12
+ def add_key(key)
13
+ self[key] = nil
14
+ self
15
+ end
16
+ end
@@ -0,0 +1,66 @@
1
+ # Base class for accessing DBs and building queries
2
+ # @attr file [String]
3
+ # @attr tsafe [Bool] When true, uses mutexes for building queries
4
+ class Heliodor::DB
5
+ attr_accessor :file, :tsafe
6
+
7
+ def initialize(file, tsafe = false)
8
+ @tsafe = tsafe
9
+ @mutex = Mutex.new
10
+ @dtable = { 'heliodor_data' => {
11
+ 'name' => 'Heliodor',
12
+ 'version' => Heliodor::VERSION,
13
+ 'bson_version' => Gem.loaded_specs['bson'].version.version,
14
+ 'last_write' => DateTime.now.strftime,
15
+ 'ruby_version' => RUBY_VERSION,
16
+ 'ruby_patchlevel' => RUBY_PATCHLEVEL.to_s,
17
+ 'ruby_platform' => RUBY_PLATFORM
18
+ } }
19
+ @file = File.expand_path(file.to_s)
20
+
21
+ if File.exist?(@file)
22
+ raw = File.read(File.expand_path(@file))
23
+ bb = BSON::ByteBuffer.new(raw)
24
+ @dat = Hash.from_bson(bb)
25
+ else
26
+ File.open(@file, 'w') do |f|
27
+ f.write(@dtable.to_bson.to_s)
28
+ @dat = @dtable.clone
29
+ end
30
+ end
31
+ end
32
+
33
+ # Entry point for building queries
34
+ # @param table [String]
35
+ # @return [Heliodor::Query]
36
+ def query(table)
37
+ if @tsafe
38
+ @mutex.synchronize do
39
+ Heliodor::Query.new(self, table, @dat)
40
+ end
41
+ else
42
+ Heliodor::Query.new(self, table, @dat)
43
+ end
44
+ end
45
+
46
+ # Returns array of table names
47
+ # @return [Array<String>] Array of table names
48
+ def tables
49
+ Hash.from_bson(BSON::ByteBuffer.new(File.read(@file))).keys
50
+ end
51
+
52
+ # Writes database to file
53
+ # @return [self]
54
+ def write(dat)
55
+ @dat = dat
56
+ File.truncate(@file, 0) if File.exist?(@file)
57
+ File.open(@file, 'w') do |f|
58
+ f.write(dat.merge(@dtable).to_bson.to_s)
59
+ end
60
+ self
61
+ end
62
+
63
+ def inspect
64
+ %(#<Heliodor::DB:#{object_id.to_s(16)} @file='#{@file}'>)
65
+ end
66
+ end
@@ -0,0 +1,150 @@
1
+ # Query which will be executed when #finish is called
2
+ # @attr db [Heliodor::DB] Query's database
3
+ # @attr table [String] Name of current table
4
+ # @attr actions [Array<Hash>] Array of all actions
5
+ class Heliodor::Query
6
+ attr_accessor :db, :actions, :table
7
+
8
+ # Initializer
9
+ def initialize(db, table, dat)
10
+ @db = db
11
+ @table = table
12
+ @full = dat
13
+ @dat = @full[@table]
14
+ @actions = []
15
+ end
16
+
17
+ # @!group Query Language
18
+
19
+ # Insert given data into table
20
+ # @param dat [Any]
21
+ # @return [self]
22
+ def insert(*dat)
23
+ actions << {
24
+ 'type' => 'insert',
25
+ 'data' => dat
26
+ }
27
+ self
28
+ end
29
+
30
+ # Creates table with current name. If it already exists - truncates
31
+ # @return [self]
32
+ def create
33
+ actions << {
34
+ 'type' => 'create'
35
+ }
36
+ self
37
+ end
38
+
39
+ # If there's no table with current name, creates it
40
+ # @return [self]
41
+ def ensure
42
+ actions << {
43
+ 'type' => 'ensure'
44
+ }
45
+ self
46
+ end
47
+
48
+ # Filters all items in current table using given block
49
+ # @yield [i] Is executed for each item in table.
50
+ # Drops values when this block returns false value
51
+ # @return [self]
52
+ def filter(&block)
53
+ actions << {
54
+ 'type' => 'filter',
55
+ 'block' => block
56
+ }
57
+ self
58
+ end
59
+
60
+ # Edits all items in current table
61
+ # @yield [i] Return updated value to update stuff
62
+ # @example Adding key to each hash in table
63
+ # db.query('table_name').map do |i|
64
+ # i[:keyname] = nil
65
+ # end.write.finish
66
+ # @example Map-reduce method of getting table length
67
+ # db.query('table_name').map{1}.reduce(&:+).finish # => length
68
+ # @return [self]
69
+ def map(&block)
70
+ actions << {
71
+ 'type' => 'map',
72
+ 'block' => block
73
+ }
74
+ self
75
+ end
76
+
77
+ # Reduces table to single element using given block
78
+ # @return [self]
79
+ def reduce(&block)
80
+ actions << {
81
+ 'type' => 'reduce',
82
+ 'block' => block
83
+ }
84
+ self
85
+ end
86
+
87
+ # Renames current table
88
+ # @param to [String]
89
+ # @return [self]
90
+ def rename(to)
91
+ actions << {
92
+ 'type' => 'rename',
93
+ 'to' => to
94
+ }
95
+ self
96
+ end
97
+
98
+ # Writes changes to database
99
+ # @return [self]
100
+ def write
101
+ actions << {
102
+ 'type' => 'write'
103
+ }
104
+ self
105
+ end
106
+
107
+ # Selects all values which match given hash
108
+ # @return [self]
109
+ def select(dat = {})
110
+ actions << {
111
+ 'type' => 'select',
112
+ 'dat' => dat
113
+ }
114
+ self
115
+ end
116
+
117
+ # Updates all items which match first hash by merging them with second hash
118
+ # @return [self]
119
+ def update(dat1 = {}, dat2 = {})
120
+ actions << {
121
+ 'type' => 'update',
122
+ 'dat1' => dat1,
123
+ 'dat2' => dat2
124
+ }
125
+ self
126
+ end
127
+
128
+ # Deletes current table. WARNING: writes changes to database
129
+ # @return [self]
130
+ def delete
131
+ actions << {
132
+ 'type' => 'delete'
133
+ }
134
+ self
135
+ end
136
+
137
+ # Ends query and processes it
138
+ # @return [Array] Data in table
139
+ def finish
140
+ _process
141
+ @dat
142
+ end
143
+ # @!endgroup
144
+
145
+ # Inspect method
146
+ def inspect
147
+ %(#<Heliodor::Query:#{object_id.to_s(16)} @table='#{@table}' @db=#{@db}) +
148
+ %( @actions.length=#{@actions.length}>)
149
+ end
150
+ end
@@ -0,0 +1,95 @@
1
+ class Heliodor::Query
2
+ private
3
+
4
+ def _gen
5
+ @dat = []
6
+ end
7
+
8
+ def _filter(&_block)
9
+ dat = []
10
+ @dat.each do |item|
11
+ dat << item if yield(item)
12
+ end
13
+ @dat = dat
14
+ end
15
+
16
+ def _select(dat = {})
17
+ out = []
18
+ @dat.each do |item|
19
+ out << item if (v.class == Hash) && (item >= dat)
20
+ end
21
+ @dat = out
22
+ end
23
+
24
+ def _update(dat1 = {}, dat2 = {})
25
+ @dat.each_with_index do |v, k|
26
+ @dat[k] = v.merge(dat2) if (v.class == Hash) && (v >= dat1)
27
+ end
28
+ end
29
+
30
+ def _map(&_block)
31
+ @dat.each_with_index do |v, k|
32
+ @dat[k] = yield(v)
33
+ end
34
+ end
35
+
36
+ def _reduce(&_block)
37
+ @dat = [@dat.reduce(&block)]
38
+ end
39
+
40
+ def _insert(dat)
41
+ dat.each do |d|
42
+ @dat << d
43
+ end
44
+ end
45
+
46
+ def _rename(to)
47
+ @full[to] = @dat
48
+ @full.delete(@table)
49
+ @table = to
50
+ @dat = @full[@table]
51
+ end
52
+
53
+ def _delete
54
+ @full.delete @table
55
+ @db.write(@full)
56
+ @dat = nil
57
+ end
58
+
59
+ def _write
60
+ @full[@table] = @dat
61
+ @db.write(@full)
62
+ end
63
+
64
+ def _process
65
+ actions.each do |action|
66
+ case action['type']
67
+ when 'create'
68
+ _gen
69
+ when 'ensure'
70
+ _gen unless @dat
71
+ when 'filter'
72
+ _filter(&action['block'])
73
+ when 'select'
74
+ _select(action['dat'])
75
+ when 'update'
76
+ _update(action['dat1'], action['dat2'])
77
+ when 'map'
78
+ _map(&action['block'])
79
+ when 'reduce'
80
+ _reduce(&action['block'])
81
+ when 'insert'
82
+ _insert(action['data'])
83
+ when 'rename'
84
+ _rename(action['to'])
85
+ when 'delete'
86
+ _delete
87
+ when 'write'
88
+ _write
89
+ else
90
+ raise NotImplementedError,
91
+ "Action `#{action['type']}` is not yet implemented!"
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,3 @@
1
+ module Heliodor
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: heliodor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nickolay
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-08 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.47'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.47'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bson
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
69
+ description: NoSQL Server-Less DB-library
70
+ email:
71
+ - nickolay02@inbox.ru
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rubocop.yml"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - heliodor.gemspec
86
+ - lib/heliodor.rb
87
+ - lib/heliodor/db.rb
88
+ - lib/heliodor/query.rb
89
+ - lib/heliodor/query_internal.rb
90
+ - lib/heliodor/version.rb
91
+ homepage: https://github.com/handicraftsman/heliodor/
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.6.8
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: NoSQL Server-Less DB-library
115
+ test_files: []