Skip to content

Model.objects.get should support consistent reads #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
5 changes: 3 additions & 2 deletions simpledb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ def values(self, *args):
def item_names(self):
return self._get_query().item_names()

def get(self, name):
return self.model.from_item(self.model.Meta.domain.get(name))
def get(self, name, consistent_read=False):
return self.model.from_item(self.model.Meta.domain.get(name,
consistent_read))

def _get_query(self):
return Query(self.model.Meta.domain)
Expand Down
21 changes: 14 additions & 7 deletions simpledb/simpledb.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ def delete_attributes(self, domain, item, attributes=None):
request = Request("POST", self._sdb_url(), data)
self._make_request(request)

def get_attributes(self, domain, item, attributes=None):
def get_attributes(self, domain, item, attributes=None, consistent_read=False):
"""
Returns all of the attributes associated with the item.

Expand All @@ -501,6 +501,9 @@ def get_attributes(self, domain, item, attributes=None):
'DomainName': domain,
'ItemName': item,
}
if consistent_read:
data.update(ConsistentRead='true')

if attributes:
for i, attr in enumerate(attributes):
data['AttributeName.%s' % i] = attr
Expand Down Expand Up @@ -657,13 +660,11 @@ def _make_condition(self, attribute, operation, value, encoder):
operation, self._quote(value))

def _make_eq_condition(self, attribute, operation, value, encoder):
value = encoder(attribute, value)
if value is None:
return '%s IS NULL' % attribute
return self._make_condition(attribute, operation, value, encoder)

def _make_noteq_condition(self, attribute, operation, value, encoder):
value = encoder(attribute, value)
if value is None:
return '%s IS NOT NULL' % attribute
return self._make_condition(attribute, operation, value, encoder)
Expand Down Expand Up @@ -920,10 +921,15 @@ def values(self, *args):
def item_names(self):
return self._get_query().item_names()

def get(self, name):
def get(self, name, consistent_read=False):
"""
if name not in self.items:
self.items[name] = Item.load(self.simpledb, self, name)
self.items[name] = Item.load(self.simpledb, self, name, consistent_read)
item = self.items[name]
"""

item = Item.load(self.simpledb, self, name, consistent_read)

if not item:
raise ItemDoesNotExist(name)
return item
Expand Down Expand Up @@ -965,8 +971,9 @@ def _get_query(self):

class Item(DictMixin):
@classmethod
def load(cls, simpledb, domain, name):
attrs = simpledb.get_attributes(domain, name)
def load(cls, simpledb, domain, name, consistent_read=False):
attrs = simpledb.get_attributes(domain, name,
consistent_read=consistent_read)
return cls(simpledb, domain, name, attrs)

def __init__(self, simpledb, domain, name, attributes=None):
Expand Down