hanahmily commented on code in PR #682:
URL: 
https://github.com/apache/skywalking-banyandb/pull/682#discussion_r2160566806


##########
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)
+       bd, err = decodeBitPacking(bd, src)
+       if err != nil {
+               return err
+       }
+       d.indices = decodeRLE(d.indices[:0], bd)
+       return nil
+}
+
+func (d *Dictionary) decodeBytesBlockWithTail(src []byte, itemsCount uint64) 
([][]byte, []byte, error) {
+       u64List := GenerateUint64List(0)
+       defer ReleaseUint64List(u64List)
+
+       var tail []byte
+       var err error
+       u64List.L, tail, err = decodeUint64Block(u64List.L[:0], src, itemsCount)
+       if err != nil {
+               return nil, nil, fmt.Errorf("cannot decode string lengths: %w", 
err)
+       }
+       aLens := u64List.L
+       src = tail
+
+       var decompressedData []byte
+       decompressedData, tail, err = decompressBlock(nil, src)
+       if err != nil {
+               return nil, nil, fmt.Errorf("cannot decode bytes block with 
strings: %w", err)
+       }
+
+       dst := d.values[:0]
+       data := decompressedData
+       for _, sLen := range aLens {
+               if uint64(len(data)) < sLen {
+                       return nil, nil, fmt.Errorf("cannot decode a string 
with the length %d bytes from %d bytes", sLen, len(data))
+               }
+               if sLen == 0 {
+                       dst = append(dst, nil)
+                       continue
+               }
+               dst = append(dst, data[:sLen])
+               data = data[sLen:]
+       }
+
+       return dst, tail, nil
+}
+
+func encodeRLE(dst []uint32, src []uint32) []uint32 {

Review Comment:
   The values suitable for a dictionary are brief. I don't think you'll achieve 
a better compression ratio. It was shown in your test case.



##########
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 {

Review Comment:
   You need to provide a method that indicates whether the block’s values are 
suitable for the dictionary encoding. If the values differ too significantly, 
we should skip this encoding.



-- 
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

Reply via email to