author | Mahlon E. Smith <mahlon@martini.nu> |
Thu, 29 Nov 2018 10:20:18 -0800 (2018-11-29) | |
changeset 15 | 38ea95105574 |
parent 13 | 686fbfe638bd |
child 16 | 87b0a389b804 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- ruby -*- |
2 |
#encoding: utf-8 |
|
3 |
||
4 |
require 'loggability' |
|
5 |
require 'configurability' |
|
6 |
require 'sequel' |
|
7 |
require 'strelka' |
|
8 |
require 'strelka/mixins' |
|
9 |
||
10 |
require 'thingfish' |
|
11 |
require 'thingfish/mixins' |
|
12 |
require 'thingfish/metastore' |
|
13 |
||
14 |
# Toplevel namespace |
|
15 |
class Thingfish::Metastore::PgGraph < Thingfish::Metastore |
|
16 |
extend Loggability, |
|
17 |
Configurability, |
|
18 |
Strelka::MethodUtilities |
|
19 |
include Thingfish::Normalization |
|
20 |
||
21 |
||
22 |
# Load Sequel extensions/plugins |
|
23 |
Sequel.extension :migration |
|
24 |
||
25 |
||
26 |
# Package version |
|
15 | 27 |
VERSION = '0.3.1' |
0 | 28 |
|
29 |
# Version control revision |
|
15 | 30 |
REVISION = %q$Revision: 686fbfe638bd $ |
0 | 31 |
|
32 |
# The data directory that contains migration files. |
|
33 |
# |
|
34 |
DATADIR = if ENV['THINGFISH_METASTORE_PGGRAPH_DATADIR'] |
|
15 | 35 |
Pathname( ENV['PEDIA_DATADIR'] ) |
36 |
elsif Gem.loaded_specs[ 'thingfish-metastore-pggraph' ] && File.exist?( Gem.loaded_specs['thingfish-metastore-pggraph'].datadir ) |
|
37 |
Pathname( Gem.loaded_specs['thingfish-metastore-pggraph'].datadir ) |
|
38 |
else |
|
39 |
Pathname( __FILE__ ).dirname.parent.parent.parent + 'data/thingfish-metastore-pggraph' |
|
40 |
end |
|
0 | 41 |
|
42 |
log_as :thingfish_metastore_pggraph |
|
43 |
||
44 |
# Configurability API -- load the `pg_metastore` |
|
11
b1f97f6063a3
Update for Configurability 3.x.
Mahlon E. Smith <mahlon@martini.nu>
parents:
8
diff
changeset
|
45 |
configurability 'thingfish.pggraph_metastore' do |
b1f97f6063a3
Update for Configurability 3.x.
Mahlon E. Smith <mahlon@martini.nu>
parents:
8
diff
changeset
|
46 |
|
b1f97f6063a3
Update for Configurability 3.x.
Mahlon E. Smith <mahlon@martini.nu>
parents:
8
diff
changeset
|
47 |
# The Sequel::Database that's used to access the metastore tables |
b1f97f6063a3
Update for Configurability 3.x.
Mahlon E. Smith <mahlon@martini.nu>
parents:
8
diff
changeset
|
48 |
setting :uri, default: 'postgres:/thingfish' |
0 | 49 |
|
11
b1f97f6063a3
Update for Configurability 3.x.
Mahlon E. Smith <mahlon@martini.nu>
parents:
8
diff
changeset
|
50 |
# The number of seconds to consider a "slow" query |
b1f97f6063a3
Update for Configurability 3.x.
Mahlon E. Smith <mahlon@martini.nu>
parents:
8
diff
changeset
|
51 |
setting :slow_query_seconds, default: 0.01 |
b1f97f6063a3
Update for Configurability 3.x.
Mahlon E. Smith <mahlon@martini.nu>
parents:
8
diff
changeset
|
52 |
end |
0 | 53 |
|
54 |
# The Sequel::Database that's used to access the metastore tables |
|
55 |
singleton_attr_accessor :db |
|
56 |
||
57 |
||
58 |
### Set up the metastore database and migrate to the latest version. |
|
59 |
def self::setup_database |
|
60 |
Sequel.extension :pg_json_ops |
|
13
686fbfe638bd
Remove deprecated calls for the Sequel ORM, add compatibility for Sequel 5.
Mahlon E. Smith <mahlon@laika.com>
parents:
11
diff
changeset
|
61 |
Sequel.split_symbols = true |
686fbfe638bd
Remove deprecated calls for the Sequel ORM, add compatibility for Sequel 5.
Mahlon E. Smith <mahlon@laika.com>
parents:
11
diff
changeset
|
62 |
Sequel::Model.require_valid_table = false |
0 | 63 |
|
64 |
self.db = Sequel.connect( self.uri ) |
|
65 |
self.db.logger = Loggability[ Thingfish::Metastore::PgGraph ] |
|
66 |
self.db.extension :pg_streaming |
|
67 |
self.db.stream_all_queries = true |
|
68 |
self.db.sql_log_level = :debug |
|
69 |
self.db.extension( :pg_json ) |
|
70 |
self.db.log_warn_duration = self.slow_query_seconds |
|
71 |
||
72 |
# Ensure the database is current. |
|
73 |
# |
|
74 |
unless Sequel::Migrator.is_current?( self.db, self.migrations_dir.to_s ) |
|
75 |
self.log.info "Installing database schema..." |
|
76 |
Sequel::Migrator.apply( self.db, self.migrations_dir.to_s ) |
|
77 |
end |
|
78 |
end |
|
79 |
||
80 |
||
81 |
### Tear down the configured metastore database. |
|
82 |
def self::teardown_database |
|
83 |
self.log.info "Tearing down database schema..." |
|
84 |
Sequel::Migrator.apply( self.db, self.migrations_dir.to_s, 0 ) |
|
85 |
end |
|
86 |
||
87 |
||
88 |
### Return the current database migrations directory as a Pathname |
|
89 |
def self::migrations_dir |
|
90 |
return DATADIR + 'migrations' |
|
91 |
end |
|
92 |
||
93 |
||
94 |
### Configurability API -- set up the metastore with the `pg_metastore` section of |
|
95 |
### the config file. |
|
96 |
def self::configure( config=nil ) |
|
11
b1f97f6063a3
Update for Configurability 3.x.
Mahlon E. Smith <mahlon@martini.nu>
parents:
8
diff
changeset
|
97 |
super |
b1f97f6063a3
Update for Configurability 3.x.
Mahlon E. Smith <mahlon@martini.nu>
parents:
8
diff
changeset
|
98 |
self.setup_database if self.uri |
0 | 99 |
end |
100 |
||
101 |
||
102 |
### Set up the metastore. |
|
103 |
def initialize( * ) # :notnew: |
|
104 |
require 'thingfish/metastore/pggraph/node' |
|
105 |
require 'thingfish/metastore/pggraph/edge' |
|
13
686fbfe638bd
Remove deprecated calls for the Sequel ORM, add compatibility for Sequel 5.
Mahlon E. Smith <mahlon@laika.com>
parents:
11
diff
changeset
|
106 |
|
686fbfe638bd
Remove deprecated calls for the Sequel ORM, add compatibility for Sequel 5.
Mahlon E. Smith <mahlon@laika.com>
parents:
11
diff
changeset
|
107 |
Thingfish::Metastore::PgGraph::Node.dataset = self.class.db[ :nodes ] |
686fbfe638bd
Remove deprecated calls for the Sequel ORM, add compatibility for Sequel 5.
Mahlon E. Smith <mahlon@laika.com>
parents:
11
diff
changeset
|
108 |
Thingfish::Metastore::PgGraph::Edge.dataset = self.class.db[ :edges ] |
0 | 109 |
@model = Thingfish::Metastore::PgGraph::Node |
110 |
end |
|
111 |
||
112 |
||
113 |
###### |
|
114 |
public |
|
115 |
###### |
|
116 |
||
117 |
## |
|
118 |
# The Sequel model representing the metadata rows. |
|
119 |
attr_reader :model |
|
120 |
||
121 |
||
122 |
# |
|
123 |
# :section: Thingfish::Metastore API |
|
124 |
# |
|
125 |
||
126 |
### Return an Array of all stored oids. |
|
127 |
def oids |
|
128 |
return self.each_oid.to_a |
|
129 |
end |
|
130 |
||
131 |
||
132 |
### Iterate over each of the store's oids, yielding to the block if one is given |
|
133 |
### or returning an Enumerator if one is not. |
|
134 |
def each_oid( &block ) |
|
135 |
return self.model.select_map( :id ).each( &block ) |
|
136 |
end |
|
137 |
||
138 |
||
139 |
### Save the +metadata+ Hash for the specified +oid+. |
|
140 |
def save( oid, metadata ) |
|
141 |
md = self.model.from_hash( metadata ) |
|
142 |
md.id = oid |
|
143 |
md.save |
|
144 |
end |
|
145 |
||
146 |
||
147 |
### Fetch the data corresponding to the given +oid+ as a Hash-ish object. |
|
148 |
def fetch( oid, *keys ) |
|
149 |
metadata = self.model[ oid ] or return nil |
|
150 |
||
151 |
if keys.empty? |
|
152 |
return metadata.to_hash |
|
153 |
else |
|
154 |
keys = normalize_keys( keys ) |
|
155 |
values = metadata.to_hash.values_at( *keys ) |
|
156 |
return Hash[ [keys, values].transpose ] |
|
157 |
end |
|
158 |
end |
|
159 |
||
160 |
||
161 |
### Fetch the value of the metadata associated with the given +key+ for the |
|
162 |
### specified +oid+. |
|
163 |
def fetch_value( oid, key ) |
|
164 |
metadata = self.model[ oid ] or return nil |
|
3
5b4ee98d698c
Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
165 |
key = key.to_sym |
5b4ee98d698c
Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
166 |
return metadata[ key ] || metadata.user_metadata[ key ] |
0 | 167 |
end |
168 |
||
169 |
||
170 |
### Fetch UUIDs related to the given +oid+. |
|
171 |
def fetch_related_oids( oid ) |
|
172 |
oid = normalize_oid( oid ) |
|
173 |
oid = self.model[ oid ] |
|
174 |
return [] unless oid |
|
175 |
return oid.related_nodes.map( &:id_c ) |
|
176 |
end |
|
177 |
||
178 |
||
179 |
### Search the metastore for UUIDs which match the specified +criteria+ and |
|
180 |
### return them as an iterator. |
|
181 |
def search( options={} ) |
|
182 |
ds = self.model.naked.select( :id ) |
|
183 |
self.log.debug "Starting search with %p" % [ ds ] |
|
184 |
||
185 |
ds = self.omit_related_resources( ds, options ) |
|
186 |
ds = self.apply_search_criteria( ds, options ) |
|
187 |
ds = self.apply_search_order( ds, options ) |
|
188 |
ds = self.apply_search_direction( ds, options ) |
|
189 |
ds = self.apply_search_limit( ds, options ) |
|
190 |
||
3
5b4ee98d698c
Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
191 |
self.log.debug "Dataset for search is: %s" % [ ds.sql ] |
5b4ee98d698c
Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
192 |
|
0 | 193 |
return ds.map {|row| row[:id] } |
194 |
end |
|
195 |
||
196 |
||
197 |
### Update the metadata for the given +oid+ with the specified +values+ hash. |
|
198 |
def merge( oid, values ) |
|
199 |
values = normalize_keys( values ) |
|
200 |
||
201 |
md = self.model[ oid ] or return nil |
|
202 |
md.merge!( values ) |
|
203 |
md.save |
|
204 |
end |
|
205 |
||
206 |
||
207 |
### Remove all metadata associated with +oid+ from the Metastore. |
|
208 |
def remove( oid, *keys ) |
|
209 |
self.model[ id: oid ].destroy |
|
210 |
end |
|
211 |
||
212 |
||
213 |
### Remove all metadata associated with +oid+ except for the specified +keys+. |
|
214 |
def remove_except( oid, *keys ) |
|
215 |
keys = normalize_keys( keys ) |
|
216 |
||
217 |
md = self.model[ oid ] or return nil |
|
218 |
md.user_metadata.keep_if {|key,_| keys.include?(key) } |
|
219 |
md.save |
|
220 |
end |
|
221 |
||
222 |
||
223 |
### Returns +true+ if the metastore has metadata associated with the specified +oid+. |
|
224 |
def include?( oid ) |
|
225 |
return self.model.count( id: oid ).nonzero? |
|
226 |
end |
|
227 |
||
228 |
||
229 |
### Returns the number of objects the store contains. |
|
230 |
def size |
|
231 |
return self.model.count |
|
232 |
end |
|
233 |
||
234 |
||
235 |
######### |
|
236 |
protected |
|
237 |
######### |
|
238 |
||
239 |
### Omit related resources from the search dataset +ds+ unless the given |
|
240 |
### +options+ specify otherwise. |
|
241 |
def omit_related_resources( ds, options ) |
|
242 |
unless options[:include_related] |
|
243 |
self.log.debug " omitting entries for related resources" |
|
244 |
ds = ds.unrelated |
|
245 |
end |
|
246 |
return ds |
|
247 |
end |
|
248 |
||
249 |
||
250 |
### Apply the search :criteria from the specified +options+ to the collection |
|
251 |
### in +ds+ and return the modified dataset. |
|
252 |
def apply_search_criteria( ds, options ) |
|
253 |
if (( criteria = options[:criteria] )) |
|
254 |
criteria.each do |field, value| |
|
255 |
self.log.debug " applying criteria: %p => %p" % [ field.to_s, value ] |
|
256 |
ds = ds.where_metadata( field => value ) |
|
257 |
end |
|
258 |
end |
|
259 |
||
260 |
return ds |
|
261 |
end |
|
262 |
||
263 |
||
264 |
### Apply the search :order from the specified +options+ to the collection in |
|
265 |
### +ds+ and return the modified dataset. |
|
266 |
def apply_search_order( ds, options ) |
|
267 |
if options[:order] |
|
268 |
columns = Array( options[:order] ) |
|
3
5b4ee98d698c
Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
269 |
self.log.debug " ordering results by columns: %p" % [ columns ] |
5b4ee98d698c
Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
270 |
ds = ds.order_metadata( columns ) |
0 | 271 |
end |
272 |
||
273 |
return ds |
|
274 |
end |
|
275 |
||
276 |
||
277 |
### Apply the search :direction from the specified +options+ to the collection |
|
278 |
### in +ds+ and return the modified dataset. |
|
279 |
def apply_search_direction( ds, options ) |
|
280 |
ds = ds.reverse if options[:direction] && options[:direction] == 'desc' |
|
281 |
return ds |
|
282 |
end |
|
283 |
||
284 |
||
285 |
### Apply the search :limit from the specified +options+ to the collection in |
|
286 |
### +ds+ and return the modified dataset. |
|
287 |
def apply_search_limit( ds, options ) |
|
288 |
if (( limit = options[:limit] )) |
|
289 |
self.log.debug " limiting to %s results" % [ limit ] |
|
290 |
offset = options[:offset] || 0 |
|
291 |
ds = ds.limit( limit, offset ) |
|
292 |
end |
|
293 |
||
294 |
return ds |
|
295 |
end |
|
296 |
||
297 |
end # class Thingfish::Metastore::PgGraph |
|
298 |