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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
use byteorder::{ByteOrder, BigEndian, WriteBytesExt};
use std::io::Write;
use std::sync::Arc;
use parking_lot::{Condvar,Mutex};
use crossbeam::channel;

const SEGMENT_SIZE_GOAL: usize = 1024*128;
const SEGMENT_SIZE_EXTRA: usize = 1024*128 + 1024*32;

pub(crate) struct Writer<W: Write+Send+'static>
{
	writer_state: Option<Arc<Mutex<WriterState<W>>>>,
	last_key: String,
	last_format: String,
	first_segment_key: String,
	current_segment_data: Vec<u8>,
	current_key_data: Vec<u8>,
	current_key_record_len: usize,
	current_timestamp: [u8; 8],
	worker_threads: Option<channel::Sender<WorkerMessage>>,
	thread_handles: Vec<std::thread::JoinHandle<std::io::Result<()>>>,
	// a counter to keep each thread writing its output in the right order
	thread_ordering: usize,
}

struct WriterState<W: Write+Send>
{
	counter: usize,
	prev_size: u32,
	writer: W,
}

struct WorkerMessage
{
	counter: usize,
	header: Vec<u8>, // not to compress
	payload: Vec<u8>, // to compress
}

/// A reason a write could not be completed
#[derive(Debug)]
pub enum WriteFailure
{
	/// The key (`.0`) came lexicographically before key `.1`.
	OrderingViolation(String, String),
	/// The formats must must for a single key.
	HeterogeneousFormats(String, String, String),
	/// An IO error from the OS
	IOError(std::io::Error),
}

impl From<std::io::Error> for WriteFailure
{
	fn from(e: std::io::Error) -> Self
	{
		WriteFailure::IOError(e)
	}
}

impl<W: Write+Send> Writer<W>
{
	pub fn new(writer: W)
		-> Writer<W>
	{
		let num_worker_threads = 4usize;

		let writer_state =
			WriterState
			{
				counter: 0,
				prev_size: 0,
				writer,
			};

		let writer_state = Arc::new(Mutex::new(writer_state));
		let writer_notifier = Arc::new(Condvar::new());

		let mut thread_handles = Vec::with_capacity(num_worker_threads);

		let (send, recv) = channel::bounded(num_worker_threads*4);
		for _ in 0..num_worker_threads
		{
			let writer_state = writer_state.clone();
			let writer_notifier = writer_notifier.clone();
			let recv = recv.clone();
			let h = std::thread::spawn(
				move ||
					worker_thread(recv, &writer_state, &writer_notifier)
			);
			thread_handles.push(h);
		}


		Writer
		{
			writer_state: Some(writer_state),
			last_key: String::new(),
			last_format: String::new(),
			first_segment_key: String::new(),
			current_key_data: Vec::with_capacity(SEGMENT_SIZE_EXTRA),
			current_segment_data: Vec::with_capacity(SEGMENT_SIZE_EXTRA),
			current_key_record_len: 0,
			current_timestamp: [0; 8],
			worker_threads: Some(send),
			thread_handles,
			thread_ordering: 0,
		}
	}

	pub(crate) fn add_record(&mut self, key: &str, format: &str, data: &[u8])
		-> std::result::Result<(), WriteFailure>
	{
		// this is the first key ever seen
		if self.current_key_data.is_empty()
		{
			self.last_key.replace_range(.., key);
			self.last_format.replace_range(.., format);
			self.first_segment_key.replace_range(.., key);
			self.current_key_record_len = data.len();

			self.current_key_data.write_u32::<BigEndian>(key.len() as u32)
				.unwrap();
			self.current_key_data.write_u32::<BigEndian>(format.len() as u32)
				.unwrap();
			self.current_key_data.write_u32::<BigEndian>(self.current_key_record_len as u32)
				.unwrap();
			// key data length, filled in later
			self.current_key_data.write_u32::<BigEndian>(0)
				.unwrap();
			self.current_key_data.write_all(key.as_bytes()).unwrap();
			self.current_key_data.write_all(format.as_bytes()).unwrap();
		}
		else
		{
			// we don't break keys into multiple segments
			if self.last_key != key
			{
				if key.as_bytes() < self.last_key.as_bytes()
				{
					return Err(WriteFailure::OrderingViolation(
						key.to_string(),
						self.last_key.clone(),
					));
				}

				// set key data length for previous key
				{
					let l = self.current_key_data.len() as u32
						- 16 - self.last_key.len() as u32
						- self.last_format.len() as u32;
					BigEndian::write_u32(&mut self.current_key_data[12..16], l);
				}
				// maybe flush the last segment, if it's full
				if ((self.current_segment_data.len()+self.current_key_data.len()) >>4)
					 >= SEGMENT_SIZE_GOAL
				{
					self.store_current_segment()?;
					self.first_segment_key.replace_range(.., key);
					assert_eq!(self.current_segment_data.len(), 0);
					std::mem::swap(&mut self.current_segment_data, &mut self.current_key_data);
				}
				else
				{
					self.current_segment_data.extend_from_slice(&self.current_key_data);
					self.current_key_data.clear();
				}
				self.last_key.replace_range(.., key);
				self.last_format.replace_range(.., format);
				self.current_key_record_len = data.len();
				self.current_key_data.write_u32::<BigEndian>(key.len() as u32)
					.unwrap();
				self.current_key_data.write_u32::<BigEndian>(format.len() as u32)
					.unwrap();
				self.current_key_data.write_u32::<BigEndian>(self.current_key_record_len as u32)
					.unwrap();
				self.current_key_data.write_u32::<BigEndian>(0)
					.unwrap();
				self.current_key_data.write_all(key.as_bytes()).unwrap();
				self.current_key_data.write_all(format.as_bytes()).unwrap();
			}
			else
			{
				if self.last_format != format
				{
					return Err(WriteFailure::HeterogeneousFormats(
						key.to_string(),
						self.last_format.to_string(),
						format.to_string(),
					));
				}
				if key.as_bytes() == self.last_key.as_bytes()
					 && data[0 .. 8] <= self.current_timestamp[..]
				{
					return Err(WriteFailure::OrderingViolation(
						key.to_string(),
						self.last_key.clone(),
					));
				}
			}
		}

		self.current_timestamp.copy_from_slice(&data[0..8]);

		assert_eq!(self.current_key_record_len, data.len());
		self.current_key_data.write_all(data).unwrap();
		Ok(())
	}

	/// send the current segment to a worker thread to get written
	pub(crate) fn store_current_segment(&mut self) -> std::io::Result<()>
	{
		let mut header = vec!();
		header.write_all(crate::segment::SEGMENT_INVOCATION)?;
		header.write_u32::<BigEndian>(self.first_segment_key.len() as u32)?;
		header.write_u32::<BigEndian>(self.last_key.len() as u32)?;
		header.write_u32::<BigEndian>(0u32)?; // compressed data size (filled by worker thread)
		header.write_u32::<BigEndian>(0u32)?; // prev_size (filled by worker thread)
		header.write_all(&self.first_segment_key.as_bytes())?;
		header.write_all(&self.last_key.as_bytes())?;

		let payload = std::mem::replace(
			&mut self.current_segment_data,
			Vec::with_capacity(SEGMENT_SIZE_EXTRA)
		);

		let message =
			WorkerMessage
			{
				counter: self.thread_ordering,
				header,
				payload,
			};
		self.thread_ordering += 1;

		self.worker_threads
			.as_ref().unwrap()
			.send(message).expect("failed to send data to worker");
		Ok(())
	}

	pub(crate) fn finish(mut self)
		-> std::io::Result<W>
	{
		self.fin()?;
		// destructure the entire writer_state to get
		// the tasty cream-filled `Write` inside
		let e = Arc::try_unwrap(
			self.writer_state
				.take()
				.expect("no writer_state???")
		);
		if let Ok(k) = e
		{
			Ok(k.into_inner().writer)
		}
		else
		{
			panic!("someone is still holding on on the writer_state");
		}
	}

	fn fin(&mut self)
		-> std::io::Result<()>
	{
		if !self.current_key_data.is_empty()
		{
			// set key data length for previous key
			{
				let l = self.current_key_data.len() as u32
					- 16 - self.last_key.len() as u32
					- self.last_format.len() as u32;
				BigEndian::write_u32(&mut self.current_key_data[12..16], l);
			}
			self.current_segment_data.extend_from_slice(&self.current_key_data);
			self.current_key_data.clear();
		}

		if !self.current_segment_data.is_empty()
		{
			self.store_current_segment()?;
		}

		self.worker_threads.take(); // close the Sender

		for th in self.thread_handles.drain(..)
			{ th.join().expect("thread can't be joined")?; }
		Ok(())
	}
}

impl<W: Write+Send> Drop for Writer<W>
{
	fn drop(&mut self)
	{
		self.fin().expect("failed to commit transaction");
	}
}

fn worker_thread<W: Write+Send>(
	recv: channel::Receiver<WorkerMessage>,
	writer_state: &Mutex<WriterState<W>>,
	writer_notifier: &Condvar,
) -> std::io::Result<()>
{
	for message in recv
	{
		let WorkerMessage { counter, mut header, payload }
			= message;

		let mut encoder = lz4::EncoderBuilder::new()
			.level(9)
			.build(vec!())
			.unwrap();
		encoder.write_all(&payload)?;
		let (compressed, e) = encoder.finish();
		e?;

		BigEndian::write_u32(&mut header[16+8 .. 16+8+4], compressed.len() as u32);

		let mut wl = writer_state.lock();
		while counter != wl.counter
		{
			writer_notifier.wait(&mut wl);
		}
		BigEndian::write_u32(&mut header[16+8+4 .. 16+8+8], wl.prev_size);

		wl.writer.write_all(&header)
			.expect("failed to write header data");
		wl.writer.write_all(&compressed)
			.expect("failed to write compressed data");
		wl.counter = counter+1;
		wl.prev_size = compressed.len() as u32+32;
		writer_notifier.notify_all();
	}
	Ok(())
}