flexirecord 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,118 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'monitor'
4
+
5
+
6
+ # Copyright (c) 2007 FlexiGuided GmbH, Berlin
7
+ #
8
+ # Author: Jan Behrens
9
+ #
10
+ # Website: http://www.flexiguided.de/publications.flexirecord.en.html
11
+ #
12
+ # -----
13
+ #
14
+ # ThreadResourcePool's are pools of resources to be exclusively used by each thread of an application. They are abstract classes, and can't be used directly. You have to create sub-classes, which implement the following three methods:
15
+ # - generate_resource
16
+ # - reset_resource
17
+ # - destroy_resource
18
+
19
+ class ThreadResourcePool
20
+
21
+ include MonitorMixin
22
+
23
+ private
24
+
25
+ # Creates a new pool with a given cache-size. If no cache-size is given, then a size of 1 is assumed.
26
+ def initialize(size=nil)
27
+ super()
28
+ @size = size ? size.to_int : 1
29
+ @free_resources = []
30
+ @resources_in_use = {}
31
+ nil
32
+ end
33
+
34
+ # Sets (or deletes, when being passed nil) the resource being associated with the current thread.
35
+ def current_resource=(value)
36
+ if value.nil?
37
+ @resources_in_use.delete(Thread.current)
38
+ else
39
+ @resources_in_use[Thread.current] = value
40
+ end
41
+ nil
42
+ end
43
+
44
+ # Returns a new resource to use for the current thread. It is taken from the cache, or, if the cache is empty, by calling the generate_resource method, which has to be implemented by a child class of TheadResourcePool.
45
+ def get_new_resource
46
+ synchronize do
47
+ if @free_resources.empty?
48
+ return self.generate_resource()
49
+ else
50
+ return @free_resources.shift
51
+ end
52
+ end
53
+ end
54
+
55
+ # Puts a used resource back to the cache (or destroys it, if the cache is full). Before it is put back to the resource cache, the reset_resource method is called, which has to be implemented by a child class of ThreadResourcePool. Only if that method returns true (or any non nil/false value) the resource is used again, otherwise it is destroyed. Destruction has to be implemented as the method destroy_resource in a child class of ThreadResourcePool.
56
+ def put_resource_back(resource)
57
+ synchronize do
58
+ if @free_resources.length <= @size
59
+ if self.reset_resource(resource)
60
+ @free_resources << resource
61
+ else
62
+ self.destroy_resource(resource)
63
+ end
64
+ else
65
+ self.destroy_resource(resource)
66
+ end
67
+ end
68
+ nil
69
+ end
70
+
71
+ # Returns a new resource to use.
72
+ # This method is to be implemented by a sub-class.
73
+ def generate_resource
74
+ raise "ThreadResourcePool#generate_resource has to be implemented (overwritten) by a sub-class."
75
+ end
76
+
77
+ # Resets a resource to be re-used by another thread. Returns true on success or false if the resource is to be destroyed.
78
+ # This method is to be implemented by a sub-class.
79
+ def reset_resource
80
+ raise "ThreadResourcePool#reset_resource has to be implemented (overwritten) by a sub-class."
81
+ end
82
+
83
+ # Destroys a resource.
84
+ # This method is to be implemented by a sub-class.
85
+ def destroy_resource
86
+ raise "ThreadResourcePool#destroy_resource has to be implemented (overwritten) by a sub-class."
87
+ end
88
+
89
+ public
90
+
91
+ # Returns the resource being associated with the current thread, or nil there is none existing.
92
+ def current_resource
93
+ @resources_in_use[Thread.current]
94
+ end
95
+
96
+ # Checks if the current thread has already an associated resource, and either yields that resource or a newly generated or cached resource to the given block. If the thread had no resource associated at the time of call, the resource will be put back to the cache or destroyed when the block ends.
97
+ def use_resource
98
+ resource = self.current_resource
99
+ unless resource.nil?
100
+ return yield(resource)
101
+ end
102
+ begin
103
+ begin
104
+ resource = self.current_resource = get_new_resource
105
+ return yield(resource)
106
+ ensure
107
+ self.current_resource = nil
108
+ end
109
+ ensure
110
+ # if thread is terminated by force here, 'current_resource' has already been set to nil
111
+ self.current_resource = nil
112
+ put_resource_back(resource) unless resource.nil?
113
+ end
114
+ end
115
+
116
+ public # end
117
+
118
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: flexirecord
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.2
7
+ date: 2007-02-07 00:00:00 +00:00
8
+ summary: Object-Oriented Database Access Library
9
+ require_paths:
10
+ - lib/
11
+ email: jan.behrens@flexiguided.de
12
+ homepage: http://www.flexiguided.de/publications.flexirecord.en.html
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: flexirecord
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Jan Behrens
31
+ files:
32
+ - ./LICENSE
33
+ - lib/thread_resource_pool.rb
34
+ - lib/flexirecord.rb
35
+ - lib/flexirecord-demo.rb
36
+ - ./flexirecord-demo.sql
37
+ test_files: []
38
+
39
+ rdoc_options:
40
+ - -a
41
+ - --title
42
+ - FlexiRecord Documentation
43
+ - --main
44
+ - FlexiRecord
45
+ - --line-numbers
46
+ extra_rdoc_files: []
47
+
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ requirements: []
53
+
54
+ dependencies: []
55
+