From ca669d5b1ab5dba4e9bbf859075cedde2b8b4647 Mon Sep 17 00:00:00 2001 From: Yevgeny Pats <16490766+yevgenypats@users.noreply.github.com> Date: Sat, 17 Dec 2022 12:47:33 +0200 Subject: [PATCH 1/7] feat: Add size in bytes to CQ types --- schema/bool.go | 4 ++++ schema/bytea.go | 4 ++++ schema/cidr.go | 4 ++++ schema/cidr_array.go | 4 ++++ schema/float8.go | 4 ++++ schema/inet.go | 4 ++++ schema/inet_array.go | 4 ++++ schema/int8.go | 4 ++++ schema/int8_array.go | 4 ++++ schema/json.go | 4 ++++ schema/macaddr.go | 4 ++++ schema/macaddr_array.go | 8 ++++++++ schema/text.go | 4 ++++ schema/text_array.go | 8 ++++++++ schema/timestamptz.go | 4 ++++ schema/types.go | 12 ++++++++++++ schema/uuid.go | 4 ++++ schema/uuid_array.go | 4 ++++ 18 files changed, 88 insertions(+) diff --git a/schema/bool.go b/schema/bool.go index 69d627bbff..9a9dabd52c 100644 --- a/schema/bool.go +++ b/schema/bool.go @@ -19,6 +19,10 @@ func (*Bool) Type() ValueType { return TypeBool } +func (*Bool) Size() int { + return 2 +} + func (dst *Bool) Equal(src CQType) bool { if src == nil { return false diff --git a/schema/bytea.go b/schema/bytea.go index f2c2697a16..4508f253fc 100644 --- a/schema/bytea.go +++ b/schema/bytea.go @@ -20,6 +20,10 @@ func (*Bytea) Type() ValueType { return TypeByteArray } +func (dst *Bytea) Size() int { + return len(dst.Bytes) +} + func (dst *Bytea) Equal(src CQType) bool { if src == nil { return false diff --git a/schema/cidr.go b/schema/cidr.go index c0ce960fd6..4a65f1e3a5 100644 --- a/schema/cidr.go +++ b/schema/cidr.go @@ -12,6 +12,10 @@ func (*CIDR) Type() ValueType { return TypeCIDR } +func (dst *CIDR) Size() int { + return 32 +} + func (dst *CIDR) String() string { if dst.Status == Present { return dst.IPNet.String() diff --git a/schema/cidr_array.go b/schema/cidr_array.go index 41a7735fd6..98c5e9ec95 100644 --- a/schema/cidr_array.go +++ b/schema/cidr_array.go @@ -22,6 +22,10 @@ func (*CIDRArray) Type() ValueType { return TypeCIDRArray } +func (dst *CIDRArray) Size() int { + return len(dst.Elements) * 32 +} + func (dst *CIDRArray) Equal(src CQType) bool { if src == nil { return false diff --git a/schema/float8.go b/schema/float8.go index 87a9189ea9..00b0ae7020 100644 --- a/schema/float8.go +++ b/schema/float8.go @@ -22,6 +22,10 @@ func (*Float8) Type() ValueType { return TypeFloat } +func (dst *Float8) Size() int { + return 8 +} + func (dst *Float8) String() string { if dst.Status == Present { return strconv.FormatFloat(dst.Float, 'f', -1, 64) diff --git a/schema/inet.go b/schema/inet.go index 88b0e3aa9d..16b10dbb88 100644 --- a/schema/inet.go +++ b/schema/inet.go @@ -29,6 +29,10 @@ func (*Inet) Type() ValueType { return TypeInet } +func (dst *Inet) Size() int { + return 32 +} + func (dst *Inet) Equal(src CQType) bool { if src == nil { return false diff --git a/schema/inet_array.go b/schema/inet_array.go index 70e9bc020c..98c0ddfbe3 100644 --- a/schema/inet_array.go +++ b/schema/inet_array.go @@ -22,6 +22,10 @@ func (*InetArray) Type() ValueType { return TypeInetArray } +func (dst *InetArray) Size() int { + return len(dst.Elements) * 32 +} + func (dst *InetArray) Equal(src CQType) bool { if src == nil { return false diff --git a/schema/int8.go b/schema/int8.go index 120cdf9079..43e9ecda1f 100644 --- a/schema/int8.go +++ b/schema/int8.go @@ -20,6 +20,10 @@ func (*Int8) Type() ValueType { return TypeInt } +func (*Int8) Size() int { + return 8 +} + func (dst *Int8) Equal(src CQType) bool { if src == nil { return false diff --git a/schema/int8_array.go b/schema/int8_array.go index 5b57e15cbf..252e851e04 100644 --- a/schema/int8_array.go +++ b/schema/int8_array.go @@ -21,6 +21,10 @@ func (*Int8Array) Type() ValueType { return TypeIntArray } +func (dst *Int8Array) Size() int { + return 8 * len(dst.Elements) +} + func (dst *Int8Array) Equal(src CQType) bool { if src == nil { return false diff --git a/schema/json.go b/schema/json.go index 76c847294e..6c6433d2a7 100644 --- a/schema/json.go +++ b/schema/json.go @@ -20,6 +20,10 @@ func (*JSON) Type() ValueType { return TypeJSON } +func (dst *JSON) Size() int { + return len(dst.Bytes) +} + func (dst *JSON) Equal(src CQType) bool { if src == nil { return false diff --git a/schema/macaddr.go b/schema/macaddr.go index fb5d0f746f..b99d676050 100644 --- a/schema/macaddr.go +++ b/schema/macaddr.go @@ -19,6 +19,10 @@ func (*Macaddr) Type() ValueType { return TypeMacAddr } +func (dst *Macaddr) Size() int { + return len(dst.Addr) +} + func (dst *Macaddr) Equal(src CQType) bool { if src == nil { return false diff --git a/schema/macaddr_array.go b/schema/macaddr_array.go index 2227b62e54..d8eb366f0c 100644 --- a/schema/macaddr_array.go +++ b/schema/macaddr_array.go @@ -22,6 +22,14 @@ func (*MacaddrArray) Type() ValueType { return TypeMacAddrArray } +func (dst *MacaddrArray) Size() int { + totalSize := 0 + for _, element := range dst.Elements { + totalSize += element.Size() + } + return totalSize +} + func (dst *MacaddrArray) Equal(src CQType) bool { if src == nil { return false diff --git a/schema/text.go b/schema/text.go index a9b82667b3..f63534e69f 100644 --- a/schema/text.go +++ b/schema/text.go @@ -19,6 +19,10 @@ func (*Text) Type() ValueType { return TypeString } +func (dst *Text) Size() int { + return len(dst.Str) +} + func (dst *Text) Equal(src CQType) bool { if src == nil { return false diff --git a/schema/text_array.go b/schema/text_array.go index 97daebe260..b201e6fedb 100644 --- a/schema/text_array.go +++ b/schema/text_array.go @@ -21,6 +21,14 @@ func (*TextArray) Type() ValueType { return TypeStringArray } +func (dst *TextArray) Size() int { + totalSize := 0 + for _, element := range dst.Elements { + totalSize += element.Size() + } + return totalSize +} + func (dst *TextArray) Equal(src CQType) bool { if src == nil { return false diff --git a/schema/timestamptz.go b/schema/timestamptz.go index 7bdd503af5..e8d50ee210 100644 --- a/schema/timestamptz.go +++ b/schema/timestamptz.go @@ -35,6 +35,10 @@ func (*Timestamptz) Type() ValueType { return TypeTimestamp } +func (dst *Timestamptz) Size() int { + return 32 +} + func (dst *Timestamptz) Equal(src CQType) bool { if src == nil { return false diff --git a/schema/types.go b/schema/types.go index 0f230748b7..e76d9377c6 100644 --- a/schema/types.go +++ b/schema/types.go @@ -212,10 +212,22 @@ type CQType interface { String() string Equal(CQType) bool Type() ValueType + Size() int } type CQTypes []CQType +// returns total number of bytes occupied by all values +// this useful to understand how much data is being transffered rather then just number +// of resources +func (c CQTypes) Size() int { + var size int + for _, v := range c { + size += v.Size() + } + return size +} + func (c CQTypes) MarshalJSON() ([]byte, error) { res := make([]map[string]interface{}, len(c)) for i, v := range c { diff --git a/schema/uuid.go b/schema/uuid.go index 34abca2811..cedbb74fda 100644 --- a/schema/uuid.go +++ b/schema/uuid.go @@ -19,6 +19,10 @@ func (*UUID) Type() ValueType { return TypeUUID } +func (dst *UUID) Size() int { + return 16 +} + func (dst *UUID) Equal(src CQType) bool { if src == nil { return false diff --git a/schema/uuid_array.go b/schema/uuid_array.go index ef69d5632d..cbcdb8b9c7 100644 --- a/schema/uuid_array.go +++ b/schema/uuid_array.go @@ -21,6 +21,10 @@ func (*UUIDArray) Type() ValueType { return TypeUUIDArray } +func (dst *UUIDArray) Size() int { + return 16 * len(dst.Elements) +} + func (dst *UUIDArray) Equal(src CQType) bool { if src == nil { return false From 77274f087ad215593fe206a5f7381a1b47bb4865 Mon Sep 17 00:00:00 2001 From: Yevgeny Pats <16490766+yevgenypats@users.noreply.github.com> Date: Sat, 17 Dec 2022 12:51:08 +0200 Subject: [PATCH 2/7] linter --- schema/cidr.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schema/cidr.go b/schema/cidr.go index 4a65f1e3a5..1be9c89c67 100644 --- a/schema/cidr.go +++ b/schema/cidr.go @@ -12,7 +12,7 @@ func (*CIDR) Type() ValueType { return TypeCIDR } -func (dst *CIDR) Size() int { +func (*CIDR) Size() int { return 32 } From cf8bff3fb28e6399a34f841937b5c8ff84257057 Mon Sep 17 00:00:00 2001 From: Yevgeny Pats <16490766+yevgenypats@users.noreply.github.com> Date: Sun, 18 Dec 2022 09:56:45 +0200 Subject: [PATCH 3/7] fix bool --- schema/bool.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schema/bool.go b/schema/bool.go index 9a9dabd52c..42a07d12da 100644 --- a/schema/bool.go +++ b/schema/bool.go @@ -20,7 +20,7 @@ func (*Bool) Type() ValueType { } func (*Bool) Size() int { - return 2 + return 1 } func (dst *Bool) Equal(src CQType) bool { From 2d6bc48b3c7f11559bba79f7859508721ec89036 Mon Sep 17 00:00:00 2001 From: Yevgeny Pats <16490766+yevgenypats@users.noreply.github.com> Date: Sun, 18 Dec 2022 09:58:16 +0200 Subject: [PATCH 4/7] cidr fix --- schema/cidr.go | 4 ++-- schema/inet.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/schema/cidr.go b/schema/cidr.go index 1be9c89c67..b9f8eef0d1 100644 --- a/schema/cidr.go +++ b/schema/cidr.go @@ -12,8 +12,8 @@ func (*CIDR) Type() ValueType { return TypeCIDR } -func (*CIDR) Size() int { - return 32 +func (dst *CIDR) Size() int { + return len(dst.IPNet.IP) + len(dst.IPNet.Mask) } func (dst *CIDR) String() string { diff --git a/schema/inet.go b/schema/inet.go index 16b10dbb88..18cb88a8aa 100644 --- a/schema/inet.go +++ b/schema/inet.go @@ -30,7 +30,7 @@ func (*Inet) Type() ValueType { } func (dst *Inet) Size() int { - return 32 + return len(dst.IPNet.IP) + len(dst.IPNet.Mask) } func (dst *Inet) Equal(src CQType) bool { From 12fa53691228aab36baa06d8b77877b20884c86b Mon Sep 17 00:00:00 2001 From: Yevgeny Pats <16490766+yevgenypats@users.noreply.github.com> Date: Sun, 18 Dec 2022 09:59:21 +0200 Subject: [PATCH 5/7] some more fixes --- schema/inet_array.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/schema/inet_array.go b/schema/inet_array.go index 98c0ddfbe3..26db273b64 100644 --- a/schema/inet_array.go +++ b/schema/inet_array.go @@ -23,7 +23,11 @@ func (*InetArray) Type() ValueType { } func (dst *InetArray) Size() int { - return len(dst.Elements) * 32 + totalSize := 0 + for _, element := range dst.Elements { + totalSize += element.Size() + } + return totalSize } func (dst *InetArray) Equal(src CQType) bool { From a4fe20340eed1605a66845f62c8ece00fedfff16 Mon Sep 17 00:00:00 2001 From: Herman Schaaf Date: Thu, 5 Jan 2023 14:04:11 +0000 Subject: [PATCH 6/7] Remove bool test --- schema/bool_test.go | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/schema/bool_test.go b/schema/bool_test.go index 6a96a1eaec..9523c0c221 100644 --- a/schema/bool_test.go +++ b/schema/bool_test.go @@ -31,29 +31,3 @@ func TestBoolSet(t *testing.T) { } } } - -func TestBool_Size(t *testing.T) { - tests := []struct { - name string - b Bool - want int - }{ - { - name: "present", - b: Bool{Bool: true, Status: Present}, - want: 1, - }, - { - name: "null", - b: Bool{Status: Null}, - want: 1, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := tt.b.Size(); got != tt.want { - t.Errorf("Bool.Size() = %v, want %v", got, tt.want) - } - }) - } -} From 8798ad6ca36e030a3570a4607286a9df59a2201c Mon Sep 17 00:00:00 2001 From: Herman Schaaf Date: Thu, 5 Jan 2023 14:05:46 +0000 Subject: [PATCH 7/7] Linting --- schema/int8_array_test.go | 2 +- schema/uuid_array_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/schema/int8_array_test.go b/schema/int8_array_test.go index fbda3c4312..c5a97b8a58 100644 --- a/schema/int8_array_test.go +++ b/schema/int8_array_test.go @@ -141,7 +141,7 @@ func TestInt8Array_Size(t *testing.T) { t.Errorf("%v.Size() = %d, want %d", r, sz, 0) } - r.Set([]int64{1, 2, 3}) + _ = r.Set([]int64{1, 2, 3}) sz = r.Size() if sz != 24 { t.Errorf("%v.Size() = %d, want %d", r, sz, 24) diff --git a/schema/uuid_array_test.go b/schema/uuid_array_test.go index ef6e1c3c5e..63f5098f6d 100644 --- a/schema/uuid_array_test.go +++ b/schema/uuid_array_test.go @@ -168,7 +168,7 @@ func TestUUIDArray_Size(t *testing.T) { t.Errorf("bad size: %d", r.Size()) } - r.Set([2][1][1][3]string{ + _ = r.Set([2][1][1][3]string{ {{{ "00010203-0405-0607-0809-0a0b0c0d0e0f", "10111213-1415-1617-1819-1a1b1c1d1e1f",