lib/thingfish/metastore/pggraph.rb
author Mahlon E. Smith <mahlon@laika.com>
Wed, 17 Jan 2018 13:33:12 -0800
changeset 13 686fbfe638bd
parent 11 b1f97f6063a3
child 15 38ea95105574
permissions -rw-r--r--
Remove deprecated calls for the Sequel ORM, add compatibility for Sequel 5.
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
13
686fbfe638bd Remove deprecated calls for the Sequel ORM, add compatibility for Sequel 5.
Mahlon E. Smith <mahlon@laika.com>
parents: 11
diff changeset
    27
	VERSION = '0.3.0'
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    30
	REVISION = %q$Revision$
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']
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    35
			Pathname.new( ENV['THINGFISH_METASTORE_PGGRAPH_DATADIR'] )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    36
		elsif Gem.datadir( 'thingfish-metastore-pggraph' )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    37
			Pathname.new( Gem.datadir('thingfish-metastore-pggraph') )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    38
		else
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    39
			Pathname.new( __FILE__ ).dirname.parent.parent.parent +
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    40
				'data' + 'thingfish-metastore-pggraph'
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    41
		end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    42
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    43
	log_as :thingfish_metastore_pggraph
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    44
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    54
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    55
	# The Sequel::Database that's used to access the metastore tables
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    56
	singleton_attr_accessor :db
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    59
	### Set up the metastore database and migrate to the latest version.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    60
	def self::setup_database
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    61
		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
    62
		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
    63
		Sequel::Model.require_valid_table = false
0
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    64
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    65
		self.db = Sequel.connect( self.uri )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    66
		self.db.logger = Loggability[ Thingfish::Metastore::PgGraph ]
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    67
		self.db.extension :pg_streaming
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    68
		self.db.stream_all_queries = true
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    69
		self.db.sql_log_level = :debug
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    70
		self.db.extension( :pg_json )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    71
		self.db.log_warn_duration = self.slow_query_seconds
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    72
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    73
		# Ensure the database is current.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    74
		#
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    75
		unless Sequel::Migrator.is_current?( self.db, self.migrations_dir.to_s )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    76
			self.log.info "Installing database schema..."
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    77
			Sequel::Migrator.apply( self.db, self.migrations_dir.to_s )
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
	end
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    82
	### Tear down the configured metastore database.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    83
	def self::teardown_database
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    84
		self.log.info "Tearing down database schema..."
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    85
		Sequel::Migrator.apply( self.db, self.migrations_dir.to_s, 0 )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    86
	end
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    89
	### Return the current database migrations directory as a Pathname
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    90
	def self::migrations_dir
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    91
		return DATADIR + 'migrations'
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    92
	end
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    95
	### Configurability API -- set up the metastore with the `pg_metastore` section of
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    96
	### the config file.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    97
	def self::configure( config=nil )
11
b1f97f6063a3 Update for Configurability 3.x.
Mahlon E. Smith <mahlon@martini.nu>
parents: 8
diff changeset
    98
		super
b1f97f6063a3 Update for Configurability 3.x.
Mahlon E. Smith <mahlon@martini.nu>
parents: 8
diff changeset
    99
		self.setup_database if self.uri
0
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   100
	end
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   103
	### Set up the metastore.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   104
	def initialize( * ) # :notnew:
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   105
		require 'thingfish/metastore/pggraph/node'
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   106
		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
   107
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::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
   109
		Thingfish::Metastore::PgGraph::Edge.dataset = self.class.db[ :edges ]
0
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   110
		@model = Thingfish::Metastore::PgGraph::Node
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   111
	end
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
	######
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   115
	public
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
	##
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   119
	# The Sequel model representing the metadata rows.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   120
	attr_reader :model
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
	#
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   124
	# :section: Thingfish::Metastore API
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   127
	### Return an Array of all stored oids.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   128
	def oids
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   129
		return self.each_oid.to_a
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   130
	end
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   133
	### 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
   134
	### or returning an Enumerator if one is not.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   135
	def each_oid( &block )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   136
		return self.model.select_map( :id ).each( &block )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   137
	end
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   140
	### Save the +metadata+ Hash for the specified +oid+.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   141
	def save( oid, metadata )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   142
		md = self.model.from_hash( metadata )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   143
		md.id = oid
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   144
		md.save
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   145
	end
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   148
	### 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
   149
	def fetch( oid, *keys )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   150
		metadata = self.model[ oid ] or return nil
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   151
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   152
		if keys.empty?
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   153
			return metadata.to_hash
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   154
		else
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   155
			keys = normalize_keys( keys )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   156
			values = metadata.to_hash.values_at( *keys )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   157
			return Hash[ [keys, values].transpose ]
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
	end
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   162
	### 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
   163
	### specified +oid+.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   164
	def fetch_value( oid, key )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   165
		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
   166
		key = key.to_sym
5b4ee98d698c Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   167
		return metadata[ key ] || metadata.user_metadata[ key ]
0
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   168
	end
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   171
	### Fetch UUIDs related to the given +oid+.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   172
	def fetch_related_oids( oid )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   173
		oid = normalize_oid( oid )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   174
		oid = self.model[ oid ]
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   175
		return [] unless oid
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   176
		return oid.related_nodes.map( &:id_c )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   177
	end
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   180
	### Search the metastore for UUIDs which match the specified +criteria+ and
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   181
	### return them as an iterator.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   182
	def search( options={} )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   183
		ds = self.model.naked.select( :id )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   184
		self.log.debug "Starting search with %p" % [ ds ]
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   185
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   186
		ds = self.omit_related_resources( ds, options )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   187
		ds = self.apply_search_criteria( ds, options )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   188
		ds = self.apply_search_order( ds, options )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   189
		ds = self.apply_search_direction( ds, options )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   190
		ds = self.apply_search_limit( ds, options )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   191
3
5b4ee98d698c Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   192
		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
   193
0
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   194
		return ds.map {|row| row[:id] }
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   195
	end
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   198
	### Update the metadata for the given +oid+ with the specified +values+ hash.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   199
	def merge( oid, values )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   200
		values = normalize_keys( values )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   201
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   202
		md = self.model[ oid ] or return nil
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   203
		md.merge!( values )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   204
		md.save
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   205
	end
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   208
	### Remove all metadata associated with +oid+ from the Metastore.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   209
	def remove( oid, *keys )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   210
		self.model[ id: oid ].destroy
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   211
	end
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   214
	### Remove all metadata associated with +oid+ except for the specified +keys+.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   215
	def remove_except( oid, *keys )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   216
		keys = normalize_keys( keys )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   217
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   218
		md = self.model[ oid ] or return nil
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   219
		md.user_metadata.keep_if {|key,_| keys.include?(key) }
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   220
		md.save
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   221
	end
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   224
	### Returns +true+ if the metastore has metadata associated with the specified +oid+.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   225
	def include?( oid )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   226
		return self.model.count( id: oid ).nonzero?
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   227
	end
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   230
	### Returns the number of objects the store contains.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   231
	def size
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   232
		return self.model.count
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   233
	end
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
	#########
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   237
	protected
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   240
	### Omit related resources from the search dataset +ds+ unless the given
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   241
	### +options+ specify otherwise.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   242
	def omit_related_resources( ds, options )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   243
		unless options[:include_related]
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   244
			self.log.debug "  omitting entries for related resources"
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   245
			ds = ds.unrelated
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   246
		end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   247
		return ds
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   248
	end
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   251
	### Apply the search :criteria from the specified +options+ to the collection
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   252
	### in +ds+ and return the modified dataset.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   253
	def apply_search_criteria( ds, options )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   254
		if (( criteria = options[:criteria] ))
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   255
			criteria.each do |field, value|
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   256
				self.log.debug "  applying criteria: %p => %p" % [ field.to_s, value ]
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   257
				ds = ds.where_metadata( field => value )
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
		end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   260
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   261
		return ds
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   262
	end
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   265
	### Apply the search :order from the specified +options+ to the collection in
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   266
	### +ds+ and return the modified dataset.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   267
	def apply_search_order( ds, options )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   268
		if options[:order]
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   269
			columns = Array( options[:order] )
3
5b4ee98d698c Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   270
			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
   271
			ds = ds.order_metadata( columns )
0
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   272
		end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   273
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   274
		return ds
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   275
	end
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   278
	### Apply the search :direction from the specified +options+ to the collection
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   279
	### in +ds+ and return the modified dataset.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   280
	def apply_search_direction( ds, options )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   281
		ds = ds.reverse if options[:direction] && options[:direction] == 'desc'
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   282
		return ds
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   283
	end
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
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   286
	### Apply the search :limit from the specified +options+ to the collection in
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   287
	### +ds+ and return the modified dataset.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   288
	def apply_search_limit( ds, options )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   289
		if (( limit = options[:limit] ))
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   290
			self.log.debug "  limiting to %s results" % [ limit ]
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   291
			offset = options[:offset] || 0
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   292
			ds = ds.limit( limit, offset )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   293
		end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   294
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   295
		return ds
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   296
	end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   297
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   298
end # class Thingfish::Metastore::PgGraph
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   299