Commit 354e0e51 authored by Kirill Smelkov's avatar Kirill Smelkov

go/zodb: Persistent - the base type to implement IPersistent objects

Add the base type, that types which want to implement persistency
could embed, and this way inherit persistent functionality. For example

	type MyObject struct {
		Persistent
		...
	}

	type myObjectState MyObject

	func (o *myObjectState) DropState() { ... }
	func (o *myObjectState) SetState(state *mem.Buf) error { ... }

Here state management methods (DropState and SetState) will be
automatically used by persistency machinery on activation and
deactivation.

For this to work MyObject class has to be registered to ZODB

	func init() {
		t := reflect.TypeOf
		zodb.RegisterClass("mymodule.MyObject", t(MyObject{}), t(myObjectState))
	}

and new instances of MyObject has to be created via zodb.NewPersistent:

	obj := zodb.NewPersistent(reflect.TypeOf(MyObject{}), jar).(*MyObject)

SetState corresponds to __setstate__ in Python. However in Go version it
is explicitly separated from class's public API - as it is the contract
between a class and persistency machinery, not between the class and its
user. Notice that SetState takes raw buffer as its argument. In the
following patch we'll add SetState cousing (PySetState) that will be
taking unpickled objects as the state - exactly how __setstate__
operates in Python. Classes will be able to choose whether to accept
state as raw bytes or as a python object.

The activation/deactivation is implemented via reference counting.

Tests are pending (for PySetState).
parent f6a27a1e
This diff is collapsed.
...@@ -68,7 +68,7 @@ ...@@ -68,7 +68,7 @@
// access (contrary to zodb/py where activation is implicit, hooked into // access (contrary to zodb/py where activation is implicit, hooked into
// __getattr__), for example: // __getattr__), for example:
// //
// var obj *MyObject // *MyObject must implement IPersistent // var obj *MyObject // *MyObject must implement IPersistent (see below)
// ... // init obj pointer, usually by traversing from another persistent object. // ... // init obj pointer, usually by traversing from another persistent object.
// //
// // make sure object's in-RAM data is present. // // make sure object's in-RAM data is present.
...@@ -92,6 +92,9 @@ ...@@ -92,6 +92,9 @@
// //
// IPersistent interface describes the details of the activation protocol. // IPersistent interface describes the details of the activation protocol.
// //
// For MyObject to implement IPersistent it must embed Persistent type.
// MyObject also has to register itself to persistency machinery with RegisterClass.
//
// Object activation protocol is safe to access from // Object activation protocol is safe to access from
// multiple goroutines simultaneously. // multiple goroutines simultaneously.
// //
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment