litequeue 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e33bb82bf866416420a67cdbc4d5106a85d378ff9d0739ea8dbdbb7022f7902d
4
- data.tar.gz: d8d721f96fb1a533fcc1446b6d193de6eba46a575b8114b7fdf537958b8e079f
3
+ metadata.gz: f97ba4711413d74fd5fc46de7253269e5b339699ee7976a75a99aa848005bd05
4
+ data.tar.gz: f01ca5ac9975a21e6c51e2d48367d622024ae3e8b894216e6ea75403c6f700fa
5
5
  SHA512:
6
- metadata.gz: 5b421f5e2375f74806ccf2564ff642698c25b9a59f26d92190fa138808d1a08e9ccbca295c8925604a0bb1b7a4383db8f46e0a97b1ae0f4c925633b356791280
7
- data.tar.gz: c57def82958f2878d7a42ea65a45af8a5a4599cd3b83a2d0481db6a8d02f8663bffed4362adbb1af732f86e948a70ed7e7e2341269b96ea8f436c8c5df06f67e
6
+ metadata.gz: b770e17b77f912b995afd83a78adde03352be6fbfd6cce62bdb798d53babf61039076f0116de757f10ab97113d1f484a9341025b21a88115af6f3b4c8acd43fb
7
+ data.tar.gz: 3ee4953f64da9a77d00132a62c85ec598c018be999f6b958a02d9fee4f2fc38e9f3fd85ab53fb4aeaae16f2a9331eba90c186ac60106aeaf30dd081b918b991e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2023-08-11
4
+
5
+ - Initial release of usable code
6
+
3
7
  ## [0.1.0] - 2023-08-09
4
8
 
5
- - Initial release
9
+ - Initial release to reserve gem name
data/Gemfile.lock CHANGED
@@ -1,15 +1,21 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- litequeue (0.1.0)
4
+ litequeue (0.2.0)
5
+ litedb (>= 0.2.1)
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
8
9
  specs:
9
10
  ast (2.4.2)
11
+ docile (1.4.0)
10
12
  json (2.6.3)
11
13
  language_server-protocol (3.17.0.3)
12
14
  lint_roller (1.1.0)
15
+ litedb (0.2.1)
16
+ litescheduler (>= 0.2.0)
17
+ sqlite3 (>= 1.5.0)
18
+ litescheduler (0.2.1)
13
19
  minitest (5.19.0)
14
20
  parallel (1.23.0)
15
21
  parser (3.2.2.3)
@@ -36,6 +42,14 @@ GEM
36
42
  rubocop (>= 1.7.0, < 2.0)
37
43
  rubocop-ast (>= 0.4.0)
38
44
  ruby-progressbar (1.13.0)
45
+ simplecov (0.22.0)
46
+ docile (~> 1.1)
47
+ simplecov-html (~> 0.11)
48
+ simplecov_json_formatter (~> 0.1)
49
+ simplecov-html (0.12.3)
50
+ simplecov_json_formatter (0.1.4)
51
+ sqlite3 (1.6.3-arm64-darwin)
52
+ sqlite3 (1.6.3-x86_64-linux)
39
53
  standard (1.30.1)
40
54
  language_server-protocol (~> 3.17.0.2)
41
55
  lint_roller (~> 1.0)
@@ -52,11 +66,13 @@ GEM
52
66
 
53
67
  PLATFORMS
54
68
  arm64-darwin-21
69
+ x86_64-linux
55
70
 
56
71
  DEPENDENCIES
57
72
  litequeue!
58
73
  minitest (~> 5.0)
59
74
  rake (~> 13.0)
75
+ simplecov
60
76
  standard (~> 1.3)
61
77
 
62
78
  BUNDLED WITH
@@ -0,0 +1,11 @@
1
+ create_table_queue: >
2
+ CREATE TABLE IF NOT EXISTS queue(
3
+ id TEXT PRIMARY KEY NOT NULL ON CONFLICT REPLACE,
4
+ name TEXT NOT NULL ON CONFLICT REPLACE,
5
+ fire_at INTEGER NOT NULL ON CONFLICT REPLACE,
6
+ value TEXT,
7
+ created_at INTEGER DEFAULT(UNIXEPOCH()) NOT NULL ON CONFLICT REPLACE
8
+ ) WITHOUT ROWID;
9
+
10
+ create_index_queue_by_name: >
11
+ CREATE INDEX IF NOT EXISTS idx_queue_by_name ON queue(name, fire_at ASC);
@@ -0,0 +1,48 @@
1
+ push: >
2
+ INSERT INTO queue(id, name, fire_at, value)
3
+ VALUES (HEX(RANDOMBLOB(32)), $1, (UNIXEPOCH('subsec') + $2), $3)
4
+ RETURNING id, name;
5
+
6
+ pop: >
7
+ DELETE FROM queue
8
+ WHERE name != '_dead'
9
+ AND (name, fire_at, id)
10
+ IN (
11
+ SELECT name, fire_at, id FROM queue
12
+ WHERE name = IFNULL($1, 'default')
13
+ AND fire_at <= (UNIXEPOCH('subsec'))
14
+ ORDER BY fire_at ASC
15
+ LIMIT IFNULL($2, 1)
16
+ )
17
+ RETURNING id, value;
18
+
19
+ repush: >
20
+ INSERT INTO queue(id, name, fire_at, value)
21
+ VALUES ($1, $2, (UNIXEPOCH('subsec') + $3), $4)
22
+ RETURNING name;
23
+
24
+ delete: >
25
+ DELETE FROM queue
26
+ WHERE id = $1
27
+ RETURNING value;
28
+
29
+ count: >
30
+ SELECT COUNT(*)
31
+ FROM queue
32
+ WHERE IIF($1 IS NULL, 1, name = $1);
33
+
34
+ clear: >
35
+ DELETE FROM queue
36
+ WHERE IIF($1 IS NULL, 1, name = $1)
37
+ RETURNING id;
38
+
39
+ info: >
40
+ SELECT
41
+ name,
42
+ COUNT(*) AS count,
43
+ AVG(UNIXEPOCH('subsec') - created_at) AS avg,
44
+ MIN(UNIXEPOCH('subsec') - created_at) AS min,
45
+ MAX(UNIXEPOCH('subsec') - created_at) AS max
46
+ FROM queue
47
+ GROUP BY name
48
+ ORDER BY count DESC;
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Litequeue
4
- VERSION = "0.1.0"
3
+ class Litequeue
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/litequeue.rb CHANGED
@@ -2,7 +2,102 @@
2
2
 
3
3
  require_relative "litequeue/version"
4
4
 
5
- module Litequeue
6
- class Error < StandardError; end
7
- # Your code goes here...
5
+ require "singleton"
6
+ require "yaml"
7
+ require "litedb"
8
+
9
+ class Litequeue
10
+ include Singleton
11
+
12
+ DEFAULT_QUEUE = "default"
13
+
14
+ Configuration = Struct.new(:path, :synchronous, :mmap_size, :journal_size_limit)
15
+
16
+ def self.configuration
17
+ @configuration ||= Configuration.new(
18
+ _path = "queue.sqlite3",
19
+ _synchronous = :OFF,
20
+ _mmap_size = 32 * 1024
21
+ )
22
+ end
23
+
24
+ def self.configure
25
+ yield(configuration)
26
+ end
27
+
28
+ def self.migrations
29
+ YAML.load_file("#{__dir__}/litequeue/migrations.sql.yml")
30
+ end
31
+
32
+ def self.statements
33
+ YAML.load_file("#{__dir__}/litequeue/statements.sql.yml")
34
+ end
35
+
36
+ def initialize
37
+ configuration = self.class.configuration
38
+ file = configuration.path
39
+ options = configuration.to_h
40
+ .slice(:synchronous, :mmap_size, :journal_size_limit)
41
+ .merge(migrations: self.class.migrations,
42
+ statements: self.class.statements)
43
+
44
+ @db = Litedb::Connection.new(file, options)
45
+ # Once the instance has been initialized, don't allow the configuration to be changed
46
+ # as it won't have any effect.
47
+ configuration.freeze
48
+ end
49
+
50
+ def push(value, queue: DEFAULT_QUEUE, delay: 0)
51
+ results = @db.run_statement(:push, queue, delay, value) # [["{id}", "{name}"]]
52
+ extract_row(results)
53
+ end
54
+
55
+ def pop(queue: DEFAULT_QUEUE, limit: 1)
56
+ results = @db.run_statement(:pop, queue, limit)
57
+
58
+ return extract_row(results) if limit == 1
59
+
60
+ results
61
+ end
62
+
63
+ def repush(id, value, queue: DEFAULT_QUEUE, delay: 0)
64
+ results = @db.run_statement(:repush, id, queue, delay, value)
65
+ extract_value(results)
66
+ end
67
+
68
+ def delete(id)
69
+ results = @db.run_statement(:delete, id)
70
+ extract_value(results)
71
+ end
72
+
73
+ def count(queue: nil)
74
+ results = @db.run_statement(:count, queue)
75
+ extract_value(results)
76
+ end
77
+
78
+ def clear(queue: nil)
79
+ results = @db.run_statement(:clear, queue)
80
+ results.count
81
+ end
82
+
83
+ def empty?
84
+ count.zero?
85
+ end
86
+
87
+ private
88
+
89
+ def extract_value(results) # [["{value}"]] || []
90
+ return if results.empty?
91
+
92
+ results
93
+ .first # [[value]] -> [value]
94
+ .first # [value] -> value
95
+ end
96
+
97
+ def extract_row(results) # [[{value}, {value}]] || []
98
+ return if results.empty?
99
+
100
+ results
101
+ .first # [[value, value]] -> [value, value]
102
+ end
8
103
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: litequeue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mohamed Hassan
@@ -9,8 +9,36 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2023-08-09 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2023-08-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: litedb
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 0.2.1
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 0.2.1
28
+ - !ruby/object:Gem::Dependency
29
+ name: simplecov
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
14
42
  description:
15
43
  email:
16
44
  - oldmoe@gmail.com
@@ -28,6 +56,8 @@ files:
28
56
  - README.md
29
57
  - Rakefile
30
58
  - lib/litequeue.rb
59
+ - lib/litequeue/migrations.sql.yml
60
+ - lib/litequeue/statements.sql.yml
31
61
  - lib/litequeue/version.rb
32
62
  - sig/litequeue.rbs
33
63
  homepage: https://github.com/litestack-ruby/litequeue