Package core :: Module shared_data

[hide private]
[frames] | no frames]

# Source Code for Module core.shared_data

      1  # Copyright 2011 the original author or authors.
      2  #
      3  # Licensed under the Apache License, Version 2.0 (the "License");
      4  # you may not use this file except in compliance with the License.
      5  # You may obtain a copy of the License at
      6  #
      7  #      http://www.apache.org/licenses/LICENSE-2.0
      8  #
      9  # Unless required by applicable law or agreed to in writing, software
     10  # distributed under the License is distributed on an "AS IS" BASIS,
     11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12  # See the License for the specific language governing permissions and
     13  # limitations under the License.
     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"
     24
     25 -class SharedData(object):
     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
     41 -    def shared_data():
     42          return org.vertx.java.deploy.impl.VertxLocator.vertx.sharedData()
     43
     44      @staticmethod
     45 -    def get_hash(key):
     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
     58 -    def get_set(key):
     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
     71 -    def remove_hash(key):
     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
     80 -    def remove_set(key):
     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
     89 -    def check_obj(obj):
     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
     97
     98 -class SharedHash(object):
     99
    100 -    def __init__(self, hash):
    101        self.java_obj = hash
    102
    103 -    def __setitem__(self, key, val):
    104          key = SharedData.check_obj(key)
    105          val = SharedData.check_obj(val)
    106          self.java_obj.put(key, val)
    107
    108 -    def __getitem__(self, key):
    109          obj = self.java_obj.get(key)
    110          if isinstance(obj, org.vertx.java.core.buffer.Buffer):
    111              obj = Buffer(obj)
    112          return obj
    113
    114 -    def __eq__(self, other):
    115          if isinstance(other, SharedHash):
    116              return self.java_obj.equals(other._to_java_map())
    117          else:
    118              return False
    119
    120 -    def __str__(self):
    121          return map_from_java(self.java_obj).__str__()
    122
    123 -    def keys(self):
    124          return map_from_java(self.java_obj).keys()
    125
    126 -    def iteritems(self):
    127          return map_from_java(self.java_obj).iteritems()
    128
    129 -    def items(self):
    130          return map_from_java(self.java_obj).items()
    131
    132 -    def _to_java_map(self):
    133          return self.java_obj
    134
    135 -class SharedSet(object):
    136
    137 -    def __init__(self, java_set):
    138          self.java_obj = java_set
    139
    140 -    def __eq__(self, other):
    141          if isinstance(other, SharedSet):
    142              return self.java_obj.hashCode() == other._to_java_set().hashCode()
    143          else:
    144              return False
    145
    146 -    def __len__(self):
    147          return self.size()
    148
    149 -    def __str__(self):
    150          return map_from_java(self.java_obj).__str__()
    151
    152 -    def __iter__(self):
    153          return map_from_java(self.java_obj).__iter__()
    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
    166 -    def clear(self):
    167          """Clear the set"""
    168          self.java_obj.clear()
    169
    170 -    def delete(self, obj):
    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
    192 -    def empty(self):
    193          """returns True if the set is empty"""
    194          return self.java_obj.isEmpty()
    195
    196 -    def include(self, obj):
    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
    208 -    def size(self):
    209          """returns the number of elements in the set"""
    210          return self.java_obj.size()
    211
    212 -    def _to_java_set(self):
    213          return self.java_obj
    214