Skip to content
Browse files

more optimal retrieval from tuplespace

  • Loading branch information...
1 parent cf5e1f8 commit a5612fa60552218f7a4694350454e213d61f5106 mvr committed May 13, 2012
Showing with 32 additions and 33 deletions.
  1. +3 −2 lib/cortex/core/bus.py
  2. +18 −16 lib/cortex/core/ground.py
  3. +3 −6 lib/cortex/services/__init__.py
  4. +8 −9 lib/cortex/services/postoffice.py
View
5 lib/cortex/core/bus.py
@@ -27,10 +27,11 @@ def has_subscription(self, key, callback):
""" override from cyrusbus enforcing simple callbacks as
subscriptions (by default cyrusbus uses dictionaries)
"""
+
if key not in self.keys():
return False
- subs=self.subscriptions[key]
- if subs==NotFound:
+ subs = self.subscriptions[key]
+ if subs == NotFound:
return False
return callback in subs
View
34 lib/cortex/core/ground.py
@@ -2,8 +2,9 @@
smart datastructures for storage
"""
-import datetime
+import sys
import pickle
+import datetime
from lindypy.TupleSpace import TSpace
@@ -195,31 +196,31 @@ def __contains__(self,other):
return other in self.keys()
def public_keys(self):
- """ like self.keys(), only respects privacy for _ and __
+ """ like _keys(), only respects privacy for _ and __
"""
FORBIDDEN_PREFIXES = '_ __'.split()
- return [ k for k in self.keys() if not any( map(k.startswith, FORBIDDEN_PREFIXES) ) ]
+ return [ k for k in self._keys() if not any( map(k.startswith, FORBIDDEN_PREFIXES) ) ]
+ keys = public_keys
def __getitem__(self, key):
""" TODO: is this tailored too much for the PostOffice, or is it sufficiently generic?
- TODO: transparent encryption, serialization, perspective warping?
+ TODO: transparent encryption, serialization, perspective warping, etc
"""
- matching_tuples = self.filter(lambda tpl: self.tuple2key(tpl)==key)
- #assert len(matching_tuples)<2,"Found duplicate matching tuples.. not really a keyspace then, is it?"
- if matching_tuples:
- first_match = matching_tuples[0]
- return self.tuple2value(first_match)
- return NotFound
-
- def subspace(self, name):
- """ returns a nested keyspace with name <name> """
- raise Exception,"Not implemented"
+ pattern = (key, object)
+ res = self.many( pattern, sys.maxint)
+ if len(res) not in [0, 1]:
+ raise TypeError("Not really a keyspace then is it?")
+ if len(res)==0:
+ return NotFound
+ else:
+ colid, match = res[0]
+ return self.tuple2value(match)
def __contains__(self,other):
""" dictionary compatibility """
return other in self.keys()
- def keys(self):
+ def _keys(self):
""" dictionary compatibility
1) compute keys from tuplespace
@@ -243,7 +244,8 @@ def _tspace_as_dict(self):
2) aggregate for every value-set that uses a given key
TODO: 3) de-dupe the aggregation?
"""
- keys = [ k for k in self.keys() if not k.startswith('__') ]
+ #keys = [ k for k in self.keys() if not k.startswith('__') ]
+ keys = self.public_keys() #[ k for k in self.public_keys() if not k.startswith('__') ]
results = {}
for k in keys:
test = lambda tpl: self.tuple2key(tpl)==k
View
9 lib/cortex/services/__init__.py
@@ -2,10 +2,11 @@
"""
from cortex.core.util import report, console
-from cortex.core.data import NOOP#, IDENTITY
+
+from cortex.core.data import NOOP
+from cortex.contrib.aima import csp
from cortex.core.agent import Agent, AgentManager
from cortex.core.manager import Manager
-from cortex.contrib.aima import csp
class ServiceManager(AgentManager):
@@ -14,10 +15,6 @@ class ServiceManager(AgentManager):
commonly used Exception.
"""
- # registering services can happen all the time..
- # we don't want to do anything noisy here like write an event
- #post_registration = NOOP
-
# TODO: might need an abstractagentmanager..
pre_load_obj = Manager.pre_load_obj
View
17 lib/cortex/services/postoffice.py
@@ -7,12 +7,10 @@
(<bound method Terminal.push_q of <Terminal-Service 176000620>>,)
# get a specific channel object
- >>> events=postoffice.event
+ >>> events = postoffice.event
>>> events
<CHAN-(EVENT_T)>
- see cortex.core.channels for further documentation on what channels can do.
-
"""
import simplejson
@@ -40,14 +38,15 @@ class PostOffice(Service, Keyspace, SelfHostingTupleBus, ChannelManager):
non-blocking.
"""
- ## Begin channel declarations ( TODO: make this more like promela )
- notice = channel.NOTICE # shortcut for publishing notices (unused)
- error = getattr(channel, ERROR_T) # shortcut for publishing errors (unused)
- event = getattr(channel, EVENT_T) # shortcut for publishing events (used by term)
- peers = getattr(channel,PEER_T)
+ ## Begin embedded channel declarations ( TODO: make this more like promela )
+ notice = channel.NOTICE # shortcut for publishing notices (unused)
+ error = getattr(channel, ERROR_T) # shortcut for publishing errors (unused)
+ event = getattr(channel, EVENT_T) # shortcut for publishing events (used by term)
+ peers = getattr(channel, PEER_T)
def __init__(self, *args, **kargs):
- """ """
+ """ PostOffice.__init__
+ """
# initialize keyspace and embedded channels
default_name = 'PostOffice::{_id}::keyspace'.format(_id=str(id(self)))
keyspace_name = self.universe or default_name

0 comments on commit a5612fa

Please sign in to comment.
Something went wrong with that request. Please try again.