Re: [ZODB-Dev] semantics of _v_ attributes?

2010-10-06 Thread Chris Withers
On 06/10/2010 15:19, Jim Fulton wrote:
> _v_ attributes should be dropped whenever an object's state is
> cleared.

It would be great if there was some clear documentation covering all 
this in the ZODB package...

> So, you want to update an object using old state.
>
> I would use:
>
>base._p_invalidate() # remove state, including volatiles, no matter what
>base._p_activate()   # make sure we're not a ghost
>base.__setstate__(state) # change the state
>base._p_changed = True   # marke object as dirty
>
> This isn't foolproof, depending on how an object uses _v_.  For
> example if __setstate__ sets _v_s if they aren't already set, then the
> above code would likely fail, as the _v_s would reflect the old state,
> not the new.

Okay, it works for the Zope 2.12 problem at hand:
https://bugs.launchpad.net/zope2/+bug/649605

> If this use case is important, it would be better for ZODB tp provide
> an API to handle this directly, so you didn't have to hack together
> existing APIs.

Well, I have customers who think it's important, but it depends on 
whether you still think Zope 2 with TTW editing is important ;-)

> I assume this code will ultimately be followed by a commit.

Correct.

> Well, for your use case, you're going to be committing a change to the
> object.  That will cause instances of the object in all other
> connections to be invalidated, dropping their volatile attributes.

Cool :-) I'll take this on trust, I don't want to contemplate trying to 
write a test that proves it ;-)

Chris

-- 
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
___
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
https://mail.zope.org/mailman/listinfo/zodb-dev


Re: [ZODB-Dev] semantics of _v_ attributes?

2010-10-06 Thread Jim Fulton
On Wed, Oct 6, 2010 at 4:52 AM, Chris Withers  wrote:
> Hi All,
>
> I'm trying to fix this bug:
> https://bugs.launchpad.net/zope2/+bug/649605
>
> ...and I'm struggling to find documentation on the semantics of _v_
> attributes.

_v_ attributes should be dropped whenever an object's state is
cleared. This normally happens in response to _p_invalidate or
_p_deactivate, although objects sometimes override _p_deactivate as a
noop to try to avoid ghostification.

> In particular, surely this code:
>
>             base._p_activate()       # make sure we're not a ghost
>             base.__setstate__(state) # change the state
>             base._p_changed = True   # marke object as dirty
>
> ...should be enough to indicate that all _v_ attributes should be dropped?

No.

So, you want to update an object using old state.

I would use:

  base._p_invalidate() # remove state, including volatiles, no matter what
  base._p_activate()   # make sure we're not a ghost
  base.__setstate__(state) # change the state
  base._p_changed = True   # marke object as dirty

This isn't foolproof, depending on how an object uses _v_.  For
example if __setstate__ sets _v_s if they aren't already set, then the
above code would likely fail, as the _v_s would reflect the old state,
not the new.

If this use case is important, it would be better for ZODB tp provide
an API to handle this directly, so you didn't have to hack together
existing APIs.

I assume this code will ultimately be followed by a commit.

> If it isn't, what is the correct way to indicate to Persistent/ZODB that
> all _v_ attribute should be dropped not only for the current object

This would be more or less guarenteed by invalidating the object.

> but
> for all occurrences of that object across all connections to the current
> ZODB?

Well, for your use case, you're going to be committing a change to the
object.  That will cause instances of the object in all other
connections to be invalidated, dropping their volatile attributes.

Jim

--
Jim Fulton
___
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
https://mail.zope.org/mailman/listinfo/zodb-dev


Re: [ZODB-Dev] semantics of _v_ attributes?

2010-10-06 Thread Shane Hathaway
On 10/06/2010 02:52 AM, Chris Withers wrote:
> Hi All,
>
> I'm trying to fix this bug:
> https://bugs.launchpad.net/zope2/+bug/649605
>
> ...and I'm struggling to find documentation on the semantics of _v_
> attributes.
>
> In particular, surely this code:
>
>   base._p_activate()   # make sure we're not a ghost
>   base.__setstate__(state) # change the state
>   base._p_changed = True   # marke object as dirty
>
> ...should be enough to indicate that all _v_ attributes should be dropped?

No.  That code does not update the object in the current ZODB 
connection.  That code only updates other connections.

> If it isn't, what is the correct way to indicate to Persistent/ZODB that
> all _v_ attribute should be dropped not only for the current object but
> for all occurrences of that object across all connections to the current
> ZODB?

Separate this into two problems.  You have to solve both:

1) You need to update the object in the current connection.

2) You need to update the object in all other connections.

#2 is easy.  All you have to do is set _p_changed (which is set 
automatically whenever you change an attribute) and commit the 
transaction; ZODB will reload the object in the other connections. 
Reloading removes _v_ attributes.

#1 is more interesting, because ZODB does not normally cause the 
connection that changed an object to reload that object.  Therefore, 
your transaction needs to do something that clears your _v_attributes 
manually.  For example, "self._v_cache = None".  ZODB will not do this 
for you.

To summarize, your code should do this:

 self.somedata = something  # Update all other connections
 self._v_cache = None   # Update this connection

That's all you have to do.

If you really, really need to clear all _v_ attributes, then do this:

 self._p_changed = True
 for key in self.__dict__:
 if key.startswith('_v_'):
 del self.__dict__[key]

Shane
___
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
https://mail.zope.org/mailman/listinfo/zodb-dev


[ZODB-Dev] semantics of _v_ attributes?

2010-10-06 Thread Chris Withers
Hi All,

I'm trying to fix this bug:
https://bugs.launchpad.net/zope2/+bug/649605

...and I'm struggling to find documentation on the semantics of _v_ 
attributes.

In particular, surely this code:

 base._p_activate()   # make sure we're not a ghost
 base.__setstate__(state) # change the state
 base._p_changed = True   # marke object as dirty

...should be enough to indicate that all _v_ attributes should be dropped?

If it isn't, what is the correct way to indicate to Persistent/ZODB that 
all _v_ attribute should be dropped not only for the current object but 
for all occurrences of that object across all connections to the current 
ZODB?

cheers,

Chris

-- 
Simplistix - Content Management, Batch Processing & Python Consulting
 - http://www.simplistix.co.uk
___
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
https://mail.zope.org/mailman/listinfo/zodb-dev