db_core/common/
defs.rs

1//! Common definitions.
2
3
4use std::io::Write;
5use std::sync::Arc;
6use std::sync::atomic::{AtomicU64, Ordering};
7use crate::common::misc::SliceToIntConverter;
8
9
10/// Object identifier.
11#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
12pub struct ObjectId
13{
14    pub file_id:    u16,
15    pub extent_id:  u16,
16    pub block_id:   u16,
17    pub entry_id:   u16,
18}
19
20impl ObjectId {
21
22    pub fn new() -> Self {
23        ObjectId {
24            file_id:   0,
25            extent_id: 0,
26            block_id:  0,
27            entry_id:  0,
28        }
29    }
30
31    pub fn init(file_id: u16,
32            extent_id: u16,
33            block_id: u16,
34            entry_id: u16) -> Self 
35    {
36        ObjectId {
37            file_id,
38            extent_id,
39            block_id,
40            entry_id,
41        }
42    }
43
44    /// Calculate bucket for certain number of buckets.
45    pub fn obj_bkt(&self, nbkt: usize) -> usize {
46    	(self.block_id as usize + 
47         self.extent_id as usize + 
48         self.entry_id as usize + 
49         self.file_id as usize) % nbkt
50    }
51}
52
53/// Block identifier.
54#[derive(Clone, Copy, Eq, Hash, PartialEq, Debug)]
55pub struct BlockId
56{
57    pub file_id:    u16,
58    pub extent_id:  u16,
59    pub block_id:   u16,
60}
61
62
63impl BlockId {
64
65    pub fn new() -> Self {
66        BlockId {
67            file_id: 0,
68            extent_id: 0,
69            block_id: 0,
70        }
71    }
72
73    pub fn init(file_id: u16, extent_id: u16, block_id: u16) -> Self {
74        BlockId {
75            file_id,
76            extent_id,
77            block_id,
78        }
79    }
80
81    pub fn from_obj(obj: &ObjectId) -> Self {
82        BlockId {
83            file_id: obj.file_id,
84            extent_id: obj.extent_id,
85            block_id: obj.block_id,
86        }
87    }
88
89    pub fn hash(&self, n: usize) -> usize {
90    	(self.block_id as usize +
91         self.extent_id as usize +
92         self.file_id as usize) % n
93    }
94}
95
96/// Numeric sequence that can be shared by several threads.
97#[derive(Clone)]
98pub struct Sequence {
99    sn: Arc<AtomicU64>,
100}
101
102impl Sequence {
103    pub fn new(sn: u64) -> Self {
104        Sequence {
105            sn: Arc::new(AtomicU64::new(sn)),
106        }
107    }
108
109    pub fn set(&self, sn: u64) {
110        self.sn.store(sn, Ordering::Relaxed)
111    }
112
113    pub fn get_next(&self) -> u64 {
114        self.sn.fetch_add(1, Ordering::Relaxed) + 1
115    }
116
117    pub fn get_cur(&self) -> u64 {
118        self.sn.load(Ordering::Relaxed)
119    }
120/*
121    pub fn swap(&self, sn: u64) {
122        let mut current = self.sn.load(Ordering::Relaxed);
123        while  current < sn {
124            current = self.sn.compare_and_swap(current, sn, Ordering::Relaxed);
125        }
126    }
127*/
128}
129
130/// Change sequence numbers that often are shared gathered into one struct.
131#[derive(Clone)]
132pub struct SharedSequences {
133    pub csn:                Sequence,
134    pub latest_commit_csn:  Arc<AtomicU64>,
135    pub checkpoint_csn:     Sequence,
136}
137
138
139
140pub const VECTOR_DATA_LENGTH: usize = 10;
141
142
143/// Vector (data pointer). 
144#[derive(Clone, Copy, Debug)]
145pub struct Vector {
146    obj:        ObjectId,
147    entry_pos:  u16,
148    data:       [u8;VECTOR_DATA_LENGTH],
149}
150
151
152impl Vector {
153    pub fn new() -> Self {
154        Vector {
155            obj: ObjectId::new(),
156            entry_pos: 0,
157            data: [0;VECTOR_DATA_LENGTH],
158        }
159    }
160
161    pub fn init(block_id: BlockId, entry_id: u16, entry_pos: u16) -> Self {
162        let obj = ObjectId::init(block_id.file_id, block_id.extent_id, block_id.block_id, entry_id);
163        Vector {
164            obj,
165            entry_pos,
166            data: [0;VECTOR_DATA_LENGTH],
167        }
168    }
169
170    pub fn update_from_buf(&mut self) {
171        self.obj.file_id = u16::slice_to_int(&self.data[0..2]).unwrap();
172        self.obj.extent_id = u16::slice_to_int(&self.data[2..4]).unwrap();
173        self.obj.block_id = u16::slice_to_int(&self.data[4..6]).unwrap();
174        self.obj.entry_id = u16::slice_to_int(&self.data[6..8]).unwrap();
175        self.entry_pos = u16::slice_to_int(&self.data[8..10]).unwrap();
176    }
177
178    pub fn to_data(&mut self) -> &[u8] {
179        let mut slice = &mut self.data[0..VECTOR_DATA_LENGTH];
180        slice.write_all(&self.obj.file_id.to_ne_bytes()).unwrap();
181        slice.write_all(&self.obj.extent_id.to_ne_bytes()).unwrap();
182        slice.write_all(&self.obj.block_id.to_ne_bytes()).unwrap();
183        slice.write_all(&self.obj.entry_id.to_ne_bytes()).unwrap();
184        slice.write_all(&self.entry_pos.to_ne_bytes()).unwrap();
185        slice.flush().unwrap();
186        &self.data
187    }
188
189    pub fn buf_mut(&mut self) -> &mut [u8] {
190        &mut self.data
191    }
192
193    pub fn buf(&self) -> &[u8] {
194        &self.data
195    }
196
197    pub fn obj_id(&self) -> ObjectId {
198        self.obj
199    }
200
201    pub fn entry_pos(&self) -> u16 {
202        self.entry_pos
203    } 
204}
205
206/// Seek postition.
207pub enum SeekFrom {
208    Start,
209    Current,
210}
211