class PIM::Services::DataModelCommons::DBMDatabase

Constants

FORMATS
FORMAT_JSON
FORMAT_KEY
FORMAT_MARSHAL
TYPES
TYPE_GDBM
TYPE_GDBM_EXTNAME
VERSION_1_0
VERSION_KEY

Attributes

format[R]
version[R]

Public Class Methods

new(filename, type: TYPE_GDBM, action: :open, format: FORMAT_JSON, version: VERSION_1_0) click to toggle source
# File services.rb, line 1137
def initialize filename, type: TYPE_GDBM, action: :open, format: FORMAT_JSON, version: VERSION_1_0

  @filename = filename
  @type = type
  @format = format
  @version = version

  @type_calls = TYPES[@type]
  raise "Unknown database type '#{@type}'" if @type_calls.nil?

  if action == :open

    raise "Could not open database '#{@filename}' of type '#{@type}'" unless open_database
    PIM.log_debug "Opened database '#{@filename}' of type '#{@type}' with version #{@version}, format #{@format}"

  elsif action == :create

    set_version_and_format(version: @version, format: @format)

    raise "Could not create database '#{@filename}' of type '#{@type}'" unless create_database
    PIM.log_debug "Created database '#{@filename}' with version #{@version}, format #{@format}"

  end

end

Public Instance Methods

close_database() click to toggle source
# File services.rb, line 1174
def close_database

  return false if database_closed?

  @type_calls[:close].call(@dbm)
  @dbm = nil

  return true
end
create_database() click to toggle source
# File services.rb, line 1184
def create_database

  return false unless database_closed?

  @dbm = @type_calls[:create].call(@filename)
  put(VERSION_KEY, @version, format: false)
  put(FORMAT_KEY, @format, format: false)

  return true
end
database_closed?() click to toggle source
# File services.rb, line 1195
def database_closed?
  return true if @dbm.nil? or @type_calls[:closed?].call(@dbm)
end
ensure_database_open() click to toggle source
# File services.rb, line 1199
def ensure_database_open
  raise "Database '#{@filename}' is not open" if database_closed?
end
get(key, format: true) click to toggle source
# File services.rb, line 1208
def get key, format: true
  ensure_database_open
  value = @type_calls[:get].call(@dbm, key)
  return nil if value.nil?
  return format ? @format_calls[:get].call(value) : value
end
open_database() click to toggle source
# File services.rb, line 1163
def open_database

  return true unless database_closed?
  return false unless File.exist?(@filename)

  @dbm = @type_calls[:open].call(@filename)
  set_version_and_format(version: get(VERSION_KEY, format: false), format: get(FORMAT_KEY, format: false))

  return true
end
put(key, value, format: true) click to toggle source
# File services.rb, line 1215
def put key, value, format: true
  ensure_database_open
  old_value = get(key, format: format)
  new_value = format ? @format_calls[:put].call(value) : value
  @type_calls[:put].call(@dbm, key, new_value)
  return old_value
end
size() click to toggle source
# File services.rb, line 1203
def size
  ensure_database_open
  @dbm.keys.size
end