Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 50e39fc

Browse files
committedAug 24, 2023
fix: add OneByteBitSet
1 parent 1485b4e commit 50e39fc

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed
 

‎common_types/src/row/bitset.rs

+23-17
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ const UNSET_BIT_MASK: [u8; 8] = [
2828
255 - 128,
2929
];
3030

31+
/// A bit set representing at most 8 bits with a underlying u8.
32+
pub struct OneByteBitSet(pub u8);
33+
34+
impl OneByteBitSet {
35+
/// Create from a given boolean slice.
36+
///
37+
/// The values in the `bits` whose index is greater than 8 will be ignored.
38+
pub fn from_slice(bits: &[bool]) -> Self {
39+
let mut v = 0u8;
40+
for (idx, set) in bits.iter().take(8).map(|v| *v as u8).enumerate() {
41+
let (_, bit_idx) = RoBitSet::compute_byte_bit_index(idx);
42+
v |= set << bit_idx
43+
}
44+
45+
Self(v)
46+
}
47+
}
48+
3149
/// A basic implementation supporting read/write.
3250
#[derive(Debug, Default, Clone)]
3351
pub struct BitSet {
@@ -46,19 +64,6 @@ impl BitSet {
4664
}
4765
}
4866

49-
/// Create a u8 according to a given 8bits array.
50-
///
51-
/// The values in the `bits` whose index is greater than 8 will be ignored.
52-
pub fn one_byte(bits: &[bool]) -> u8 {
53-
let mut v = 0u8;
54-
for (idx, set) in bits.iter().take(8).map(|v| *v as u8).enumerate() {
55-
let (_, bit_idx) = RoBitSet::compute_byte_bit_index(idx);
56-
v |= set << bit_idx
57-
}
58-
59-
v
60-
}
61-
6267
/// Initialize a [`BitSet`] with all bits set.
6368
pub fn all_set(num_bits: usize) -> Self {
6469
Self {
@@ -185,6 +190,7 @@ mod tests {
185190
use std::assert_eq;
186191

187192
use super::BitSet;
193+
use crate::row::bitset::OneByteBitSet;
188194

189195
#[test]
190196
fn test_set_op() {
@@ -245,17 +251,17 @@ mod tests {
245251
#[test]
246252
fn test_one_byte() {
247253
let bits = [false, false, false, false, false, false];
248-
assert_eq!(0, BitSet::one_byte(&bits));
254+
assert_eq!(0, OneByteBitSet::from_slice(&bits).0);
249255

250256
let bits = [true, false, false, false, false, false];
251-
assert_eq!(1, BitSet::one_byte(&bits));
257+
assert_eq!(1, OneByteBitSet::from_slice(&bits).0);
252258

253259
let bits = [false, false, false, true, false, false, true, true];
254-
assert_eq!(128 + 64 + 8, BitSet::one_byte(&bits));
260+
assert_eq!(128 + 64 + 8, OneByteBitSet::from_slice(&bits).0);
255261

256262
let bits = [
257263
false, false, false, false, false, false, true, true, true, true,
258264
];
259-
assert_eq!(128 + 64, BitSet::one_byte(&bits));
265+
assert_eq!(128 + 64, OneByteBitSet::from_slice(&bits).0);
260266
}
261267
}

‎components/codec/src/columnar/bool.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
use bytes_ext::{Buf, BufMut};
16-
use common_types::row::bitset::{BitSet, RoBitSet};
16+
use common_types::row::bitset::{BitSet, OneByteBitSet, RoBitSet};
1717
use snafu::{ensure, OptionExt};
1818

1919
use super::{
@@ -48,7 +48,7 @@ enum Compression {
4848

4949
impl Encoding {
5050
const COMPRESSION_SIZE: usize = 1;
51-
/// The overhead for compression is 5B, so it is not good to always enable
51+
/// The overhead for compression is 4B, so it is not good to always enable
5252
/// the compression.
5353
const COMPRESS_THRESHOLD: usize = 10;
5454
const NUM_VALUES_SIZE: usize = 4;
@@ -169,8 +169,8 @@ impl Encoding {
169169
one_byte_bits[offset] = v;
170170
offset += 1;
171171
if offset == 8 {
172-
let bit_set = BitSet::one_byte(&one_byte_bits);
173-
buf.put_u8(bit_set);
172+
let bit_set = OneByteBitSet::from_slice(&one_byte_bits);
173+
buf.put_u8(bit_set.0);
174174

175175
// Reset the offset and the bits buf.
176176
offset = 0;
@@ -180,8 +180,8 @@ impl Encoding {
180180

181181
// Put the remaining bits.
182182
if offset > 0 {
183-
let bit_set = BitSet::one_byte(&one_byte_bits);
184-
buf.put_u8(bit_set);
183+
let bit_set = OneByteBitSet::from_slice(&one_byte_bits);
184+
buf.put_u8(bit_set.0);
185185
}
186186

187187
buf.put_u8(Compression::BitSet as u8);

0 commit comments

Comments
 (0)
Please sign in to comment.