Mercurial > libervia-pubsub
annotate idavoll/memory_storage.py @ 124:c4ee16bc48e5
Change Node.set_configuration() to set cached configuration in a callback.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Tue, 12 Apr 2005 12:34:25 +0000 |
parents | 8892331314c8 |
children | ffc3c706b0fb |
rev | line source |
---|---|
107 | 1 import copy |
2 from zope.interface import implements | |
3 from twisted.internet import defer | |
4 from twisted.words.protocols.jabber import jid | |
5 import storage | |
6 | |
115 | 7 default_config = {"pubsub#persist_items": True, |
8 "pubsub#deliver_payloads": True} | |
107 | 9 |
10 class Storage: | |
11 | |
12 implements(storage.IStorage) | |
13 | |
14 def __init__(self): | |
15 self._nodes = {} | |
16 | |
17 def get_node(self, node_id): | |
18 try: | |
19 node = self._nodes[node_id] | |
20 except KeyError: | |
21 return defer.fail(storage.NodeNotFound()) | |
22 | |
23 return defer.succeed(node) | |
24 | |
25 def get_node_ids(self): | |
26 return defer.succeed(self._nodes.keys()) | |
27 | |
28 def create_node(self, node_id, owner, config = None, type='leaf'): | |
29 if node_id in self._nodes: | |
30 return defer.fail(storage.NodeExists()) | |
31 | |
32 if not config: | |
33 config = copy.copy(default_config) | |
34 | |
35 if type != 'leaf': | |
36 raise NotImplementedError | |
37 | |
38 node = LeafNode(node_id, owner, config) | |
39 self._nodes[node_id] = node | |
40 | |
41 return defer.succeed(None) | |
42 | |
43 def delete_node(self, node_id): | |
44 try: | |
45 del self._nodes[node_id] | |
46 except KeyError: | |
47 return defer.fail(storage.NodeNotFound()) | |
48 | |
49 return defer.succeed(None) | |
50 | |
51 def get_affiliations(self, entity): | |
112 | 52 entity = entity.userhost() |
53 return defer.succeed([(node.id, node._affiliations[entity]) | |
107 | 54 for name, node in self._nodes.iteritems() |
112 | 55 if entity in node._affiliations]) |
107 | 56 |
57 def get_subscriptions(self, entity): | |
58 subscriptions = [] | |
59 for node in self._nodes.itervalues(): | |
60 for subscriber, subscription in node._subscriptions.iteritems(): | |
61 subscriber = jid.JID(subscriber) | |
112 | 62 if subscriber.userhostJID() == entity.userhostJID(): |
107 | 63 subscriptions.append((node.id, subscriber, |
64 subscription.state)) | |
65 | |
66 return defer.succeed(subscriptions) | |
67 | |
68 class Node: | |
69 | |
70 implements(storage.INode) | |
71 | |
72 def __init__(self, node_id, owner, config): | |
73 self.id = node_id | |
112 | 74 self._affiliations = {owner.userhost(): 'owner'} |
107 | 75 self._subscriptions = {} |
76 self._config = config | |
77 | |
78 def get_type(self): | |
79 return self.type | |
80 | |
81 def get_configuration(self): | |
82 return self._config | |
83 | |
84 def get_meta_data(self): | |
85 config = copy.copy(self._config) | |
86 config["pubsub#node_type"] = self.type | |
87 return config | |
88 | |
89 def set_configuration(self, options): | |
90 for option in options: | |
91 if option in self._config: | |
92 self._config[option] = options[option] | |
93 | |
94 return defer.succeed(None) | |
95 | |
96 def get_affiliation(self, entity): | |
97 return defer.succeed(self._affiliations.get(entity.full())) | |
98 | |
120
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
99 def get_subscription(self, subscriber): |
107 | 100 try: |
101 subscription = self._subscriptions[subscriber.full()] | |
120
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
102 except KeyError: |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
103 state = None |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
104 else: |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
105 state = subscription.state |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
106 return defer.succeed(state) |
107 | 107 |
120
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
108 def add_subscription(self, subscriber, state): |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
109 if self._subscriptions.get(subscriber.full()): |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
110 return defer.fail(storage.SubscriptionExists()) |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
111 |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
112 subscription = Subscription(state) |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
113 self._subscriptions[subscriber.full()] = subscription |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
114 return defer.succeed(None) |
107 | 115 |
116 def remove_subscription(self, subscriber): | |
120
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
117 try: |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
118 del self._subscriptions[subscriber.full()] |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
119 except KeyError: |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
120 return defer.fail(storage.SubscriptionNotFound()) |
107 | 121 |
122 return defer.succeed(None) | |
123 | |
124 def get_subscribers(self): | |
125 subscribers = [jid.JID(subscriber) for subscriber, subscription | |
126 in self._subscriptions.iteritems() | |
127 if subscription.state == 'subscribed'] | |
128 | |
129 return defer.succeed(subscribers) | |
130 | |
131 def is_subscribed(self, subscriber): | |
132 try: | |
133 subscription = self._subscriptions[subscriber.full()] | |
134 except KeyError: | |
135 return defer.succeed(False) | |
136 | |
137 return defer.succeed(subscription.state == 'subscribed') | |
138 | |
139 class LeafNode(Node): | |
140 | |
141 implements(storage.ILeafNode) | |
142 type = 'leaf' | |
143 | |
144 def __init__(self, node_id, owner, config): | |
145 Node.__init__(self, node_id, owner, config) | |
146 self._items = {} | |
147 self._itemlist = [] | |
148 | |
149 def store_items(self, items, publisher): | |
150 for data in items: | |
151 id = data["id"] | |
152 item = (data.toXml(), publisher) | |
153 if id in self._items: | |
154 self._itemlist.remove(self._items[id]) | |
155 self._items[id] = item | |
156 self._itemlist.append(item) | |
157 | |
158 return defer.succeed(None) | |
159 | |
160 def remove_items(self, item_ids): | |
161 deleted = [] | |
162 | |
163 for item_id in item_ids: | |
164 try: | |
165 item = self._items[item_id] | |
166 self._itemlist.remove(item) | |
167 del self._items[item_id] | |
168 deleted.append(item_id) | |
169 except KeyError: | |
170 pass | |
171 | |
172 return defer.succeed(deleted) | |
173 | |
174 def get_items(self, max_items=None): | |
175 if max_items: | |
176 list = self._itemlist[-max_items:] | |
177 else: | |
178 list = self._itemlist | |
179 return defer.succeed([item[0] for item in list]) | |
180 | |
181 def get_items_by_id(self, item_ids): | |
182 items = [] | |
183 for item_id in item_ids: | |
184 try: | |
185 item = self._items[item_id] | |
186 except KeyError: | |
187 pass | |
188 else: | |
189 items.append(item[0]) | |
190 return defer.succeed(items) | |
191 | |
192 def purge(self): | |
193 self._items = {} | |
194 self._itemlist = [] | |
195 | |
196 return defer.succeed(None) | |
197 | |
198 class Subscription: | |
199 | |
200 implements(storage.ISubscription) | |
201 | |
202 def __init__(self, state): | |
203 self.state = state |