dddr 2.1.0 → 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -1
- data/.rspecrc +4 -0
- data/lib/dddr/sdbm.rb +49 -38
- data/lib/dddr/sequel.rb +40 -61
- data/lib/dddr/version.rb +1 -1
- data/lib/dddr.rb +18 -6
- metadata +15 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d2f6afaa1eb4dfe12d0b23863245fed79c2bceaa2d4b07a800d6139f25ff4c11
|
4
|
+
data.tar.gz: ef76a7d071a37b57ad42872b8ce08acff77ceb613c5d8fbcdbd180ae5066b6c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 86b6bbe5171e50157b17bf4a1ba79852950b331c8e1f13e5219f81363228341cfd7f9166830ced1ef2d0cd93ff6d29580a49f31e50ac35aadcb8e64beedfb978
|
7
|
+
data.tar.gz: d11b34ef26630fac36b37ebe45bbc63d768fe7a96db17df10f9cf8e65f68da5dda2b440f1add108a816e00d20ca6cd2df4a314ab4102ebb3b931cbd10d756335
|
data/.rspec
CHANGED
data/.rspecrc
ADDED
data/lib/dddr/sdbm.rb
CHANGED
@@ -31,6 +31,7 @@ module Dddr
|
|
31
31
|
|
32
32
|
class RepositoryBase
|
33
33
|
def initialize(entity_class)
|
34
|
+
@mutex = Mutex.new
|
34
35
|
@entity_class = entity_class
|
35
36
|
setup_data_directory(entity_class)
|
36
37
|
@serializer = Dddr::Serializer.new(Dddr.configuration.serializer)
|
@@ -62,31 +63,31 @@ module Dddr
|
|
62
63
|
def count
|
63
64
|
all.count
|
64
65
|
end
|
65
|
-
|
66
|
+
|
66
67
|
def to_csv
|
67
68
|
env = Dddr.configuration.env
|
68
69
|
data_dir = Dddr.configuration.data_dir
|
69
|
-
container = Dddr.configuration.container
|
70
|
+
container = Dddr.configuration.container
|
70
71
|
CSV.open("#{data_dir}/#{container}/#{env}/#{self.class.name}.csv", "w") do |csv|
|
71
72
|
csv = all.first&.to_hash&.keys
|
72
73
|
all.each do |entity|
|
73
74
|
csv << entity.to_hash.values
|
74
|
-
end
|
75
|
-
end
|
75
|
+
end
|
76
|
+
end
|
76
77
|
end
|
77
78
|
|
78
|
-
|
79
|
-
|
80
79
|
def add(entity)
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
80
|
+
@mutex.synchronize do
|
81
|
+
uid = SecureRandom.uuid
|
82
|
+
entity.uid = uid
|
83
|
+
entity.created_at ||= DateTime.now.to_s
|
84
|
+
entity.last_updated_at ||= entity.created_at
|
85
|
+
|
86
|
+
SDBM.open(@data_dir) do |db|
|
87
|
+
db[uid] = @serializer.dump(entity.to_hash)
|
88
|
+
end
|
89
|
+
uid
|
88
90
|
end
|
89
|
-
uid
|
90
91
|
end
|
91
92
|
|
92
93
|
alias_method :create, :add
|
@@ -97,11 +98,13 @@ module Dddr
|
|
97
98
|
|
98
99
|
def update(entity)
|
99
100
|
return unless entity.uid
|
100
|
-
|
101
|
-
|
102
|
-
|
101
|
+
@mutex.synchronize do
|
102
|
+
entity.last_updated_at = DateTime.now.to_s
|
103
|
+
SDBM.open(@data_dir) do |db|
|
104
|
+
db[entity.uid] = @serializer.dump(entity.to_hash)
|
105
|
+
end
|
106
|
+
entity
|
103
107
|
end
|
104
|
-
entity
|
105
108
|
end
|
106
109
|
|
107
110
|
alias_method :modify, :update
|
@@ -112,8 +115,10 @@ module Dddr
|
|
112
115
|
|
113
116
|
def delete(entity)
|
114
117
|
return unless entity.uid
|
115
|
-
|
116
|
-
|
118
|
+
@mutex.synchronize do
|
119
|
+
SDBM.open(@data_dir) do |db|
|
120
|
+
db.delete(entity.uid)
|
121
|
+
end
|
117
122
|
end
|
118
123
|
end
|
119
124
|
|
@@ -124,28 +129,32 @@ module Dddr
|
|
124
129
|
alias_method :wipe, :delete
|
125
130
|
|
126
131
|
def get(uid)
|
127
|
-
|
128
|
-
|
129
|
-
|
132
|
+
@mutex.synchronize do
|
133
|
+
SDBM.open(@data_dir) do |db|
|
134
|
+
data = db[uid]
|
135
|
+
return unless data
|
130
136
|
|
131
|
-
|
132
|
-
|
133
|
-
|
137
|
+
entity = @entity_class.new
|
138
|
+
entity.from_hash(uid, @serializer.load(data))
|
139
|
+
entity
|
140
|
+
end
|
134
141
|
end
|
135
142
|
end
|
136
143
|
|
137
144
|
alias_method :find, :get
|
138
145
|
|
139
146
|
def all
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
147
|
+
@mutex.synchronize do
|
148
|
+
result = []
|
149
|
+
SDBM.open(@data_dir) do |db|
|
150
|
+
db.each do |key, value|
|
151
|
+
entity = @entity_class.new
|
152
|
+
entity.from_hash(key, @serializer.load(value))
|
153
|
+
result << entity unless entity.deleted
|
154
|
+
end
|
146
155
|
end
|
156
|
+
result
|
147
157
|
end
|
148
|
-
result
|
149
158
|
end
|
150
159
|
end
|
151
160
|
|
@@ -159,11 +168,13 @@ module Dddr
|
|
159
168
|
end
|
160
169
|
|
161
170
|
def from_hash(uid, data_hash)
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
171
|
+
Mutex.new.synchronize do
|
172
|
+
self.uid = uid
|
173
|
+
data_hash&.each do |attribute, value|
|
174
|
+
attribute = attribute.to_sym
|
175
|
+
if respond_to?(:"#{attribute}=")
|
176
|
+
send(:"#{attribute}=", value)
|
177
|
+
end
|
167
178
|
end
|
168
179
|
end
|
169
180
|
end
|
data/lib/dddr/sequel.rb
CHANGED
@@ -1,6 +1,29 @@
|
|
1
|
+
require "sequel"
|
2
|
+
|
3
|
+
::Sequel::Model.plugin :timestamps
|
4
|
+
|
1
5
|
module Dddr
|
2
6
|
module Sequel
|
3
7
|
module ClassMethods
|
8
|
+
def create_properties(*names)
|
9
|
+
names.each do |name|
|
10
|
+
name.each do |name|
|
11
|
+
attr_accessor name
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def from_row(row)
|
17
|
+
item = new
|
18
|
+
item.from_hash(row[:uid], row)
|
19
|
+
item
|
20
|
+
end
|
21
|
+
|
22
|
+
def table(name = nil)
|
23
|
+
@@table_name = name unless name.nil?
|
24
|
+
@@table_name
|
25
|
+
end
|
26
|
+
|
4
27
|
def queries(&block)
|
5
28
|
@queries_module ||= Module.new
|
6
29
|
@queries_module.module_eval(&block)
|
@@ -30,63 +53,29 @@ module Dddr
|
|
30
53
|
end
|
31
54
|
|
32
55
|
class RepositoryBase
|
56
|
+
attr_reader :dataset
|
57
|
+
|
33
58
|
def initialize(entity_class)
|
34
59
|
@entity_class = entity_class
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
def setup_data_directory(entity_class)
|
39
|
-
env = Dddr.configuration.env
|
40
|
-
data_dir = Dddr.configuration.data_dir
|
41
|
-
container = Dddr.configuration.container
|
42
|
-
raise Dddr::Error, "Container name is required" unless container
|
43
|
-
|
44
|
-
# Construct the target directory path
|
45
|
-
@name = "#{data_dir}/#{container}/#{env}/#{entity_class.name.downcase}"
|
46
|
-
|
47
|
-
begin
|
48
|
-
# Ensure all directories in the path are created
|
49
|
-
FileUtils.mkdir_p(@name)
|
50
|
-
|
51
|
-
# Change ownership only if the path starts with "/var/"
|
52
|
-
if @name.start_with?("/var/")
|
53
|
-
FileUtils.chown_R(ENV["USER"], nil, @name)
|
54
|
-
end
|
55
|
-
rescue Errno::ENOENT => e
|
56
|
-
# Log or handle the error
|
57
|
-
raise "Failed to create or access the directory: #{e.message}"
|
58
|
-
end
|
60
|
+
@dataset = Dddr.configuration.db[@entity_class.table]
|
61
|
+
@entity_class.create_properties(@dataset.columns)
|
59
62
|
end
|
60
63
|
|
61
64
|
def count
|
62
|
-
|
65
|
+
@dataset.count
|
63
66
|
end
|
64
67
|
|
65
68
|
def add(entity)
|
66
69
|
uid = SecureRandom.uuid
|
67
70
|
entity.uid = uid
|
68
|
-
entity.
|
69
|
-
entity.last_updated_at ||= entity.created_at
|
70
|
-
|
71
|
-
SDBM.open(@name) do |db|
|
72
|
-
db[uid] = Marshal.dump(entity.to_hash)
|
73
|
-
end
|
71
|
+
@dataset.insert(entity.to_hash)
|
74
72
|
uid
|
75
73
|
end
|
76
|
-
|
77
74
|
alias_method :create, :add
|
78
75
|
alias_method :insert, :add
|
79
|
-
alias_method :put, :add
|
80
|
-
alias_method :append, :add
|
81
|
-
alias_method :store, :add
|
82
76
|
|
83
77
|
def update(entity)
|
84
|
-
|
85
|
-
entity.last_updated_at = DateTime.now.to_s
|
86
|
-
SDBM.open(@name) do |db|
|
87
|
-
db[entity.uid] = Marshal.dump(entity.to_hash)
|
88
|
-
end
|
89
|
-
entity
|
78
|
+
@dataset.where(uid: entity.uid).update(entity.to_hash)
|
90
79
|
end
|
91
80
|
|
92
81
|
alias_method :modify, :update
|
@@ -96,10 +85,7 @@ module Dddr
|
|
96
85
|
alias_method :alter, :update
|
97
86
|
|
98
87
|
def delete(entity)
|
99
|
-
|
100
|
-
SDBM.open(@name) do |db|
|
101
|
-
db.delete(entity.uid)
|
102
|
-
end
|
88
|
+
@dataset.where(uid: entity.uid).delete
|
103
89
|
end
|
104
90
|
|
105
91
|
alias_method :remove, :delete
|
@@ -109,28 +95,21 @@ module Dddr
|
|
109
95
|
alias_method :wipe, :delete
|
110
96
|
|
111
97
|
def get(uid)
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
entity.from_hash(uid, Marshal.load(data))
|
118
|
-
entity
|
119
|
-
end
|
98
|
+
row = @dataset.where(uid: uid).first
|
99
|
+
raise Dddr::Error, "UID is required" unless row
|
100
|
+
item = Item.new
|
101
|
+
item.from_hash(uid, row)
|
102
|
+
item
|
120
103
|
end
|
121
104
|
|
122
105
|
alias_method :find, :get
|
123
106
|
|
124
107
|
def all
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
entity.from_hash(key, Marshal.load(value))
|
130
|
-
result << entity unless entity.deleted
|
131
|
-
end
|
108
|
+
@dataset.all.map do |row|
|
109
|
+
item = Item.new
|
110
|
+
item.from_hash(row[:uid], row)
|
111
|
+
item
|
132
112
|
end
|
133
|
-
result
|
134
113
|
end
|
135
114
|
end
|
136
115
|
|
data/lib/dddr/version.rb
CHANGED
data/lib/dddr.rb
CHANGED
@@ -3,12 +3,12 @@
|
|
3
3
|
require "sdbm"
|
4
4
|
require "securerandom"
|
5
5
|
require "date"
|
6
|
-
require
|
6
|
+
require "csv"
|
7
7
|
require "ostruct"
|
8
8
|
require "fileutils"
|
9
9
|
require "oj"
|
10
10
|
require "msgpack"
|
11
|
-
|
11
|
+
require "thread"
|
12
12
|
require_relative "dddr/sdbm"
|
13
13
|
require_relative "dddr/sequel"
|
14
14
|
require_relative "dddr/version"
|
@@ -22,8 +22,9 @@ module Dddr
|
|
22
22
|
|
23
23
|
class MessagePack
|
24
24
|
def self.load(obj)
|
25
|
-
::MessagePack.unpack(obj)
|
25
|
+
::MessagePack.unpack(obj)
|
26
26
|
end
|
27
|
+
|
27
28
|
def self.dump(obj)
|
28
29
|
obj.to_msgpack
|
29
30
|
end
|
@@ -31,15 +32,20 @@ module Dddr
|
|
31
32
|
|
32
33
|
class Serializer
|
33
34
|
def initialize(engine)
|
35
|
+
@mutex = Mutex.new
|
34
36
|
@engine = engine
|
35
37
|
end
|
36
38
|
|
37
39
|
def dump(obj)
|
38
|
-
@
|
40
|
+
@mutex.synchronize do
|
41
|
+
@engine.dump(obj)
|
42
|
+
end
|
39
43
|
end
|
40
44
|
|
41
45
|
def load(obj)
|
42
|
-
@
|
46
|
+
@mutex.synchronize do
|
47
|
+
@engine.load(obj)
|
48
|
+
end
|
43
49
|
end
|
44
50
|
end
|
45
51
|
|
@@ -55,6 +61,12 @@ module Dddr
|
|
55
61
|
class Error < StandardError; end
|
56
62
|
|
57
63
|
module Entity
|
64
|
+
def self.create_properties(*names)
|
65
|
+
names.each do |name|
|
66
|
+
attr_accessor name
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
58
70
|
def self.included(base)
|
59
71
|
if Dddr.configuration.engine == :sdbm
|
60
72
|
Dddr::ENTITIES << base
|
@@ -63,7 +75,7 @@ module Dddr
|
|
63
75
|
end
|
64
76
|
if Dddr.configuration.engine == :sequel
|
65
77
|
include Dddr::Sequel
|
66
|
-
base.extend(Dddr::
|
78
|
+
base.extend(Dddr::Sequel::ClassMethods)
|
67
79
|
end
|
68
80
|
end
|
69
81
|
|
metadata
CHANGED
@@ -1,57 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dddr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Delaney Burke
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sdbm
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: sequel
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: oj
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '0'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: msgpack
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -77,6 +77,7 @@ extensions: []
|
|
77
77
|
extra_rdoc_files: []
|
78
78
|
files:
|
79
79
|
- ".rspec"
|
80
|
+
- ".rspecrc"
|
80
81
|
- ".standard.yml"
|
81
82
|
- CHANGELOG.md
|
82
83
|
- CODE_OF_CONDUCT.md
|