Skip to content

Commit

Permalink
Overhaul how queryless all() works.
Browse files Browse the repository at this point in the history
  • Loading branch information
kadams54 committed Dec 15, 2016
1 parent 53dc8bc commit 412b99e
Showing 1 changed file with 25 additions and 32 deletions.
57 changes: 25 additions & 32 deletions switchboard/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,43 +156,36 @@ def remove(cls, key):

@classmethod
def all(cls):
if cls._is_redis_datastore():
return cls._redis_all()
else:
query = datastore.Query(_key())
return [cls(**data) for data in cls.ds.query(query)]

@classmethod
def _is_redis_datastore(cls):
'''
Check to see if the datastore is Redis.
'''
query = datastore.Query(_key())
try:
import datastore.redis
except ImportError:
return False
return isinstance(cls.ds, datastore.redis.RedisDatastore)
results = cls.ds.query(query)
except NotImplementedError:
results = cls._queryless_all()
return [cls(**result) for result in results]

@classmethod
def _redis_all(cls):
def _queryless_all(cls):
'''
This is a hack because Datastore's Redis layer doesn't support
querying.
This is a hack because some datastore implementations don't support
querying. Right now the solution is to drop down to the underlying
native client and query all, which means that this section is ugly.
If it were architected properly, you might be able to do something
like inject an implementation of a NativeClient interface, which would
let Switchboard users write their own NativeClient wrappers that
implement all. However, at this point I'm just happy getting datastore
to work, so quick-and-dirty will suffice.
'''
r = cls.ds._redis
keys = r.keys()
# Need to deserialize.
serializer = cls.ds.child_datastore.serializer

def get_instance(k):
value = r.get(k)
if value is None:
return value
else:
deserialized_value = serializer.loads(value)
return cls(**deserialized_value)

return [get_instance(k) for k in keys]
if hasattr(cls.ds, '_redis'):
r = cls.ds._redis
keys = r.keys()
serializer = cls.ds.child_datastore.serializer

def get_value(k):
value = r.get(k)
return value if value is None else serializer.loads(value)
return [get_value(k) for k in keys]
else:
raise NotImplementedError

@classmethod
def drop(cls):
Expand Down

0 comments on commit 412b99e

Please sign in to comment.