Skip to content

Commit

Permalink
Added first support for the dbaccelerator (faster get_multi) in respo…
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteinruecken committed May 20, 2021
1 parent 013b455 commit 4572822
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions core/db.py
Expand Up @@ -13,6 +13,10 @@
import binascii
from dataclasses import dataclass, field
from contextvars import ContextVar
try:
from viur import dbaccelerator
except ImportError:
dbaccelerator = None

"""
Tiny wrapper around *google.appengine.api.datastore*.
Expand Down Expand Up @@ -129,13 +133,19 @@ def Get(keys: Union[KeyClass, List[KeyClass]]) -> Union[List[Entity], Entity, No
for key in keys:
if not key.kind.startswith("viur"):
dataLog.add(key)
# GetMulti does not obey orderings - results can be returned in any order. We'll need to fix this here
resList = list(__client__.get_multi(keys))
resList.sort(key=lambda x: keys.index(x.key) if x else -1)
return resList
if dbaccelerator and not IsInTransaction(): # Use the fast-path fetch
return dbaccelerator.fetchMulti(keys)
else:
# GetMulti does not obey orderings - results can be returned in any order. We'll need to fix this here
resList = list(__client__.get_multi(keys))
resList.sort(key=lambda x: keys.index(x.key) if x else -1)
return resList
if not keys.kind.startswith("viur"):
dataLog.add(keys)
return __client__.get(keys)
if dbaccelerator and not IsInTransaction(): # Use the fast-path fetch
return dbaccelerator.fetchMulti([keys])[0]
else:
return __client__.get(keys)


def Put(entity: Union[Entity, List[Entity]]):
Expand Down Expand Up @@ -658,8 +668,13 @@ def _runSingleFilterQuery(self, query, limit):
qry.order = [x[0] if x[1] == SortOrder.Ascending else "-" + x[0] for x in newSortOrder]
else:
qry.order = [x[0] if x[1] == SortOrder.Ascending else "-" + x[0] for x in query.orders]
qryRes = qry.fetch(limit=limit, start_cursor=query.startCursor, end_cursor=query.endCursor)
res = next(qryRes.pages)
if dbaccelerator and not IsInTransaction(): # Use the fast-path fetch
qry.keys_only()
qryRes = qry.fetch(limit=limit, start_cursor=query.startCursor, end_cursor=query.endCursor)
res = dbaccelerator.fetchMulti([x.key for x in next(qryRes.pages)])
else:
qryRes = qry.fetch(limit=limit, start_cursor=query.startCursor, end_cursor=query.endCursor)
res = list(next(qryRes.pages))
query.currentCursor = qryRes.next_page_token
return res

Expand Down

0 comments on commit 4572822

Please sign in to comment.