openwferu 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- data/{README → README.txt} +16 -13
- data/bin/validate-workflow.rb +46 -22
- data/examples/README.txt +8 -0
- data/examples/homeworkreview.rb +66 -0
- data/examples/quotereporter.rb +154 -0
- data/lib/{openwferu.rb → openwfe.rb} +6 -8
- data/lib/{ru → openwfe}/contextual.rb +11 -3
- data/lib/{ru → openwfe/engine}/engine.rb +50 -36
- data/lib/{ru/participant.rb → openwfe/engine/file_persisted_engine.rb} +21 -22
- data/lib/openwfe/expool/expressionpool.rb +534 -0
- data/lib/openwfe/expool/expstorage.rb +184 -0
- data/lib/openwfe/expool/journalexpstorage.rb +312 -0
- data/lib/openwfe/expool/yamlexpstorage.rb +127 -0
- data/lib/{ru → openwfe/expressions}/environment.rb +19 -14
- data/lib/{ru → openwfe/expressions}/expressionmap.rb +48 -21
- data/lib/{ru → openwfe/expressions}/fe_concurrence.rb +111 -35
- data/lib/openwfe/expressions/fe_cursor.rb +236 -0
- data/lib/{ru → openwfe/expressions}/fe_define.rb +5 -5
- data/lib/openwfe/expressions/fe_fqv.rb +99 -0
- data/lib/openwfe/expressions/fe_iterator.rb +182 -0
- data/lib/{ru/fe_misc.rb → openwfe/expressions/fe_losfor.rb} +14 -56
- data/lib/openwfe/expressions/fe_misc.rb +102 -0
- data/lib/{ru → openwfe/expressions}/fe_participant.rb +25 -14
- data/lib/{ru → openwfe/expressions}/fe_raw.rb +39 -75
- data/lib/{ru/fe_base.rb → openwfe/expressions/fe_sequence.rb} +40 -35
- data/lib/{ru → openwfe/expressions}/fe_subprocess.rb +30 -14
- data/lib/{ru → openwfe/expressions}/fe_time.rb +59 -31
- data/lib/{ru → openwfe/expressions}/fe_utils.rb +42 -26
- data/lib/{ru → openwfe/expressions}/fe_value.rb +20 -14
- data/lib/openwfe/expressions/flowexpression.rb +434 -0
- data/lib/openwfe/expressions/raw_prog.rb +391 -0
- data/lib/openwfe/expressions/raw_xml.rb +128 -0
- data/lib/openwfe/flowexpressionid.rb +148 -0
- data/lib/{ru → openwfe}/logging.rb +10 -6
- data/lib/{osocket.rb → openwfe/osocket.rb} +36 -35
- data/lib/{otime.rb → openwfe/otime.rb} +71 -21
- data/lib/openwfe/participants/atomparticipants.rb +144 -0
- data/lib/openwfe/participants/enoparticipant.rb +73 -0
- data/lib/openwfe/participants/participant.rb +85 -0
- data/lib/{ru → openwfe/participants}/participantmap.rb +40 -12
- data/lib/{ru → openwfe/participants}/participants.rb +41 -12
- data/lib/openwfe/participants/soapparticipants.rb +96 -0
- data/lib/{controlclient.rb → openwfe/rest/controlclient.rb} +12 -13
- data/lib/{definitions.rb → openwfe/rest/definitions.rb} +3 -3
- data/lib/{exception.rb → openwfe/rest/exception.rb} +3 -3
- data/lib/{restclient.rb → openwfe/rest/restclient.rb} +13 -22
- data/lib/{worklistclient.rb → openwfe/rest/worklistclient.rb} +33 -46
- data/lib/openwfe/rest/xmlcodec.rb +575 -0
- data/lib/{ru → openwfe}/rudefinitions.rb +32 -4
- data/lib/{ru → openwfe}/service.rb +20 -8
- data/lib/openwfe/storage/yamlfilestorage.rb +159 -0
- data/lib/{ru → openwfe/util}/dollar.rb +10 -8
- data/lib/openwfe/util/lru_cache.rb +149 -0
- data/lib/{ru → openwfe/util}/scheduler.rb +18 -10
- data/lib/{ru → openwfe/util}/schedulers.rb +7 -7
- data/lib/{utils.rb → openwfe/utils.rb} +93 -9
- data/lib/openwfe/workitem.rb +366 -0
- data/lib/openwfe/worklist/worklists.rb +175 -0
- data/test/README.txt +27 -0
- data/test/atomtest.rb +99 -0
- data/test/crontest.rb +58 -0
- data/test/dollartest.rb +3 -3
- data/test/feitest.rb +42 -14
- data/test/file_persistence_test.rb +93 -0
- data/test/flowtestbase.rb +72 -26
- data/test/ft_0.rb +1 -97
- data/test/ft_0b_sequence.rb +33 -0
- data/test/ft_0c_testname.rb +29 -0
- data/test/ft_10_loop.rb +48 -0
- data/test/ft_11_ppd.rb +292 -0
- data/test/ft_12_blockparticipant.rb +45 -0
- data/test/ft_13_eno.rb +51 -0
- data/test/ft_14_subprocess.rb +90 -0
- data/test/ft_14b_subprocess.rb +40 -0
- data/test/ft_15_iterator.rb +70 -0
- data/test/ft_16_fqv.rb +57 -0
- data/test/ft_1_unset.rb +25 -1
- data/test/ft_2_concurrence.rb +10 -5
- data/test/ft_3_equals.rb +35 -1
- data/test/ft_4_misc.rb +16 -1
- data/test/ft_5_time.rb +26 -1
- data/test/ft_6_lambda.rb +2 -1
- data/test/{ft_7_losfor.rb → ft_7_lose.rb} +41 -35
- data/test/ft_8_forget.rb +46 -0
- data/test/ft_9_cursor.rb +94 -0
- data/test/journal_persistence_test.rb +147 -0
- data/test/misctest.rb +13 -9
- data/test/rake_ptest.rb +18 -0
- data/test/rake_qtest.rb +43 -0
- data/test/{fulltest.rb → rake_test.rb} +2 -2
- data/test/raw_prog_test.rb +236 -0
- data/test/rest_test.rb +189 -0
- data/test/rutest_utils.rb +1 -1
- data/test/timetest.rb +42 -34
- metadata +125 -82
- data/lib/codec.rb +0 -573
- data/lib/flowexpressionid.rb +0 -139
- data/lib/ru/expressionpool.rb +0 -382
- data/lib/ru/expressionstorage.rb +0 -99
- data/lib/ru/flowexpression.rb +0 -272
- data/lib/ru/ruutils.rb +0 -70
- data/lib/test.rb +0 -222
- data/lib/workitem.rb +0 -249
- data/test/quicktest.rb +0 -21
@@ -1,6 +1,6 @@
|
|
1
1
|
#
|
2
2
|
#--
|
3
|
-
# Copyright (c) 2006, John Mettraux, OpenWFE.org
|
3
|
+
# Copyright (c) 2006-2007, John Mettraux, OpenWFE.org
|
4
4
|
# All rights reserved.
|
5
5
|
#
|
6
6
|
# Redistribution and use in source and binary forms, with or without
|
@@ -37,7 +37,7 @@
|
|
37
37
|
# "made in Japan"
|
38
38
|
#
|
39
39
|
|
40
|
-
module
|
40
|
+
module OpenWFE
|
41
41
|
|
42
42
|
#OPENWFE_VERSION = '1.7.2pre17'
|
43
43
|
OPENWFE_VERSION = '0.9.2'
|
@@ -45,8 +45,9 @@ module OpenWFEru
|
|
45
45
|
#
|
46
46
|
# service names
|
47
47
|
|
48
|
-
S_LOGGER =
|
48
|
+
S_LOGGER = :logger
|
49
49
|
|
50
|
+
S_ENGINE = 'engine'
|
50
51
|
S_EXPRESSION_MAP = 'expressionMap'
|
51
52
|
S_EXPRESSION_POOL = 'expressionPool'
|
52
53
|
S_EXPRESSION_STORAGE = 'expressionStorage'
|
@@ -66,12 +67,39 @@ module OpenWFEru
|
|
66
67
|
#
|
67
68
|
# some expression attribute names
|
68
69
|
|
69
|
-
A_FORGET = 'forget'
|
70
|
+
#A_FORGET = 'forget'
|
71
|
+
# replaced by a symbol
|
70
72
|
|
71
73
|
A_SYNC = 'sync'
|
72
74
|
A_COUNT = 'count'
|
73
75
|
A_REMAINING = 'remaining'
|
74
76
|
REM_CANCEL = 'cancel'
|
77
|
+
|
78
|
+
#
|
79
|
+
# some file storage default values
|
80
|
+
DEFAULT_FILE_STORAGE_PATH = "./work"
|
81
|
+
|
82
|
+
#
|
83
|
+
# A mixin for easy OpenWFE service lookup
|
84
|
+
# (assumes the presence of an application context instance var)
|
85
|
+
#
|
86
|
+
module OwfeServiceLocator
|
87
|
+
def get_engine
|
88
|
+
return @application_context[S_ENGINE]
|
89
|
+
end
|
90
|
+
def get_expression_map
|
91
|
+
return @application_context[S_EXPRESSION_MAP]
|
92
|
+
end
|
93
|
+
def get_expression_pool
|
94
|
+
return @application_context[S_EXPRESSION_POOL]
|
95
|
+
end
|
96
|
+
def get_expression_storage
|
97
|
+
return @application_context[S_EXPRESSION_STORAGE]
|
98
|
+
end
|
99
|
+
def get_participant_map
|
100
|
+
return @application_context[S_PARTICIPANT_MAP]
|
101
|
+
end
|
102
|
+
end
|
75
103
|
|
76
104
|
end
|
77
105
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
#
|
2
|
-
|
3
|
-
# Copyright (c) 2006, John Mettraux, OpenWFE.org
|
2
|
+
#--
|
3
|
+
# Copyright (c) 2006-2007, John Mettraux, OpenWFE.org
|
4
4
|
# All rights reserved.
|
5
5
|
#
|
6
6
|
# Redistribution and use in source and binary forms, with or without
|
@@ -28,7 +28,7 @@
|
|
28
28
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
29
29
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
30
30
|
# POSSIBILITY OF SUCH DAMAGE.
|
31
|
-
|
31
|
+
#++
|
32
32
|
#
|
33
33
|
# $Id: definitions.rb 2725 2006-06-02 13:26:32Z jmettraux $
|
34
34
|
#
|
@@ -39,28 +39,40 @@
|
|
39
39
|
# John Mettraux at openwfe.org
|
40
40
|
#
|
41
41
|
|
42
|
-
require '
|
43
|
-
require '
|
42
|
+
require 'openwfe/logging'
|
43
|
+
require 'openwfe/contextual'
|
44
44
|
|
45
45
|
|
46
|
-
module
|
46
|
+
module OpenWFE
|
47
47
|
|
48
48
|
module ServiceMixin
|
49
49
|
include Contextual, Logging
|
50
50
|
|
51
51
|
attr_accessor \
|
52
52
|
:service_name
|
53
|
+
|
54
|
+
def service_init (service_name, application_context)
|
55
|
+
|
56
|
+
@service_name = service_name
|
57
|
+
@application_context = application_context
|
58
|
+
end
|
53
59
|
end
|
54
60
|
|
55
61
|
class Service
|
56
62
|
include ServiceMixin
|
57
63
|
|
58
64
|
def initialize (service_name, application_context)
|
59
|
-
|
60
|
-
|
65
|
+
|
66
|
+
service_init(service_name, application_context)
|
61
67
|
end
|
62
68
|
|
63
69
|
end
|
64
70
|
|
71
|
+
def register (service)
|
72
|
+
|
73
|
+
service.application_context[service.service_name] = service
|
74
|
+
return service
|
75
|
+
end
|
76
|
+
|
65
77
|
end
|
66
78
|
|
@@ -0,0 +1,159 @@
|
|
1
|
+
#
|
2
|
+
#--
|
3
|
+
# Copyright (c) 2006-2007, Nicolas Modryzk and John Mettraux, OpenWFE.org
|
4
|
+
# All rights reserved.
|
5
|
+
#
|
6
|
+
# Redistribution and use in source and binary forms, with or without
|
7
|
+
# modification, are permitted provided that the following conditions are met:
|
8
|
+
#
|
9
|
+
# . Redistributions of source code must retain the above copyright notice, this
|
10
|
+
# list of conditions and the following disclaimer.
|
11
|
+
#
|
12
|
+
# . Redistributions in binary form must reproduce the above copyright notice,
|
13
|
+
# this list of conditions and the following disclaimer in the documentation
|
14
|
+
# and/or other materials provided with the distribution.
|
15
|
+
#
|
16
|
+
# . Neither the name of the "OpenWFE" nor the names of its contributors may be
|
17
|
+
# used to endorse or promote products derived from this software without
|
18
|
+
# specific prior written permission.
|
19
|
+
#
|
20
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
21
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
22
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
23
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
24
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
25
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
26
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
27
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
28
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
29
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
30
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
#++
|
32
|
+
#
|
33
|
+
# $Id: definitions.rb 2725 2006-06-02 13:26:32Z jmettraux $
|
34
|
+
#
|
35
|
+
|
36
|
+
#
|
37
|
+
# "made in Japan"
|
38
|
+
#
|
39
|
+
# Nicolas Modrzyk at openwfe.org
|
40
|
+
# John Mettraux at openwfe.org
|
41
|
+
#
|
42
|
+
|
43
|
+
require 'yaml'
|
44
|
+
require 'fileutils'
|
45
|
+
|
46
|
+
require 'openwfe/utils'
|
47
|
+
require 'openwfe/service'
|
48
|
+
|
49
|
+
require 'openwfe/expressions/flowexpression'
|
50
|
+
require 'openwfe/expressions/raw_xml'
|
51
|
+
#
|
52
|
+
# making sure classes in those files are loaded
|
53
|
+
# before their yaml persistence is tuned
|
54
|
+
# (else the reopening of the class is interpreted as
|
55
|
+
# a definition of the class...)
|
56
|
+
|
57
|
+
|
58
|
+
module OpenWFE
|
59
|
+
|
60
|
+
|
61
|
+
#
|
62
|
+
# yaml expression storage
|
63
|
+
#
|
64
|
+
class YamlFileStorage
|
65
|
+
include ServiceMixin
|
66
|
+
|
67
|
+
attr_accessor :basepath
|
68
|
+
|
69
|
+
def initialize (service_name, application_context, path)
|
70
|
+
service_init(service_name, application_context)
|
71
|
+
@basepath = path
|
72
|
+
FileUtils.makedirs @basepath
|
73
|
+
end
|
74
|
+
|
75
|
+
def []= (id, object)
|
76
|
+
|
77
|
+
fei_path = compute_file_path(id)
|
78
|
+
fei_parent_path = File.dirname(fei_path)
|
79
|
+
|
80
|
+
FileUtils.makedirs(fei_parent_path) \
|
81
|
+
if not File.exist?(fei_parent_path)
|
82
|
+
|
83
|
+
fd = IO.sysopen(fei_path , "w+")
|
84
|
+
io = IO.open(fd , "w+")
|
85
|
+
|
86
|
+
data = YAML.dump(object)
|
87
|
+
|
88
|
+
io.write(data)
|
89
|
+
io.close
|
90
|
+
end
|
91
|
+
|
92
|
+
#
|
93
|
+
# deletes the whole storage directory... beware...
|
94
|
+
#
|
95
|
+
def purge
|
96
|
+
FileUtils.remove_dir @basepath
|
97
|
+
end
|
98
|
+
|
99
|
+
#
|
100
|
+
# check whether the key has a file in the file storage
|
101
|
+
def has_key? (fei)
|
102
|
+
File.exist?(compute_file_path(fei))
|
103
|
+
end
|
104
|
+
|
105
|
+
def remove (fei, workitem)
|
106
|
+
|
107
|
+
fei_path = compute_file_path(fei)
|
108
|
+
|
109
|
+
if File.exist?(fei_path)
|
110
|
+
File.delete(fei_path)
|
111
|
+
else
|
112
|
+
raise "Object not found at #{fei_path}"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def [] (fei)
|
117
|
+
fei_path = compute_file_path(fei)
|
118
|
+
|
119
|
+
return nil if not File.exist?(fei_path)
|
120
|
+
|
121
|
+
data = IO.read(compute_file_path(fei))
|
122
|
+
object = YAML.load(data)
|
123
|
+
|
124
|
+
object.application_context = @application_context
|
125
|
+
|
126
|
+
return object
|
127
|
+
end
|
128
|
+
|
129
|
+
def length
|
130
|
+
return count_expression(0, @basepath)
|
131
|
+
end
|
132
|
+
|
133
|
+
protected
|
134
|
+
|
135
|
+
def count_expression (count, item)
|
136
|
+
|
137
|
+
return count + 1 if OpenWFE::ends_with(item, ".yaml")
|
138
|
+
|
139
|
+
if File.stat(item).directory?
|
140
|
+
|
141
|
+
d = Dir.new(item)
|
142
|
+
d.each do |i|
|
143
|
+
next if i == "." or i == ".."
|
144
|
+
i = item + "/" + i
|
145
|
+
count = count_expression(count, i)
|
146
|
+
end
|
147
|
+
d.close()
|
148
|
+
end
|
149
|
+
|
150
|
+
return count
|
151
|
+
end
|
152
|
+
|
153
|
+
def compute_file_path (fei)
|
154
|
+
raise "this should be implemented in a subclass"
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
#
|
2
|
-
|
3
|
-
# Copyright (c) 2006, John Mettraux, OpenWFE.org
|
2
|
+
#--
|
3
|
+
# Copyright (c) 2006-2007, John Mettraux, OpenWFE.org
|
4
4
|
# All rights reserved.
|
5
5
|
#
|
6
6
|
# Redistribution and use in source and binary forms, with or without
|
@@ -28,7 +28,7 @@
|
|
28
28
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
29
29
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
30
30
|
# POSSIBILITY OF SUCH DAMAGE.
|
31
|
-
|
31
|
+
#++
|
32
32
|
#
|
33
33
|
# $Id: definitions.rb 2725 2006-06-02 13:26:32Z jmettraux $
|
34
34
|
#
|
@@ -45,9 +45,9 @@
|
|
45
45
|
# 'dollar notation' implementation in Ruby
|
46
46
|
#
|
47
47
|
|
48
|
-
module
|
48
|
+
module OpenWFE
|
49
49
|
|
50
|
-
def
|
50
|
+
def OpenWFE.dsub (text, dict)
|
51
51
|
|
52
52
|
#puts "### text is >#{text}<"
|
53
53
|
|
@@ -100,11 +100,11 @@ module OpenWFEru
|
|
100
100
|
return dsub("#{pre}#{value}#{text[j+1..-1]}", dict)
|
101
101
|
end
|
102
102
|
|
103
|
-
def
|
103
|
+
def OpenWFE.unescape (text)
|
104
104
|
return text.gsub("\\\\\\$\\{", "\\${")
|
105
105
|
end
|
106
106
|
|
107
|
-
def
|
107
|
+
def OpenWFE.dosub (text, flow_expression, workitem)
|
108
108
|
return dsub(text, FlowDict.new(flow_expression, workitem))
|
109
109
|
end
|
110
110
|
|
@@ -154,10 +154,12 @@ module OpenWFEru
|
|
154
154
|
end
|
155
155
|
|
156
156
|
def call_function (function_name)
|
157
|
+
#"function '#{function_name}' is not implemented"
|
158
|
+
"functions are not yet implemented"
|
157
159
|
end
|
158
160
|
|
159
161
|
def call_ruby (ruby_code)
|
160
|
-
eval(ruby_code).to_s
|
162
|
+
eval(ruby_code, @flow_expression.get_binding).to_s
|
161
163
|
end
|
162
164
|
end
|
163
165
|
|
@@ -0,0 +1,149 @@
|
|
1
|
+
#
|
2
|
+
#--
|
3
|
+
# Copyright (c) 2006-2007, Nicolas Modryzk, OpenWFE.org
|
4
|
+
# All rights reserved.
|
5
|
+
#
|
6
|
+
# Redistribution and use in source and binary forms, with or without
|
7
|
+
# modification, are permitted provided that the following conditions are met:
|
8
|
+
#
|
9
|
+
# . Redistributions of source code must retain the above copyright notice, this
|
10
|
+
# list of conditions and the following disclaimer.
|
11
|
+
#
|
12
|
+
# . Redistributions in binary form must reproduce the above copyright notice,
|
13
|
+
# this list of conditions and the following disclaimer in the documentation
|
14
|
+
# and/or other materials provided with the distribution.
|
15
|
+
#
|
16
|
+
# . Neither the name of the "OpenWFE" nor the names of its contributors may be
|
17
|
+
# used to endorse or promote products derived from this software without
|
18
|
+
# specific prior written permission.
|
19
|
+
#
|
20
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
21
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
22
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
23
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
24
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
25
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
26
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
27
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
28
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
29
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
30
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
#++
|
32
|
+
#
|
33
|
+
# $Id: definitions.rb 2725 2006-06-02 13:26:32Z jmettraux $
|
34
|
+
#
|
35
|
+
|
36
|
+
#
|
37
|
+
# "made in Japan"
|
38
|
+
#
|
39
|
+
# Nicolas Modrzyk at openwfe.org
|
40
|
+
#
|
41
|
+
|
42
|
+
module OpenWFE
|
43
|
+
|
44
|
+
# Simple LRU cache based on
|
45
|
+
|
46
|
+
class LRUCache
|
47
|
+
|
48
|
+
# initialize the LRU.
|
49
|
+
# cache contains the mapping key,value
|
50
|
+
# queue contains the list of keys, according to the LRU
|
51
|
+
# max is the maximum number of items to keep in the cache
|
52
|
+
def initialize(max=20, maxttl=nil)
|
53
|
+
@cache = {}
|
54
|
+
@queue = []
|
55
|
+
@ttl_cache = {}
|
56
|
+
@max = max
|
57
|
+
@maxttl = maxttl
|
58
|
+
@finalizers = []
|
59
|
+
end
|
60
|
+
|
61
|
+
# Check to see if the cache contains the given key.
|
62
|
+
def include?(key)
|
63
|
+
@cache.include?(key)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Return the element identified by the given key.
|
67
|
+
def [](key)
|
68
|
+
r = nil
|
69
|
+
if @cache.has_key? key
|
70
|
+
r = @cache[key]
|
71
|
+
@queue.push @queue.delete(key)
|
72
|
+
@ttl_cache[key] = Time.now.to_i
|
73
|
+
end
|
74
|
+
r
|
75
|
+
end
|
76
|
+
|
77
|
+
# Set the element of the cache identified by the given key.
|
78
|
+
def []=(key, val)
|
79
|
+
if @queue.length == @max
|
80
|
+
d = @queue.shift
|
81
|
+
@cache.delete d
|
82
|
+
end
|
83
|
+
if @queue.first and @maxttl
|
84
|
+
now = Time.now.to_i
|
85
|
+
while ((@ttl_cache[@queue.first] + @maxttl) < now)
|
86
|
+
d = @queue.shift
|
87
|
+
do_finalization(d,@cache[d])
|
88
|
+
@cache.delete d
|
89
|
+
end
|
90
|
+
end
|
91
|
+
@cache[key] = val
|
92
|
+
@ttl_cache[key] = Time.now.to_i
|
93
|
+
@queue.push key
|
94
|
+
end
|
95
|
+
|
96
|
+
# delete the key.
|
97
|
+
# TODO: check this is working
|
98
|
+
def delete(key)
|
99
|
+
d = @queue.delete(key)
|
100
|
+
do_finalization(d,@cache[d])
|
101
|
+
@cache.delete key
|
102
|
+
end
|
103
|
+
|
104
|
+
# Allows one to set the maximum size of the cache queue. If the queue
|
105
|
+
# is currently larger than the size that it is being set to, elements
|
106
|
+
# will be expired until the queue is at the maximum size.
|
107
|
+
|
108
|
+
def size=(max)
|
109
|
+
@max = max
|
110
|
+
while @queue.length > @max
|
111
|
+
d = @queue.shift
|
112
|
+
do_finalization(d,@cache[d])
|
113
|
+
@cache.delete d
|
114
|
+
end
|
115
|
+
@max
|
116
|
+
end
|
117
|
+
alias maxsize= size=
|
118
|
+
|
119
|
+
# Return the maximum size of the cache.
|
120
|
+
def maxsize
|
121
|
+
@max
|
122
|
+
end
|
123
|
+
|
124
|
+
# Return the current size of the cache.
|
125
|
+
def size
|
126
|
+
@queue.length
|
127
|
+
end
|
128
|
+
|
129
|
+
# Return a copy of current set of keys to cache elements.
|
130
|
+
def queue
|
131
|
+
@queue.dup
|
132
|
+
end
|
133
|
+
|
134
|
+
def add_finalizer(*args,&block)
|
135
|
+
@finalizers.push [block,args]
|
136
|
+
end
|
137
|
+
|
138
|
+
def do_finalization(key,obj)
|
139
|
+
begin
|
140
|
+
@finalizers.each do |f|
|
141
|
+
f[0].call(key,obj,*f[1])
|
142
|
+
end
|
143
|
+
rescue Exception => e
|
144
|
+
puts e, e.backtrace
|
145
|
+
raise e
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|