assembly_data/fdb/core/
mod.rs1pub mod iter;
20
21use std::collections::BTreeMap;
22use std::fmt;
23
24use super::{
25 common::{Context, Value, ValueType},
26 mem::Field as MemField,
27};
28
29#[derive(Debug, PartialEq, Eq)]
31pub struct OwnedContext;
32
33impl Context for OwnedContext {
34 type String = String;
35 type I64 = i64;
36 type XML = String;
37}
38
39pub type Field = Value<OwnedContext>;
41
42impl From<MemField<'_>> for Field {
43 fn from(src: MemField<'_>) -> Self {
44 match src {
45 MemField::Nothing => Field::Nothing,
46 MemField::Integer(v) => Field::Integer(v),
47 MemField::Float(v) => Field::Float(v),
48 MemField::Text(v) => Field::Text(v.decode().into_owned()),
49 MemField::Boolean(v) => Field::Boolean(v),
50 MemField::BigInt(v) => Field::BigInt(v),
51 MemField::VarChar(v) => Field::VarChar(v.decode().into_owned()),
52 }
53 }
54}
55
56impl PartialEq<MemField<'_>> for Field {
57 fn eq(&self, other: &MemField<'_>) -> bool {
58 match other {
59 Value::Nothing => matches!(self, Self::Nothing),
60 Value::Integer(x) => matches!(self, Self::Integer(y) if x == y),
61 Value::Float(x) => matches!(self, Self::Float(y) if x == y),
62 Value::Text(x) => matches!(self, Self::Text(y) if x.decode().as_ref() == y),
63 Value::Boolean(x) => matches!(self, Self::Boolean(y) if x == y),
64 Value::BigInt(x) => matches!(self, Self::BigInt(y) if x == y),
65 Value::VarChar(x) => matches!(self, Self::VarChar(y) if x.decode().as_ref() == y),
66 }
67 }
68}
69
70impl fmt::Display for Field {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 match self {
73 Field::Nothing => write!(f, "NULL"),
74 Field::Integer(i) => write!(f, "{}", i),
75 Field::Float(v) => write!(f, "{}", v),
76 Field::Text(t) => write!(f, "{:?}", t),
77 Field::Boolean(b) => write!(f, "{}", b),
78 Field::BigInt(i) => write!(f, "{}", i),
79 Field::VarChar(v) => write!(f, "{:?}", v),
80 }
81 }
82}
83
84#[derive(Debug, Default)]
86pub struct Row(Vec<Field>);
87
88impl From<Vec<Field>> for Row {
89 fn from(fields: Vec<Field>) -> Self {
90 Row(fields)
91 }
92}
93
94impl Row {
95 pub fn new() -> Row {
97 Row(Vec::new())
98 }
99
100 pub fn into_fields(self) -> Vec<Field> {
102 self.0
103 }
104
105 pub fn fields(&self) -> &Vec<Field> {
107 &self.0
108 }
109
110 pub fn fields_mut(&mut self) -> &mut Vec<Field> {
112 &mut self.0
113 }
114}
115
116#[derive(Debug, Default)]
118pub struct Bucket(pub Vec<Row>);
119
120impl Bucket {
121 pub fn new() -> Bucket {
123 Bucket(Vec::new())
124 }
125
126 pub fn rows(self) -> Vec<Row> {
128 self.0
129 }
130
131 pub fn rows_ref(&self) -> &Vec<Row> {
133 &self.0
134 }
135
136 pub fn rows_mut(&mut self) -> &mut Vec<Row> {
138 &mut self.0
139 }
140}
141
142#[derive(Debug)]
144pub struct Column {
145 pub name: String,
147 pub field_type: ValueType,
149}
150
151impl From<(&str, ValueType)> for Column {
152 fn from(data: (&str, ValueType)) -> Self {
153 Column {
154 name: String::from(data.0),
155 field_type: data.1,
156 }
157 }
158}
159
160#[derive(Debug)]
162pub struct TableDef {
163 pub columns: Vec<Column>,
165 pub name: String,
167}
168
169#[derive(Debug, Default)]
171pub struct TableData {
172 pub buckets: Vec<Bucket>,
174}
175
176impl TableData {
177 pub fn new() -> Self {
179 TableData {
180 buckets: Vec::new(),
181 }
182 }
183}
184
185#[derive(Debug)]
187pub struct Table {
188 definition: TableDef,
189 data: TableData,
190}
191
192impl Table {
193 pub fn from(definition: TableDef, data: TableData) -> Self {
195 Table { definition, data }
196 }
197
198 pub fn new(definition: TableDef) -> Self {
200 let data = TableData::new();
201 Table { definition, data }
202 }
203
204 pub fn into_buckets(self) -> Vec<Bucket> {
206 self.data.buckets
207 }
208
209 pub fn buckets(&self) -> &[Bucket] {
211 &self.data.buckets
212 }
213
214 pub fn buckets_mut(&mut self) -> &mut Vec<Bucket> {
216 &mut self.data.buckets
217 }
218
219 pub fn into_columns(self) -> Vec<Column> {
221 self.definition.columns
222 }
223
224 pub fn columns(&self) -> &[Column] {
226 &self.definition.columns
227 }
228
229 pub fn columns_mut(&mut self) -> &mut Vec<Column> {
231 &mut self.definition.columns
232 }
233
234 pub fn name(&self) -> &str {
236 self.definition.name.as_ref()
237 }
238}
239
240#[derive(Debug, Default)]
245pub struct Schema {
246 pub tables: BTreeMap<String, Table>,
248}
249
250impl Schema {
251 pub fn new() -> Schema {
253 Schema {
254 tables: BTreeMap::new(),
255 }
256 }
257
258 pub fn table(&self, name: &str) -> Option<&Table> {
260 self.tables.get(name)
261 }
262
263 pub fn table_mut(&mut self, name: &str) -> Option<&mut Table> {
265 self.tables.get_mut(name)
266 }
267
268 pub fn table_count(&self) -> usize {
270 self.tables.len()
271 }
272}
273
274impl From<Vec<Table>> for Schema {
275 fn from(tables: Vec<Table>) -> Self {
276 let mut tree = BTreeMap::new();
277 for table in tables {
278 tree.insert(table.name().to_string(), table);
279 }
280 Schema { tables: tree }
281 }
282}