Class: LWAC::DatabaseStorageManager

Inherits:
Object
  • Object
show all
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

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

#closeObject



96
97
98
# File 'lib/lwac/server/storage_manager.rb', line 96

def close
  @db.close
end

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 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

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 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

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

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