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
/// Different reusable definitions


use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};


/// object identifier
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
pub struct ObjectId
{
    pub file_id:    u16,
    pub extent_id:  u16,
    pub block_id:   u16,
    pub entry_id:   u16,
}

impl ObjectId {

    pub fn new() -> Self {
        ObjectId {
            file_id:   0,
            extent_id: 0,
            block_id:  0,
            entry_id:  0,
        }
    }

    pub fn init(file_id: u16,
            extent_id: u16,
            block_id: u16,
            entry_id: u16) -> Self 
    {
        ObjectId {
            file_id,
            extent_id,
            block_id,
            entry_id,
        }
    }

    /// Calculate bucket for certain number of buckets.
    pub fn obj_bkt(&self, nbkt: usize) -> usize {
    	(self.block_id as usize + 
         self.extent_id as usize + 
         self.entry_id as usize + 
         self.file_id as usize) % nbkt
    }
}

/// block identifier
#[derive(Clone, Copy, Eq, Hash, PartialEq, Debug)]
pub struct BlockId
{
    pub file_id:    u16,
    pub extent_id:  u16,
    pub block_id:   u16,
}


impl BlockId {

    pub fn new() -> Self {
        BlockId {
            file_id: 0,
            extent_id: 0,
            block_id: 0,
        }
    }

    pub fn init(file_id: u16, extent_id: u16, block_id: u16) -> Self {
        BlockId {
            file_id,
            extent_id,
            block_id,
        }
    }

    pub fn from_obj(obj: &ObjectId) -> Self {
        BlockId {
            file_id: obj.file_id,
            extent_id: obj.extent_id,
            block_id: obj.block_id,
        }
    }

    pub fn hash(&self, n: usize) -> usize {
    	(self.block_id as usize +
         self.extent_id as usize +
         self.file_id as usize) % n
    }
}

/// Numberic sequence
#[derive(Clone)]
pub struct Sequence {
    sn: Arc<AtomicU64>,
}

impl Sequence {
    pub fn new(sn: u64) -> Self {
        Sequence {
            sn: Arc::new(AtomicU64::new(sn)),
        }
    }

    pub fn set(&self, sn: u64) {
        self.sn.store(sn, Ordering::Relaxed)
    }

    pub fn get_next(&self) -> u64 {
        self.sn.fetch_add(1, Ordering::Relaxed)
    }

    pub fn get_cur(&self) -> u64 {
        self.sn.load(Ordering::Relaxed)
    }
/*
    pub fn swap(&self, sn: u64) {
        let mut current = self.sn.load(Ordering::Relaxed);
        while  current < sn {
            current = self.sn.compare_and_swap(current, sn, Ordering::Relaxed);
        }
    }
*/
}

/// Change sequence numbers that often are shared gathered into one struct.
#[derive(Clone)]
pub struct SharedSequences {
    pub csn:                Sequence,
    pub latest_commit_csn:  Arc<AtomicU64>,
    pub checkpoint_csn:     Sequence,
}