| 139 | } |
| 140 | |
| 141 | void TableBuilder::WriteBlock(BlockBuilder* block, BlockHandle* handle) { |
| 142 | // File format contains a sequence of blocks where each block has: |
| 143 | // block_data: uint8[n] |
| 144 | // type: uint8 |
| 145 | // crc: uint32 |
| 146 | assert(ok()); |
| 147 | Rep* r = rep_; |
| 148 | Slice raw = block->Finish(); |
| 149 | |
| 150 | Slice block_contents; |
| 151 | CompressionType type = r->options.compression; |
| 152 | // TODO(postrelease): Support more compression options: zlib? |
| 153 | switch (type) { |
| 154 | case kNoCompression: |
| 155 | block_contents = raw; |
| 156 | break; |
| 157 | |
| 158 | case kSnappyCompression: { |
| 159 | std::string* compressed = &r->compressed_output; |
| 160 | if (port::Snappy_Compress(raw.data(), raw.size(), compressed) && |
| 161 | compressed->size() < raw.size() - (raw.size() / 8u)) { |
| 162 | block_contents = *compressed; |
| 163 | } else { |
| 164 | // Snappy not supported, or compressed less than 12.5%, so just |
| 165 | // store uncompressed form |
| 166 | block_contents = raw; |
| 167 | type = kNoCompression; |
| 168 | } |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | WriteRawBlock(block_contents, type, handle); |
| 173 | r->compressed_output.clear(); |
| 174 | block->Reset(); |
| 175 | } |
| 176 | |
| 177 | void TableBuilder::WriteRawBlock(const Slice& block_contents, |
| 178 | CompressionType type, BlockHandle* handle) { |