mem_db 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e0576e79f06874cadbf804ca183629fbe4aaa5bb11f5f428aab5e306a3d713b9
4
+ data.tar.gz: f8409a01d2cc4f2e906366dd7743c53b6612b2e8e28db633f883de1c5dd47375
5
+ SHA512:
6
+ metadata.gz: b70381b36c4d60d7f807a064c1f368f55d3febdca24ea7d291f33765089197f401b816165d608d467b37327f23c570f80342e4b9d6e390e4a1d380290fc49b46
7
+ data.tar.gz: b8ea9ca471be55b1c7c9b695299c03b155f262de90d158442a916430b28d1f961b33678b562770bd3d312e1ed62b4b2c1adc6a2521e139e06cd993708099b8a9
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 2.5.5
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.5
17
+ bundle install
18
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,43 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
14
+
15
+ Layout/SpaceInsideHashLiteralBraces:
16
+ EnforcedStyle: no_space
17
+
18
+ Layout/FirstArrayElementIndentation:
19
+ EnforcedStyle: consistent
20
+
21
+ Style/WordArray:
22
+ Enabled: false
23
+
24
+ Style/SymbolArray:
25
+ Enabled: false
26
+
27
+ Style/SafeNavigation:
28
+ Enabled: false
29
+
30
+ Style/GuardClause:
31
+ Enabled: false
32
+
33
+ Style/Documentation:
34
+ Enabled: false
35
+
36
+ Naming/MethodParameterName:
37
+ Enabled: false
38
+
39
+ Metrics/BlockLength:
40
+ Enabled: false
41
+
42
+ Metrics/MethodLength:
43
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in mem_db.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.0"
10
+ gem "rubocop", "~> 1.7"
11
+ gem "ruby-prof", "~> 1.4"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Dmitry Bochkarev
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.
data/README.md ADDED
@@ -0,0 +1,105 @@
1
+ # MemDb
2
+
3
+ Embedded database with support of composite indices and complex document matching.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mem_db'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mem_db
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ # Define how fields will be matched
25
+ fields = MemDB::Fields.new([
26
+ MemDB::Field::Enum.new(:category),
27
+ MemDB::Field::Enum.new(:"!category").query(:category).negative,
28
+
29
+ MemDB::Field::Regexp.new(:text),
30
+ MemDB::Field::Regexp.new(:"!text").query(:text).negative,
31
+
32
+ MemDB::Field::Pattern.new(:breadcrumbs),
33
+ MemDB::Field::Pattern.new(:"!breadcrumbs").query(:breadcrumbs).negative,
34
+ ].map!(&:may_missing))
35
+
36
+ # Specify index
37
+
38
+ index = MemDB::Index.compose([
39
+ MemDB::Index::Enum::Bucket.new(
40
+ idx: MemDB::Idx::Itself.new(:category).default_any
41
+ ).accept_any,
42
+ MemDB::Index::PatternMatch::Bucket.new(
43
+ idx: MemDB::Idx::Pattern.new(:breadcrumbs).default_any
44
+ ).accept_any,
45
+ ])
46
+
47
+ # Create database instance
48
+
49
+ db = MemDB.new(fields, index)
50
+
51
+ # Indexate
52
+
53
+ indexation = db.new_indexation
54
+
55
+ indexation.add(
56
+ {
57
+ category: "food",
58
+ text: ".*fish.*",
59
+ breadcrumbs: "*/retail/*"
60
+ },
61
+ :food_fish
62
+ )
63
+ indexation.add(
64
+ {
65
+ "!category": "food",
66
+ text: ".*fish.*",
67
+ breadcrumbs: "*/retail/*"
68
+ },
69
+ :not_food_fish
70
+ )
71
+
72
+ # Query
73
+
74
+ MemDB::Query.new({
75
+ category: "toy",
76
+ text: "animated fish",
77
+ breadcrumbs: "/shops/retail/123"
78
+ }).using(database)
79
+ # => [:not_food_fish]
80
+
81
+ MemDB::Query.new({
82
+ category: "food",
83
+ text: "delicious fish",
84
+ breadcrumbs: "/shops/retail/123"
85
+ }).using(database)
86
+ # => [:food_fish]
87
+ ```
88
+
89
+ ## Development
90
+
91
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
92
+
93
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
94
+
95
+ ## Contributing
96
+
97
+ Bug reports and pull requests are welcome on GitHub at https://github.com/DmitryBochkarev/mem_db.
98
+
99
+ ## License
100
+
101
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
102
+
103
+ ```
104
+
105
+ ```
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "mem_db"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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
data/lib/mem_db.rb ADDED
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "mem_db/version"
4
+ require "set"
5
+
6
+ class MemDB
7
+ class Error < StandardError; end
8
+
9
+ class Entries
10
+ class Entry
11
+ attr_reader :id, :matching, :value
12
+
13
+ def initialize(id, matching, value)
14
+ @id = id
15
+ @matching = matching
16
+ @value = value
17
+ end
18
+ end
19
+
20
+ def initialize
21
+ @entries = []
22
+ end
23
+
24
+ def [](id)
25
+ @entries[id]
26
+ end
27
+
28
+ def add(matching, value)
29
+ id = @entries.length
30
+
31
+ entry = Entry.new(id, matching, value)
32
+ @entries.push(entry)
33
+
34
+ id
35
+ end
36
+ end
37
+
38
+ def initialize(fields, index)
39
+ @fields = fields
40
+ @index = index
41
+ @entries = Entries.new
42
+ end
43
+
44
+ def new_indexation
45
+ MemDB::Indexation.new(self)
46
+ end
47
+
48
+ def add(obj, value)
49
+ matching = @fields.new_matching(obj)
50
+ entry_id = @entries.add(matching, value)
51
+
52
+ @index.add(obj, entry_id)
53
+ end
54
+
55
+ def query(query)
56
+ checked = Set.new
57
+ found = []
58
+
59
+ @index.query(query).each do |entry_id|
60
+ next if checked.include?(entry_id)
61
+
62
+ checked.add(entry_id)
63
+
64
+ entry = @entries[entry_id]
65
+
66
+ found.push(entry.value) if entry.matching.match?(query)
67
+ end
68
+
69
+ found
70
+ end
71
+ end
72
+
73
+ require_relative "mem_db/out"
74
+ require_relative "mem_db/idx"
75
+ require_relative "mem_db/query"
76
+ require_relative "mem_db/indexing_object"
77
+ require_relative "mem_db/indexation"
78
+ require_relative "mem_db/index"
79
+ require_relative "mem_db/fields"
80
+ require_relative "mem_db/field"
81
+
82
+ require_relative "mem_db/idx/bytes"
83
+ require_relative "mem_db/idx/chars"
84
+ require_relative "mem_db/idx/itself"
85
+ require_relative "mem_db/idx/reverse"
86
+ require_relative "mem_db/idx/uniq"
87
+ require_relative "mem_db/idx/pattern"
88
+ require_relative "mem_db/idx/default"
89
+
90
+ require_relative "mem_db/index/bucket"
91
+ require_relative "mem_db/index/any"
92
+ require_relative "mem_db/index/enum"
93
+ require_relative "mem_db/index/prefix_tree"
94
+ require_relative "mem_db/index/sequence_match"
95
+ require_relative "mem_db/index/pattern_match"
96
+
97
+ require_relative "mem_db/field/matching"
98
+ require_relative "mem_db/field/may_missing"
99
+ require_relative "mem_db/field/negative"
100
+ require_relative "mem_db/field/enum"
101
+ require_relative "mem_db/field/pattern"
102
+ require_relative "mem_db/field/regexp"
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ class MemDB
4
+ class Bucket
5
+ def initialize
6
+ @values = []
7
+ end
8
+
9
+ def add(_obj, value)
10
+ @values.push(value)
11
+ end
12
+
13
+ def query(_query, out:)
14
+ out.add(@values)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ class MemDB
4
+ module Field
5
+ def may_missing
6
+ MemDB::Field::MayMissing.new(self)
7
+ end
8
+
9
+ def negative
10
+ MemDB::Field::Negative.new(self)
11
+ end
12
+
13
+ def field
14
+ raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
15
+ end
16
+
17
+ def query(query_field)
18
+ @query_field = query_field
19
+ self
20
+ end
21
+
22
+ def query_field
23
+ @query_field || field
24
+ end
25
+
26
+ def new_matching(_obj)
27
+ raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
28
+ end
29
+
30
+ def query_value(query)
31
+ query[query_field]
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mem_db/field"
4
+ require "mem_db/field/matching"
5
+
6
+ class MemDB
7
+ module Field
8
+ class Enum
9
+ include MemDB::Field
10
+
11
+ class MultiMatching
12
+ include MemDB::Field::Matching
13
+
14
+ def initialize(enum, arr)
15
+ @enum = enum
16
+ @arr = arr
17
+ end
18
+
19
+ def match?(query)
20
+ @enum.query_value(query).any? do |el|
21
+ @arr.include?(el)
22
+ end
23
+ end
24
+ end
25
+
26
+ class SingleMatching
27
+ include MemDB::Field::Matching
28
+
29
+ def initialize(enum, el)
30
+ @enum = enum
31
+ @el = el
32
+ end
33
+
34
+ def match?(query)
35
+ @enum.query_value(query).include?(@el)
36
+ end
37
+ end
38
+
39
+ attr_reader :field
40
+
41
+ def initialize(field)
42
+ @field = field
43
+ end
44
+
45
+ def new_matching(obj)
46
+ val = obj[field]
47
+
48
+ if val.is_a?(Array)
49
+ MultiMatching.new(self, val)
50
+ else
51
+ SingleMatching.new(self, val)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end