Hi Nick, Nevermind. At last I can implement desired db_hook function. Thank you for the help.
Regards, Detailed implementation is here(for those who is interested in): http://code.google.com/p/kay-framework/source/detail?r=688c802289158b1cdd1c00eda5c31218083ebcc1 On Tue, Aug 11, 2009 at 11:11 PM, Takashi Matsuo<matsuo.taka...@gmail.com> wrote: > Hi Nick, > > On Tue, Aug 11, 2009 at 7:50 PM, Nick Johnson > (Google)<nick.john...@google.com> wrote: >> >> On Tue, Aug 11, 2009 at 7:29 AM, Takashi Matsuo<matsuo.taka...@gmail.com> >> wrote: >>> >>> Hi Nick, >>> >>> On Thu, Jul 30, 2009 at 4:48 PM, Nick Johnson >>> (Google)<nick.john...@google.com> 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 > > Thanks for your suggestion, but I've already read this article. In > case I've missed something, I read it again carefully, but what I want > to know is not written in the article. > > I should have explain my question more precise and concretely. > OK, here is pseudo codes I'm working on. > --------------------------------------------------------------------------------- > 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') > ---------------------------------------------------------------------------------- > > When I tested with dev_appserver, this execute_hooks function was > invoked even if the transaction that contains this PUT operation, was > rollbacked. > > I want to invoke execute_hooks function only when this PUT operation > is committed. > Is there any way to do that? > > -- Takashi > >> -Nick Johnson >> >>> >>> --- Takashi >>> >>>> -Nick Johnson >>>> >>>> On Thu, Jul 30, 2009 at 7:19 AM, Takashi Matsuo <matsuo.taka...@gmail.com> >>>> 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 google-appengine@googlegroups.com To unsubscribe from this group, send email to google-appengine+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en -~----------~----~----~----~------~----~------~--~---