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 6 from datastore_models import PersistentObjectStoreItem 7 from environment import IsDevServer 8 from future import Future 9 from object_store import ObjectStore 10 11 12 class PersistentObjectStore(ObjectStore): 13 '''Stores data persistently using the AppEngine Datastore API. 14 ''' 15 def __init__(self, namespace): 16 self._namespace = namespace 17 18 def SetMulti(self, mapping): 19 futures = [] 20 for key, value in mapping.items(): 21 futures.append(db.put_async( 22 PersistentObjectStoreItem.CreateItem(self._namespace, key, value))) 23 # If running the dev server, the futures don't complete until the server is 24 # *quitting*. This is annoying. Flush now. 25 if IsDevServer(): 26 [future.wait() for future in futures] 27 28 def GetMulti(self, keys): 29 db_futures = dict( 30 (k, db.get_async( 31 PersistentObjectStoreItem.CreateKey(self._namespace, k))) 32 for k in keys) 33 def resolve(): 34 return dict((key, future.get_result().GetValue()) 35 for key, future in db_futures.iteritems() 36 if future.get_result() is not None) 37 return Future(callback=resolve) 38 39 def DelMulti(self, keys): 40 futures = [] 41 for key in keys: 42 futures.append(db.delete_async( 43 PersistentObjectStoreItem.CreateKey(self._namespace, key))) 44 # If running the dev server, the futures don't complete until the server is 45 # *quitting*. This is annoying. Flush now. 46 if IsDevServer(): 47 [future.wait() for future in futures] 48