mem_db 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/main.yml +18 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.rubocop.yml +43 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +21 -0
- data/README.md +105 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/mem_db.rb +102 -0
- data/lib/mem_db/bucket.rb +17 -0
- data/lib/mem_db/field.rb +34 -0
- data/lib/mem_db/field/enum.rb +56 -0
- data/lib/mem_db/field/matching.rb +11 -0
- data/lib/mem_db/field/may_missing.rb +44 -0
- data/lib/mem_db/field/negative.rb +40 -0
- data/lib/mem_db/field/pattern.rb +125 -0
- data/lib/mem_db/field/regexp.rb +68 -0
- data/lib/mem_db/fields.rb +25 -0
- data/lib/mem_db/idx.rb +41 -0
- data/lib/mem_db/idx/bytes.rb +25 -0
- data/lib/mem_db/idx/chars.rb +85 -0
- data/lib/mem_db/idx/default.rb +33 -0
- data/lib/mem_db/idx/itself.rb +25 -0
- data/lib/mem_db/idx/pattern.rb +71 -0
- data/lib/mem_db/idx/reverse.rb +27 -0
- data/lib/mem_db/idx/uniq.rb +36 -0
- data/lib/mem_db/index.rb +32 -0
- data/lib/mem_db/index/any.rb +60 -0
- data/lib/mem_db/index/bucket.rb +20 -0
- data/lib/mem_db/index/enum.rb +54 -0
- data/lib/mem_db/index/pattern_match.rb +104 -0
- data/lib/mem_db/index/prefix_tree.rb +110 -0
- data/lib/mem_db/index/sequence_match.rb +146 -0
- data/lib/mem_db/indexation.rb +17 -0
- data/lib/mem_db/indexing_object.rb +58 -0
- data/lib/mem_db/out.rb +25 -0
- data/lib/mem_db/query.rb +49 -0
- data/lib/mem_db/version.rb +5 -0
- data/mem_db.gemspec +25 -0
- metadata +86 -0
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
data/.rspec
ADDED
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
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
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
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"
|
data/lib/mem_db/field.rb
ADDED
@@ -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
|