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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use std::path::{PathBuf,Path};
use crate::write::Writer;
use std::io::{Write,Seek};
pub struct CreateTx
{
writer: Option<Writer<std::fs::File>>,
tmp_name: PathBuf,
dir: PathBuf,
}
impl CreateTx
{
pub fn new(dir: &Path) -> std::io::Result<CreateTx>
{
for attempt in 0..
{
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.expect("duration_since epoch")
.as_secs();
let n = format!("tx.{:016x}", timestamp);
let tmp_name = dir.join(n + ".tmp");
let f = std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&tmp_name);
if let Err(e) = f
{
if attempt == 1000
{ return Err(e); }
std::thread::sleep(std::time::Duration::from_millis(100));
continue;
}
let writer = f.unwrap();
let writer = Writer::new(writer);
let tx = CreateTx
{
writer: Some(writer),
tmp_name,
dir: dir.to_owned(),
};
return Ok(tx);
}
unreachable!();
}
pub fn add_record(&mut self, key: &str, format: &str, data: &[u8])
-> std::result::Result<(), crate::write::WriteFailure>
{
self.writer.as_mut().unwrap().add_record(key, format, data)
}
pub fn commit_to(mut self, final_name: &Path)
-> std::io::Result<()>
{
let writer = self.writer.take().unwrap();
let mut file = writer.finish()?;
file.flush()?;
let len = file.seek(std::io::SeekFrom::End(0))? as usize;
if len == 0
{
drop(file);
let _ = std::fs::remove_file(&self.tmp_name);
if final_name.file_name().map(|n| n == "main") != Some(true)
{ let _ = std::fs::remove_file(&final_name); }
return Ok(());
}
file.sync_all()?;
drop(file);
std::fs::rename(&self.tmp_name, &final_name)
}
pub fn commit(self)
-> std::io::Result<()>
{
{
let mainpath = self.dir.join("main");
let maininfo = std::fs::metadata(&mainpath)?;
if maininfo.len() == 0
{
use fs2::FileExt;
let lock = std::fs::File::create(self.dir.join(".compact"))?;
lock.lock_exclusive()?;
let maininfo = std::fs::metadata(&mainpath)?;
if maininfo.len() == 0
{
return self.commit_to(&mainpath);
}
}
}
for attempt in 0..
{
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.expect("duration_since epoch")
.as_secs();
let n = format!("tx.{:016x}", timestamp);
let final_name = self.dir.join(n);
let f = std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&final_name);
if let Err(e) = f
{
if attempt == 1000
{ return Err(e); }
std::thread::sleep(std::time::Duration::from_millis(100));
continue;
}
else
{
let tmp_name = self.tmp_name.clone();
if let Err(e) = self.commit_to(&final_name)
{
eprintln!("failure committing {:?} {:?}", tmp_name, final_name);
std::fs::remove_file(&tmp_name)
.expect("failed to remove file");
std::fs::remove_file(&final_name)
.expect("failed to remove file");
return Err(e);
}
break;
}
}
Ok(())
}
}
impl Drop for CreateTx
{
fn drop(&mut self)
{
if self.writer.is_some()
{
drop(self.writer.take());
let _ = std::fs::remove_file(&self.tmp_name);
}
}
}