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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
extern crate tokio;
extern crate fxhash;

use crate::crypto::Hash;

use super::conf::*;
use super::chain::*;
#[allow(unused_imports)]
use super::header::*;
use super::event::*;
#[allow(unused_imports)]
use super::meta::*;

extern crate rmp_serde as rmps;

use async_trait::async_trait;
use cached::Cached;
#[allow(unused_imports)]
use std::{collections::VecDeque, io::SeekFrom, ops::DerefMut};
#[allow(unused_imports)]
use tokio::{io::{AsyncReadExt, AsyncWriteExt, AsyncSeekExt}, time::sleep, time::Duration};
use tokio::io::Result;
use tokio::io::BufStream;
use tokio::io::Error;
use tokio::io::ErrorKind;
use bytes::BytesMut;
use bytes::Bytes;
use bytes::{Buf};
use std::mem::size_of;
use tokio::sync::Mutex as MutexAsync;
use cached::*;
use fxhash::FxHashMap;
use parking_lot::Mutex as MutexSync;

#[cfg(test)]
use tokio::runtime::Runtime;

#[allow(dead_code)]
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub(crate) struct LogFilePointer
{
    pub(crate) version: u32,
    pub(crate) size: u32,
    pub(crate) offset: u64,
}

struct LogFileCache
{
    pub(crate) flush: FxHashMap<LogFilePointer, LoadResult>,
    pub(crate) write: TimedSizedCache<LogFilePointer, LoadResult>,
    pub(crate) read: TimedSizedCache<LogFilePointer, LoadResult>,
}

struct LogFile
{
    pub(crate) version: u32,
    pub(crate) log_path: String,
    pub(crate) log_back: Option<tokio::fs::File>,
    pub(crate) log_random_access: MutexAsync<tokio::fs::File>,
    pub(crate) log_stream: BufStream<tokio::fs::File>,
    pub(crate) log_off: u64,
    pub(crate) log_temp: bool,
    pub(crate) log_count: u64,
    pub(crate) cache: MutexSync<LogFileCache>,
}

impl LogFile {
    pub(crate) fn check_open(&self) -> Result<()> {
        match self.log_back.as_ref() {
            Some(_) => Ok(()),
            None => return Result::Err(Error::new(ErrorKind::NotConnected, "The log file has already been closed.")),
        }
    }

    async fn copy(&mut self) -> Result<LogFile>
    {
        // We have to flush the stream in-case there is outstanding IO that is not yet written to the backing disk
        self.log_stream.flush().await?;

        // Copy the file handles
        self.check_open()?;
        let log_back = self.log_back.as_ref().unwrap().try_clone().await?;
        let log_random_access = self.log_random_access.lock().await.try_clone().await?;

        let cache = {
            let cache = self.cache.lock();
            MutexSync::new(LogFileCache {
                flush: cache.flush.clone(),
                read: cached::TimedSizedCache::with_size_and_lifespan(cache.read.cache_capacity().unwrap(), cache.read.cache_lifespan().unwrap()),
                write: cached::TimedSizedCache::with_size_and_lifespan(cache.write.cache_capacity().unwrap(), cache.write.cache_lifespan().unwrap()),
            })
        };

        Ok(
            LogFile {
                version: self.version,
                log_path: self.log_path.clone(),
                log_stream: BufStream::new(log_back.try_clone().await?),
                log_back: Some(log_back),
                log_random_access: MutexAsync::new(log_random_access),
                log_off: self.log_off,
                log_temp: self.log_temp,
                log_count: self.log_count,
                cache,
            }
        )
    }

    async fn new(temp_file: bool, path_log: String, truncate: bool, cache_size: usize, cache_ttl: u64) -> Result<LogFile> {
        let log_back = match truncate {
            true => tokio::fs::OpenOptions::new().read(true).write(true).truncate(true).create(true).open(path_log.clone()).await?,
               _ => tokio::fs::OpenOptions::new().read(true).write(true).create(true).open(path_log.clone()).await?,
        };
        let mut log_stream = BufStream::new(log_back.try_clone().await.unwrap());

        let version = match log_stream.read_u32().await {
            Ok(a) => a,
            Err(err) if err.kind() == ErrorKind::UnexpectedEof => {
                let new_version = fastrand::u32(..);
                let _ = log_stream.write_u32(new_version).await?;
                let _ = log_stream.flush().await?;
                new_version
            },
            Err(err) => return Result::Err(err)
        };
        
        let log_random_access = tokio::fs::OpenOptions::new().read(true).open(path_log.clone()).await?;

        let ret = LogFile {
            version: version,
            log_path: path_log.clone(),
            log_stream: log_stream,
            log_back: Some(log_back),
            log_random_access: MutexAsync::new(log_random_access),
            log_off: std::mem::size_of::<u32>() as u64,
            log_temp: temp_file,
            log_count: 0,
            cache: MutexSync::new(LogFileCache {
                flush: FxHashMap::default(),
                read: TimedSizedCache::with_size_and_lifespan(cache_size, cache_ttl),
                write: TimedSizedCache::with_size_and_lifespan(cache_size, cache_ttl),
            })
        };

        if temp_file {
            let _ = std::fs::remove_file(path_log);
        }

        Ok(ret)
    }

    async fn read_all(&mut self, to: &mut VecDeque<EventEntry>) -> Result<()> {
        self.check_open()?;

        while let Some(head) = self.read_once_internal().await? {
            to.push_back(head);
        }
        Ok(())
    }

    async fn read_once_internal(&mut self) -> Result<Option<EventEntry>>
    {
        // Read the metadata
        let size_meta = match self.log_stream.read_u32().await {
            Result::Ok(s) => s,
            Result::Err(err) => {
                if err.kind() == ErrorKind::UnexpectedEof {
                    return Ok(None);
                }
                return Result::Err(err)
            }
        } as usize;

        let mut buff_meta = BytesMut::with_capacity(size_meta);
        let read = self.log_stream.read_buf(&mut buff_meta).await?;
        if read != size_meta {
            return Result::Err(tokio::io::Error::new(tokio::io::ErrorKind::Other, format!("Failed to read the metadata of the event from the log file ({} bytes vs {} bytes)", read, size_meta)));
        }
        let buff_meta = buff_meta.freeze();

        // Read the body and hash the data
        let size_body = self.log_stream.read_u32().await? as usize;
        let body_hash = match size_body {
            _ if size_body > 0 => {
                let mut buff_body = BytesMut::with_capacity(size_body);
                let read = self.log_stream.read_buf(&mut buff_body).await?;
                if read != size_body {
                    return Result::Err(tokio::io::Error::new(tokio::io::ErrorKind::Other, format!("Failed to read the main body of the event from the log file ({} bytes vs {} bytes)", read, size_body)));
                }

                Some(super::crypto::Hash::from_bytes(&buff_body[..]))
            },
            _ => None
        };
        
        // Insert it into the log index
        let size = size_of::<u32>() as u64 + size_meta as u64 + size_of::<u32>() as u64 + size_body as u64;
        let pointer = LogFilePointer { version: self.version, offset: self.log_off, size: size as u32 };
        self.log_count = self.log_count + 1;

        // Compute the new offset
        self.log_off = self.log_off + size;

        Ok(
            Some(EventEntry {
                meta_hash: Hash::from_bytes(&buff_meta[..]),
                meta: buff_meta,
                data_hash: body_hash,
                pointer: pointer,
            })
        )
    }

    async fn write(&mut self, meta: Bytes, body: Option<Bytes>) -> Result<LogFilePointer>
    {
        self.check_open()?;

        let meta_len = meta.len() as u32;
        let body_len = match body.as_ref() {
            Some(a) => a.len() as u32,
            None => 0 as u32,
        };

        // Write the data to the log stream
        self.log_stream.write(&meta_len.to_be_bytes()).await?;
        self.log_stream.write_all(&meta[..]).await?;
        self.log_stream.write(&body_len.to_be_bytes()).await?;
        match body.as_ref() {
            Some(a) => {
                self.log_stream.write_all(&a[..]).await?;
            },
            _ => {}
        }

        // Build the log pointer and update the offset
        let size = size_of::<u32>() as u64 + meta.len() as u64 + size_of::<u32>() as u64 + body_len as u64;
        let pointer = LogFilePointer { version: self.version, offset: self.log_off, size: size as u32 };
        self.log_count = self.log_count + 1;
        self.log_off = self.log_off + size;

        // Hash body
        let body_hash = match &body {
            Some(data) => Some(super::crypto::Hash::from_bytes(&data[..])),
            None => None,
        };

        // Cache the data
        {
            let mut cache = self.cache.lock();
            cache.flush.insert(pointer.clone(), LoadResult {
                evt: EventData {
                    meta_hash: super::crypto::Hash::from_bytes(&meta[..]),
                    meta: meta.clone(),
                    data: body.clone(),
                    data_hash: body_hash,
                    pointer: pointer,
                },
                pointer,
            });
        }
        
        // Return the log pointer
        Ok(pointer)
    }

    async fn copy_event(&mut self, from_log: &LogFile, from_pointer: &LogFilePointer) -> Result<LogFilePointer>
    {
        self.check_open()?;
        from_log.check_open()?;
        
        let mut buff = BytesMut::with_capacity(from_pointer.size as usize);
        
        let read = {
            let mut lock = from_log.log_random_access.lock().await;
            lock.seek(SeekFrom::Start(from_pointer.offset)).await?;
            lock.read_buf(&mut buff).await?
        };        
        if read != from_pointer.size as usize {
            return Result::Err(tokio::io::Error::new(tokio::io::ErrorKind::Other, "Failed to copy the event from another log file"));
        }

        self.log_stream.write_all(&buff[..]).await?;

        let pointer = LogFilePointer { version: self.version, offset: self.log_off, size: from_pointer.size };
        self.log_count = self.log_count + 1;
        self.log_off = self.log_off + from_pointer.size as u64;

        Ok(pointer)
    }

    async fn load(&self, pointer: LogFilePointer) -> Result<LoadResult> {
        self.check_open()?;

        if pointer.version != self.version {
            return Result::Err(Error::new(ErrorKind::Other, format!("Could not find data object as it is from a different redo log (pointer.version=0x{:X?}, log.version=0x{:X?})", pointer.version, self.version)));
        }

        // Check the caches
        {
            let mut cache = self.cache.lock();
            if let Some(result) = cache.flush.get(&pointer) {
                return Ok(result.clone());
            }
            if let Some(result) = cache.read.cache_get(&pointer) {
                return Ok(result.clone());
            }
            if let Some(result) = cache.write.cache_remove(&pointer) {
                return Ok(result);
            }
        }

        // First read all the data into a buffer
        let mut buff = BytesMut::with_capacity(pointer.size as usize);
        let read = {
            let mut lock = self.log_random_access.lock().await;
            lock.seek(SeekFrom::Start(pointer.offset as u64)).await?;
            lock.read_buf(&mut buff).await?
        };
        if read != pointer.size as usize {
            return Result::Err(tokio::io::Error::new(tokio::io::ErrorKind::Other, format!("Failed to read the data object event slice from the redo log ({} bytes vs {} bytes)", read, pointer.size)));
        }
        
        // Read all the data
        let size_meta = buff.get_u32();
        if size_meta > buff.remaining() as u32 {
            return Result::Err(tokio::io::Error::new(tokio::io::ErrorKind::Other, format!("Failed to read the data object metadata from the redo log as the header exceeds the event slice ({} bytes exceeds remaining event slice {})", size_meta, buff.remaining())));
        }
        let buff_meta = buff.copy_to_bytes(size_meta as usize);
        
        let size_body = buff.get_u32();
        let buff_body = match size_body {
            0 => None,
            _ if size_body > buff.remaining() as u32 => {
                return Result::Err(tokio::io::Error::new(tokio::io::ErrorKind::Other, format!("Failed to read the data object data from the redo log as the header exceeds the event slice ({} bytes exceeds remaining event slice {})", size_body, buff.remaining())));
            },
            n => Some(buff.copy_to_bytes(n as usize)),
        };

        // Hash body
        let body_hash = match &buff_body {
            Some(data) => Some(super::crypto::Hash::from_bytes(&data[..])),
            None => None,
        };

        // Add it to the read cache
        let ret = LoadResult {
            evt: EventData {
                meta_hash: super::crypto::Hash::from_bytes(&buff_meta[..]),
                meta: buff_meta,
                data: buff_body,
                data_hash: body_hash,
                pointer: pointer,
            },
            pointer,
        };
        {
            let mut cache = self.cache.lock();
            cache.read.cache_set(pointer.clone(), ret.clone());
        }

        Ok(
            ret
        )
    }

    fn move_log_file(&mut self, new_path: &String) -> Result<()> {
        self.check_open()?;

        if self.log_temp == false {
            std::fs::rename(self.log_path.clone(), new_path)?;
        }
        self.log_path = new_path.clone();
        Ok(())
    }

    async fn flush(&mut self) -> Result<()>
    {
        self.check_open()?;

        // Make a note of all the cache lines we need to move
        let mut keys = Vec::new();
        {
            let cache = self.cache.lock();
            for k in cache.flush.keys() {
                keys.push(k.clone());
            }
        }

        // Flush the data to disk
        self.log_stream.flush().await?;
        self.log_back.as_ref().unwrap().sync_all().await?;


        // Move the cache lines into the write cache from the flush cache which
        // will cause them to be released after the TTL is reached
        {
            let mut cache = self.cache.lock();
            for k in keys.into_iter() {
                if let Some(v) =  cache.flush.remove(&k) {
                    cache.write.cache_set(k, v);
                }
            }
        }

        Ok(())
    }

    #[allow(dead_code)]
    fn count(&self) -> usize {
        self.log_count as usize
    }

    fn destroy(&mut self) -> Result<()> {
        self.check_open()?;

        std::fs::remove_file(self.log_path.clone())?;
        self.log_back = None;
        Ok(())
    }

    fn is_open(&self) -> bool {
        match self.log_back {
            Some(_) => true,
            _ => false,
        }
    }
}

impl Drop
for LogFile
{
    fn drop(&mut self) {
        futures::executor::block_on(self.log_stream.flush()).unwrap();
    }
}

struct DeferredWrite {
    pub meta: Bytes,
    pub data: Option<Bytes>,
    pub orphan: LogFilePointer,
}

impl DeferredWrite {
    pub fn new(meta: Bytes, data: Option<Bytes>, orphan: LogFilePointer) -> DeferredWrite {
        DeferredWrite {
            meta: meta,
            data: data,
            orphan: orphan,
        }
    }
}

#[async_trait]
pub(crate) trait LogWritable {
    async fn write(&mut self, meta: Bytes, data: Option<Bytes>) -> Result<LogFilePointer>;
    async fn flush(&mut self) -> Result<()>;
    async fn copy_event(&mut self, from_log: &RedoLog, from_pointer: &LogFilePointer) -> Result<LogFilePointer>;
}

pub(crate) struct FlippedLogFile {
    log_file: LogFile,
    event_summary: Vec<EventEntry>,
}

#[async_trait]
impl LogWritable for FlippedLogFile
{
    #[allow(dead_code)]
    async fn write(&mut self, meta: Bytes, data: Option<Bytes>) -> Result<LogFilePointer> {
        let meta_hash = Hash::from_bytes(&meta[..]);
        let data_hash = match &data {
            Some(data) => Some(super::crypto::Hash::from_bytes(&data[..])),
            None => None,
        };
        let ret = self.log_file.write(meta.clone(), data).await?;

        let summary = EventEntry {
            meta_hash: meta_hash,
            meta: meta,
            data_hash: data_hash,
            pointer: ret.clone(),
        };
        self.event_summary.push(summary);
        Ok(ret)
    }

    async fn flush(&mut self) -> Result<()> {
        self.log_file.flush().await
    }

    #[allow(dead_code)]
    async fn copy_event(&mut self, from_log: &RedoLog, from_pointer: &LogFilePointer) -> Result<LogFilePointer> {
        Ok(self.log_file.copy_event(&from_log.log_file, from_pointer).await?)
    }
}

impl FlippedLogFile
{
    async fn copy_log_file(&mut self) -> Result<LogFile> {
        let new_log_file = self.log_file.copy().await?;
        Ok(new_log_file)
    }

    #[allow(dead_code)]
    fn count(&self) -> usize {
        self.log_file.count()
    }

    fn drain_events(&mut self) -> Vec<EventEntry>
    {
        let mut ret = Vec::new();
        for evt in self.event_summary.drain(..) {
            ret.push(evt);
        }
        ret
    }
}

struct RedoLogFlip {
    deferred: Vec<DeferredWrite>,
}

#[derive(Default)]
pub(crate) struct RedoLogLoader {
    entries: VecDeque<EventEntry>
}

impl RedoLogLoader {
    #[allow(dead_code)]
    pub(crate) fn pop(&mut self) -> Option<EventEntry> {
        self.entries.pop_front()   
    }
}

#[derive(Debug, Clone)]
pub(crate) struct LoadResult
{
    pub(crate) pointer: LogFilePointer,
    pub(crate) evt: EventData,
}

pub(crate) struct RedoLog {
    log_temp: bool,
    log_path: String,
    log_file: LogFile,
    flip: Option<RedoLogFlip>,
}

impl RedoLog
{
    async fn new(cfg: &Config, path_log: String, truncate: bool, cache_size: usize, cache_ttl: u64) -> Result<(RedoLog, RedoLogLoader)> {
        let mut ret = RedoLog {
            log_temp: cfg.log_temp,
            log_path: path_log.clone(),
            log_file: LogFile::new(cfg.log_temp, path_log.clone(), truncate, cache_size, cache_ttl).await?,
            flip: None,
        };

        let mut loader = RedoLogLoader::default();
        ret.log_file.read_all(&mut loader.entries).await?;

        Ok((ret, loader))
    }

    pub(crate) async fn begin_flip(&mut self) -> Result<FlippedLogFile> {
        
        match self.flip
        {
            None => {
                let path_flip = format!("{}.flip", self.log_path);

                let flip = {
                    let cache = self.log_file.cache.lock();
                    FlippedLogFile {
                        log_file: LogFile::new(
                            self.log_temp, 
                            path_flip, 
                            true, 
                            cache.read.cache_capacity().unwrap(), 
                            cache.read.cache_lifespan().unwrap()
                        ).await?,
                        event_summary: Vec::new(),
                    }
                };
                
                self.flip = Some(RedoLogFlip {
                    deferred: Vec::new(),
                });

                Ok(flip)
            },
            Some(_) => {
                Result::Err(Error::new(ErrorKind::Other, "Flip operation is already underway"))
            },
        }
    }

    pub(crate) async fn finish_flip(&mut self, mut flip: FlippedLogFile) -> Result<Vec<EventEntry>>
    {
        match &mut self.flip
        {
            Some(inside) =>
            {
                let mut event_summary = flip.drain_events();
                let mut new_log_file = flip.copy_log_file().await?;

                for d in inside.deferred.drain(..) {
                    let data_hash = match &d.data {
                        Some(data) => Some(super::crypto::Hash::from_bytes(&data[..])),
                        None => None,
                    };
                    let pointer = new_log_file.write(d.meta.clone(), d.data).await?;

                    let meta_hash = Hash::from_bytes(&d.meta[..]);
                    let summary = EventEntry {
                        meta_hash: meta_hash,
                        meta: d.meta,
                        data_hash: data_hash,
                        pointer: pointer,
                    };
                    event_summary.push(summary);
                }
                
                new_log_file.flush().await?;
                new_log_file.move_log_file(&self.log_path)?;

                self.log_file = new_log_file;
                self.flip = None;

                Ok(event_summary)
            },
            None =>
            {
                Result::Err(Error::new(ErrorKind::Other, "There is no outstanding flip operation to end."))
            }
        }
    }

    pub(crate) async fn load(&self, pointer: LogFilePointer) -> Result<LoadResult> {
        Ok(self.log_file.load(pointer).await?)
    }

    #[allow(dead_code)]
    pub(crate) fn count(&self) -> usize {
        self.log_file.count()
    }

    #[allow(dead_code)]
    pub(crate) async fn create(cfg: &Config, key: &ChainKey) -> Result<RedoLog> {
        let _ = std::fs::create_dir_all(cfg.log_path.clone());

        let path_log = format!("{}/{}.log", cfg.log_path, key.name);

        let (log, _) = RedoLog::new(
            cfg,
            path_log.clone(),
            true,
            cfg.load_cache_size,
            cfg.load_cache_ttl
        ).await?;

        Result::Ok(
            log
        )
    }

    #[allow(dead_code)]
    pub(crate) async fn open(cfg: &Config, key: &ChainKey, truncate: bool) -> Result<(RedoLog, RedoLogLoader)> {
        let _ = std::fs::create_dir_all(cfg.log_path.clone());

        let path_log = format!("{}/{}.log", cfg.log_path, key.name);

        let (log, loader) = RedoLog::new(
            cfg,
            path_log.clone(),
            truncate,
            cfg.load_cache_size,
            cfg.load_cache_ttl,
        ).await?;

        Result::Ok(
            (
                log,
                loader
            )
        )
    }

    #[allow(dead_code)]
    pub(crate) fn destroy(&mut self) -> Result<()> {
        self.log_file.destroy()
    }

    pub fn is_open(&self) -> bool {
        self.log_file.is_open()
    }
}

#[async_trait]
impl LogWritable for RedoLog
{
    async fn write(&mut self, meta: Bytes, body: Option<Bytes>) -> Result<LogFilePointer> {
        let pointer = self.log_file.write(meta.clone(), body.clone()).await?;   
        if let Some(flip) = &mut self.flip {
            flip.deferred.push(DeferredWrite::new(meta, body, pointer));
        }

        Ok(pointer)
    }

    async fn flush(&mut self) -> Result<()> {
        self.log_file.flush().await?;
        Ok(())
    }

    async fn copy_event(&mut self, from_log: &RedoLog, from_pointer: &LogFilePointer) -> Result<LogFilePointer> {
        Ok(self.log_file.copy_event(&from_log.log_file, from_pointer).await?)
    }
}

/* 
TESTS 
*/

#[cfg(test)]
async fn test_write_data(log: &mut dyn LogWritable, key: PrimaryKey, body: Option<Vec<u8>>, flush: bool) -> LogFilePointer
{
    let mut meta = Metadata::for_data(key);
    meta.core.push(CoreMetadata::Author("test@nowhere.com".to_string()));
    let meta_bytes = Bytes::from(rmps::to_vec(&meta).unwrap());

    // Write some data to the flipped buffer
    let mock_body = match body {
        Some(a) => Some(Bytes::from(a)),
        None => None,  
    };
    let ret = log.write(meta_bytes, mock_body)
        .await.expect("Failed to write the object");

    if flush == true {
        let _ = log.flush().await;
    }

    ret
}

#[cfg(test)]
async fn test_read_data(log: &mut RedoLog, read_header: LogFilePointer, test_key: PrimaryKey, test_body: Option<Vec<u8>>)
{
    let result = log.load(read_header.clone())
        .await
        .expect(&format!("Failed to read the entry {:?}", read_header));
    
    let mut meta = Metadata::for_data(test_key);
    meta.core.push(CoreMetadata::Author("test@nowhere.com".to_string()));
    let meta_bytes = Bytes::from(rmps::to_vec(&meta).unwrap());

    let test_body = match test_body {
        Some(a) => Some(Bytes::from(a)),
        None => None,  
    };

    assert_eq!(meta_bytes, result.evt.meta);
    assert_eq!(test_body, result.evt.data);
}

#[test]
fn test_redo_log() {
    let rt = Runtime::new().unwrap();

    let blah1 = PrimaryKey::generate();
    let blah2 = PrimaryKey::generate();
    let blah3 = PrimaryKey::generate();
    let blah4 = PrimaryKey::generate();
    let blah5 = PrimaryKey::generate();
    let blah6 = PrimaryKey::generate();
    let blah7 = PrimaryKey::generate();

    rt.block_on(async {
        let mut mock_cfg = mock_test_config();
        mock_cfg.log_temp = false;

        let mock_chain_key = ChainKey::default()
            .with_temp_name("test_redo".to_string());
            
        {
            // Open the log once for writing
            println!("test_redo_log - creating the redo log");
            let mut rl = RedoLog::create(&mock_cfg, &mock_chain_key).await.expect("Failed to load the redo log");
            
            // Test that its empty
            println!("test_redo_log - confirming no more data");
            assert_eq!(0, rl.count());

            // First test a simple case of a push and read
            println!("test_redo_log - writing test data to log - blah1");
            let halb1 = test_write_data(&mut rl, blah1, Some(vec![1; 10]), true).await;
            assert_eq!(1, rl.count());
            println!("test_redo_log - testing read result of blah1");
            test_read_data(&mut rl, halb1, blah1, Some(vec![1; 10])).await;

            // Now we push some data in to get ready for more tests
            println!("test_redo_log - writing test data to log - blah3");
            let halb2 = test_write_data(&mut rl, blah2, None, true).await;
            assert_eq!(2, rl.count());
            println!("test_redo_log - writing test data to log - blah3");
            let _ = test_write_data(&mut rl, blah3, Some(vec![3; 10]), true).await;
            assert_eq!(3, rl.count());

            // Begin an operation to flip the redo log
            println!("test_redo_log - beginning the flip operation");
            let mut flip = rl.begin_flip().await.unwrap();

            // Read the earlier pushed data
            println!("test_redo_log - testing read result of blah2");
            test_read_data(&mut rl, halb2, blah2, None).await;

            // Write some data to the redo log and the backing redo log
            println!("test_redo_log - writing test data to flip - blah1 (again)");
            let _ = test_write_data(&mut flip, blah1, Some(vec![10; 10]), true).await;
            assert_eq!(1, flip.count());
            assert_eq!(3, rl.count());
            #[allow(unused_variables)]
            let halb4 = test_write_data(&mut flip, blah4, Some(vec![4; 10]), true).await;
            assert_eq!(2, flip.count());
            assert_eq!(3, rl.count());
            println!("test_redo_log - writing test data to log - blah5");
            let halb5 = test_write_data(&mut rl, blah5, Some(vec![5; 10]), true).await;
            assert_eq!(4, rl.count());

            // The deferred writes do not take place until after the flip ends
            assert_eq!(2, flip.count());
            
            // End the flip operation
            println!("test_redo_log - finishing the flip operation");
            rl.finish_flip(flip).await.expect("Failed to end the flip operation");
            assert_eq!(3, rl.count());

            // Write some more data
            println!("test_redo_log - writing test data to log - blah6");
            let halb6 = test_write_data(&mut rl, blah6, Some(vec![6; 10]), false).await;
            assert_eq!(4, rl.count());

            // The old log file pointer should now be invalid
            println!("test_redo_log - make sure old pointers are now invalid");
            rl.load(halb5.clone()).await.expect_err("The old log file entry should not work anymore");

            // Attempt to read blah 6 before its flushed should result in an error
            rl.load(halb6.clone()).await.expect("The log file read should have worked now");

            println!("test_redo_log - closing redo log");
        }

        {
            // Open it up again which should check that it loads data properly
            println!("test_redo_log - reopening the redo log");
            let (mut rl, mut loader) = RedoLog::open(&mock_cfg, &mock_chain_key, false).await.expect("Failed to load the redo log");
            
            // Check that the correct data is read
            println!("test_redo_log - testing read result of blah1 (again)");
            test_read_data(&mut rl, loader.pop().unwrap().pointer, blah1, Some(vec![10; 10])).await;
            println!("test_redo_log - testing read result of blah4");
            test_read_data(&mut rl, loader.pop().unwrap().pointer, blah4, Some(vec![4; 10])).await;
            println!("test_redo_log - testing read result of blah5");
            test_read_data(&mut rl, loader.pop().unwrap().pointer, blah5, Some(vec![5; 10])).await;
            println!("test_redo_log - testing read result of blah6");
            test_read_data(&mut rl, loader.pop().unwrap().pointer, blah6, Some(vec![6; 10])).await;
            println!("test_redo_log - confirming no more data");
            assert_eq!(loader.pop().is_none(), true);

            // Write some data to the redo log and the backing redo log
            println!("test_redo_log - writing test data to log - blah7");
            let halb7 = test_write_data(&mut rl, blah7, Some(vec![7; 10]), true).await;
            assert_eq!(5, rl.count());
    
            // Read the test data again
            println!("test_redo_log - testing read result of blah7");
            test_read_data(&mut rl, halb7, blah7, Some(vec![7; 10])).await;
            println!("test_redo_log - confirming no more data");
            assert_eq!(5, rl.count());

            rl.destroy().unwrap();
        }
    });
}