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
use bytes::Bytes;
use hash::H256;
use ser::{serialize, List, deserialize};
use chain::{Transaction as ChainTransaction, BlockHeader};
use storage::{TransactionMeta};

pub const COL_COUNT: u32 = 10;
pub const COL_META: u32 = 0;
pub const COL_BLOCK_HASHES: u32 = 1;
pub const COL_BLOCK_HEADERS: u32 = 2;
pub const COL_BLOCK_TRANSACTIONS: u32 = 3;
pub const COL_TRANSACTIONS: u32 = 4;
pub const COL_TRANSACTIONS_META: u32 = 5;
pub const COL_BLOCK_NUMBERS: u32 = 6;
pub const COL_CONFIGURATION: u32 = 7;

#[derive(Debug)]
pub enum Operation {
	Insert(KeyValue),
	Delete(Key),
}

#[derive(Debug)]
pub enum KeyValue {
	Meta(&'static str, Bytes),
	BlockHash(u32, H256),
	BlockHeader(H256, BlockHeader),
	BlockTransactions(H256, List<H256>),
	Transaction(H256, ChainTransaction),
	TransactionMeta(H256, TransactionMeta),
	BlockNumber(H256, u32),
	Configuration(&'static str, Bytes),
}

#[derive(Debug)]
pub enum Key {
	Meta(&'static str),
	BlockHash(u32),
	BlockHeader(H256),
	BlockTransactions(H256),
	Transaction(H256),
	TransactionMeta(H256),
	BlockNumber(H256),
	Configuration(&'static str),
}

#[derive(Debug, Clone)]
pub enum Value {
	Meta(Bytes),
	BlockHash(H256),
	BlockHeader(BlockHeader),
	BlockTransactions(List<H256>),
	Transaction(ChainTransaction),
	TransactionMeta(TransactionMeta),
	BlockNumber(u32),
	Configuration(Bytes),
}

impl Value {
	pub fn for_key(key: &Key, bytes: &[u8]) -> Result<Self, String> {
		match *key {
			Key::Meta(_) => deserialize(bytes).map(Value::Meta),
			Key::BlockHash(_) => deserialize(bytes).map(Value::BlockHash),
			Key::BlockHeader(_) => deserialize(bytes).map(Value::BlockHeader),
			Key::BlockTransactions(_) => deserialize(bytes).map(Value::BlockTransactions),
			Key::Transaction(_) => deserialize(bytes).map(Value::Transaction),
			Key::TransactionMeta(_) => deserialize(bytes).map(Value::TransactionMeta),
			Key::BlockNumber(_) => deserialize(bytes).map(Value::BlockNumber),
			Key::Configuration(_) => deserialize(bytes).map(Value::Configuration),
		}.map_err(|e| format!("{:?}", e))
	}

	pub fn as_meta(self) -> Option<Bytes> {
		match self {
			Value::Meta(bytes) => Some(bytes),
			_ => None,
		}
	}

	pub fn as_block_hash(self) -> Option<H256> {
		match self {
			Value::BlockHash(block_hash) => Some(block_hash),
			_ => None,
		}
	}

	pub fn as_block_header(self) -> Option<BlockHeader> {
		match self {
			Value::BlockHeader(block_header) => Some(block_header),
			_ => None,
		}
	}

	pub fn as_block_transactions(self) -> Option<List<H256>> {
		match self {
			Value::BlockTransactions(list) => Some(list),
			_ => None,
		}
	}

	pub fn as_transaction(self) -> Option<ChainTransaction> {
		match self {
			Value::Transaction(transaction) => Some(transaction),
			_ => None,
		}
	}

	pub fn as_transaction_meta(self) -> Option<TransactionMeta> {
		match self {
			Value::TransactionMeta(meta) => Some(meta),
			_ => None,
		}
	}

	pub fn as_block_number(self) -> Option<u32> {
		match self {
			Value::BlockNumber(number) => Some(number),
			_ => None,
		}
	}

	pub fn as_configuration(self) -> Option<Bytes> {
		match self {
			Value::Configuration(bytes) => Some(bytes),
			_ => None,
		}
	}
}

#[derive(Debug, Clone)]
pub enum KeyState<V> {
	Insert(V),
	Delete,
	Unknown,
}

impl<V> Default for KeyState<V> {
	fn default() -> Self {
		KeyState::Unknown
	}
}

impl<V> KeyState<V> {
	pub fn map<U, F>(self, f: F) -> KeyState<U> where F: FnOnce(V) -> U {
		match self {
			KeyState::Insert(value) => KeyState::Insert(f(value)),
			KeyState::Delete => KeyState::Delete,
			KeyState::Unknown => KeyState::Unknown,
		}
	}

	pub fn into_option(self) -> Option<V> {
		match self {
			KeyState::Insert(value) => Some(value),
			KeyState::Delete => None,
			KeyState::Unknown => None,
		}
	}

	pub fn into_operation<K, I, D>(self, key: K, insert: I, delete: D) -> Option<Operation>
	where I: FnOnce(K, V) -> KeyValue, D: FnOnce(K) -> Key {
		match self {
			KeyState::Insert(value) => Some(Operation::Insert(insert(key, value))),
			KeyState::Delete => Some(Operation::Delete(delete(key))),
			KeyState::Unknown => None,
		}
	}
}

#[derive(Debug)]
pub struct Transaction {
	pub operations: Vec<Operation>,
}

impl Default for Transaction {
	fn default() -> Self {
		Transaction {
			operations: Vec::with_capacity(32),
		}
	}
}

impl Transaction {
	pub fn new() -> Self {
		Transaction::default()
	}

	pub fn insert(&mut self, insert: KeyValue) {
		self.operations.push(Operation::Insert(insert));
	}

	pub fn delete(&mut self, delete: Key) {
		self.operations.push(Operation::Delete(delete));
	}
}

#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
pub enum Location {
	DB,
	Column(u32),
}

impl From<u32> for Location {
	fn from(column: u32) -> Location {
		Location::Column(column)
	}
}

pub enum RawOperation {
	Insert(RawKeyValue),
	Delete(RawKey),
}

pub struct RawKeyValue {
	pub location: Location,
	pub key: Bytes,
	pub value: Bytes,
}

impl<'a> From<&'a KeyValue> for RawKeyValue {
	fn from(i: &'a KeyValue) -> Self {
		let (location, key, value) = match *i {
			KeyValue::Meta(ref key, ref value) => (COL_META, serialize(key), serialize(value)),
			KeyValue::BlockHash(ref key, ref value) => (COL_BLOCK_HASHES, serialize(key), serialize(value)),
			KeyValue::BlockHeader(ref key, ref value) => (COL_BLOCK_HEADERS, serialize(key), serialize(value)),
			KeyValue::BlockTransactions(ref key, ref value) => (COL_BLOCK_TRANSACTIONS, serialize(key), serialize(value)),
			KeyValue::Transaction(ref key, ref value) => (COL_TRANSACTIONS, serialize(key), serialize(value)),
			KeyValue::TransactionMeta(ref key, ref value) => (COL_TRANSACTIONS_META, serialize(key), serialize(value)),
			KeyValue::BlockNumber(ref key, ref value) => (COL_BLOCK_NUMBERS, serialize(key), serialize(value)),
			KeyValue::Configuration(ref key, ref value) => (COL_CONFIGURATION, serialize(key), serialize(value)),
		};

		RawKeyValue {
			location: location.into(),
			key: key,
			value: value,
		}
	}
}

pub struct RawKey {
	pub location: Location,
	pub key: Bytes,
}

impl RawKey {
	pub fn new<B>(location: Location, key: B) -> Self where B: Into<Bytes> {
		RawKey {
			location: location,
			key: key.into(),
		}
	}
}

impl<'a> From<&'a Key> for RawKey {
	fn from(d: &'a Key) -> Self {
		let (location, key) = match *d {
			Key::Meta(ref key) => (COL_META, serialize(key)),
			Key::BlockHash(ref key) => (COL_BLOCK_HASHES, serialize(key)),
			Key::BlockHeader(ref key) => (COL_BLOCK_HEADERS, serialize(key)),
			Key::BlockTransactions(ref key) => (COL_BLOCK_TRANSACTIONS, serialize(key)),
			Key::Transaction(ref key) => (COL_TRANSACTIONS, serialize(key)),
			Key::TransactionMeta(ref key) => (COL_TRANSACTIONS_META, serialize(key)),
			Key::BlockNumber(ref key) => (COL_BLOCK_NUMBERS, serialize(key)),
			Key::Configuration(ref key) => (COL_CONFIGURATION, serialize(key)),
		};

		RawKey {
			location: location.into(),
			key: key,
		}
	}
}

impl<'a> From<&'a Operation> for RawOperation {
	fn from(o: &'a Operation) -> Self {
		match *o {
			Operation::Insert(ref insert) => RawOperation::Insert(insert.into()),
			Operation::Delete(ref delete) => RawOperation::Delete(delete.into()),
		}
	}
}

pub struct RawTransaction {
	pub operations: Vec<RawOperation>,
}

impl<'a> From<&'a Transaction> for RawTransaction {
	fn from(tx: &'a Transaction) -> Self {
		RawTransaction {
			operations: tx.operations.iter().map(Into::into).collect()
		}
	}
}

impl Default for RawTransaction {
	fn default() -> Self {
		RawTransaction {
			operations: Vec::with_capacity(32),
		}
	}
}

impl RawTransaction {
	pub fn new() -> RawTransaction {
		RawTransaction::default()
	}

	pub fn insert_raw(&mut self, location: Location, key: &[u8], value: &[u8]) {
		let operation = RawOperation::Insert(RawKeyValue {
			location: location,
			key: key.into(),
			value: value.into(),
		});
		self.operations.push(operation);
	}

	pub fn delete_raw(&mut self, location: Location, key: &[u8]) {
		let operation = RawOperation::Delete(RawKey {
			location: location,
			key: key.into(),
		});
		self.operations.push(operation);
	}
}