| 163 | } |
| 164 | |
| 165 | void TableBuilder::WriteBlock(BlockBuilder* block, BlockHandle* handle) { |
| 166 | // File format contains a sequence of blocks where each block has: |
| 167 | // block_data: uint8[n] |
| 168 | // type: uint8 |
| 169 | // crc: uint32 |
| 170 | assert(ok()); |
| 171 | Rep* r = rep_; |
| 172 | StringPiece raw = block->Finish(); |
| 173 | |
| 174 | StringPiece block_contents; |
| 175 | CompressionType type = r->options.compression; |
| 176 | // TODO(postrelease): Support more compression options: zlib? |
| 177 | switch (type) { |
| 178 | case kNoCompression: |
| 179 | block_contents = raw; |
| 180 | break; |
| 181 | |
| 182 | case kSnappyCompression: { |
| 183 | string* compressed = &r->compressed_output; |
| 184 | if (port::Snappy_Compress(raw.data(), raw.size(), compressed) && |
| 185 | compressed->size() < raw.size() - (raw.size() / 8u)) { |
| 186 | block_contents = *compressed; |
| 187 | } else { |
| 188 | // Snappy not supported, or compressed less than 12.5%, so just |
| 189 | // store uncompressed form |
| 190 | block_contents = raw; |
| 191 | type = kNoCompression; |
| 192 | } |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | WriteRawBlock(block_contents, type, handle); |
| 197 | r->compressed_output.clear(); |
| 198 | block->Reset(); |
| 199 | } |
| 200 | |
| 201 | void TableBuilder::WriteRawBlock(const StringPiece& block_contents, |
| 202 | CompressionType type, BlockHandle* handle) { |