go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

commit 35b41792f61b43cf97071ba94d1edc3f6ccc51cb
parent d746f4696a2da36a0303160da76a367eaafe6c33
Author: lash <dev@holbrook.no>
Date:   Tue, 31 Dec 2024 08:28:05 +0000

Remove premature close

Diffstat:
Mdb/dump.go | 14++++++++++++++
Mdb/postgres/dump.go | 19++++++++++++++-----
2 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/db/dump.go b/db/dump.go @@ -5,9 +5,11 @@ import ( ) type DumperFunc func(ctx context.Context) ([]byte, []byte) +type CloseFunc func() error type Dumper struct { fn DumperFunc + cfn CloseFunc k []byte v []byte nexted bool @@ -29,6 +31,11 @@ func(d *Dumper) WithFirst(k []byte, v []byte) *Dumper { return d } +func(d *Dumper) WithClose(fn func() error) *Dumper { + d.cfn = fn + return d +} + func(d *Dumper) Next(ctx context.Context) ([]byte, []byte) { d.nexted = true k := d.k @@ -40,3 +47,10 @@ func(d *Dumper) Next(ctx context.Context) ([]byte, []byte) { logg.TraceCtxf(ctx, "next value is", "k", d.k, "v", d.v) return k, v } + +func(d *Dumper) Close() error { + if d.cfn != nil { + return d.cfn() + } + return nil +} diff --git a/db/postgres/dump.go b/db/postgres/dump.go @@ -15,23 +15,23 @@ func(pdb *pgDb) Dump(ctx context.Context, key []byte) (*db.Dumper, error) { k := append([]byte{db.DATATYPE_USERDATA}, key...) - query := fmt.Sprintf("SELECT key, value FROM %s.kv_vise WHERE key >= $1 AND key < $2", pdb.schema) + query := fmt.Sprintf("SELECT key, value FROM %s.kv_vise WHERE key >= $1", pdb.schema) logg.TraceCtxf(ctx, "getkey", "q", query, "key", k) - rs, err := tx.Query(ctx, query, k, []byte{k[0] + 1}) + rs, err := tx.Query(ctx, query, k) if err != nil { logg.Debugf("query fail", "err", err) tx.Rollback(ctx) return nil, err } - defer rs.Close() + //defer rs.Close() if rs.Next() { r := rs.RawValues() - tx.Commit(ctx) //tx.Rollback(ctx) + tx.Commit(ctx) pdb.it = rs pdb.itBase = k - return db.NewDumper(pdb.dumpFunc).WithFirst(r[0], r[1]), nil + return db.NewDumper(pdb.dumpFunc).WithClose(pdb.closeFunc).WithFirst(r[0], r[1]), nil } return nil, db.NewErrNotFound(k) @@ -39,6 +39,7 @@ func(pdb *pgDb) Dump(ctx context.Context, key []byte) (*db.Dumper, error) { func(pdb *pgDb) dumpFunc(ctx context.Context) ([]byte, []byte) { if !pdb.it.Next() { + logg.DebugCtxf(ctx, "no more data in pg iterator") pdb.it = nil pdb.itBase = nil return nil, nil @@ -46,3 +47,11 @@ func(pdb *pgDb) dumpFunc(ctx context.Context) ([]byte, []byte) { r := pdb.it.RawValues() return r[0], r[1] } + +func(pdb *pgDb) closeFunc() error { + if pdb.it != nil { + pdb.it.Close() + pdb.it = nil + } + return nil +}