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

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


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

def collecting_metrics?
  @collecting_metrics
end

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

override execute to capture metrics



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/litestack/litedb.rb', line 79

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)



67
68
69
70
71
72
73
74
75
76
# File 'lib/litestack/litedb.rb', line 67

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



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

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



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

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



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/litestack/litedb.rb', line 53

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

#transaction(mode = :immediate) ⇒ Object

enforce immediate mode to avoid deadlocks for a small performance penalty



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

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