mirror of
https://github.com/twitter/the-algorithm.git
synced 2024-12-22 02:01:51 +01:00
Merge 1020c610ef
into 72eda9a24f
This commit is contained in:
commit
0c1212dd5a
@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include <fstream>
|
||||
#include <twml/defines.h>
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
@ -27,19 +28,18 @@ namespace twml {
|
||||
class BlockFormatWriter {
|
||||
private:
|
||||
const char *file_name_;
|
||||
FILE *outputfile_;
|
||||
std::fstream outputfile_;
|
||||
char temp_file_name_[PATH_MAX];
|
||||
int record_index_;
|
||||
int records_per_block_;
|
||||
|
||||
int pack_tag_and_wiretype(FILE *file, uint32_t tag, uint32_t wiretype);
|
||||
int pack_varint_i32(FILE *file, int value);
|
||||
int pack_string(FILE *file, const char *in, size_t in_len);
|
||||
int write_int(FILE *file, int value);
|
||||
int pack_tag_and_wiretype(std::fstream& fs, std::uint32_t tag, std::uint32_t wiretype);
|
||||
int pack_varint_i32(std::fstream& fs, int value);
|
||||
int pack_string(std::fstream& fs, const char *in, size_t in_len);
|
||||
int write_int(std::fstream& fs, int value);
|
||||
|
||||
public:
|
||||
BlockFormatWriter(const char *file_name, int record_per_block);
|
||||
~BlockFormatWriter();
|
||||
int write(const char *class_name, const char *record, int record_len) ;
|
||||
int flush();
|
||||
block_format_writer getHandle();
|
||||
|
@ -11,7 +11,7 @@
|
||||
#endif
|
||||
|
||||
#define MARKER_SIZE (16)
|
||||
static uint8_t _marker[MARKER_SIZE] = {
|
||||
static std::uint8_t _marker[MARKER_SIZE] = {
|
||||
0x29, 0xd8, 0xd5, 0x06, 0x58, 0xcd, 0x4c, 0x29,
|
||||
0xb2, 0xbc, 0x57, 0x99, 0x21, 0x71, 0xbd, 0xff
|
||||
};
|
||||
@ -20,29 +20,28 @@ namespace twml {
|
||||
BlockFormatWriter::BlockFormatWriter(const char *file_name, int record_per_block) :
|
||||
file_name_(file_name), record_index_(0), records_per_block_(record_per_block) {
|
||||
snprintf(temp_file_name_, PATH_MAX, "%s.block", file_name);
|
||||
outputfile_ = fopen(file_name_, "a");
|
||||
std::fstream outputfile_(file_name_, std::ios::app);
|
||||
if (!outputfile_.is_open()) {
|
||||
throw std::runtime_error("Failed to open the output file");
|
||||
}
|
||||
}
|
||||
|
||||
BlockFormatWriter::~BlockFormatWriter() {
|
||||
fclose(outputfile_);
|
||||
}
|
||||
// TODO: use fstream
|
||||
int BlockFormatWriter::pack_tag_and_wiretype(FILE *buffer, uint32_t tag, uint32_t wiretype) {
|
||||
uint8_t x = ((tag & 0x0f) << 3) | (wiretype & 0x7);
|
||||
size_t n = fwrite(&x, 1, 1, buffer);
|
||||
if (n != 1) {
|
||||
int BlockFormatWriter::pack_tag_and_wiretype(std::fstream& fs, std::uint32_t tag, std::uint32_t wiretype) {
|
||||
std::uint8_t x = ((tag & 0x0f) << 3) | (wiretype & 0x7);
|
||||
fs.write(reinterpret_cast<char*>(&x), sizeof(x));
|
||||
if (fs.fail()) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BlockFormatWriter::pack_varint_i32(FILE *buffer, int value) {
|
||||
int BlockFormatWriter::pack_varint_i32(std::fstream& fs, int value) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
uint8_t x = value & 0x7F;
|
||||
std::uint8_t x = value & 0x7F;
|
||||
value = value >> 7;
|
||||
if (value != 0) x |= 0x80;
|
||||
size_t n = fwrite(&x, 1, 1, buffer);
|
||||
if (n != 1) {
|
||||
fs.write(reinterpret_cast<const char*>(&x), sizeof(x));
|
||||
if (fs.fail()) {
|
||||
return -1;
|
||||
}
|
||||
if (value == 0) break;
|
||||
@ -50,21 +49,23 @@ namespace twml {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BlockFormatWriter::pack_string(FILE *buffer, const char *in, size_t in_len) {
|
||||
if (pack_varint_i32(buffer, in_len)) return -1;
|
||||
size_t n = fwrite(in, 1, in_len, buffer);
|
||||
if (n != in_len) return -1;
|
||||
int BlockFormatWriter::pack_string(std::fstream& fs, const char *in, size_t in_len) {
|
||||
if (pack_varint_i32(fs, in_len)) return -1;
|
||||
fs.write(in, in_len);
|
||||
if (fs.fail()) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BlockFormatWriter::write_int(FILE *buffer, int value) {
|
||||
uint8_t buff[4];
|
||||
buff[0] = value & 0xff;
|
||||
buff[1] = (value >> 8) & 0xff;
|
||||
buff[2] = (value >> 16) & 0xff;
|
||||
buff[3] = (value >> 24) & 0xff;
|
||||
size_t n = fwrite(buff, 1, 4, buffer);
|
||||
if (n != 4) {
|
||||
int BlockFormatWriter::write_int(std::fstream& fs, int value) {
|
||||
std::uint8_t buff[4];
|
||||
buff[0] = static_cast<std::uint8_t>(value & 0xff);
|
||||
buff[1] = static_cast<std::uint8_t>((value >> 8) & 0xff);
|
||||
buff[2] = static_cast<std::uint8_t>((value >> 16) & 0xff);
|
||||
buff[3] = static_cast<std::uint8_t>((value >> 24) & 0xff);
|
||||
fs.write(reinterpret_cast<char*>(buff), sizeof(buff));
|
||||
if (fs.fail()) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
@ -74,9 +75,9 @@ namespace twml {
|
||||
if (record) {
|
||||
record_index_++;
|
||||
// The buffer holds max records_per_block_ of records (block).
|
||||
FILE *buffer = fopen(temp_file_name_, "a");
|
||||
if (!buffer) return -1;
|
||||
if (ftell(buffer) == 0) {
|
||||
std::fstream buffer(temp_file_name_, std::ios::app);
|
||||
if (!buffer.is_open()) return -1;
|
||||
if (buffer.tellg() == 0) {
|
||||
if (pack_tag_and_wiretype(buffer, 1, WIRE_TYPE_VARINT))
|
||||
throw std::invalid_argument("Error writting tag and wiretype");
|
||||
if (pack_varint_i32(buffer, 1))
|
||||
@ -90,7 +91,6 @@ namespace twml {
|
||||
throw std::invalid_argument("Error writtig tag and wiretype");
|
||||
if (pack_string(buffer, record, record_len))
|
||||
throw std::invalid_argument("Error writting record");
|
||||
fclose(buffer);
|
||||
}
|
||||
|
||||
if ((record_index_ % records_per_block_) == 0) {
|
||||
@ -101,24 +101,24 @@ namespace twml {
|
||||
|
||||
int BlockFormatWriter::flush() {
|
||||
// Flush the records in the buffer to outputfile
|
||||
FILE *buffer = fopen(temp_file_name_, "r");
|
||||
std::fstream buffer(temp_file_name_, std::ios::in);
|
||||
if (buffer) {
|
||||
fseek(buffer, 0, SEEK_END);
|
||||
int64_t block_size = ftell(buffer);
|
||||
fseek(buffer, 0, SEEK_SET);
|
||||
buffer.seekg(0, buffer.end);
|
||||
int64_t block_size = buffer.tellg();
|
||||
buffer.seekg(0, buffer.beg);
|
||||
|
||||
if (fwrite(_marker, sizeof(_marker), 1, outputfile_) != 1) return 1;
|
||||
if (outputfile_.write(reinterpret_cast<const char*>(_marker), sizeof(_marker)).fail() != 1) return 1;
|
||||
if (write_int(outputfile_, block_size)) return 1;
|
||||
uint8_t buff[4096];
|
||||
std::uint8_t buff[4096];
|
||||
while (1) {
|
||||
size_t n = fread(buff, 1, sizeof(buff), buffer);
|
||||
buffer.read(reinterpret_cast<char*>(buff), sizeof(buff));
|
||||
std::streamsize n = buffer.gcount();
|
||||
if (n) {
|
||||
size_t x = fwrite(buff, 1, n, outputfile_);
|
||||
if (x != n) return 1;
|
||||
outputfile_.write(reinterpret_cast<const char*>(buff), n);
|
||||
if (outputfile_.fail()) return 1;
|
||||
}
|
||||
if (n != sizeof(buff)) break;
|
||||
}
|
||||
fclose(buffer);
|
||||
// Remove the buffer
|
||||
if (remove(temp_file_name_)) return 1;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user