<< SimpleIndex | Index | FulltextIndex >>
#!/usr/bin/env ruby
require 'odba'
require 'odba/index_definition'
class User
attr_accessor :first_name, :last_name, :year_of_birth
include ODBA::Persistable
def initialize(first_name, last_name, yob)
@first_name = first_name
@last_name = last_name
@year_of_birth = yob
end
def to_s
"#@first_name #@last_name"
end
end
index_def = YAML.load <<-EOD
--- !ruby/object:ODBA::IndexDefinition
index_name: 'users'
origin_klass: 'User'
target_klass: 'User'
resolve_search_term:
lastname:
resolve: 'last_name'
type: varchar(20)
firstname:
resolve: 'first_name'
year:
type: integer
resolve: year_of_birth
resolve_target: ''
resolve_origin: ''
EOD
ODBA.storage.dbi = DBI.connect('DBI:pg:test', 'test', '')
ODBA.cache.setup
begin
ODBA.cache.drop_index('users')
rescue
end
ODBA.cache.create_index(index_def, Object)
father = User.new('Johann Sebastian', 'Bach', 1685)
father.odba_store
son = User.new('Carl Philipp Emanuel', 'Bach', 1714)
son.odba_store
conditions = {
'lastname' => {
'value' => 'Bach',
}
}
puts ODBA.cache.retrieve_from_index('users', conditions)
# -> Carl Philipp Emanuel Bach
# Johann Sebastian Bach
conditions = {
'lastname' => {
'value' => 'Bach',
},
'firstname' => {
'value' => 'Carl',
'condition' => 'like',
}
}
# -> Carl Philipp Emanuel Bach
puts ODBA.cache.retrieve_from_index('users', conditions)
conditions = {
'lastname' => {
'value' => 'Bach',
},
'year' => {
'value' => 1700,
'condition' => '<',
}
}
puts ODBA.cache.retrieve_from_index('users', conditions)
# -> Johann Sebastian Bach