Commit 02828321 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent d4ff5502
......@@ -267,7 +267,7 @@ func (bt *btreeState) PySetState(pystate interface{}) error {
// btree with 1 child bucket without oid
if len(t) == 1 {
bucket := &Bucket{/* FIXME */}
bucket := zodb.NewPersistent(reflect.TypeOf(Bucket{}), bt.PJar()).(*Bucket)
err := (*bucketState)(bucket).PySetState(t[0])
if err != nil {
// XXX
......
......@@ -107,12 +107,12 @@ func TestBucket(t *testing.T) {
}
if !ok {
t.Errorf("get %v -> ø; want %v", kv.key, kv.value)
t.Errorf("%s: get %v -> ø; want %v", tt.oid, kv.key, kv.value)
continue
}
if value != kv.value {
t.Errorf("get %v -> %v; want %v", kv.key, value, kv.value)
t.Errorf("%s: get %v -> %v; want %v", tt.oid, kv.key, value, kv.value)
}
// XXX .next == nil
......
......@@ -403,6 +403,21 @@ func RegisterClass(class string, typ, stateType reflect.Type) {
typeTab[typ] = zc
}
// NewPersistent creates new instance of persistent type.
//
// typ must embed Persistent and must be registered with RegisterClass.
//
// Created instance will be associated with jar, but will have no oid assigned
// until transaction commit.
func NewPersistent(typ reflect.Type, jar *Connection) IPersistent {
zc := typeTab[typ]
if zc == nil {
panic(fmt.Sprintf("new persistent: type %s not registered", typ))
}
xpobj := reflect.New(zc.typ)
return persistentInit(xpobj, zc, jar, InvalidOid, InvalidTid, UPTODATE/*XXX ok?*/)
}
// newGhost creates new ghost object corresponding to class, oid and jar.
//
......@@ -422,13 +437,25 @@ func newGhost(class string, oid Oid, jar *Connection) IPersistent {
xpobj = reflect.New(zc.typ)
}
return persistentInit(xpobj, zc, jar, oid, InvalidTid, GHOST)
}
// persistentInit inits Persistent embedded into an object
// and returns .instance .
func persistentInit(xpobj reflect.Value, zc *zclass, jar *Connection, oid Oid, serial Tid, state ObjectState) IPersistent {
xobj := xpobj.Elem() // typ
pbase := xobj.FieldByName("Persistent").Addr().Interface().(*Persistent)
pbase.zclass = zc
pbase.jar = jar
pbase.oid = oid
pbase.serial = 0
pbase.state = GHOST
pbase.serial = serial
pbase.state = state
if state > GHOST {
// if state is not ghost, init loading state so that activate works.
pbase.loading = &loadState{ready: make(chan struct{})}
close(pbase.loading.ready)
}
obj := xpobj.Interface()
pbase.instance = obj.(IPersistent)
......
......@@ -164,14 +164,17 @@ import (
// by all cumulated transaction changes from database beginning up to, and
// including, transaction specified by tid.
//
// 0 is invalid Tid.
// 0 is invalid Tid, but canonical invalid Tid value is InvalidTid. XXX ok? (-∞ ?)
type Tid uint64
// ZODB/py defines maxtid to be max signed int64 since Jun 7 2016:
// https://github.com/zopefoundation/ZODB/commit/baee84a6
// (same in neo/py with "SQLite does not accept numbers above 2^63-1" comment)
const TidMax Tid = 1<<63 - 1 // 0x7fffffffffffffff
const (
TidMax Tid = 1<<63 - 1 // 0x7fffffffffffffff
InvalidTid Tid = 1<<64 - 1 // 0xffffffffffffffff
)
// Oid is object identifier.
//
......@@ -181,10 +184,13 @@ const TidMax Tid = 1<<63 - 1 // 0x7fffffffffffffff
// uniquely addresses corresponding data record.
//
// 0 is valid Oid and represents root database object.
// InvalidOid represents an invalid Oid.
//
// See also: Xid.
type Oid uint64
const InvalidOid Oid = 1<<64 - 1 // 0xffffffffffffffff
// Xid is "extended" oid - that fully specifies object and query for its revision.
//
// At specifies whole database state at which object identified with Oid should
......
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