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