Class: Litedb
- Inherits:
-
SQLite3::Database
- Object
- SQLite3::Database
- Litedb
- 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
- #collecting_metrics? ⇒ Boolean
-
#execute(sql, bind_vars = [], *args, &block) ⇒ Object
override execute to capture metrics.
-
#initialize(file, options = {}, zfs = nil) ⇒ Litedb
constructor
override the original initilaizer to allow for connection configuration.
-
#prepare(sql) ⇒ Object
override prepare to return Litedb::Statement (and pass the sql to it).
- #schema_object_count(type = nil) ⇒ Object
-
#size ⇒ Object
return the size of the database file.
-
#snapshot ⇒ Object
collect snapshot information.
- #sqlite_version ⇒ Object
-
#transaction(mode = :immediate) ⇒ Object
enforce immediate mode to avoid deadlocks for a small performance penalty.
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, = {}, zfs = nil) if block_given? super(file, , zfs) do |db| init unless [:noinit] == true yield db end else super(file, , zfs) init unless [:noinit] == true end @running = true @collecting_metrics = [:metrics] collect_metrics if @collecting_metrics end |
Instance Method Details
#collecting_metrics? ⇒ 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 |
#size ⇒ Object
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 |
#snapshot ⇒ Object
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_version ⇒ Object
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 |