hanahmily commented on code in PR #682: URL: https://github.com/apache/skywalking-banyandb/pull/682#discussion_r2165251237
########## pkg/encoding/dictionary.go: ########## @@ -0,0 +1,250 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package encoding + +import ( + "bytes" + "fmt" + "math/bits" +) + +// Dictionary is used for dictionary encoding. +type Dictionary struct { + m map[string]uint32 + values [][]byte + indices []uint32 +} + +// NewDictionary creates a dictionary. +func NewDictionary() *Dictionary { + return &Dictionary{ + m: make(map[string]uint32), + values: make([][]byte, 0), + indices: make([]uint32, 0), + } +} + +// Reset resets the dictionary. +func (d *Dictionary) Reset() { + clear(d.m) + d.values = d.values[:0] + d.indices = d.indices[:0] +} + +// Add adds a value to the dictionary. +func (d *Dictionary) Add(value []byte) { + if index, ok := d.m[string(value)]; ok { + d.indices = append(d.indices, index) + return + } + d.values = append(d.values, value) + index := uint32(len(d.values) - 1) + d.m[string(value)] = index + d.indices = append(d.indices, index) +} + +// Encode encodes the dictionary. +func (d *Dictionary) Encode(dst []byte) []byte { + dst = VarUint64ToBytes(dst, uint64(len(d.values))) + dst = EncodeBytesBlock(dst, d.values) + re := make([]uint32, 0) Review Comment: Reuse this slice ########## pkg/encoding/dictionary.go: ########## @@ -0,0 +1,250 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package encoding + +import ( + "bytes" + "fmt" + "math/bits" +) + +// Dictionary is used for dictionary encoding. +type Dictionary struct { + m map[string]uint32 + values [][]byte + indices []uint32 +} + +// NewDictionary creates a dictionary. +func NewDictionary() *Dictionary { + return &Dictionary{ + m: make(map[string]uint32), + values: make([][]byte, 0), + indices: make([]uint32, 0), + } +} + +// Reset resets the dictionary. +func (d *Dictionary) Reset() { + clear(d.m) + d.values = d.values[:0] + d.indices = d.indices[:0] +} + +// Add adds a value to the dictionary. +func (d *Dictionary) Add(value []byte) { + if index, ok := d.m[string(value)]; ok { + d.indices = append(d.indices, index) + return + } + d.values = append(d.values, value) + index := uint32(len(d.values) - 1) + d.m[string(value)] = index + d.indices = append(d.indices, index) +} + +// Encode encodes the dictionary. +func (d *Dictionary) Encode(dst []byte) []byte { + dst = VarUint64ToBytes(dst, uint64(len(d.values))) + dst = EncodeBytesBlock(dst, d.values) + re := make([]uint32, 0) + re = encodeRLE(re, d.indices) + be := encodeBitPacking(re) + dst = append(dst, be...) + return dst +} + +// Decode decodes the dictionary. +func (d *Dictionary) Decode(src []byte) error { Review Comment: Would you write a benchmark for Encode and Decode? ########## pkg/encoding/dictionary.go: ########## @@ -0,0 +1,250 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package encoding + +import ( + "bytes" + "fmt" + "math/bits" +) + +// Dictionary is used for dictionary encoding. +type Dictionary struct { + m map[string]uint32 + values [][]byte + indices []uint32 +} + +// NewDictionary creates a dictionary. +func NewDictionary() *Dictionary { + return &Dictionary{ + m: make(map[string]uint32), + values: make([][]byte, 0), + indices: make([]uint32, 0), + } +} + +// Reset resets the dictionary. +func (d *Dictionary) Reset() { + clear(d.m) + d.values = d.values[:0] + d.indices = d.indices[:0] +} + +// Add adds a value to the dictionary. +func (d *Dictionary) Add(value []byte) { Review Comment: ```suggestion func (d *Dictionary) Add(value []byte) bool { ``` If the size of unique values exceeds a threshold(128 or 256?), returning false indicates that the block cannot use dict encoding. ########## pkg/encoding/dictionary.go: ########## @@ -0,0 +1,250 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package encoding + +import ( + "bytes" + "fmt" + "math/bits" +) + +// Dictionary is used for dictionary encoding. +type Dictionary struct { + m map[string]uint32 Review Comment: ```suggestion ``` You don't need a map. Instead, adding can loop values to find the index. The volume is reasonable for looping. ########## pkg/encoding/dictionary.go: ########## @@ -0,0 +1,250 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package encoding + +import ( + "bytes" + "fmt" + "math/bits" +) + +// Dictionary is used for dictionary encoding. +type Dictionary struct { + m map[string]uint32 + values [][]byte + indices []uint32 +} + +// NewDictionary creates a dictionary. +func NewDictionary() *Dictionary { + return &Dictionary{ + m: make(map[string]uint32), + values: make([][]byte, 0), + indices: make([]uint32, 0), + } +} + +// Reset resets the dictionary. +func (d *Dictionary) Reset() { + clear(d.m) + d.values = d.values[:0] + d.indices = d.indices[:0] +} + +// Add adds a value to the dictionary. +func (d *Dictionary) Add(value []byte) { + if index, ok := d.m[string(value)]; ok { + d.indices = append(d.indices, index) + return + } + d.values = append(d.values, value) + index := uint32(len(d.values) - 1) + d.m[string(value)] = index + d.indices = append(d.indices, index) +} + +// Encode encodes the dictionary. +func (d *Dictionary) Encode(dst []byte) []byte { + dst = VarUint64ToBytes(dst, uint64(len(d.values))) + dst = EncodeBytesBlock(dst, d.values) + re := make([]uint32, 0) + re = encodeRLE(re, d.indices) + be := encodeBitPacking(re) + dst = append(dst, be...) + return dst +} + +// Decode decodes the dictionary. +func (d *Dictionary) Decode(src []byte) error { + src, count := BytesToVarUint64(src) + if count == 0 { + return nil + } + + values, src, err := d.decodeBytesBlockWithTail(src, count) + if err != nil { + return err + } + d.values = values + d.m = make(map[string]uint32, len(d.values)) + for i, value := range d.values { + d.m[string(value)] = uint32(i) + } + + bd := make([]uint32, 0) Review Comment: Reuse this slice. It can be field shared between Encode and Decode. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@skywalking.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org