def load_multi(cls, ids): """ Loads multiple entities from the datastore in batches. """ _log.debug('Looking up %s entities with ids: %s.' % (cls.__name__, ids)) def get_key(id): key = googledatastore.Key() path = key.path_element.add() path.kind = cls.__name__ if isinstance(id, basestring): path.name = id else: path.id = id return key def get_entity(result): entity = cls() entity._set_entity(result.entity) return entity l = [] for id in ids: l.append(id) if len(l) >= 10: _log.debug('Fetching %s' % l) req = googledatastore.LookupRequest() req.key.extend(map(get_key, l)) resp = googledatastore.lookup(req) for res in resp.found: yield get_entity(res) l = [] if len(l) > 0: _log.debug('Fetching %s' % l) req = googledatastore.LookupRequest() req.key.extend(map(get_key, l)) resp = googledatastore.lookup(req) for res in resp.found: yield get_entity(res)
def read_rec(table_name, objid): """Generator that yields keyed recs from store.""" req = datastore.LookupRequest() req.key.extend([make_key(table_name, objid)]) for found in datastore.lookup(req).found: yield extract_entity(found)
def __lookup(self, transaction=None): _log.debug('%s.__lookup()', type(self).__name__) req = googledatastore.LookupRequest() req.key.extend([self.__get_key()]) if transaction: req.read_options.transaction = transaction return googledatastore.lookup(req)
def checkDB(self,gitkit_user): req = datastore.BeginTransactionRequest() resp = datastore.begin_transaction(req) tx = resp.transaction req = datastore.LookupRequest() key = datastore.Key() path = key.path_element.add() path.kind = 'PSUsers' path.name = gitkit_user.user_id req.key.extend([key]) req.read_options.transaction = tx resp = datastore.lookup(req) req = datastore.CommitRequest() req.transaction = tx if resp.missing: user = req.mutation.insert.add() user.key.CopyFrom(key) userid_property = user.property.add() userid_property.name = 'userid' userid_property.value.string_value = gitkit_user.user_id display_name_property = user.property.add() display_name_property.name = 'display_name' display_name_property.value.string_value = gitkit_user.name photo_url_property = user.property.add() photo_url_property.name = 'photo_url' if gitkit_user.photo_url: photo_url_property.value.string_value = gitkit_user.photo_url else: photo_url_property.value.string_value = "/images/home/slider/slide1/cloud1.png" email_property = user.property.add() email_property.name = 'email' email_property.value.string_value = gitkit_user.email last_login_property = user.property.add() last_login_property.name = 'last_login' last_login_property.value.timestamp_microseconds_value = long(time.time() * 1e6) elif resp.found: user = resp.found[0].entity last_login_property = datastore.Property() last_login_property.name = 'last_login' for prop in user.property: if prop.name == 'last_login': prop.value.timestamp_microseconds_value = long(time.time() * 1e6) req.mutation.update.extend([user]) resp = datastore.commit(req) return None
def __lookup(self, transaction = None): _log.debug('%s.__lookup()', type(self).__name__) req = googledatastore.LookupRequest() req.key.extend([self.__get_key()]) if transaction: req.read_options.transaction = transaction return googledatastore.lookup(req)
def clouddatastore_client(): try: logFormatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') root = logging.getLogger() root.setLevel(logging.INFO) ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.INFO) ch.setFormatter(logFormatter) root.addHandler(ch) logging.getLogger('oauth2service.client').setLevel(logging.INFO) logging.getLogger('apiclient.discovery').setLevel(logging.INFO) credentials = GoogleCredentials.get_application_default() http = httplib2.Http() http = credentials.authorize(http) credentials.refresh(http) credentials.access_token = 'foo' print credentials.access_token datastore.set_options(project_id='p0', credentials=credentials) req = datastore.LookupRequest() key = datastore.Key() path = key.path.add() path.kind = 'Employee' path.name = 'aguadypoogznoqofmgmy' req.keys.extend([key]) resp = datastore.lookup(req) if resp.found: entity = resp.found[0].entity print (str(entity)) for prop in entity.properties: print 'Lookup: ' + str(prop) else: print 'entity not found; initialize entity and insert..' req = datastore.CommitRequest() req.mode = datastore.CommitRequest.NON_TRANSACTIONAL employee = req.mutations.add().insert path = employee.key.path.add() path.kind = 'Employee' path.name = 'aguadypoogznoqofmgmy' res = datastore.commit(req) print res except HttpError as err: print 'Error:', pprint.pprint(err.content) except AccessTokenRefreshError: print ("Credentials have been revoked or expired, please re-run" "the application to re-authorize")
def tryit(): try: # Create a RPC request to begin a new transaction. # XXX req = datastore.BeginTransactionRequest() # Execute the RPC synchronously. #XXX resp = datastore.begin_transaction(req) # Get the transaction handle from the response. # Create a RPC request to get entities by key. req = datastore.LookupRequest() # Create a new entity key. key = datastore.Key() # Set the entity key with only one `path_element`: no parent. path = key.path_element.add() path.kind = 'kindlooptest' path.name = 'ebadbec' #XXX path.name = 'bogus' # Add one key to the lookup request. req.key.extend([key]) # Set the transaction, so we get a consistent snapshot of the # entity at the time the transaction started. # Execute the RPC and get the response. resp = datastore.lookup(req) # Create a RPC request to commit the transaction. # Set the transaction to commit. if resp.found: # print "found" pass else: print "missing" except datastore.RPCError as e: # RPCError is raised if any error happened during a RPC. # It includes the `method` called and the `reason` of the # failure as well as the original `HTTPResponse` object. logging.error('Error while doing datastore operation') logging.error('RPCError: %(method)s %(reason)s', {'method': e.method, 'reason': e.reason}) logging.error('HTTPError: %(status)s %(reason)s', {'status': e.response.status, 'reason': e.response.reason}) return
def save_batch(entities): req = googledatastore.BeginTransactionRequest() resp = googledatastore.begin_transaction(req) transaction = resp.transaction # Bulk lookup req = googledatastore.LookupRequest() req.key.extend([entity.__get_key() for entity in entities]) req.read_options.transaction = transaction resp = googledatastore.lookup(req) # Update created / modified times. missing = set() for result in resp.missing: key = result.entity.key.path_element[0] missing.add(key.id or key.name) for entity in entities: entity.modified_time = time if entity._get_id() in missing: entity.created_time = time # Bulk upsert req = googledatastore.CommitRequest() req.transaction = transaction req.mutation.upsert.extend([entity._get_entity() for entity in entities]) googledatastore.commit(req)
def save_batch(entities): req = googledatastore.BeginTransactionRequest() resp = googledatastore.begin_transaction(req) transaction = resp.transaction # Bulk lookup req = googledatastore.LookupRequest() req.key.extend([entity.__get_key() for entity in entities]) req.read_options.transaction = transaction resp = googledatastore.lookup(req) # Update created / modified times. missing = set() for result in resp.missing: key = result.entity.key.path_element[0] missing.add(key.id or key.name) for entity in entities: entity.modified_time = time if entity._get_id() in missing: entity.created_time = time # Bulk upsert req = googledatastore.CommitRequest() req.transaction = transaction req.mutation.upsert.extend( [entity._get_entity() for entity in entities]) googledatastore.commit(req)
def cbquery(self, camid, field): camkey = datastore.Key() path = camkey.path_element.add() path.kind = 'PSCams' path.name = camid req = datastore.LookupRequest() req.key.extend([camkey]) resp = datastore.lookup(req) if resp.found: pscam = resp.found[0].entity password_property = datastore.Property() password_property.name = field for prop in pscam.property: if prop.name == field: val = prop.value.string_value result = [camid, val] else: result = [camid, field] return result
def cbquery(self,camid,field): camkey = datastore.Key() path = camkey.path_element.add() path.kind = 'PSCams' path.name = camid req = datastore.LookupRequest() req.key.extend([camkey]) resp = datastore.lookup(req) if resp.found: pscam = resp.found[0].entity password_property = datastore.Property() password_property.name = field for prop in pscam.property: if prop.name == field: val = prop.value.string_value result = [camid,val] else: result = [camid,field] return result
def main(): # Set dataset id from command line argument. if len(sys.argv) < 2: print 'Usage: adams.py <DATASET_ID>' sys.exit(1) # Set the dataset from the command line parameters. datastore.set_options(dataset=sys.argv[1]) try: # Create a RPC request to write mutations outside of a transaction. req = datastore.BlindWriteRequest() # Add mutation that update or insert one entity. entity = req.mutation.upsert.add() # Set the entity key with only one `path_element`: no parent. path = entity.key.path_element.add() path.kind = 'Trivia' path.name = 'hgtg' # Add two entity properties: # - a utf-8 string: `question` property = entity.property.add() property.name = 'questions' value = property.value.add() value.string_value = 'Meaning of life?' # - a 64bit integer: `answer` property = entity.property.add() property.name = 'answer' value = property.value.add() value.integer_value = 42 # Execute the RPC synchronously and ignore the response. datastore.blind_write(req) # Create a RPC request to get entities by key. req = datastore.LookupRequest() # Add one key to lookup w/ the same entity key. req.key.extend([entity.key]) # Execute the RPC and get the response. resp = datastore.lookup(req) # Found one entity result. entity = resp.found[0].entity # Get question property value. question = entity.property[0].value[0].string_value # Get answer property value. answer = entity.property[1].value[0].integer_value # Print the question and read one line from stdin. print question result = raw_input('> ') if result == str(answer): print ('fascinating, extraordinary and, ' 'when you think hard about it, completely obvious.') else: print "Don't Panic!" except datastore.RPCError as e: # RPCError is raised if any error happened during a RPC. # It includes the `method` called and the `reason` of the # failure as well as the original `HTTPResponse` object. logging.error('Error while doing datastore operation') logging.error('RPCError: %(method)s %(reason)s', {'method': e.method, 'reason': e.reason}) logging.error('HTTPError: %(status)s %(reason)s', {'status': e.response.status, 'reason': e.response.reason}) return
def read_rec(table_name, objid): req = datastore.LookupRequest() req.key.extend([make_key(table_name, objid)]) for found in datastore.lookup(req).found: yield extract_entity(found)
def main(): # Set dataset id from command line argument. if len(sys.argv) < 2: print 'Usage: adams.py <DATASET_ID>' sys.exit(1) # Set the dataset from the command line parameters. datastore.set_options(dataset=sys.argv[1]) try: # Create a RPC request to begin a new transaction. req = datastore.BeginTransactionRequest() # Execute the RPC synchronously. resp = datastore.begin_transaction(req) # Get the transaction handle from the response. tx = resp.transaction # Create a RPC request to get entities by key. req = datastore.LookupRequest() # Create a new entity key. key = datastore.Key() # Set the entity key with only one `path_element`: no parent. path = key.path_element.add() path.kind = 'Trivia' path.name = 'hgtg' # Add one key to the lookup request. req.key.extend([key]) # Set the transaction, so we get a consistent snapshot of the # entity at the time the transaction started. req.read_options.transaction = tx # Execute the RPC and get the response. resp = datastore.lookup(req) # Create a RPC request to commit the transaction. req = datastore.CommitRequest() # Set the transaction to commit. req.transaction = tx if resp.found: # Get the entity from the response if found. entity = resp.found[0].entity else: # If no entity was found, insert a new one in the commit request mutation. entity = req.mutation.insert.add() # Copy the entity key. entity.key.CopyFrom(key) # Add two entity properties: # - a utf-8 string: `question` prop = entity.property.add() prop.name = 'question' prop.value.string_value = 'Meaning of life?' # - a 64bit integer: `answer` prop = entity.property.add() prop.name = 'answer' prop.value.integer_value = 42 # Execute the Commit RPC synchronously and ignore the response: # Apply the insert mutation if the entity was not found and close # the transaction. datastore.commit(req) props = get_property_dict(entity) # Get question property value. question = props['question'].string_value # Get answer property value. answer = props['answer'].integer_value # Print the question and read one line from stdin. print question result = raw_input('> ') if result == str(answer): print ('fascinating, extraordinary and, ' 'when you think hard about it, completely obvious.') else: print "Don't Panic!" except datastore.RPCError as e: # RPCError is raised if any error happened during a RPC. # It includes the `method` called and the `reason` of the # failure as well as the original `HTTPResponse` object. logging.error('Error while doing datastore operation') logging.error('RPCError: %(method)s %(reason)s', {'method': e.method, 'reason': e.reason}) logging.error('HTTPError: %(status)s %(reason)s', {'status': e.response.status, 'reason': e.response.reason}) return
def checkDB(self, gitkit_user): req = datastore.BeginTransactionRequest() resp = datastore.begin_transaction(req) tx = resp.transaction req = datastore.LookupRequest() key = datastore.Key() path = key.path_element.add() path.kind = 'PSUsers' path.name = gitkit_user.user_id req.key.extend([key]) req.read_options.transaction = tx resp = datastore.lookup(req) req = datastore.CommitRequest() req.transaction = tx if resp.missing: user = req.mutation.insert.add() user.key.CopyFrom(key) userid_property = user.property.add() userid_property.name = 'userid' userid_property.value.string_value = gitkit_user.user_id display_name_property = user.property.add() display_name_property.name = 'display_name' display_name_property.value.string_value = gitkit_user.name photo_url_property = user.property.add() photo_url_property.name = 'photo_url' if gitkit_user.photo_url: photo_url_property.value.string_value = gitkit_user.photo_url else: photo_url_property.value.string_value = "/images/home/slider/slide1/cloud1.png" email_property = user.property.add() email_property.name = 'email' email_property.value.string_value = gitkit_user.email last_login_property = user.property.add() last_login_property.name = 'last_login' last_login_property.value.timestamp_microseconds_value = long( time.time() * 1e6) elif resp.found: user = resp.found[0].entity last_login_property = datastore.Property() last_login_property.name = 'last_login' for prop in user.property: if prop.name == 'last_login': prop.value.timestamp_microseconds_value = long( time.time() * 1e6) req.mutation.update.extend([user]) resp = datastore.commit(req) return None
def main(): # Set project id from command line argument. if len(sys.argv) < 2: print 'Usage: adams.py <PROJECT_ID>' sys.exit(1) # Set the project from the command line parameters. datastore.set_options(project_id=sys.argv[1]) try: # Create a RPC request to begin a new transaction. req = datastore.BeginTransactionRequest() # Execute the RPC synchronously. resp = datastore.begin_transaction(req) # Get the transaction handle from the response. tx = resp.transaction # Create a RPC request to get entities by key. req = datastore.LookupRequest() # Create a new entity key. key = datastore.Key() # Set the entity key with only one `path` element: no parent. elem = key.path.add() elem.kind = 'Trivia' elem.name = 'hgtg' # Add one key to the lookup request. req.keys.extend([key]) # Set the transaction, so we get a consistent snapshot of the # entity at the time the transaction started. req.read_options.transaction = tx # Execute the RPC and get the response. resp = datastore.lookup(req) # Create a RPC request to commit the transaction. req = datastore.CommitRequest() # Set the transaction to commit. req.transaction = tx if resp.found: # Get the entity from the response if found. entity = resp.found[0].entity else: # If no entity was found, insert a new one in the commit request mutation. entity = req.mutations.add().insert # Copy the entity key. entity.key.CopyFrom(key) # Add two entity properties: # - a utf-8 string: `question` entity.properties['question'].string_value ='Meaning of life?' # - a 64bit integer: `answer` prop = entity.properties['answer'].integer_value = 42 # Execute the Commit RPC synchronously and ignore the response: # Apply the insert mutation if the entity was not found and close # the transaction. datastore.commit(req) # Get question property value. question = entity.properties['question'].string_value # Get answer property value. answer = entity.properties['answer'].integer_value # Print the question and read one line from stdin. print question result = raw_input('> ') if result == str(answer): print ('fascinating, extraordinary and, ' 'when you think hard about it, completely obvious.') else: print "Don't Panic!" except datastore.RPCError as e: # RPCError is raised if any error happened during a RPC. # It includes the `method` called, the canonical error code `code`, # and the `message` of the failure. logging.error('Error while doing datastore operation') logging.error('RPCError: %(method)s %(reason)s', {'method': e.method, 'code': e.code, 'reason': e.message}) return
def main(): datastore.set_options(dataset='glowing-thunder-842') try: # Create a RPC request to begin a new transaction. req = datastore.BeginTransactionRequest() # Execute the RPC synchronously. resp = datastore.begin_transaction(req) # Get the transaction handle from the response. tx = resp.transaction # Create a RPC request to get entities by key. req = datastore.LookupRequest() # Create a new entity key. key = datastore.Key() # Set the entity key with only one `path_element`: no parent. path = key.path_element.add() path.kind = 'kindlooptest' path.name = randstr() # Add one key to the lookup request. req.key.extend([key]) # Set the transaction, so we get a consistent snapshot of the # entity at the time the transaction started. req.read_options.transaction = tx # Execute the RPC and get the response. resp = datastore.lookup(req) # Create a RPC request to commit the transaction. req = datastore.CommitRequest() # Set the transaction to commit. req.transaction = tx if resp.found: # Get the entity from the response if found. entity = resp.found[0].entity else: # If no entity was found, insert a new one in the commit request mutation. entity = req.mutation.insert.add() # Copy the entity key. entity.key.CopyFrom(key) # Add two entity properties: # - a utf-8 string: `question` prop = entity.property.add() prop.name = 'prop1test' prop.value.string_value = randstr() # - a 64bit integer: `answer` prop = entity.property.add() prop.name = 'prop2test' prop.value.integer_value = 77 # Execute the Commit RPC synchronously and ignore the response: # Apply the insert mutation if the entity was not found and close # the transaction. datastore.commit(req) # Get question property value. question = entity.property[0].value.string_value # Get answer property value. answer = entity.property[1].value.integer_value # Print the question and read one line from stdin. print answer except datastore.RPCError as e: # RPCError is raised if any error happened during a RPC. # It includes the `method` called and the `reason` of the # failure as well as the original `HTTPResponse` object. logging.error('Error while doing datastore operation') logging.error('RPCError: %(method)s %(reason)s', {'method': e.method, 'reason': e.reason}) logging.error('HTTPError: %(status)s %(reason)s', {'status': e.response.status, 'reason': e.response.reason}) return