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
#![allow(clippy::cast_precision_loss)]

use std::cmp::max;
use std::ffi::{c_int, c_uint, c_ulong, CStr};
use std::marker::PhantomData;
use std::path::Path;
use std::{mem, ptr};

use super::{Handlerton, StorageError, StorageResult, MAX_RECORD_LENGTH};
use crate::sql::{MAX_DATA_LENGTH_FOR_KEY, MAX_REFERENCE_PARTS};
use crate::{bindings, MemRoot, TableShare};

pub const IO_SIZE: usize = bindings::IO_SIZE as usize;

#[derive(Clone, Default)]
pub struct TableFlags(bindings::handler_Table_flags);
#[derive(Clone, Default)]
pub struct IndexFlags(c_ulong);
#[derive(Debug)]
pub struct IoAndCpuCost(bindings::IO_AND_CPU_COST);

impl IoAndCpuCost {
    pub fn new(io: f64, cpu: f64) -> Self {
        Self(bindings::IO_AND_CPU_COST { io, cpu })
    }

    pub fn io(&self) -> f64 {
        self.0.io
    }

    pub fn cpu(&self) -> f64 {
        self.0.cpu
    }
}

#[repr(transparent)]
pub struct CreateInfo<'a> {
    inner: bindings::HA_CREATE_INFO,
    phantom: PhantomData<&'a ()>,
}

impl<'a> CreateInfo<'a> {
    pub(crate) unsafe fn from_raw(ptr: *const bindings::HA_CREATE_INFO) -> &'a Self {
        unsafe { &*ptr.cast() }
    }
}

#[repr(transparent)]
pub struct HandlerCtx<'a> {
    inner: bindings::handler,
    phantom: PhantomData<&'a ()>,
}

impl<'a> HandlerCtx<'a> {
    fn stats(&self) -> &'a Statistics {
        unsafe { *ptr::addr_of!(self.inner.stats).cast() }
    }
    fn costs(&self) -> &'a OptimizerCosts {
        unsafe { *ptr::addr_of!(self.inner.costs).cast() }
    }
}

#[repr(transparent)]
pub struct Statistics(bindings::ha_statistics);

impl Statistics {
    pub fn data_file_length(&self) -> usize {
        self.0.data_file_length.try_into().unwrap()
    }
    pub fn max_data_file_length(&self) -> usize {
        self.0.max_data_file_length.try_into().unwrap()
    }
    pub fn index_file_length(&self) -> usize {
        self.0.index_file_length.try_into().unwrap()
    }
    pub fn max_index_file_length(&self) -> usize {
        self.0.max_index_file_length.try_into().unwrap()
    }
    pub fn delete_length(&self) -> usize {
        self.0.delete_length.try_into().unwrap()
    }
    pub fn auto_increment_value(&self) -> usize {
        self.0.auto_increment_value.try_into().unwrap()
    }
    pub fn records(&self) -> usize {
        self.0.records.try_into().unwrap()
    }
    pub fn deleted(&self) -> usize {
        self.0.deleted.try_into().unwrap()
    }
    pub fn mean_rec_length(&self) -> usize {
        self.0.mean_rec_length.try_into().unwrap()
    }
    pub fn block_size(&self) -> usize {
        self.0.block_size.try_into().unwrap()
    }
    pub fn checksum(&self) -> u32 {
        self.0.checksum
    }
}

#[repr(transparent)]
pub struct OptimizerCosts(bindings::OPTIMIZER_COSTS);

impl OptimizerCosts {
    fn index_block_copy_cost(&self) -> f64 {
        self.0.index_block_copy_cost
    }
}

#[repr(transparent)]
pub struct Mode(c_int);
#[repr(transparent)]
pub struct OpenOp(c_uint);

pub trait Handler: 'static {
    type Handlerton: Handlerton;

    /// Set this to true if index support is available. If so, this type must
    /// also implement `IndexHandler`.
    const SUPPORTS_INDEX: bool = false;

    // const TABLE_FLAGS: TableFlags = TableFlags(0);
    const MAX_SUPPORTED_RECORD_LENGTH: usize = bindings::HA_MAX_REC_LENGTH as usize;

    /// Create a new handler
    ///
    /// # When is this called?
    ///
    /// - Every time there is a new connection
    fn new(table: &TableShare, mem_root: MemRoot) -> Self;

    /// Open a table, the name will be the name of the file.
    ///
    /// Closing happens by dropping this item.
    ///
    /// # When is this called?
    ///
    /// Not for every request, more to come...
    // TODO: figure out the interaction with ::extra in the C docs, maybe refactor
    fn open(name: &Path, mode: Mode, open_options: OpenOp) -> StorageResult;

    /// Create a new table and exit
    ///
    /// This should
    ///
    /// # When is this called?
    ///
    /// - SQL `CREATE TABLE` statements
    fn create(&self, name: &CStr, form: TableShare, create_info: &CreateInfo) {}

    fn table_flags(&self) -> TableFlags {
        TableFlags::default()
    }

    /// This is an INSERT statement
    fn write_row(buf: &CStr) {}

    fn update_row() {}

    fn delete_row() {}

    /// Delete all rows
    ///
    /// ## When is this called?
    ///
    /// - SQL `DELETE` with no `WHERE` clause
    fn delete_all_rows() {}

    fn store_lock(&self) {}

    /// Time for a full table data scan
    fn scan_time(&self, ctx: HandlerCtx) -> IoAndCpuCost {
        // Duplicated from handler.h
        let length = ctx.stats().data_file_length() as f64;
        let io = length / (IO_SIZE as f64);
        let bsize = ctx.stats().block_size() as f64;
        let cpu = ((length + bsize - 1.0) / bsize) + ctx.costs().index_block_copy_cost();
        let cpu = cpu.clamp(0.0, 1e200);
        IoAndCpuCost::new(io, cpu)
    }

    /// Cost of fetching `rows` records through `rnd_pos`.
    fn rnd_pos_time(&self, ctx: HandlerCtx, rows: usize) -> IoAndCpuCost {
        let r = rows as f64;
        let io = ((ctx.stats().block_size() + IO_SIZE).saturating_sub(1) as f64) / (IO_SIZE as f64);
        let cpu = r + ctx.costs().index_block_copy_cost();

        IoAndCpuCost::new(io, cpu)
    }
}

pub trait RepairingHandler: Handler {
    fn pre_calculate_checksum(&self) -> u32 {
        0
    }

    fn calculate_checksum(&self) -> u32 {
        0
    }

    fn is_crashed(&self) -> bool {
        false
    }

    fn auto_repair(error: i32) -> bool {
        false
    }
}

pub trait IndexableHandler: Handler {
    fn max_supported_record_length(&self) -> usize {
        MAX_RECORD_LENGTH
    }
    fn max_supported_keys(&self) -> usize {
        0
    }

    fn max_supported_key_parts(&self) -> usize {
        MAX_REFERENCE_PARTS
    }

    fn max_supported_key_length(&self) -> usize {
        MAX_DATA_LENGTH_FOR_KEY
    }

    /// Return the display name of the index, if any
    fn index_type(&self, index_number: usize) -> Option<&'static CStr> {
        None
    }

    fn index_flags(&self, index: usize, part: usize, all_parts: bool) -> IndexFlags {
        IndexFlags::default()
    }

    /// Calculate the cost of `index_only` scan for a given index, a number of ranges,
    /// and a number of records.
    ///
    /// - `index`: the index to read
    /// - `rows`: number of records to read
    /// - `blocks`: number of IO blocks that need to be accessed, or 0 if not known.
    fn keyread_time(
        &self,
        ctx: &HandlerCtx,
        index: usize,
        ranges: usize,
        rows: usize,
        blocks: usize,
    ) -> IoAndCpuCost {
        todo!()
    }

    /// Time for a full table index scan without copy or compare cost.
    fn key_scan_time(&self, ctx: &HandlerCtx, index: usize, rows: usize) -> IoAndCpuCost {
        Self::keyread_time(self, ctx, index, 1, max(rows, 1), 0)
    }

    fn index_read_map() {}
    fn index_next() {}
    fn index_prev() {}
    fn index_first() {}
    fn index_last() {}
}

pub trait InplaceAlterTable {}

// fn foo<T>(a: Box<dyn Handler<Handlerton = T>>) {
//     todo!()
// }
// fn bar<T>(a: Box<dyn IndexHandler<Handlerton = T>>) {
// todo!()
// }