author | Mahlon E. Smith <mahlon@laika.com> |
Mon, 16 Jan 2017 12:48:37 -0800 | |
changeset 12 | 2f9a295bdba7 |
parent 11 | b1f97f6063a3 |
child 13 | 686fbfe638bd |
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 |
|
11
b1f97f6063a3
Update for Configurability 3.x.
Mahlon E. Smith <mahlon@martini.nu>
parents:
8
diff
changeset
|
27 |
VERSION = '0.2.0' |
0 | 28 |
|
29 |
# Version control revision |
|
30 |
REVISION = %q$Revision$ |
|
31 |
||
32 |
# The data directory that contains migration files. |
|
33 |
# |
|
34 |
DATADIR = if ENV['THINGFISH_METASTORE_PGGRAPH_DATADIR'] |
|
35 |
Pathname.new( ENV['THINGFISH_METASTORE_PGGRAPH_DATADIR'] ) |
|
36 |
elsif Gem.datadir( 'thingfish-metastore-pggraph' ) |
|
37 |
Pathname.new( Gem.datadir('thingfish-metastore-pggraph') ) |
|
38 |
else |
|
39 |
Pathname.new( __FILE__ ).dirname.parent.parent.parent + |
|
40 |
'data' + 'thingfish-metastore-pggraph' |
|
41 |
end |
|
42 |
||
43 |
log_as :thingfish_metastore_pggraph |
|
44 |
||
45 |
# Configurability API -- load the `pg_metastore` |
|
11
b1f97f6063a3
Update for Configurability 3.x.
Mahlon E. Smith <mahlon@martini.nu>
parents:
8
diff
changeset
|
46 |
configurability 'thingfish.pggraph_metastore' do |
b1f97f6063a3
Update for Configurability 3.x.
Mahlon E. Smith <mahlon@martini.nu>
parents:
8
diff
changeset
|
47 |
|
b1f97f6063a3
Update for Configurability 3.x.
Mahlon E. Smith <mahlon@martini.nu>
parents:
8
diff
changeset
|
48 |
# 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
|
49 |
setting :uri, default: 'postgres:/thingfish' |
0 | 50 |
|
11
b1f97f6063a3
Update for Configurability 3.x.
Mahlon E. Smith <mahlon@martini.nu>
parents:
8
diff
changeset
|
51 |
# 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
|
52 |
setting :slow_query_seconds, default: 0.01 |
b1f97f6063a3
Update for Configurability 3.x.
Mahlon E. Smith <mahlon@martini.nu>
parents:
8
diff
changeset
|
53 |
end |
0 | 54 |
|
55 |
# The Sequel::Database that's used to access the metastore tables |
|
56 |
singleton_attr_accessor :db |
|
57 |
||
58 |
||
59 |
### Set up the metastore database and migrate to the latest version. |
|
60 |
def self::setup_database |
|
61 |
Sequel.extension :pg_json_ops |
|
62 |
||
63 |
self.db = Sequel.connect( self.uri ) |
|
64 |
self.db.logger = Loggability[ Thingfish::Metastore::PgGraph ] |
|
65 |
self.db.extension :pg_streaming |
|
66 |
self.db.stream_all_queries = true |
|
67 |
self.db.optimize_model_load = 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' |
|
106 |
Thingfish::Metastore::PgGraph::Node.db = self.class.db |
|
107 |
Thingfish::Metastore::PgGraph::Edge.db = self.class.db |
|
108 |
@model = Thingfish::Metastore::PgGraph::Node |
|
109 |
end |
|
110 |
||
111 |
||
112 |
###### |
|
113 |
public |
|
114 |
###### |
|
115 |
||
116 |
## |
|
117 |
# The Sequel model representing the metadata rows. |
|
118 |
attr_reader :model |
|
119 |
||
120 |
||
121 |
# |
|
122 |
# :section: Thingfish::Metastore API |
|
123 |
# |
|
124 |
||
125 |
### Return an Array of all stored oids. |
|
126 |
def oids |
|
127 |
return self.each_oid.to_a |
|
128 |
end |
|
129 |
||
130 |
||
131 |
### Iterate over each of the store's oids, yielding to the block if one is given |
|
132 |
### or returning an Enumerator if one is not. |
|
133 |
def each_oid( &block ) |
|
134 |
return self.model.select_map( :id ).each( &block ) |
|
135 |
end |
|
136 |
||
137 |
||
138 |
### Save the +metadata+ Hash for the specified +oid+. |
|
139 |
def save( oid, metadata ) |
|
140 |
md = self.model.from_hash( metadata ) |
|
141 |
md.id = oid |
|
142 |
md.save |
|
143 |
end |
|
144 |
||
145 |
||
146 |
### Fetch the data corresponding to the given +oid+ as a Hash-ish object. |
|
147 |
def fetch( oid, *keys ) |
|
148 |
metadata = self.model[ oid ] or return nil |
|
149 |
||
150 |
if keys.empty? |
|
151 |
return metadata.to_hash |
|
152 |
else |
|
153 |
keys = normalize_keys( keys ) |
|
154 |
values = metadata.to_hash.values_at( *keys ) |
|
155 |
return Hash[ [keys, values].transpose ] |
|
156 |
end |
|
157 |
end |
|
158 |
||
159 |
||
160 |
### Fetch the value of the metadata associated with the given +key+ for the |
|
161 |
### specified +oid+. |
|
162 |
def fetch_value( oid, key ) |
|
163 |
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
|
164 |
key = key.to_sym |
5b4ee98d698c
Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
165 |
return metadata[ key ] || metadata.user_metadata[ key ] |
0 | 166 |
end |
167 |
||
168 |
||
169 |
### Fetch UUIDs related to the given +oid+. |
|
170 |
def fetch_related_oids( oid ) |
|
171 |
oid = normalize_oid( oid ) |
|
172 |
oid = self.model[ oid ] |
|
173 |
return [] unless oid |
|
174 |
return oid.related_nodes.map( &:id_c ) |
|
175 |
end |
|
176 |
||
177 |
||
178 |
### Search the metastore for UUIDs which match the specified +criteria+ and |
|
179 |
### return them as an iterator. |
|
180 |
def search( options={} ) |
|
181 |
ds = self.model.naked.select( :id ) |
|
182 |
self.log.debug "Starting search with %p" % [ ds ] |
|
183 |
||
184 |
ds = self.omit_related_resources( ds, options ) |
|
185 |
ds = self.apply_search_criteria( ds, options ) |
|
186 |
ds = self.apply_search_order( ds, options ) |
|
187 |
ds = self.apply_search_direction( ds, options ) |
|
188 |
ds = self.apply_search_limit( ds, options ) |
|
189 |
||
3
5b4ee98d698c
Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
190 |
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
|
191 |
|
0 | 192 |
return ds.map {|row| row[:id] } |
193 |
end |
|
194 |
||
195 |
||
196 |
### Update the metadata for the given +oid+ with the specified +values+ hash. |
|
197 |
def merge( oid, values ) |
|
198 |
values = normalize_keys( values ) |
|
199 |
||
200 |
md = self.model[ oid ] or return nil |
|
201 |
md.merge!( values ) |
|
202 |
md.save |
|
203 |
end |
|
204 |
||
205 |
||
206 |
### Remove all metadata associated with +oid+ from the Metastore. |
|
207 |
def remove( oid, *keys ) |
|
208 |
self.model[ id: oid ].destroy |
|
209 |
end |
|
210 |
||
211 |
||
212 |
### Remove all metadata associated with +oid+ except for the specified +keys+. |
|
213 |
def remove_except( oid, *keys ) |
|
214 |
keys = normalize_keys( keys ) |
|
215 |
||
216 |
md = self.model[ oid ] or return nil |
|
217 |
md.user_metadata.keep_if {|key,_| keys.include?(key) } |
|
218 |
md.save |
|
219 |
end |
|
220 |
||
221 |
||
222 |
### Returns +true+ if the metastore has metadata associated with the specified +oid+. |
|
223 |
def include?( oid ) |
|
224 |
return self.model.count( id: oid ).nonzero? |
|
225 |
end |
|
226 |
||
227 |
||
228 |
### Returns the number of objects the store contains. |
|
229 |
def size |
|
230 |
return self.model.count |
|
231 |
end |
|
232 |
||
233 |
||
234 |
######### |
|
235 |
protected |
|
236 |
######### |
|
237 |
||
238 |
### Omit related resources from the search dataset +ds+ unless the given |
|
239 |
### +options+ specify otherwise. |
|
240 |
def omit_related_resources( ds, options ) |
|
241 |
unless options[:include_related] |
|
242 |
self.log.debug " omitting entries for related resources" |
|
243 |
ds = ds.unrelated |
|
244 |
end |
|
245 |
return ds |
|
246 |
end |
|
247 |
||
248 |
||
249 |
### Apply the search :criteria from the specified +options+ to the collection |
|
250 |
### in +ds+ and return the modified dataset. |
|
251 |
def apply_search_criteria( ds, options ) |
|
252 |
if (( criteria = options[:criteria] )) |
|
253 |
criteria.each do |field, value| |
|
254 |
self.log.debug " applying criteria: %p => %p" % [ field.to_s, value ] |
|
255 |
ds = ds.where_metadata( field => value ) |
|
256 |
end |
|
257 |
end |
|
258 |
||
259 |
return ds |
|
260 |
end |
|
261 |
||
262 |
||
263 |
### Apply the search :order from the specified +options+ to the collection in |
|
264 |
### +ds+ and return the modified dataset. |
|
265 |
def apply_search_order( ds, options ) |
|
266 |
if options[:order] |
|
267 |
columns = Array( options[:order] ) |
|
3
5b4ee98d698c
Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents:
1
diff
changeset
|
268 |
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
|
269 |
ds = ds.order_metadata( columns ) |
0 | 270 |
end |
271 |
||
272 |
return ds |
|
273 |
end |
|
274 |
||
275 |
||
276 |
### Apply the search :direction from the specified +options+ to the collection |
|
277 |
### in +ds+ and return the modified dataset. |
|
278 |
def apply_search_direction( ds, options ) |
|
279 |
ds = ds.reverse if options[:direction] && options[:direction] == 'desc' |
|
280 |
return ds |
|
281 |
end |
|
282 |
||
283 |
||
284 |
### Apply the search :limit from the specified +options+ to the collection in |
|
285 |
### +ds+ and return the modified dataset. |
|
286 |
def apply_search_limit( ds, options ) |
|
287 |
if (( limit = options[:limit] )) |
|
288 |
self.log.debug " limiting to %s results" % [ limit ] |
|
289 |
offset = options[:offset] || 0 |
|
290 |
ds = ds.limit( limit, offset ) |
|
291 |
end |
|
292 |
||
293 |
return ds |
|
294 |
end |
|
295 |
||
296 |
end # class Thingfish::Metastore::PgGraph |
|
297 |