1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 import org.vertx.java.deploy.impl.VertxLocator
16 import org.vertx.java.core.buffer
17
18 from core.javautils import map_from_java
19 from core.buffer import Buffer
20
21 __author__ = "Scott Horn"
22 __email__ = "scott@hornmicro.com"
23 __credits__ = "Based entirely on work by Tim Fox http://tfox.org"
26 """Sometimes it is desirable to share immutable data between different event loops, for example to implement a
27 cache of data.
28
29 This class allows instances of shareddata data structures to be looked up and used from different event loops.
30 The data structures themselves will only allow certain data types to be stored into them. This shields the
31 user from worrying about any thread safety issues might occur if mutable objects were shareddata between event loops.
32
33 The following types can be stored in a shareddata data structure:
34
35 String
36 FixNum
37 Float
38 Buffer - this will be automatically copied, and the copy will be stored in the structure.
39 """
40 @staticmethod
42 return org.vertx.java.deploy.impl.VertxLocator.vertx.sharedData()
43
44 @staticmethod
46 """Return a Hash with the specific name. All invocations of this method with the same value of name
47 are guaranteed to return the same Hash instance.
48
49 Keyword arguments:
50 @param key: Get the hash with the key.
51
52 @return: the hash.
53 """
54 map = SharedData.shared_data().getMap(key)
55 return SharedHash(map)
56
57 @staticmethod
59 """Return a Set with the specific name. All invocations of this method with the same value of name
60 are guaranteed to return the same Set instance.
61
62 Keyword arguments:
63 @param key: Get the set with the key.
64
65 @return: the shared set.
66 """
67 set_ = SharedData.shared_data().getSet(key)
68 return SharedSet(set_)
69
70 @staticmethod
72 """Remove the hash
73
74 Keyword arguments:
75 @param key: The key of the hash.
76 """
77 return SharedData.shared_data().removeMap(key)
78
79 @staticmethod
81 """Remove the set
82
83 Keyword arguments:
84 @param key: The key of the set.
85 """
86 return SharedData.shared_data().removeSet(key)
87
88 @staticmethod
90 """Convert to corresponding Java objects
91 and make copies where appropriate (the underlying java map will also make copies for some data types too)
92 """
93 if isinstance(obj, Buffer):
94 obj = obj._to_java_buffer()
95 return obj
96
119
122
125
128
131
134
136
138 self.java_obj = java_set
139
145
148
151
154
155 - def add(self, obj):
156 """ Add an object to the set
157
158 Keyword arguments:
159 @param obj: The object to add
160 @return: self
161 """
162 obj = SharedData.check_obj(obj)
163 self.java_obj.add(obj)
164 return self
165
167 """Clear the set"""
168 self.java_obj.clear()
169
171 """Delete an object from the set
172
173 Keyword arguments:
174 @param obj: the object to delete
175 """
176 self.java_obj.remove(obj)
177
178
179 - def each(self, func):
180 """Call the func for every element of the set
181
182 Keyword arguments:
183 @param func: The function to call.
184 """
185 iter = self.java_obj.iterator()
186 while iter.hasNext():
187 obj = iter.next()
188 if isinstance(obj, org.vertx.java.core.buffer.Buffer):
189 obj = Buffer(obj)
190 func(obj)
191
193 """returns True if the set is empty"""
194 return self.java_obj.isEmpty()
195
197 """Does the set contain an element?
198
199 Keyword arguments:
200 @param obj: the object to check if the set contains
201
202 @return: True if the object is contained in the set
203 """
204 if isinstance(obj, Buffer):
205 obj = obj._to_java_buffer()
206 return self.java_obj.contains(obj)
207
209 """returns the number of elements in the set"""
210 return self.java_obj.size()
211
214