From 15f7cef45e82e54a039e5486f08134c4f9a05471 Mon Sep 17 00:00:00 2001 From: Michael Vrable Date: Wed, 7 Feb 2007 13:45:30 -0800 Subject: [PATCH] Tag objects with a 4-byte type code. --- scandir.cc | 8 +++++--- store.cc | 30 +++++++++++++++++------------- store.h | 13 +++++++++---- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/scandir.cc b/scandir.cc index c2a093f..e253527 100644 --- a/scandir.cc +++ b/scandir.cc @@ -82,7 +82,8 @@ void dumpfile(int fd, dictionary &file_info) struct uuid segment_uuid; int object_id; OutputStream *index_data = index_segment->new_object(&segment_uuid, - &object_id); + &object_id, + "DREF"); SHA1Checksum hash; while (true) { @@ -95,7 +96,8 @@ void dumpfile(int fd, dictionary &file_info) hash.process(block_buf, bytes); OutputStream *block = data_segment->new_object(&block_segment_uuid, - &block_object_id); + &block_object_id, + "DATA"); block->write(block_buf, bytes); index_data->write_uuid(block_segment_uuid); index_data->write_u32(block_object_id); @@ -250,7 +252,7 @@ int main(int argc, char *argv[]) segment_store = new SegmentStore("."); SegmentWriter *sw = segment_store->new_segment(); - info_dump = sw->new_object(NULL); + info_dump = sw->new_object(NULL, "ROOT"); index_segment = new SegmentPartitioner(segment_store); data_segment = new SegmentPartitioner(segment_store); diff --git a/store.cc b/store.cc index e7c373c..0a833ab 100644 --- a/store.cc +++ b/store.cc @@ -223,8 +223,9 @@ SegmentWriter::~SegmentWriter() for (object_table::const_iterator i = objects.begin(); i != objects.end(); ++i) { - out->write_s64(i->first); - out->write_s64(i->second); + out->write_s64(i->offset); + out->write_s64(i->size); + out->write(i->type, sizeof(i->type)); } static const char signature2[] = "LBSEND"; @@ -243,18 +244,21 @@ SegmentWriter::~SegmentWriter() delete raw_out; } -OutputStream *SegmentWriter::new_object(int *id) +OutputStream *SegmentWriter::new_object(int *id, const char *type) { if (object_stream) finish_object(); - object_start_offset = out->get_pos(); - object_stream = new WrapperOutputStream(*out); - - if (id != NULL) { + if (id != NULL) *id = objects.size(); - } + struct index_info info; + info.offset = out->get_pos(); + info.size = -1; // Will be filled in when object is finished + strncpy(info.type, type, sizeof(info.type)); + objects.push_back(info); + + object_stream = new WrapperOutputStream(*out); return object_stream; } @@ -262,9 +266,8 @@ void SegmentWriter::finish_object() { assert(object_stream != NULL); - // store (start, length) information for locating this object - objects.push_back(std::make_pair(object_start_offset, - object_stream->get_pos())); + // Fill in object size, which could not be stored at start + objects.back().size = object_stream->get_pos(); delete object_stream; object_stream = NULL; @@ -321,7 +324,8 @@ SegmentPartitioner::~SegmentPartitioner() delete segment; } -OutputStream *SegmentPartitioner::new_object(struct uuid *uuid, int *id) +OutputStream *SegmentPartitioner::new_object(struct uuid *uuid, int *id, + const char *type) { if (segment != NULL && segment->get_size() > target_size) { delete segment; @@ -334,5 +338,5 @@ OutputStream *SegmentPartitioner::new_object(struct uuid *uuid, int *id) if (uuid != NULL) *uuid = segment->get_uuid(); - return segment->new_object(id); + return segment->new_object(id, type); } diff --git a/store.h b/store.h index e1244f0..5d877c6 100644 --- a/store.h +++ b/store.h @@ -162,7 +162,7 @@ public: struct uuid get_uuid() const { return id; } // Start writing out a new object to this segment. - OutputStream *new_object(int *id); + OutputStream *new_object(int *id, const char *type); void finish_object(); // Determine size of segment data written out so far. @@ -173,13 +173,18 @@ public: static std::string format_uuid(const struct uuid u); private: - typedef std::vector > object_table; + struct index_info { + int64_t offset; // File offset at which object starts + int64_t size; // Size of object in bytes + char type[4]; // Object type code + }; + + typedef std::vector object_table; ChecksumOutputStream *out; // Output stream with checksumming enabled OutputStream *raw_out; // Raw output stream, without checksumming struct uuid id; - int64_t object_start_offset; OutputStream *object_stream; object_table objects; @@ -209,7 +214,7 @@ public: explicit SegmentPartitioner(SegmentStore *s); ~SegmentPartitioner(); - OutputStream *new_object(struct uuid *uuid, int *id); + OutputStream *new_object(struct uuid *uuid, int *id, const char *type); private: size_t target_size; -- 2.20.1