Skip to content

Commit

Permalink
merge: branch '3630-schema-updates' into 'main'
Browse files Browse the repository at this point in the history
Schema tweaks [#3630]

Closes #3630

See merge request accumulatenetwork/accumulate!1094
  • Loading branch information
firelizzard18 committed Oct 14, 2024
2 parents 795ae87 + 0fda35e commit 184ea0f
Show file tree
Hide file tree
Showing 16 changed files with 467 additions and 226 deletions.
5 changes: 3 additions & 2 deletions cmd/accumulated/run/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (
"github.com/multiformats/go-multiaddr"
"gitlab.com/accumulatenetwork/core/schema/pkg/binary"
"gitlab.com/accumulatenetwork/core/schema/pkg/json"
"gitlab.com/accumulatenetwork/core/schema/pkg/widget"
)

var wMultiaddr = multiaddrWidget{}
var wPeerID = peerIdWidget{}
func wMultiaddr() widget.Widget[*multiaddr.Multiaddr] { return multiaddrWidget{} }
func wPeerID() widget.Widget[*peer.ID] { return peerIdWidget{} }

type multiaddrWidget struct{}

Expand Down
4 changes: 2 additions & 2 deletions cmd/accumulated/run/schema_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions exp/ioutil/mapped_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ import (
"sync/atomic"

"github.com/edsrzf/mmap-go"
"gitlab.com/accumulatenetwork/core/schema/pkg/binary"
"gitlab.com/accumulatenetwork/accumulate/internal/util/pool"
)

type MappedFile struct {
mu *sync.RWMutex
file *os.File
data mmap.MMap
pool *binary.Pool[*MappedFileRange]
pool *pool.Pool[*MappedFileRange]
}

func OpenMappedFile(name string, flags int, perm fs.FileMode) (_ *MappedFile, err error) {
f := new(MappedFile)
f.mu = new(sync.RWMutex)
f.pool = binary.NewPointerPool[MappedFileRange]()
f.pool = pool.New[MappedFileRange]()
defer func() {
if err != nil {
_ = f.Close()
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ require (
github.com/sergi/go-diff v1.2.0
github.com/ulikunitz/xz v0.5.11
github.com/vektra/mockery/v2 v2.42.3
gitlab.com/accumulatenetwork/core/schema v0.2.1-0.20240713035306-1121dbc75e5d
gitlab.com/accumulatenetwork/core/schema v0.2.1-0.20241002211257-5a76a2dad5eb
gitlab.com/firelizzard/go-script v0.0.0-20240404234115-d5f0a716003d
go.opentelemetry.io/otel v1.27.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.27.0
Expand Down Expand Up @@ -377,7 +377,7 @@ require (
github.com/uudashr/gocognit v1.1.2 // indirect
github.com/yagipy/maintidx v1.0.0 // indirect
github.com/yeya24/promlinter v0.2.0 // indirect
gitlab.com/accumulatenetwork/utils/jsonrpc v0.1.0
gitlab.com/accumulatenetwork/utils/jsonrpc v0.1.1-0.20240904211734-7057029b6ad1
gitlab.com/bosi/decorder v0.4.1 // indirect
go.etcd.io/bbolt v1.3.6
go.uber.org/multierr v1.11.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1170,10 +1170,10 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
gitlab.com/accumulatenetwork/core/schema v0.2.1-0.20240713035306-1121dbc75e5d h1:6crG3cl/LB7if3Lt3MbE3c8qgTzl/aLvdB+RmGpnDXE=
gitlab.com/accumulatenetwork/core/schema v0.2.1-0.20240713035306-1121dbc75e5d/go.mod h1:FTl7W44SWhDenzAtvKkLu30Cin8DAr249mH4eg7BNLY=
gitlab.com/accumulatenetwork/utils/jsonrpc v0.1.0 h1:t4Akt7LvcROop2bh++KRIhbv+NORJT1m5WWIsiEUIF8=
gitlab.com/accumulatenetwork/utils/jsonrpc v0.1.0/go.mod h1:Kopfqa//YI6CkXkVpHRMOZqODMNFwifB8IvKw+m52no=
gitlab.com/accumulatenetwork/core/schema v0.2.1-0.20241002211257-5a76a2dad5eb h1:AeG7HjYtyMj/3Xq736CwELkpplss4ieFW8fsSEf1dh8=
gitlab.com/accumulatenetwork/core/schema v0.2.1-0.20241002211257-5a76a2dad5eb/go.mod h1:ZEUsUF0Gr3daHqbDee9MnLB0IX3hdg7AYUogv+Xm2js=
gitlab.com/accumulatenetwork/utils/jsonrpc v0.1.1-0.20240904211734-7057029b6ad1 h1:w7bF8KV1SmNXwdA+MVxyL/u997CgvHggApyH4pa9mpQ=
gitlab.com/accumulatenetwork/utils/jsonrpc v0.1.1-0.20240904211734-7057029b6ad1/go.mod h1:/lKDPIe8tS8Tui1Qw7C0HajOwtljBfBXNkvmmfbYgTA=
gitlab.com/bosi/decorder v0.4.1 h1:VdsdfxhstabyhZovHafFw+9eJ6eU0d2CkFNJcZz/NU4=
gitlab.com/bosi/decorder v0.4.1/go.mod h1:jecSqWUew6Yle1pCr2eLWTensJMmsxHsBwt+PVbkAqA=
gitlab.com/ethan.reesor/vscode-notebooks/go-playbooks v0.0.0-20220417214602-1121b9fae118 h1:UnyYFTz6dWVMBzLUyqHPIQwMrdpiuE+CE7p/5kUfvmk=
Expand Down
23 changes: 23 additions & 0 deletions internal/util/pool/pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2024 The Accumulate Authors
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

package pool

import "sync"

func New[T any]() *Pool[*T] {
return (*Pool[*T])(&sync.Pool{New: func() any { return new(T) }})
}

type Pool[T any] sync.Pool

func (p *Pool[T]) Get() T {
return (*sync.Pool)(p).Get().(T)
}

func (p *Pool[T]) Put(v T) {
(*sync.Pool)(p).Put(v)
}
15 changes: 15 additions & 0 deletions pkg/database/bpt/types_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions pkg/database/keyvalue/block/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ import (
"sync"
"sync/atomic"

"gitlab.com/accumulatenetwork/accumulate/internal/util/pool"
"gitlab.com/accumulatenetwork/accumulate/pkg/database/keyvalue"
"gitlab.com/accumulatenetwork/accumulate/pkg/database/keyvalue/memory"
"gitlab.com/accumulatenetwork/accumulate/pkg/types/record"
"gitlab.com/accumulatenetwork/core/schema/pkg/binary"
)

var poolEncoder = binary.NewPointerPool[binary.Encoder]()
var poolDecoder = binary.NewPointerPool[binary.Decoder]()
var poolLocation = binary.NewPointerPool[recordLocation]()
var poolBuffer = binary.NewPointerPool[bytes.Buffer]()
var poolEncoder = pool.New[binary.Encoder]()
var poolDecoder = pool.New[binary.Decoder]()
var poolLocation = pool.New[recordLocation]()
var poolBuffer = binary.NewBufferPool()

type Database struct {
config
Expand Down
18 changes: 18 additions & 0 deletions pkg/database/keyvalue/block/types_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkg/types/encoding/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import (
"time"

"gitlab.com/accumulatenetwork/accumulate/pkg/url"
bin2 "gitlab.com/accumulatenetwork/core/schema/pkg/binary"
)

var ErrFieldsOutOfOrder = errors.New("fields are out of order")
var ErrFieldsOutOfOrder = bin2.ErrFieldsOutOfOrder

const MaxValueSize = 1 << 24

Expand Down
49 changes: 14 additions & 35 deletions pkg/types/encoding/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,30 @@ import (

"gitlab.com/accumulatenetwork/core/schema/pkg/binary"
"gitlab.com/accumulatenetwork/core/schema/pkg/json"
"gitlab.com/accumulatenetwork/core/schema/pkg/widget"
)

type Time time.Time
func TimeWidget() widget.Widget[*time.Time] { return timeWidget{} }

func (u Time) Get() time.Time { return time.Time(u) }
func (u *Time) Set(v time.Time) { *u = Time(v) }
type timeWidget struct{}

func (u *Time) String() string { return u.Get().String() }
func (u *Time) Copy() *Time { return u }
func (u *Time) Equal(v *Time) bool { return strings.EqualFold(u.String(), v.String()) }
func (timeWidget) IsNil(v *time.Time) bool { return false }
func (timeWidget) Empty(v *time.Time) bool { return *v == time.Time{} }
func (timeWidget) CopyTo(dst, src *time.Time) { *dst = *src }
func (timeWidget) Equal(a, b *time.Time) bool { return (*a).Equal(*b) }
func (timeWidget) MarshalJSON(e *json.Encoder, v *time.Time) error { return e.Encode(v) }

Check failure on line 27 in pkg/types/encoding/time.go

View workflow job for this annotation

GitHub Actions / Lint

method MarshalJSON(e *json.Encoder, v *time.Time) error should have signature MarshalJSON() ([]byte, error)
func (timeWidget) UnmarshalJSON(d *json.Decoder, v *time.Time) error { return d.Decode(v) }

Check failure on line 28 in pkg/types/encoding/time.go

View workflow job for this annotation

GitHub Actions / Lint

method UnmarshalJSON(d *json.Decoder, v *time.Time) error should have signature UnmarshalJSON([]byte) error

func (u *Time) MarshalJSON() ([]byte, error) {
return json.Marshal(u)
}

func (u *Time) UnmarshalJSON(b []byte) error {
return json.Unmarshal(b, u)
}

func (u *Time) MarshalJSONV2(e *json.Encoder) error {
return e.Encode(u.Get())
}

func (u *Time) UnmarshalJSONV2(d *json.Decoder) error {
return d.Decode((*time.Time)(u))
}

func (u *Time) MarshalBinary() ([]byte, error) {
return binary.Marshal(u)
}

func (u *Time) UnmarshalBinary(b []byte) error {
return binary.Unmarshal(b, u)
}

func (u *Time) MarshalBinaryV2(e *binary.Encoder) error {
return e.EncodeInt(u.Get().UTC().Unix())
func (timeWidget) MarshalBinary(e *binary.Encoder, v *time.Time) error {
return e.EncodeInt(v.UTC().Unix())
}

func (u *Time) UnmarshalBinaryV2(d *binary.Decoder) error {
v, err := d.DecodeInt()
func (timeWidget) UnmarshalBinary(d *binary.Decoder, v *time.Time) error {
u, err := d.DecodeInt()
if err != nil {
return err
}
u.Set(time.Unix(v, 0).UTC())
*v = time.Unix(u, 0).UTC()
return nil
}

Expand Down
17 changes: 15 additions & 2 deletions tools/cmd/gen-model/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func (c *Counter) Next() int {

var goFuncs = template.FuncMap{
"fullName": fullName,
"structName": structName,
"hasChains": hasChains,
"fieldType": fieldType,
"recordType": recordType,
Expand Down Expand Up @@ -75,15 +76,27 @@ func wrapped(r typegen.Record) bool {
return typ.Code == typegen.TypeCodeUnknown && typ.Name == "raw"
}

func fullName(r typegen.Record) string {
func structName(r typegen.Record) string {
e, ok := r.(*typegen.EntityRecord)
if !ok || !e.Interface {
return r.FullName()
}
name := e.FullName()
name := r.FullName()
return strings.ToLower(name[:1]) + name[1:]
}

func fullName(r typegen.Record) string {
name := structName(r)
if len(r.GetTypeParameters()) == 0 {
return name
}
var params []string
for _, p := range r.GetTypeParameters() {
params = append(params, p.Name)
}
return name + "[" + strings.Join(params, ", ") + "]"
}

func hasChains(r typegen.Record) bool {
switch r := r.(type) {
case *typegen.ChainRecord:
Expand Down
Loading

0 comments on commit 184ea0f

Please sign in to comment.