From b25ef519a5639b3975d97a5725a7daeabf7081cc Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Mon, 14 Jan 2019 10:12:07 +0100 Subject: [PATCH] PS 2 --- dframe/README.md | 2 +- dframe/dfmat/dfmat.go | 10 ++++++---- dframe/dframe.go | 18 +++++++----------- dframe/dframe_example_test.go | 4 ---- 4 files changed, 14 insertions(+), 20 deletions(-) diff --git a/dframe/README.md b/dframe/README.md index 72f697c..0a055dd 100644 --- a/dframe/README.md +++ b/dframe/README.md @@ -70,7 +70,7 @@ Using Arrow should also allow seamless interoperability with other data wranglin Operations on a `QFrame`, such as copying columns, dropping columns, sorting them or applying some kind of operation on columns, return a new `QFrame`, leaving the original untouched. Arrow uses a ref-counting mechanism for all the types that involve memory allocation (mainly to address workloads involving memory allocated on a GPGPU, by a SQL database or a mmap-file.) -This ref-counting mechanism is presented to the user as a pair of methods `Retain/Release` that resp. increment and decrement that reference count. +This ref-counting mechanism is presented to the user as a pair of methods `Retain`/`Release` that increment and decrement that reference count. At first, it would seem this mechanism would prevent to expose an API with "chained methods", as the intermediate `Frame` would be "leaked": ```go diff --git a/dframe/dfmat/dfmat.go b/dframe/dfmat/dfmat.go index 0ddc6f0..b730643 100644 --- a/dframe/dfmat/dfmat.go +++ b/dframe/dfmat/dfmat.go @@ -49,19 +49,20 @@ func newConfig(n int) *config { func FromMatrix(m mat.Matrix, opts ...Option) *dframe.Frame { var ( mem = memory.NewGoAllocator() - bld = array.NewFloat64Builder(mem) r, c = m.Dims() arrs = make([]array.Interface, c) fields = make([]arrow.Field, c) ) - defer bld.Release() cfg := newConfig(c) for _, opt := range opts { opt(cfg) } + bld := array.NewFloat64Builder(mem) + defer bld.Release() + switch m := m.(type) { case mat.RawColViewer: for i := 0; i < c; i++ { @@ -99,19 +100,20 @@ func FromMatrix(m mat.Matrix, opts ...Option) *dframe.Frame { func FromVector(vec mat.Vector, opts ...Option) *dframe.Frame { var ( mem = memory.NewGoAllocator() - bld = array.NewFloat64Builder(mem) rows = vec.Len() arrs = make([]array.Interface, 1) fields = make([]arrow.Field, 1) ) - defer bld.Release() cfg := newConfig(1) for _, opt := range opts { opt(cfg) } + bld := array.NewFloat64Builder(mem) + defer bld.Release() + switch vec := vec.(type) { case mat.RawColViewer: col := vec.RawColView(0) diff --git a/dframe/dframe.go b/dframe/dframe.go index 425c9db..b563b20 100644 --- a/dframe/dframe.go +++ b/dframe/dframe.go @@ -35,6 +35,8 @@ type Frame struct { rows int64 } +var _ array.Table = (*Frame)(nil) + // Dict is a map of string to array of data. type Dict map[string]interface{} @@ -271,14 +273,12 @@ func FromCols(cols []array.Column, opts ...Option) (*Frame, error) { } } - { - fields := make([]arrow.Field, len(cols)) - for i, col := range cols { - fields[i].Name = col.Name() - fields[i].Type = col.DataType() - } - df.schema = arrow.NewSchema(fields, nil) + fields := make([]arrow.Field, len(cols)) + for i, col := range cols { + fields[i].Name = col.Name() + fields[i].Type = col.DataType() } + df.schema = arrow.NewSchema(fields, nil) // validate the data frame and its constituents. // note we retain the columns after having validated the data frame @@ -622,7 +622,3 @@ func (tx *Tx) Drop(cols ...string) *Tx { tx.df.schema = sc return tx } - -var ( - _ array.Table = (*Frame)(nil) -) diff --git a/dframe/dframe_example_test.go b/dframe/dframe_example_test.go index f5ba738..fd339e5 100644 --- a/dframe/dframe_example_test.go +++ b/dframe/dframe_example_test.go @@ -15,7 +15,6 @@ import ( ) func ExampleFrame_fromTable() { - pool := memory.NewGoAllocator() schema := arrow.NewSchema( @@ -90,7 +89,6 @@ func ExampleFrame_fromTable() { } func ExampleFrame_fromArrays() { - pool := memory.NewGoAllocator() schema := arrow.NewSchema( @@ -149,7 +147,6 @@ func ExampleFrame_fromArrays() { } func ExampleFrame_fromCols() { - pool := memory.NewGoAllocator() schema := arrow.NewSchema( @@ -220,7 +217,6 @@ func ExampleFrame_fromCols() { } func ExampleFrame_fromFrame() { - pool := memory.NewGoAllocator() schema := arrow.NewSchema(