Last active
May 24, 2020 07:02
-
-
Save baranovxyz/58721d85cd4b906b9a747bbf04ff0c3f to your computer and use it in GitHub Desktop.
This file contains 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
function calculateColumnSize(column) { | |
let size = 0; | |
size += 4; // 4 bytes for int32 for a document size | |
// `_id` key-value where _id value is a string | |
// 10 bytes + ? = 1(value type) + 3(`_id` key length) + 1(key terminator) + 4(int32 string length) + ?(value length) + 1(string value terminator) | |
size += 10 + column._id.length; | |
// `type` key value with 'string' as value | |
// 17 bytes = 1(value type) + 4(`type` key length) + 1(key terminator) + 4(int32 string length) + 6(`string` length) + 1(string value terminator) | |
size += 17; | |
// `values` array with string or null values | |
// it will be encoded as key-value first | |
// 8 bytes + ? = 1(array type) + 6(`values` key size) + 1(key terminator) + ?(array BSON) | |
// array BSON size 5bytes + ? = 4(int32 length) + ?(key-value pairs encoded) +1(BSON terminator) | |
// totally 13 bytes for what we know + key-value pairs encoded | |
size += 13; | |
for (let i = 0, len = column.values.length; i < len; i++) { | |
const value = column.values[i]; | |
// 1byte for value type encoded | |
size += 1; | |
// size = key length + 1 byte for key terminator | |
size += String(i).length + 1; | |
// encoding values | |
if (value === null) { | |
// for null we do not need to count anything else | |
} else if (typeof value === 'string') { | |
// size = 4(int32 string length) + ?(value length) + 1(value string terminator) | |
size += 4 + Buffer.from(value).size + 1; | |
} else { | |
throw new Error('Not implemented'); | |
} | |
} | |
// 1 byte for document terminator | |
size += 1; | |
return size; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment