-
Notifications
You must be signed in to change notification settings - Fork 6.2k
util: extend row format with checksum #42859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,28 +16,98 @@ package rowcodec | |
|
|
||
| import ( | ||
| "encoding/binary" | ||
| "hash/crc32" | ||
| ) | ||
|
|
||
| // row is the struct type used to access a row. | ||
| const ( | ||
| rowFlagLarge byte = 1 << iota | ||
| rowFlagChecksum | ||
| ) | ||
|
|
||
| const ( | ||
| checksumMaskVersion byte = 0b0111 | ||
| checksumFlagExtra byte = 0b1000 | ||
| ) | ||
|
|
||
| // row is the struct type used to access a row and the row format is shown as the following. | ||
| // | ||
| // Row Format | ||
| // | ||
| // 0 1 2 3 | ||
| // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| // | VER | FLAGS | NOT_NULL_COL_CNT | | ||
| // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| // | NULL_COL_CNT | ...NOT_NULL_COL_IDS... | | ||
| // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| // | ...NULL_COL_IDS... | ...NOT_NULL_COL_OFFSETS... | | ||
| // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| // | ...NOT_NULL_COL_DATA... | | ||
| // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| // | ...CHECKSUM... | | ||
| // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| // | ||
| // - FLAGS | ||
| // - 0x01: large (when max(col_ids) > 255 or len(col_data) > max_u16) | ||
| // - size of col_id = large ? 4 : 1 | ||
| // - size of col_offset = large ? 4 : 2 | ||
| // - 0x02: has checksum | ||
| // | ||
| // Checksum | ||
| // | ||
| // 0 1 2 3 4 5 6 7 8 | ||
| // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| // | |E| VER | CHECKSUM | EXTRA_CHECKSUM(OPTIONAL) | | ||
| // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| // HEADER | ||
| // | ||
| // - HEADER | ||
| // - VER: version | ||
| // - E: has extra checksum | ||
| // - CHECKSUM | ||
| // - little-endian CRC32(IEEE) when hdr.ver = 0 (default) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @3AceShowHand |
||
| type row struct { | ||
| // small: colID []byte, offsets []uint16, optimized for most cases. | ||
| // large: colID []uint32, offsets []uint32. | ||
| large bool | ||
| flags byte | ||
| checksumHeader byte | ||
| numNotNullCols uint16 | ||
| numNullCols uint16 | ||
| colIDs []byte | ||
|
|
||
| // for small row: colID []byte, offsets []uint16, optimized for most cases. | ||
| colIDs []byte | ||
| offsets []uint16 | ||
| data []byte | ||
|
|
||
| // for large row | ||
| // for large row: colID []uint32, offsets []uint32. | ||
| colIDs32 []uint32 | ||
| offsets32 []uint32 | ||
|
|
||
| data []byte | ||
| checksum1 uint32 | ||
| checksum2 uint32 | ||
| } | ||
|
|
||
| func (r *row) large() bool { return r.flags&rowFlagLarge > 0 } | ||
|
|
||
| func (r *row) hasChecksum() bool { return r.flags&rowFlagChecksum > 0 } | ||
|
|
||
| func (r *row) hasExtraChecksum() bool { return r.checksumHeader&checksumFlagExtra > 0 } | ||
|
|
||
| func (r *row) checksumVersion() int { return int(r.checksumHeader & checksumMaskVersion) } | ||
|
|
||
| func (r *row) calcChecksum() error { | ||
| if r.checksumVersion() != 0 { | ||
| return errInvalidChecksumVer | ||
| } | ||
| r.checksum1 = crc32.ChecksumIEEE(r.data) | ||
| return nil | ||
| } | ||
|
|
||
| func (r *row) setExtraChecksum(v uint32) { | ||
| r.checksumHeader |= checksumFlagExtra | ||
| r.checksum2 = v | ||
| } | ||
|
|
||
| func (r *row) getData(i int) []byte { | ||
| var start, end uint32 | ||
| if r.large { | ||
| if r.large() { | ||
| if i > 0 { | ||
| start = r.offsets32[i-1] | ||
| } | ||
|
|
@@ -55,46 +125,76 @@ func (r *row) fromBytes(rowData []byte) error { | |
| if rowData[0] != CodecVer { | ||
| return errInvalidCodecVer | ||
| } | ||
| r.large = rowData[1]&1 > 0 | ||
| r.flags = rowData[1] | ||
| r.numNotNullCols = binary.LittleEndian.Uint16(rowData[2:]) | ||
| r.numNullCols = binary.LittleEndian.Uint16(rowData[4:]) | ||
| cursor := 6 | ||
| if r.large { | ||
| lastOffset := 0 | ||
| if r.large() { | ||
| colIDsLen := int(r.numNotNullCols+r.numNullCols) * 4 | ||
| r.colIDs32 = bytesToU32Slice(rowData[cursor : cursor+colIDsLen]) | ||
| cursor += colIDsLen | ||
| offsetsLen := int(r.numNotNullCols) * 4 | ||
| r.offsets32 = bytesToU32Slice(rowData[cursor : cursor+offsetsLen]) | ||
| cursor += offsetsLen | ||
| if n := len(r.offsets32); n > 0 { | ||
| lastOffset = int(r.offsets32[n-1]) | ||
| } | ||
| } else { | ||
| colIDsLen := int(r.numNotNullCols + r.numNullCols) | ||
| r.colIDs = rowData[cursor : cursor+colIDsLen] | ||
| cursor += colIDsLen | ||
| offsetsLen := int(r.numNotNullCols) * 2 | ||
| r.offsets = bytes2U16Slice(rowData[cursor : cursor+offsetsLen]) | ||
| cursor += offsetsLen | ||
| if n := len(r.offsets); n > 0 { | ||
| lastOffset = int(r.offsets[n-1]) | ||
| } | ||
| } | ||
| r.data = rowData[cursor : cursor+lastOffset] | ||
| cursor += lastOffset | ||
|
|
||
| if r.hasChecksum() { | ||
| r.checksumHeader = rowData[cursor] | ||
| if r.checksumVersion() != 0 { | ||
| return errInvalidChecksumVer | ||
| } | ||
| cursor++ | ||
| r.checksum1 = binary.LittleEndian.Uint32(rowData[cursor:]) | ||
| cursor += 4 | ||
| if r.hasExtraChecksum() { | ||
| r.checksum2 = binary.LittleEndian.Uint32(rowData[cursor:]) | ||
| } else { | ||
| r.checksum2 = 0 | ||
| } | ||
| } else { | ||
| r.checksumHeader = 0 | ||
| r.checksum1 = 0 | ||
| r.checksum2 = 0 | ||
| } | ||
| r.data = rowData[cursor:] | ||
| return nil | ||
| } | ||
|
|
||
| func (r *row) toBytes(buf []byte) []byte { | ||
| buf = append(buf, CodecVer) | ||
| flag := byte(0) | ||
| if r.large { | ||
| flag = 1 | ||
| } | ||
| buf = append(buf, flag) | ||
| buf = append(buf, r.flags) | ||
| buf = append(buf, byte(r.numNotNullCols), byte(r.numNotNullCols>>8)) | ||
| buf = append(buf, byte(r.numNullCols), byte(r.numNullCols>>8)) | ||
| if r.large { | ||
| if r.large() { | ||
| buf = append(buf, u32SliceToBytes(r.colIDs32)...) | ||
| buf = append(buf, u32SliceToBytes(r.offsets32)...) | ||
| } else { | ||
| buf = append(buf, r.colIDs...) | ||
| buf = append(buf, u16SliceToBytes(r.offsets)...) | ||
| } | ||
| buf = append(buf, r.data...) | ||
| if r.hasChecksum() { | ||
| buf = append(buf, r.checksumHeader) | ||
| buf = binary.LittleEndian.AppendUint32(buf, r.checksum1) | ||
| if r.hasExtraChecksum() { | ||
| buf = binary.LittleEndian.AppendUint32(buf, r.checksum2) | ||
| } | ||
| } | ||
| return buf | ||
| } | ||
|
|
||
|
|
@@ -105,7 +205,7 @@ func (r *row) findColID(colID int64) (idx int, isNil, notFound bool) { | |
| h := int(uint(i+j) >> 1) // avoid overflow when computing h | ||
| // i ≤ h < j | ||
| var v int64 | ||
| if r.large { | ||
| if r.large() { | ||
| v = int64(r.colIDs32[h]) | ||
| } else { | ||
| v = int64(r.colIDs[h]) | ||
|
|
@@ -126,7 +226,7 @@ func (r *row) findColID(colID int64) (idx int, isNil, notFound bool) { | |
| h := int(uint(i+j) >> 1) // avoid overflow when computing h | ||
| // i ≤ h < j | ||
| var v int64 | ||
| if r.large { | ||
| if r.large() { | ||
| v = int64(r.colIDs32[h]) | ||
| } else { | ||
| v = int64(r.colIDs[h]) | ||
|
|
@@ -144,6 +244,23 @@ func (r *row) findColID(colID int64) (idx int, isNil, notFound bool) { | |
| return | ||
| } | ||
|
|
||
| // GetChecksum returns the checksum of row data (not null columns). | ||
| func (r *row) GetChecksum() (uint32, bool) { | ||
| if !r.hasChecksum() { | ||
| return 0, false | ||
| } | ||
| return r.checksum1, true | ||
| } | ||
|
|
||
| // GetExtraChecksum returns the extra checksum which shall be calculated in the last stable schema version (whose | ||
| // elements are all public). | ||
| func (r *row) GetExtraChecksum() (uint32, bool) { | ||
| if !r.hasExtraChecksum() { | ||
| return 0, false | ||
| } | ||
| return r.checksum2, true | ||
| } | ||
|
|
||
| // ColumnIsNull returns if the column value is null. Mainly used for count column aggregation. | ||
| // this method will used in unistore. | ||
| func (r *row) ColumnIsNull(rowData []byte, colID int64, defaultVal []byte) (bool, error) { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.