Commit c1ba9a90 authored by Kirill Smelkov's avatar Kirill Smelkov

go/zodb/btree: Turn it into template

2dba8607 (go/zodb/btree: New package to work with ZODB BTrees (draft))
added btree module with btree.BTree essentially being LOBTree (int64 key
-> object). Since for wendelin.core we also need IOBTree (int32 key ->
object), which is used in ZBlk1

	https://lab.nexedi.com/nexedi/wendelin.core/blob/v0.12-6-g318efce/bigfile/file_zodb.py#L267
	https://lab.nexedi.com/nexedi/wendelin.core/blob/v0.12-6-g318efce/bigfile/file_zodb.py#L374

let's turn btree module into template internally and generate code for
both LOBTree and IOBTree.

For the reference BTree/py takes similar approach with respect to
templating.
parent fcab58ca
This diff is collapsed.
This diff is collapsed.
......@@ -33,7 +33,7 @@ import (
// kv is one (key, value) pair.
type kv struct {
key KEY
key int64
value interface{}
}
......@@ -52,7 +52,7 @@ type testEntry struct {
// bmapping represents Get of Bucket or BTree.
type bmapping interface {
Get(context.Context, KEY) (interface{}, bool, error)
Get(context.Context, int64) (interface{}, bool, error)
}
func TestBTree(t *testing.T) {
......@@ -83,18 +83,18 @@ func TestBTree(t *testing.T) {
obj, ok := xobj.(bmapping)
if !ok {
t.Fatalf("%s: got %T; want Bucket|BTree", tt.oid, xobj)
t.Fatalf("%s: got %T; want LOBucket|LOBTree", tt.oid, xobj)
}
want := ""
switch tt.kind {
case kindBucket:
if _, ok = obj.(*Bucket); !ok {
want = "Bucket"
if _, ok = obj.(*LOBucket); !ok {
want = "LOBucket"
}
case kindBTree:
if _, ok = obj.(*BTree); !ok {
want = "BTree"
if _, ok = obj.(*LOBTree); !ok {
want = "LOBTree"
}
default:
panic(0)
......@@ -132,12 +132,12 @@ func TestBTree(t *testing.T) {
if err != nil {
t.Fatal(err)
}
B3, ok := xB3.(*BTree)
B3, ok := xB3.(*LOBTree)
if !ok {
t.Fatalf("B3: %v; got %T; want BTree", B3_oid, xB3)
t.Fatalf("B3: %v; got %T; want LOBTree", B3_oid, xB3)
}
for i := KEY(0); i <= B3_maxkey; i++ {
for i := int64(0); i <= B3_maxkey; i++ {
v, ok, err := B3.Get(ctx, i)
if err != nil {
t.Fatal(err)
......@@ -151,21 +151,21 @@ func TestBTree(t *testing.T) {
}
// verifyFirstBucket verifies that b.firstbucket is correct and returns it.
var verifyFirstBucket func(b *BTree) *Bucket
verifyFirstBucket = func(b *BTree) *Bucket {
var verifyFirstBucket func(b *LOBTree) *LOBucket
verifyFirstBucket = func(b *LOBTree) *LOBucket {
err := b.PActivate(ctx); X(err)
defer b.PDeactivate()
var firstbucket *Bucket
var firstbucket *LOBucket
switch child := b.data[0].child.(type) {
default:
t.Fatalf("btree(%s): child[0] is %T", b.POid(), b.data[0].child)
case *BTree:
case *LOBTree:
firstbucket = verifyFirstBucket(child)
case *Bucket:
case *LOBucket:
firstbucket = child
}
......
#!/bin/bash -e
# btree.go.in -> specialized with concrete types
# gen-btree KIND KEY out
# Copyright (C) 2018 Nexedi SA and Contributors.
# Kirill Smelkov <kirr@nexedi.com>
#
# This program is free software: you can Use, Study, Modify and Redistribute
# it under the terms of the GNU General Public License version 3, or (at your
# option) any later version, as published by the Free Software Foundation.
#
# You can also Link and Combine this program with other software covered by
# the terms of any of the Free Software licenses or any of the Open Source
# Initiative approved licenses and Convey the resulting work. Corresponding
# source of such a combination shall include the source code for all other
# software used.
#
# This program is distributed WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See COPYING file for full licensing terms.
# See https://www.nexedi.com/licensing for rationale and options.
KIND=$1
KEY=$2
out=$3
kind=${KIND,,} # IO -> io
Key=${KEY^}
input=$(dirname $0)/btree.go.in
echo "// Code generated by gen-btree; DO NOT EDIT." >$out
echo >>$out
sed \
-e "s/<kind>/$kind/g" \
-e "s/<KIND>/$KIND/g" \
-e "s/KEY/$KEY/g" \
-e "s/<Key>/$Key/g" \
-e "s/\bBTree\b/${KIND}BTree/g" \
-e "s/\b_BTreeItem\b/_${KIND}BTreeItem/g" \
-e "s/\bBucket\b/${KIND}Bucket/g" \
-e "s/\bbtreeState\b/${kind}btreeState/g" \
-e "s/\bbucketState\b/${kind}bucketState/g" \
$input >>$out
This diff is collapsed.
This diff is collapsed.
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