| 1 |
require 'rdf/redland' |
| 2 |
|
| 3 |
module Redland |
| 4 |
|
| 5 |
|
| 6 |
class TripleStore |
| 7 |
attr_accessor :store_type, :store |
| 8 |
|
| 9 |
|
| 10 |
def initialize(store_type='memory',name='',options='') |
| 11 |
case store_type |
| 12 |
when "memory"; |
| 13 |
when "hashes"; |
| 14 |
when "file"; |
| 15 |
when "uri"; |
| 16 |
when "mysql"; |
| 17 |
when "sqlite"; |
| 18 |
when "postgresql"; |
| 19 |
else |
| 20 |
raise RedlandError.new("Failed to initialize storage, unknown storage type [#{store_type}]") |
| 21 |
end |
| 22 |
@name = name |
| 23 |
@store_type = store_type |
| 24 |
@store=Redland.librdf_new_storage($world.world,store_type,name,options) |
| 25 |
unless @store |
| 26 |
raise RedlandError.new("Creating Storage Failed") |
| 27 |
end |
| 28 |
|
| 29 |
ObjectSpace.define_finalizer(self,TripleStore.create_finalizer(@store)) |
| 30 |
end |
| 31 |
|
| 32 |
|
| 33 |
def TripleStore.create_finalizer(store) |
| 34 |
proc {|id| |
| 35 |
|
| 36 |
Redland::librdf_free_storage(store) if store |
| 37 |
} |
| 38 |
end |
| 39 |
|
| 40 |
|
| 41 |
end |
| 42 |
|
| 43 |
|
| 44 |
class MemoryStore < TripleStore |
| 45 |
def initialize(mem_name="",options_string="") |
| 46 |
super('memory',mem_name,options_string) |
| 47 |
end |
| 48 |
end |
| 49 |
|
| 50 |
|
| 51 |
class HashStore < TripleStore |
| 52 |
|
| 53 |
attr_accessor :hash_type |
| 54 |
|
| 55 |
|
| 56 |
def initialize(hash_type='memory',name='',dir='.', want_new=true,write=true,contexts=true) |
| 57 |
@hash_type = hash_type |
| 58 |
@dir = dir |
| 59 |
unless ( (hash_type == 'memory') || (hash_type == 'bdb')) |
| 60 |
raise RedlandError.new('Hash must be memory or bdb') |
| 61 |
end |
| 62 |
|
| 63 |
if hash_type=='bdb' then |
| 64 |
unless (name != '') |
| 65 |
raise RedlandError.new('bdb must have a filename') |
| 66 |
end |
| 67 |
end |
| 68 |
if (( want_new == true)|| (want_new == 'yes') || (want_new == 'Yes')) |
| 69 |
want_new = 'yes' |
| 70 |
elsif |
| 71 |
( (want_new == false) || (want_new == 'no') || (want_new == 'No')) |
| 72 |
want_new = 'no' |
| 73 |
end |
| 74 |
if ((write == true) || (write == 'yes') || (write == 'Yes')) |
| 75 |
write = 'yes' |
| 76 |
elsif |
| 77 |
( (write == false) || (write == 'no') || (write == 'No')) |
| 78 |
write = 'no' |
| 79 |
end |
| 80 |
if ((contexts == true) || (contexts == 'yes') || (contexts == 'Yes')) |
| 81 |
contexts = 'yes' |
| 82 |
elsif ((contexts == false) || (contexts == 'no') || (contexts == 'No')) |
| 83 |
contexts = 'no' |
| 84 |
end |
| 85 |
|
| 86 |
options = "hash-type='#{hash_type}',new='#{want_new}', dir='#{dir}', write='#{write}',contexts='#{contexts}'" |
| 87 |
super('hashes',name,options) |
| 88 |
end |
| 89 |
|
| 90 |
def HashStore.read_store(name,dir='.',write=true) |
| 91 |
return HashStore.new('bdb',name,dir,false,write) |
| 92 |
end |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
end |
| 97 |
|
| 98 |
|
| 99 |
class HashOpen < TripleStore |
| 100 |
|
| 101 |
def initialize(name) |
| 102 |
@store = Redland::librdf_new_storage $world.world, "hashes",name, "new='no',hash-type='bdb',dir='.'" |
| 103 |
end |
| 104 |
|
| 105 |
end |
| 106 |
|
| 107 |
|
| 108 |
class FileStore < TripleStore |
| 109 |
|
| 110 |
def initialize(mem_name,options) |
| 111 |
super("file",mem_name,options) |
| 112 |
end |
| 113 |
|
| 114 |
end |
| 115 |
|
| 116 |
|
| 117 |
end |
| 118 |
|
| 119 |
|