Class: LWAC::DatabaseStorageManager
- Inherits:
-
Object
- Object
- LWAC::DatabaseStorageManager
- Defined in:
- lib/lwac/server/storage_manager.rb
Overview
Database engine for links only.
By default this is read-only, as all but the import tool should not be able to edit the database.
Instance Method Summary collapse
- #close ⇒ Object
-
#count_links(min_id = nil) ⇒ Object
Count the number of links.
-
#initialize(config, read_only = true) ⇒ DatabaseStorageManager
constructor
A new instance of DatabaseStorageManager.
-
#insert_link(uri) ⇒ Object
Insert a link.
-
#read_link(id) ⇒ Object
Retrieve a single link with a given ID.
-
#read_link_ids(from = 0, n = nil) ⇒ Object
Read all the link IDs TODO — what if lowest ID is below 0?.
-
#read_links(range_low = nil, range_high = nil) ⇒ Object
Retrieve a list of links from the db.
-
#read_links_from_array(ids = []) ⇒ Object
Retrieve many links from an array of IDs.
Constructor Details
#initialize(config, read_only = true) ⇒ DatabaseStorageManager
Returns a new instance of DatabaseStorageManager.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/lwac/server/storage_manager.rb', line 21 def initialize(config, read_only=true) $log.debug "Connecting to #{config[:engine]} database..." klass = case(config[:engine]) when :mysql MySQLDatabaseConnection else SQLite3DatabaseConnection end @db = klass.new( config[:engine_conf] ) $log.debug "Connected to database." # Set config, hash as default @config = config # Read-only mode designed for servers. @read_only = read_only end |
Instance Method Details
#close ⇒ Object
96 97 98 |
# File 'lib/lwac/server/storage_manager.rb', line 96 def close @db.close end |
#count_links(min_id = nil) ⇒ Object
Count the number of links
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/lwac/server/storage_manager.rb', line 85 def count_links(min_id = nil) where = nil if min_id != nil then where = "#{@config[:fields][:id]} > #{min_id}" end count = @db.select(@config[:table], ["count(*)"], where) return count[0][0].to_i end |
#insert_link(uri) ⇒ Object
Insert a link
42 43 44 45 |
# File 'lib/lwac/server/storage_manager.rb', line 42 def insert_link(uri) raise "Attempt to insert link whilst in read-only mode." if @read_only @db.insert(@config[:table], {"uri" => uri}) end |
#read_link(id) ⇒ Object
Retrieve a single link with a given ID
67 68 69 70 |
# File 'lib/lwac/server/storage_manager.rb', line 67 def read_link(id) link = @db.select(@config[:table], @config[:fields].values, "#{@config[:fields][:id]} == #{id}") return Link.new(link[0][0], link[0][1]) end |
#read_link_ids(from = 0, n = nil) ⇒ Object
Read all the link IDs TODO — what if lowest ID is below 0?
58 59 60 61 62 63 64 |
# File 'lib/lwac/server/storage_manager.rb', line 58 def read_link_ids(from=0, n=nil) where = "id > #{from.to_i}" where += " limit #{n}" if n ids = @db.select(@config[:table], [@config[:fields][:id]], where).flatten return Set.new(ids) end |
#read_links(range_low = nil, range_high = nil) ⇒ Object
Retrieve a list of links from the db
48 49 50 51 52 53 54 |
# File 'lib/lwac/server/storage_manager.rb', line 48 def read_links(range_low=nil, range_high=nil) where = "" where = "#{@config[:fields][:id]} < #{range_high} AND #{@config[:fields][:id]} > #{range_low}" if range_low and range_high links = @db.select(@config[:table], @config[:fields].values, where) links.map!{|id, uri| Link.new(id, uri) } end |
#read_links_from_array(ids = []) ⇒ Object
Retrieve many links from an array of IDs
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/lwac/server/storage_manager.rb', line 73 def read_links_from_array(ids = []) links = [] return links if ids.length == 0 @db.select(@config[:table], @config[:fields].values, "#{@config[:fields][:id]} in (#{ids.join(',')})").each{|l| links << Link.new(l[0], l[1]) } return links end |