Class: Litedb

Inherits:
SQLite3::Database
  • Object
show all
Includes:
Litemetric::Measurable, Litesearch
Defined in:
lib/litestack/litedb.rb

Overview

Litedb inherits from the SQLite3::Database class and adds a few initialization options

Defined Under Namespace

Classes: Statement

Instance Method Summary collapse

Methods included from Litesearch

#litesearch_index_cache, #search_index

Methods included from Litemetric::Measurable

#capture, #capture_snapshot, #collect_metrics, #create_snapshotter, #measure, #metrics_identifier

Constructor Details

#initialize(file, options = {}, zfs = nil) ⇒ Litedb

override the original initilaizer to allow for connection configuration



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/litestack/litedb.rb', line 19

def initialize(file, options = {}, zfs = nil)
  if block_given?
    super(file, options, zfs) do |db|
      init unless options[:noinit] == true
      yield db
    end
  else
    super(file, options, zfs)
    init unless options[:noinit] == true
  end
  @running = true
  @collecting_metrics = options[:metrics]
  collect_metrics if @collecting_metrics
end

Instance Method Details

#collecting_metrics?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/litestack/litedb.rb', line 38

def collecting_metrics?
  @collecting_metrics
end

#execute(sql, bind_vars = [], *args, &block) ⇒ Object

override execute to capture metrics



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/litestack/litedb.rb', line 83

def execute(sql, bind_vars = [], *args, &block)
  if bind_vars.nil? || !args.empty?
    bind_vars = if args.empty?
      []
    else
      [bind_vars] + args
    end
  end

  prepare(sql) do |stmt|
    measure(stmt.stmt_type, stmt.sql) do
      stmt.bind_params(bind_vars)
      stmt = SQLite3::ResultSet.new self, stmt
    end
    if block
      stmt.each do |row|
        yield row
      end
    else
      stmt.to_a
    end
  end
end

#prepare(sql) ⇒ Object

override prepare to return Litedb::Statement (and pass the sql to it)



71
72
73
74
75
76
77
78
79
80
# File 'lib/litestack/litedb.rb', line 71

def prepare(sql)
  stmt = Litedb::Statement.new(self, sql)
  stmt.sql = sql.strip.upcase
  return stmt unless block_given?
  begin
    yield stmt
  ensure
    stmt.close unless stmt.closed?
  end
end

#schema_object_count(type = nil) ⇒ Object



52
53
54
# File 'lib/litestack/litedb.rb', line 52

def schema_object_count(type = nil)
  execute("SELECT count(*) FROM SQLITE_MASTER WHERE iif(?1 IS NOT NULL, type = ?1, TRUE)", type)[0][0]
end

#sizeObject

return the size of the database file



48
49
50
# File 'lib/litestack/litedb.rb', line 48

def size
  execute("SELECT s.page_size * c.page_count FROM pragma_page_size() AS s, pragma_page_count() AS c")[0][0]
end

#snapshotObject

collect snapshot information



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/litestack/litedb.rb', line 57

def snapshot
  {
    summary: {
      path: filename,
      journal_mode: journal_mode,
      synchronous: synchronous,
      size: size.to_f / (1024 * 1024),
      tables: schema_object_count("table"),
      indexes: schema_object_count("index")
    }
  }
end

#sqlite_versionObject



34
35
36
# File 'lib/litestack/litedb.rb', line 34

def sqlite_version
  SQLite3::SQLITE_VERSION_NUMBER
end

#transaction(mode = :immediate) ⇒ Object

enforce immediate mode to avoid deadlocks for a small performance penalty



43
44
45
# File 'lib/litestack/litedb.rb', line 43

def transaction(mode = :immediate)
  super(mode)
end