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
use std::collections::BTreeMap;

/// Value datatypes used in the database
#[derive(Debug)]
pub enum ValueType {
    Nothing,
    Integer,
    Float,
    Text,
    Boolean,
    BigInt,
    VarChar,
    Unknown(u32),
}

impl From<ValueType> for u32 {
    fn from(value_type: ValueType) -> u32 {
        match value_type {
            ValueType::Nothing => 0,
            ValueType::Integer => 1,
            ValueType::Float => 3,
            ValueType::Text => 4,
            ValueType::Boolean => 5,
            ValueType::BigInt => 6,
            ValueType::VarChar => 8,
            ValueType::Unknown(key) => key,
        }
    }
}

impl From<u32> for ValueType {
    fn from(value_type: u32) -> ValueType {
        match value_type {
            0 => ValueType::Nothing,
            1 => ValueType::Integer,
            3 => ValueType::Float,
            4 => ValueType::Text,
            5 => ValueType::Boolean,
            6 => ValueType::BigInt,
            8 => ValueType::VarChar,
            k => ValueType::Unknown(k),
        }
    }
}

/// A database single field
#[derive(Debug)]
pub enum Field {
    Nothing,
    Integer(i32),
    Float(f32),
    Text(String),
    Boolean(bool),
    BigInt(i64),
    VarChar(String),
}

impl From<Field> for ValueType {
    fn from(val: Field) -> Self {
        match val {
            Field::Nothing => ValueType::Nothing,
            Field::Integer(_) => ValueType::Integer,
            Field::Float(_) => ValueType::Float,
            Field::Text(_) => ValueType::Text,
            Field::Boolean(_) => ValueType::Boolean,
            Field::BigInt(_) => ValueType::BigInt,
            Field::VarChar(_) => ValueType::VarChar,
        }
    }
}

/// A sequence of fields
#[derive(Debug)]
pub struct Row(Vec<Field>);

impl From<Vec<Field>> for Row {
    fn from(fields: Vec<Field>) -> Self {
        Row(fields)
    }
}

impl Row {
    #[allow(dead_code)]
    pub fn new() -> Row {
        Row(Vec::new())
    }

    pub fn fields(self) -> Vec<Field> {
        self.0
    }
}

/// A container of rows with the same hash value
pub struct Bucket(pub Vec<Row>);

impl Bucket {
    pub fn new() -> Bucket {
        Bucket(Vec::new())
    }

    pub fn rows(self) -> Vec<Row> {
        self.0
    }
}

/// Name and default type for one field in each row
#[derive(Debug)]
pub struct Column {
    name: String,
    field_type: ValueType,
}

impl From<(&str, ValueType)> for Column {
    fn from(data: (&str, ValueType)) -> Self {
        Column { name: String::from(data.0), field_type: data.1 }
    }
}

/// A list of buckets and thus collection of rows with a name
#[allow(dead_code)]
pub struct Table {
    name: String,
    buckets: Vec<Bucket>,
    columns: Vec<Column>,
}

impl Table {
    pub fn new(name: String, buckets: Vec<Bucket>, columns: Vec<Column>) -> Self {
        Table {
            name: name,
            buckets: buckets,
            columns: columns,
        }
    }

    pub fn buckets(self) -> Vec<Bucket> {
        self.buckets
    }

    pub fn columns(self) -> Vec<Column> {
        self.columns
    }

    pub fn name(self) -> String {
        self.name
    }
}

/// A collection of tables
pub struct Schema {
    tables: BTreeMap<String, Table>,
}

impl Schema {
    pub fn new() -> Schema {
        Schema {
            tables: BTreeMap::new(),
        }
    }

    pub fn table(&self, name: &str) -> Option<&Table> {
        self.tables.get(name)
    }
}

impl From<Vec<Table>> for Schema {
    fn from(tables: Vec<Table>) -> Self {
        let mut tree = BTreeMap::new();
        for table in tables {
            tree.insert(table.name.clone(), table);
        }
        Schema { tables: tree }
    }
}