forked from zbackup/zbackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup_creator.hh
92 lines (73 loc) · 2.92 KB
/
backup_creator.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) 2012-2014 Konstantin Isakov <[email protected]> and ZBackup contributors, see CONTRIBUTORS
// Part of ZBackup. Licensed under GNU GPLv2 or later + OpenSSL, see LICENSE
#ifndef BACKUP_CREATOR_HH_INCLUDED
#define BACKUP_CREATOR_HH_INCLUDED
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
#include <stddef.h>
#include <string>
#include <vector>
#include "chunk_id.hh"
#include "chunk_index.hh"
#include "chunk_storage.hh"
#include "file.hh"
#include "nocopy.hh"
#include "rolling_hash.hh"
#include "sptr.hh"
#include "zbackup.pb.h"
#include "config.hh"
using std::vector;
using std::string;
/// Creates a backup by processing input data and matching/writing chunks
class BackupCreator: ChunkIndex::ChunkInfoInterface, NoCopy
{
unsigned chunkMaxSize;
ChunkIndex & chunkIndex;
ChunkStorage::Writer & chunkStorageWriter;
vector< char > ringBuffer;
// Ring buffer vars
char * begin;
char * end;
char * head;
char * tail;
unsigned ringBufferFill;
/// In this buffer we assemble the next chunk to be eventually stored. We
/// copy the bytes from the ring buffer. While the copying may be avoided in
/// some cases, the plan is to move to multi-threaded chunk storage in the
/// future, where it would be necessary in any case
vector< char > chunkToSave;
unsigned chunkToSaveFill; /// Number of bytes accumulated in chunkToSave
/// When we have data in chunkToSave, this points to the record in backupData
/// which should store it
unsigned recordIndexToSaveDataInto;
RollingHash rollingHash;
string backupData;
sptr< google::protobuf::io::StringOutputStream > backupDataStream;
/// Sees if the current block in the ring buffer exists in the chunk store.
/// If it does, the reference is emitted and the ring buffer is cleared
void addChunkIfMatched();
/// Outputs data contained in chunkToSave as a new chunk
void saveChunkToSave();
/// Move the given amount of bytes from the ring buffer to the chunk to save.
/// Ring buffer must have at least that many bytes
void moveFromRingBufferToChunkToSave( unsigned bytes );
/// Outputs the given instruction to the backup stream
void outputInstruction( BackupInstruction const & );
bool chunkIdGenerated;
ChunkId generatedChunkId;
virtual ChunkId const & getChunkId();
public:
BackupCreator( Config const &, ChunkIndex &, ChunkStorage::Writer & );
/// The data is fed the following way: the user fills getInputBuffer() with
/// up to getInputBufferSize() bytes, then calls handleMoreData() with the
/// number of bytes written
void * getInputBuffer();
size_t getInputBufferSize();
void handleMoreData( unsigned );
/// Flushes any remaining data and finishes the process. No additional data
/// may be added after this call is made
void finish();
/// Returns the result of the backup creation. Can only be called once the
/// finish() was called and the backup is complete
void getBackupData( string & );
};
#endif