using std::string;
using std::vector;
+static SegmentStore *segment_store;
static OutputStream *info_dump = NULL;
+static SegmentPartitioner *index_segment, *data_segment;
+
void scandir(const string& path);
/* Converts time to microseconds since the epoch. */
break;
} else {
hash.process(buf, res);
+ OutputStream *block = data_segment->new_object();
+ block->write(buf, res);
size += res;
}
}
int main(int argc, char *argv[])
{
- SegmentStore ss(".");
- SegmentWriter *sw = ss.new_segment();
+ segment_store = new SegmentStore(".");
+ SegmentWriter *sw = segment_store->new_segment();
info_dump = sw->new_object();
+ index_segment = new SegmentPartitioner(segment_store);
+ data_segment = new SegmentPartitioner(segment_store);
+
string uuid = SegmentWriter::format_uuid(sw->get_uuid());
printf("Backup UUID: %s\n", uuid.c_str());
fprintf(stderr, "IOException: %s\n", e.getError().c_str());
}
+ delete index_segment;
+ delete data_segment;
delete sw;
return 0;
return new SegmentWriter(new FileOutputStream(f), id);
}
+
+SegmentPartitioner::SegmentPartitioner(SegmentStore *s)
+ : store(s),
+ segment(NULL),
+ object(NULL)
+{
+ // Default target size is around 1 MB
+ target_size = 1024 * 1024;
+}
+
+SegmentPartitioner::~SegmentPartitioner()
+{
+ if (segment)
+ delete segment;
+}
+
+OutputStream *SegmentPartitioner::new_object()
+{
+ if (segment != NULL && segment->get_size() > target_size) {
+ delete segment;
+ segment = NULL;
+ }
+
+ if (segment == NULL)
+ segment = store->new_segment();
+
+ return segment->new_object();
+}
OutputStream *new_object();
void finish_object();
+ // Determine size of segment data written out so far.
+ size_t get_size() const { return raw_out->get_pos(); }
+
// Utility functions for generating and formatting UUIDs for display.
static struct uuid generate_uuid();
static std::string format_uuid(const struct uuid u);
std::string directory;
};
+/* A SegmentPartitioner helps to divide objects up among a collection of
+ * segments to meet a rough size limit per segment. Like a SegmentWriter, only
+ * one object should be written at a time; however, multiple
+ * SegmentPartitioners can be created using the same base SegmentStore. */
+class SegmentPartitioner {
+public:
+ explicit SegmentPartitioner(SegmentStore *s);
+ ~SegmentPartitioner();
+
+ OutputStream *new_object();
+
+private:
+ size_t target_size;
+
+ SegmentStore *store;
+ SegmentWriter *segment;
+ OutputStream *object;
+};
+
#endif // _LBS_STORE_H