baza 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.0.7
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "baza"
8
- s.version = "0.0.6"
8
+ s.version = "0.0.7"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kasper Johansen"]
12
- s.date = "2013-04-17"
12
+ s.date = "2013-04-18"
13
13
  s.description = "A database abstraction layer, model framework and database framework."
14
14
  s.email = "kj@gfish.com"
15
15
  s.extra_rdoc_files = [
@@ -717,8 +717,8 @@ class Baza::Db
717
717
  end
718
718
 
719
719
  #Yields a query-buffer and flushes at the end of the block given.
720
- def q_buffer(&block)
721
- Baza::QueryBuffer.new(:db => self, &block)
720
+ def q_buffer(args = {}, &block)
721
+ Baza::QueryBuffer.new(args.merge(:db => self), &block)
722
722
  return nil
723
723
  end
724
724
 
@@ -1,5 +1,8 @@
1
1
  #This class buffers a lot of queries and flushes them out via transactions.
2
2
  class Baza::QueryBuffer
3
+ attr_reader :thread_async
4
+
5
+ INITIALIZE_ARGS_ALLOWED = [:db, :debug, :flush_async]
3
6
  #Constructor. Takes arguments to be used and a block.
4
7
  def initialize(args)
5
8
  @args = args
@@ -16,6 +19,7 @@ class Baza::QueryBuffer
16
19
  yield(self)
17
20
  ensure
18
21
  self.flush
22
+ thread_async_join
19
23
  end
20
24
  end
21
25
  end
@@ -68,6 +72,32 @@ class Baza::QueryBuffer
68
72
 
69
73
  #Flushes all queries out in a transaction. This will automatically be called for every 1000 queries.
70
74
  def flush
75
+ if @args[:flush_async]
76
+ flush_async
77
+ else
78
+ flush_real
79
+ end
80
+ end
81
+
82
+ private
83
+
84
+ #Runs the flush in a thread in the background.
85
+ def flush_async
86
+ thread_async_join
87
+
88
+ @thread_async = Thread.new do
89
+ flush_real
90
+ end
91
+ end
92
+
93
+ def thread_async_join
94
+ if thread = @thread_async
95
+ thread.join
96
+ end
97
+ end
98
+
99
+ #Flushes the queries for real.
100
+ def flush_real
71
101
  return nil if @queries_count <= 0
72
102
 
73
103
  @lock.synchronize do
@@ -342,9 +342,24 @@ describe "Baza" do
342
342
  count += 1
343
343
  end
344
344
 
345
- db.q_buffer do |buffer|
345
+ #Test the flush-async which flushes transactions in a thread asyncronous.
346
+ db.q_buffer(:flush_async => true) do |buffer|
347
+ count = 0
346
348
  db.select(:test_table) do |row|
349
+ count += 1
350
+
351
+ if count == 1000
352
+ time_start = Time.now.to_f
353
+ end
354
+
347
355
  buffer.delete(:test_table, {:id => row[:id]})
356
+
357
+ if count == 1000
358
+ time_end = Time.now.to_f
359
+
360
+ time_spent = time_end - time_start
361
+ raise "Too much time spent: '#{time_spent}'." if time_spent > 0.01
362
+ end
348
363
  end
349
364
  end
350
365
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baza
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-17 00:00:00.000000000 Z
12
+ date: 2013-04-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: datet
@@ -235,7 +235,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
235
235
  version: '0'
236
236
  segments:
237
237
  - 0
238
- hash: 1450717990956176451
238
+ hash: 3943301269923844356
239
239
  required_rubygems_version: !ruby/object:Gem::Requirement
240
240
  none: false
241
241
  requirements: