forked from iabudiab/ObjectiveRocks
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRocksDBBackupEngine.mm
More file actions
158 lines (136 loc) · 3.7 KB
/
Copy pathRocksDBBackupEngine.mm
File metadata and controls
158 lines (136 loc) · 3.7 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// RocksDBBackupEngine.m
// ObjectiveRocks
//
// Created by Iska on 28/12/14.
// Copyright (c) 2014 BrainCookie. All rights reserved.
//
#import "RocksDBBackupEngine.h"
#import "RocksDBError.h"
#import "RocksDBBackupInfo.h"
#include <rocksdb/db.h>
#include <rocksdb/utilities/backupable_db.h>
#pragma mark - Informal Protocols
@interface RocksDB ()
@property (nonatomic, assign) rocksdb::DB *db;
@end
@interface RocksDBBackupInfo ()
@property (nonatomic, assign) uint32_t backupId;
@property (nonatomic, assign) int64_t timestamp;
@property (nonatomic, assign) uint64_t size;
@property (nonatomic, assign) uint32_t numberFiles;
@end
#pragma mark - Impl
@interface RocksDBBackupEngine ()
{
NSString *_path;
rocksdb::BackupEngine *_backupEngine;
}
@end
@implementation RocksDBBackupEngine
#pragma mark - Lifecycle
- (instancetype)initWithPath:(NSString *)path
{
self = [super init];
if (self) {
_path = [path copy];
rocksdb::Status status = rocksdb::BackupEngine::Open(rocksdb::Env::Default(),
rocksdb::BackupableDBOptions(_path.UTF8String),
&_backupEngine);
if (!status.ok()) {
NSLog(@"Error opening database backup: %@", [RocksDBError errorWithRocksStatus:status]);
return nil;
}
}
return self;
}
- (void)dealloc
{
[self close];
}
- (void)close
{
@synchronized(self) {
if (_backupEngine != NULL) {
delete _backupEngine;
_backupEngine = NULL;
}
}
}
#pragma mark - Backup & Restore
- (BOOL)createBackupForDatabase:(RocksDB *)database error:(NSError * __autoreleasing *)error
{
rocksdb::Status status = _backupEngine->CreateNewBackup(database.db);
if (!status.ok()) {
NSError *temp = [RocksDBError errorWithRocksStatus:status];
if (error && *error == nil) {
*error = temp;
return NO;
}
}
return YES;
}
- (BOOL)restoreBackupToDestinationPath:(NSString *)destination error:(NSError * __autoreleasing *)error
{
rocksdb::Status status = _backupEngine->RestoreDBFromLatestBackup(destination.UTF8String, destination.UTF8String);
if (!status.ok()) {
NSError *temp = [RocksDBError errorWithRocksStatus:status];
if (error && *error == nil) {
*error = temp;
return NO;
}
}
return YES;
}
- (BOOL)restoreBackupWithId:(uint32_t)backupId toDestinationPath:(NSString *)destination error:(NSError *__autoreleasing *)error
{
rocksdb::Status status = _backupEngine->RestoreDBFromBackup(backupId, destination.UTF8String, destination.UTF8String);
if (!status.ok()) {
NSError *temp = [RocksDBError errorWithRocksStatus:status];
if (error && *error == nil) {
*error = temp;
return NO;
}
}
return YES;
}
- (BOOL)purgeOldBackupsKeepingLast:(uint32_t)countBackups error:(NSError *__autoreleasing *)error
{
rocksdb::Status status = _backupEngine->PurgeOldBackups(countBackups);
if (!status.ok()) {
NSError *temp = [RocksDBError errorWithRocksStatus:status];
if (error && *error == nil) {
*error = temp;
return NO;
}
}
return YES;
}
- (BOOL)deleteBackupWithId:(uint32_t)backupId error:(NSError *__autoreleasing *)error
{
rocksdb::Status status = _backupEngine->DeleteBackup(backupId);
if (!status.ok()) {
NSError *temp = [RocksDBError errorWithRocksStatus:status];
if (error && *error == nil) {
*error = temp;
return NO;
}
}
return YES;
}
- (NSArray *)backupInfo
{
std::vector<rocksdb::BackupInfo> backup_info;
_backupEngine->GetBackupInfo(&backup_info);
NSMutableArray *backupInfo = [NSMutableArray array];
for (auto it = std::begin(backup_info); it != std::end(backup_info); ++it) {
RocksDBBackupInfo *info = [RocksDBBackupInfo new];
info.backupId = (*it).backup_id;
info.timestamp = (*it).timestamp;
info.size = (*it).size;
info.numberFiles = (*it).number_files;
[backupInfo addObject:info];
}
return backupInfo;
}
@end