Class: RdfContext::SQLite3Store

Inherits:
AbstractSQLStore show all
Defined in:
lib/rdf_context/store/sqlite3_store.rb

Overview

SQLite3 store context-ware and formula-aware implementation. It stores it's triples in the following partitions:

  • Asserted non rdf:type statements

  • Asserted rdf:type statements (in a table which models Class membership). The motivation for this partition is primarily query speed and scalability as most graphs will always have more rdf:type statements than others

  • All Quoted statements

In addition it persists namespace mappings in a seperate table

Based on Python RdfLib SQLite

Constant Summary

CREATE_ASSERTED_STATEMENTS_TABLE =
%(
    CREATE TABLE %s_asserted_statements (
subject       text not NULL,
predicate     text not NULL,
object        text not NULL,
context       text not NULL,
termComb      tinyint unsigned not NULL))
CREATE_ASSERTED_TYPE_STATEMENTS_TABLE =
%(
    CREATE TABLE %s_type_statements (
member        text not NULL,
klass         text not NULL,
context       text not NULL,
termComb      tinyint unsigned not NULL))
CREATE_LITERAL_STATEMENTS_TABLE =
%(
    CREATE TABLE %s_literal_statements (
subject       text not NULL,
predicate     text not NULL,
object        text,
context       text not NULL,
termComb      tinyint unsigned not NULL,
objLanguage   varchar(3),
objDatatype   text))
CREATE_QUOTED_STATEMENTS_TABLE =
%(
    CREATE TABLE %s_quoted_statements (
subject       text not NULL,
predicate     text not NULL,
object        text,
context       text not NULL,
termComb      tinyint unsigned not NULL,
objLanguage   varchar(3),
objDatatype   text))
CREATE_NS_BINDS_TABLE =
%(
    CREATE TABLE %s_namespace_binds (
prefix        varchar(20) UNIQUE not NULL,
uri           text,
PRIMARY KEY (prefix)))
DROP_ASSERTED_STATEMENTS_TABLE =
%(DROP TABLE %s_asserted_statements)
DROP_ASSERTED_TYPE_STATEMENTS_TABLE =
%(DROP TABLE %s_type_statements)
DROP_LITERAL_STATEMENTS_TABLE =
%(DROP TABLE %s_literal_statements)
DROP_QUOTED_STATEMENTS_TABLE =
%(DROP TABLE %s_quoted_statements)
DROP_NS_BINDS_TABLE =
%(DROP TABLE %s_namespace_binds)

Constants inherited from AbstractSQLStore

AbstractSQLStore::ASSERTED_LITERAL_PARTITION, AbstractSQLStore::ASSERTED_NON_TYPE_PARTITION, AbstractSQLStore::ASSERTED_TYPE_PARTITION, AbstractSQLStore::CONTEXT_SELECT, AbstractSQLStore::COUNT_SELECT, AbstractSQLStore::FULL_TRIPLE_PARTITIONS, AbstractSQLStore::INTERNED_PREFIX, AbstractSQLStore::QUOTED_PARTITION, AbstractSQLStore::STRONGLY_TYPED_TERMS, AbstractSQLStore::TRIPLE_SELECT, AbstractSQLStore::TRIPLE_SELECT_NO_ORDER

Constants included from TermUtils

TermUtils::CONTEXT, TermUtils::GRAPH_TERM_DICT, TermUtils::OBJECT, TermUtils::PREDICATE, TermUtils::REVERSE_TERM_COMBINATIONS, TermUtils::SUBJECT, TermUtils::TERM_COMBINATIONS, TermUtils::TERM_INSTANTIATION_DICT

Instance Attribute Summary

Attributes inherited from AbstractStore

#identifier, #nsbinding, #uri_binding

Instance Method Summary collapse

Methods inherited from AbstractSQLStore

#_normalizeSQLCmd, #add, #asserted_table, #asserted_type_table, #bind, #buildClause, #buildLitDTypeClause, #buildLitLanguageClause, #buildLiteralTripleSQLCommand, #buildTripleSQLCommand, #buildTypeSQLCommand, #close, #commit, #contains?, #context_aware?, #contexts, #createTerm, #destroy, #extractTriple, #formula_aware?, #literal_table, #namespace, #namespace_binds, #normalizeTerm, #nsbinding, #prefix, #queryAnalysis, #quoted_table, #remove, #remove_context, #rollback, #size, #transaction_aware?, #triples, #unionSELECT, #uri_binding

Methods included from TermUtils

#constructGraph, #normalizeGraph, #statement2TermCombination, #term2Letter, #type2TermCombination

Methods inherited from AbstractStore

#add, #bind, #bnodes, #close, #commit, #contains?, #context_aware?, #contexts, #destroy, #formula_aware?, #inspect, #item, #namespace, #objects, #predicates, #prefix, #remove, #rollback, #size, #subjects, #transaction_aware?, #triples

Constructor Details

#initialize(identifier = nil, configuration = {}) ⇒ SQLite3Store

Create a new SQLite3Store Store @param configuration Specific to type of storage

Parameters:

  • identifier (URIRef) (defaults to: nil)
  • configuration (Hash) (defaults to: {})

    a customizable set of options

Options Hash (configuration):

  • :db (String)

    Path to database file

  • :connection (String)

    ActiveRecord::Base.connection.raw_connection



22
23
24
25
26
27
28
# File 'lib/rdf_context/store/sqlite3_store.rb', line 22

def initialize(identifier = nil, configuration = {})
  @path = configuration[:path] ||= File.join(Dir.getwd, "#{@internedId}.db")

  super(identifier, configuration)

  @autocommit_default = false
end

Instance Method Details

#buildContextClause(context, tableName) ⇒ Object (protected)

Where clase utility functions



181
182
183
184
185
186
187
188
189
190
# File 'lib/rdf_context/store/sqlite3_store.rb', line 181

def buildContextClause(context, tableName)
  context = normalizeTerm(context) if context

  #    case context
  #    when REGEXTerm
  #    when Array
  #    else
    ["#{tableName}.context=?", context] if context
  #    end
end

#buildObjClause(object, tableName) ⇒ Object (protected)

Where clase utility functions



169
170
171
172
173
174
175
176
177
178
# File 'lib/rdf_context/store/sqlite3_store.rb', line 169

def buildObjClause(object, tableName)
  case object
  #    when REGEXTerm
  #    when Array
when Graph
  ["#{tableName}.object=?", self.normalizeTerm(object.identifier)]
  else
    ["#{tableName}.object=?", object] if object
  end
end

#buildPredClause(predicate, tableName) ⇒ Object (protected)



159
160
161
162
163
164
165
166
# File 'lib/rdf_context/store/sqlite3_store.rb', line 159

def buildPredClause(predicate, tableName)
  #    case predicate
  #    when REGEXTerm
  #    when Array
  #    else
    ["#{tableName}.predicate=?", predicate] if predicate
  #    end
end

#buildSubjClause(subject, tableName) ⇒ Object (protected)

Where clase utility functions



148
149
150
151
152
153
154
155
156
157
# File 'lib/rdf_context/store/sqlite3_store.rb', line 148

def buildSubjClause(subject, tableName)
  case subject
  #    when REGEXTerm
  #    when Array
  when Graph
     ["#{tableName}.subject=?", self.normalizeTerm(subject.identifier)]
  else
    ["#{tableName}.subject=?", subject] if subject
  end
end

#buildTypeClassClause(object, tableName) ⇒ Object (protected)

Where clase utility functions



203
204
205
206
207
208
209
# File 'lib/rdf_context/store/sqlite3_store.rb', line 203

def buildTypeClassClause(object, tableName)
  #    case context
  #    when REGEXTerm
  #    else
    ["#{tableName}.klass=?", object] if object
  #    end
end

#buildTypeMemberClause(subject, tableName) ⇒ Object (protected)

Where clase utility functions



193
194
195
196
197
198
199
200
# File 'lib/rdf_context/store/sqlite3_store.rb', line 193

def buildTypeMemberClause(subject, tableName)
  #    case context
  #    when REGEXTerm
  #    when Array
  #    else
    ["#{tableName}.member=?", subject] if subject
  #    end
end

#executeSQL(qStr, *params, &block) ⇒ Object (protected)

This takes the query string and parameters and (depending on the SQL implementation) either fill in the parameter in-place or pass it on to the DB impl (if it supports this). The default (here) is to fill the parameters in-place surrounding each param with quote characters

Yields each row



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/rdf_context/store/sqlite3_store.rb', line 216

def executeSQL(qStr, *params, &block)
  @statement_cache ||= {}
  #@statement_cache[qStr] ||= @db.prepare(qStr)
  @statement_cache[qStr] ||= qStr

  puts "executeSQL: '#{qStr}', '#{params.join("', '")}'" if ::RdfContext::debug?
  if block_given?
    @db.execute(@statement_cache[qStr], *params) do |row|
      puts "executeSQL res: #{row.inspect}" if ::RdfContext::debug?
      row = row.keys.select{|k| k.is_a?(Integer)}.sort.map{|k| row[k]} if row.is_a?(Hash)
      yield(row)
    end
  else
    puts "executeSQL no block given" if ::RdfContext::debug?
    @db.execute(@statement_cache[qStr], *params)
  end
rescue SQLite3::SQLException => e
  puts "SQL Exception (ignored): #{e.message}" if ::RdfContext::debug?
end

#open(options) ⇒ Object

Opens the store specified by the configuration hash. If create is True a store will be created if it does not already exist. If create is False and a store does not already exist an exception is raised. An exception is also raised if a store exists, but there is insufficient permissions to open the store.

Parameters:

  • options[String] (Hash)

    a customizable set of options

Raises:



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rdf_context/store/sqlite3_store.rb', line 38

def open(options)
  @db ||= options[:connection]
  return @db if @db

  if options[:path] && !File.exist?(options[:path])
    @db = SQLite3::Database.new(options[:path])
    setup
  end

  raise StoreException.new("Attempt to open missing database file #{options[:path]}") unless File.exist?(options[:path])
  @db = SQLite3::Database.new(options[:path])
end

#setupObject

Create necessary tables and indecies for this database



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rdf_context/store/sqlite3_store.rb', line 52

def setup
  executeSQL(CREATE_ASSERTED_STATEMENTS_TABLE % @internedId)
  executeSQL(CREATE_ASSERTED_TYPE_STATEMENTS_TABLE % @internedId)
  executeSQL(CREATE_QUOTED_STATEMENTS_TABLE % @internedId)
  executeSQL(CREATE_NS_BINDS_TABLE % @internedId)
  executeSQL(CREATE_LITERAL_STATEMENTS_TABLE % @internedId)
  
  # Create indicies
  {
    asserted_table => {
      "#{@internedId}_A_termComb_index" => %w(termComb),
      "#{@internedId}_A_s_index" => %w(subject),
      "#{@internedId}_A_p_index" => %w(predicate),
      "#{@internedId}_A_o_index" => %w(object),
      "#{@internedId}_A_c_index" => %w(context),
    },
    asserted_type_table => {
      "#{@internedId}_T_termComb_index" => %w(termComb),
      "#{@internedId}_T_member_index" => %w(member),
      "#{@internedId}_T_klass_index" => %w(klass),
      "#{@internedId}_T_c_index" => %w(context),
    },
    literal_table => {
      "#{@internedId}_L_termComb_index" => %w(termComb),
      "#{@internedId}_L_s_index" => %w(subject),
      "#{@internedId}_L_p_index" => %w(predicate),
      "#{@internedId}_L_c_index" => %w(context),
    },
    quoted_table => {
      "#{@internedId}_Q_termComb_index" => %w(termComb),
      "#{@internedId}_Q_s_index" => %w(subject),
      "#{@internedId}_Q_p_index" => %w(predicate),
      "#{@internedId}_Q_o_index" => %w(object),
      "#{@internedId}_Q_c_index" => %w(context),
    },
    namespace_binds => {
      "#{@internedId}_uri_index" => %w(uri),
    }
  }.each_pair do |tablename, indicies|
    indicies.each_pair do |index, columns|
      executeSQL("CREATE INDEX #{index} on #{tablename} ('#{columns.join(', ')}')")
    end
  end
end

#teardownObject

Teardown DB files



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rdf_context/store/sqlite3_store.rb', line 98

def teardown
  # Drop indicies
  {
    asserted_table => {
      "#{@internedId}_A_termComb_index" => %w(termComb),
      "#{@internedId}_A_s_index" => %w(subject),
      "#{@internedId}_A_p_index" => %w(predicate),
      "#{@internedId}_A_o_index" => %w(object),
      "#{@internedId}_A_c_index" => %w(context),
    },
    asserted_type_table => {
      "#{@internedId}_T_termComb_index" => %w(termComb),
      "#{@internedId}_T_member_index" => %w(member),
      "#{@internedId}_T_klass_index" => %w(klass),
      "#{@internedId}_T_c_index" => %w(context),
    },
    literal_table => {
      "#{@internedId}_L_termComb_index" => %w(termComb),
      "#{@internedId}_L_s_index" => %w(subject),
      "#{@internedId}_L_p_index" => %w(predicate),
      "#{@internedId}_L_c_index" => %w(context),
    },
    quoted_table => {
      "#{@internedId}_Q_termComb_index" => %w(termComb),
      "#{@internedId}_Q_s_index" => %w(subject),
      "#{@internedId}_Q_p_index" => %w(predicate),
      "#{@internedId}_Q_o_index" => %w(object),
      "#{@internedId}_Q_c_index" => %w(context),
    },
    namespace_binds => {
      "#{@internedId}_uri_index" => %w(uri),
    }
  }.each_pair do |tablename, indicies|
    tn = "#{@internedId}_#{tablename}"
    indicies.each_pair do |index, columns|
      executeSQL("DROP INDEX #{index} ON #{tn}")
    end
  end
  
  # Drop tables
  executeSQL("DROP TABLE #{namespace_binds}")
  executeSQL("DROP TABLE #{quoted_table}")
  executeSQL("DROP TABLE #{literal_table}")
  executeSQL("DROP TABLE #{asserted_type_table}")
  executeSQL("DROP TABLE #{asserted_table}")
end