Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 from appengine_wrappers import db, IsDevServer | |
| 6 from datastore_models import PersistentObjectStoreItem | |
| 7 from future import Future | |
| 8 import logging | |
| 9 from object_store import ObjectStore | |
| 10 | |
| 11 def log(s): | |
|
cduvall
2013/04/17 23:30:37
take out
not at google - send to devlin
2013/04/18 04:05:41
Done.
| |
| 12 print(s) | |
| 13 logging.error(s) | |
| 14 | |
| 15 class _AsyncGetFuture(object): | |
| 16 def __init__(self, object_store, keys): | |
| 17 self._futures = dict( | |
| 18 (k, db.get_async( | |
| 19 PersistentObjectStoreItem.CreateKey(object_store._namespace, k))) | |
| 20 for k in keys) | |
| 21 | |
| 22 def Get(self): | |
| 23 def has_result(key, future): | |
| 24 return future.get_result() is not None | |
| 25 #if future.get_result() is None: | |
|
cduvall
2013/04/17 23:30:37
take out
not at google - send to devlin
2013/04/18 04:05:41
Done.
not at google - send to devlin
2013/04/18 04:05:41
Done.
| |
| 26 # log('NO: %s' % key) | |
| 27 # return False | |
| 28 #log('YES: %s' % key) | |
| 29 #return True | |
| 30 return dict((key, future.get_result().GetValue()) | |
| 31 for key, future in self._futures.iteritems() | |
| 32 if has_result(key, future)) | |
| 33 | |
| 34 class PersistentObjectStore(ObjectStore): | |
| 35 '''Stores data persistently using the AppEngine Datastore API. | |
| 36 ''' | |
| 37 def __init__(self, namespace): | |
| 38 self._namespace = namespace | |
| 39 | |
| 40 def SetMulti(self, mapping): | |
| 41 futures = [] | |
| 42 for key, value in mapping.items(): | |
| 43 futures.append(db.put_async( | |
| 44 PersistentObjectStoreItem.CreateItem(self._namespace, key, value))) | |
| 45 # If running the dev server, the futures don't complete until the server is | |
| 46 # *quitting*. This is annoying. Flush now. | |
| 47 if IsDevServer(): | |
| 48 [future.wait() for future in futures] | |
| 49 | |
| 50 def GetMulti(self, keys): | |
| 51 return Future(delegate=_AsyncGetFuture(self, keys)) | |
| 52 | |
| 53 def DelMulti(self, keys): | |
| 54 futures = [] | |
| 55 for key in keys: | |
| 56 futures.append(db.delete_async( | |
| 57 PersistentObjectStoreItem.CreateKey(self._namespace, key))) | |
| 58 # If running the dev server, the futures don't complete until the server is | |
| 59 # *quitting*. This is annoying. Flush now. | |
| 60 if IsDevServer(): | |
| 61 [future.wait() for future in futures] | |
| OLD | NEW |