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