On Tue, Aug 11, 2009 at 7:29 AM, Takashi Matsuo<[email protected]> wrote:
>
> Hi Nick,
>
> On Thu, Jul 30, 2009 at 4:48 PM, Nick Johnson
> (Google)<[email protected]> wrote:
>> Hi Takashi,
>>
>> If the entity being put has neither key name nor id, a new id will be
>> created for it when it's put, so it will always result in a new entity being
>> inserted rather than an existing entity being replaced. Likewise, if you see
>> an entity with an id being put, it is almost certainly an update to an
>> existing entity (unless someone was messing around with the low-level API),
>> or the entity was deleted and is now being reinserted.
>>
>> If the entity being put has a key name, the only way to tell for certain if
>> it's replacing an existing entity is to do the put operation in a
>> transaction, and do a get on the key first.
>
> Thank you for clarification. Here comes another question.
> Is there any way to tell if particular PUT operation is in a
> transaction or not, and
> tell if the transaction is committed or rollbacked?
>
> I'd like to add a capability for adding arbitrary callback that will
> be executed only when entities are actually put (that won't be
> executed when the put operation is rollback-ed).

You probably want to look into the datastore's support for hooks:
http://code.google.com/appengine/articles/hooks.html

-Nick Johnson

>
> --- Takashi
>
>> -Nick Johnson
>>
>> On Thu, Jul 30, 2009 at 7:19 AM, Takashi Matsuo <[email protected]>
>> wrote:
>>>
>>> Hi,
>>>
>>> I'm writing a capability for registering db_hook dynamically. The code
>>> snippet follows the mail body.
>>> It works well except my code can not distinguish newly created entity
>>> from updated entity.
>>>
>>> I'd like to know how can I know if the entity is created or just
>>> updated. What is the most appropriate way?
>>>
>>> Regards,
>>>
>>>
>>> ------------------------------------------------------------------------------------------
>>> from google.appengine.api import apiproxy_stub_map
>>> from google.appengine.api import datastore
>>> from google.appengine.ext import db
>>>
>>> post_save_hooks = {}
>>>
>>> def register_post_save_hook(func, model):
>>>  global post_save_hooks
>>>  kind = model.kind()
>>>  func_list = post_save_hooks.get(kind, None)
>>>  if func_list is None:
>>>    func_list = []
>>>  func_list.append(func)
>>>  post_save_hooks[kind] = func_list
>>>
>>>
>>> def execute_hooks(kind, key, entity):
>>>  func_list = post_save_hooks.get(kind, None)
>>>  if func_list is not None:
>>>    entity.key_.CopyFrom(key)
>>>    e = datastore.Entity._FromPb(entity)
>>>    instance = db.class_for_kind(kind).from_entity(e)
>>>    for func in func_list:
>>>      func(instance)
>>>
>>> def db_hook(service, call, request, response):
>>>  if call == 'Put':
>>>    from kay.utils.db_hook import execute_hooks
>>>    for key, entity in zip(response.key_list(), request.entity_list()):
>>>      kind = model_name_from_key(key)
>>>      execute_hooks(kind, key, entity)
>>>
>>> apiproxy_stub_map.apiproxy.GetPostCallHooks().Append(
>>>  'db_hook', db_hook, 'datastore_v3')
>>>
>>> ------------------------------------------------------------------------------------------
>>>
>>> --
>>> Takashi Matsuo
>>>
>>>
>>
>>
>>
>> --
>> Nick Johnson, Developer Programs Engineer, App Engine
>>
>> >
>>
>
> >
>



-- 
Nick Johnson, Developer Programs Engineer, App Engine

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to