-
Notifications
You must be signed in to change notification settings - Fork 26
/
descriptors.rs
599 lines (546 loc) · 19.6 KB
/
descriptors.rs
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
use std::convert::{TryFrom, TryInto};
use std::sync::Arc;
use std::collections::{BTreeMap, BTreeSet};
use bytes::{Buf, BufMut, BytesMut};
use edgedb_errors::{Error, ErrorKind, DescriptorMismatch, ClientEncodingError};
use snafu::{ensure, OptionExt};
use uuid::Uuid;
use crate::codec::{Codec, build_codec};
use crate::common::{Cardinality, State};
use crate::encoding::{Decode, Input};
use crate::errors::{InvalidTypeDescriptor, UnexpectedTypePos};
use crate::errors::{self, DecodeError, CodecError};
use crate::features::ProtocolVersion;
use crate::query_arg::{self, QueryArg, Encoder};
use crate::queryable;
use crate::value::Value;
pub use crate::common::RawTypedesc;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct TypePos(pub u16);
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Descriptor {
Set(SetDescriptor),
ObjectShape(ObjectShapeDescriptor),
BaseScalar(BaseScalarTypeDescriptor),
Scalar(ScalarTypeDescriptor),
Tuple(TupleTypeDescriptor),
NamedTuple(NamedTupleTypeDescriptor),
Array(ArrayTypeDescriptor),
Range(RangeTypeDescriptor),
Enumeration(EnumerationTypeDescriptor),
InputShape(InputShapeTypeDescriptor),
TypeAnnotation(TypeAnnotationDescriptor),
}
#[derive(Debug)]
pub struct Typedesc {
pub(crate) proto: ProtocolVersion,
pub(crate) array: Vec<Descriptor>,
pub(crate) root_id: Uuid,
pub(crate) root_pos: Option<TypePos>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SetDescriptor {
pub id: Uuid,
pub type_pos: TypePos,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ObjectShapeDescriptor {
pub id: Uuid,
pub elements: Vec<ShapeElement>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InputShapeTypeDescriptor {
pub id: Uuid,
pub elements: Vec<ShapeElement>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ShapeElement {
pub flag_implicit: bool,
pub flag_link_property: bool,
pub flag_link: bool,
pub cardinality: Option<Cardinality>,
pub name: String,
pub type_pos: TypePos,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BaseScalarTypeDescriptor {
pub id: Uuid,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScalarTypeDescriptor {
pub id: Uuid,
pub base_type_pos: TypePos,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TupleTypeDescriptor {
pub id: Uuid,
pub element_types: Vec<TypePos>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NamedTupleTypeDescriptor {
pub id: Uuid,
pub elements: Vec<TupleElement>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TupleElement {
pub name: String,
pub type_pos: TypePos,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ArrayTypeDescriptor {
pub id: Uuid,
pub type_pos: TypePos,
pub dimensions: Vec<Option<u32>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RangeTypeDescriptor {
pub id: Uuid,
pub type_pos: TypePos,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EnumerationTypeDescriptor {
pub id: Uuid,
pub members: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TypeAnnotationDescriptor {
pub annotated_type: u8,
pub id: Uuid,
pub annotation: String,
}
pub struct StateBorrow<'a> {
pub module: &'a Option<String>,
pub aliases: &'a BTreeMap<String, String>,
pub config: &'a BTreeMap<String, Value>,
pub globals: &'a BTreeMap<String, Value>,
}
impl Typedesc {
pub fn id(&self) -> &Uuid {
&self.root_id
}
pub fn descriptors(&self) -> &[Descriptor] {
&self.array
}
pub fn root_pos(&self) -> Option<TypePos> {
self.root_pos
}
pub fn build_codec(&self) -> Result<Arc<dyn Codec>, CodecError> {
build_codec(self.root_pos(), self.descriptors())
}
pub fn get(&self, type_pos: TypePos) -> Result<&Descriptor, CodecError> {
self.array.get(type_pos.0 as usize)
.context(UnexpectedTypePos { position: type_pos.0 })
}
pub fn is_empty_tuple(&self) -> bool {
match self.root() {
Some(Descriptor::Tuple(t))
=> t.id == Uuid::from_u128(0xFF) && t.element_types.is_empty(),
_ => false,
}
}
pub fn root(&self) -> Option<&Descriptor> {
self.root_pos.and_then(|pos| self.array.get(pos.0 as usize))
}
pub(crate) fn decode_with_id(root_id: Uuid, buf: &mut Input)
-> Result<Self, DecodeError>
{
let mut descriptors = Vec::new();
while buf.remaining() > 0 {
match Descriptor::decode(buf)? {
Descriptor::TypeAnnotation(_) => {}
item => descriptors.push(item),
}
}
let root_pos = if root_id == Uuid::from_u128(0) {
None
} else {
let idx = descriptors.iter().position(|x| *x.id() == root_id)
.context(errors::UuidNotFound { uuid: root_id })?;
let pos = idx.try_into().ok()
.context(errors::TooManyDescriptors { index: idx })?;
Some(TypePos(pos))
};
Ok(Typedesc {
proto: buf.proto().clone(),
array: descriptors,
root_id: root_id.clone(),
root_pos,
})
}
pub fn as_query_arg_context(&self) -> query_arg::DescriptorContext {
query_arg::DescriptorContext {
proto: &self.proto,
descriptors: self.descriptors(),
root_pos: self.root_pos,
}
}
pub fn as_queryable_context(&self) -> queryable::DescriptorContext {
let mut ctx = queryable::DescriptorContext::new(self.descriptors());
ctx.has_implicit_id = self.proto.has_implicit_id();
ctx.has_implicit_tid = self.proto.has_implicit_tid();
ctx
}
pub fn serialize_state(&self, state: &StateBorrow)
-> Result<State, Error>
{
#[derive(Debug)]
struct Indices {
module: (u32, TypePos),
aliases: (u32, TypePos),
config: (u32, TypePos),
globals: (u32, TypePos),
}
let mut buf = BytesMut::with_capacity(128);
let ctx = self.as_query_arg_context();
let mut enc = Encoder::new(&ctx, &mut buf);
let root = enc.ctx.root_pos
.ok_or_else(|| DescriptorMismatch::with_message(
"invalid state descriptor"))
.and_then(|p| enc.ctx.get(p))?;
let indices = match root {
Descriptor::InputShape(desc) => {
let mut module = None;
let mut aliases = None;
let mut config = None;
let mut globals = None;
for (i, elem) in desc.elements.iter().enumerate() {
let i = i as u32;
match &elem.name[..] {
"module" => module = Some((i, elem.type_pos)),
"aliases" => aliases = Some((i, elem.type_pos)),
"config" => config = Some((i, elem.type_pos)),
"globals" => globals = Some((i, elem.type_pos)),
_ => {}
}
}
Indices {
module: module.ok_or_else(|| {
DescriptorMismatch::with_message(
"no `module` field in state")
})?,
aliases: aliases.ok_or_else(|| {
DescriptorMismatch::with_message(
"no `aliases` field in state")
})?,
config: config.ok_or_else(|| {
DescriptorMismatch::with_message(
"no `config` field in state")
})?,
globals: globals.ok_or_else(|| {
DescriptorMismatch::with_message(
"no `globals` field in state")
})?,
}
}
_ => return Err(DescriptorMismatch::with_message(
"invalid state descriptor")),
};
enc.buf.reserve(4 + 8*4);
enc.buf.put_u32(4);
let module = state.module.as_deref().unwrap_or("default");
module.check_descriptor(enc.ctx, indices.module.1)?;
enc.buf.reserve(8);
enc.buf.put_u32(indices.module.0);
module.encode_slot(&mut enc)?;
match enc.ctx.get(indices.aliases.1)? {
Descriptor::Array(arr) => match enc.ctx.get(arr.type_pos)? {
Descriptor::Tuple(tup) => {
if tup.element_types.len() != 2 {
return Err(DescriptorMismatch::with_message(
"invalid type descriptor for aliases"));
}
"".check_descriptor(enc.ctx, tup.element_types[0])?;
"".check_descriptor(enc.ctx, tup.element_types[1])?;
}
_ => {
return Err(DescriptorMismatch::with_message(
"invalid type descriptor for aliases"));
}
},
_ => {
return Err(DescriptorMismatch::with_message(
"invalid type descriptor for aliases"));
}
}
enc.buf.reserve(4 + 16 + state.aliases.len()*(4+(8+4)*2));
enc.buf.put_u32(indices.aliases.0);
enc.length_prefixed(|enc| {
enc.buf.put_u32(state.aliases.len().try_into()
.map_err(|_| ClientEncodingError::with_message(
"too many aliases"))?);
for (key, value) in state.aliases {
enc.length_prefixed(|enc| {
enc.buf.reserve(4 + (8+4)*2);
enc.buf.put_u32(2);
enc.buf.put_u32(0); // reserved
key.encode_slot(enc)?;
value.encode_slot(enc)?;
Ok(())
})?;
}
Ok(())
})?;
enc.buf.reserve(4);
enc.buf.put_u32(indices.config.0);
enc.length_prefixed(|enc| {
serialize_variables(enc, state.config, indices.config.1, "config")
})?;
enc.buf.reserve(4);
enc.buf.put_u32(indices.globals.0);
enc.length_prefixed(|enc| {
serialize_variables(enc,
state.globals, indices.globals.1, "globals")
})?;
let data = buf.freeze();
Ok(State {
typedesc_id: self.root_id.clone(),
data,
})
}
pub fn proto(&self) -> &ProtocolVersion {
&self.proto
}
}
fn serialize_variables(enc: &mut Encoder, variables: &BTreeMap<String, Value>,
type_pos: TypePos, tag: &str)
-> Result<(), Error>
{
enc.buf.reserve(4 + variables.len()*(4 + 4));
enc.buf.put_u32(variables.len().try_into()
.map_err(|_| ClientEncodingError::with_message(
format!("too many items in {}", tag)))?);
let desc = match enc.ctx.get(type_pos)? {
Descriptor::InputShape(desc) => desc,
_ => {
return Err(DescriptorMismatch::with_message(
format!("invalid type descriptor for {}", tag)));
}
};
let mut serialized = 0;
for (idx, el) in desc.elements.iter().enumerate() {
if let Some(value) = variables.get(&el.name) {
value.check_descriptor(&enc.ctx, el.type_pos)?;
serialized += 1;
enc.buf.reserve(8);
enc.buf.put_u32(idx as u32);
value.encode_slot(enc)?;
}
}
if serialized != variables.len() {
let mut extra_vars = variables.keys().collect::<BTreeSet<_>>();
for el in &desc.elements {
extra_vars.remove(&el.name);
}
return Err(ClientEncodingError::with_message(format!(
"non-existing entries {} of {}",
extra_vars.into_iter().map(|x| &x[..]).collect::<Vec<_>>().join(", "),
tag)));
}
Ok(())
}
impl Descriptor {
pub fn id(&self) -> &Uuid {
use Descriptor::*;
match self {
Set(i) => &i.id,
ObjectShape(i) => &i.id,
BaseScalar(i) => &i.id,
Scalar(i) => &i.id,
Tuple(i) => &i.id,
NamedTuple(i) => &i.id,
Array(i) => &i.id,
Range(i) => &i.id,
Enumeration(i) => &i.id,
InputShape(i) => &i.id,
TypeAnnotation(i) => &i.id,
}
}
pub fn decode(buf: &mut Input) -> Result<Descriptor, DecodeError> {
<Descriptor as Decode>::decode(buf)
}
}
impl Decode for Descriptor {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
use Descriptor as D;
ensure!(buf.remaining() >= 1, errors::Underflow);
match buf.chunk()[0] {
0 => SetDescriptor::decode(buf).map(D::Set),
1 => ObjectShapeDescriptor::decode(buf).map(D::ObjectShape),
2 => BaseScalarTypeDescriptor::decode(buf).map(D::BaseScalar),
3 => ScalarTypeDescriptor::decode(buf).map(D::Scalar),
4 => TupleTypeDescriptor::decode(buf).map(D::Tuple),
5 => NamedTupleTypeDescriptor::decode(buf).map(D::NamedTuple),
6 => ArrayTypeDescriptor::decode(buf).map(D::Array),
7 => EnumerationTypeDescriptor::decode(buf).map(D::Enumeration),
8 => InputShapeTypeDescriptor::decode(buf).map(D::InputShape),
9 => RangeTypeDescriptor::decode(buf).map(D::Range),
0x7F..=0xFF => {
TypeAnnotationDescriptor::decode(buf).map(D::TypeAnnotation)
}
descriptor => InvalidTypeDescriptor { descriptor }.fail()?
}
}
}
impl Decode for SetDescriptor {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 19, errors::Underflow);
assert!(buf.get_u8() == 0);
let id = Uuid::decode(buf)?;
let type_pos = TypePos(buf.get_u16());
Ok(SetDescriptor { id, type_pos })
}
}
impl Decode for ObjectShapeDescriptor {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 19, errors::Underflow);
assert!(buf.get_u8() == 1);
let id = Uuid::decode(buf)?;
let element_count = buf.get_u16();
let mut elements = Vec::with_capacity(element_count as usize);
for _ in 0..element_count {
elements.push(ShapeElement::decode(buf)?);
}
Ok(ObjectShapeDescriptor { id, elements })
}
}
impl Decode for InputShapeTypeDescriptor {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 19, errors::Underflow);
assert!(buf.get_u8() == 8);
let id = Uuid::decode(buf)?;
let element_count = buf.get_u16();
let mut elements = Vec::with_capacity(element_count as usize);
for _ in 0..element_count {
elements.push(ShapeElement::decode(buf)?);
}
Ok(InputShapeTypeDescriptor { id, elements })
}
}
impl Decode for ShapeElement {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 7, errors::Underflow);
let (flags, cardinality) = if buf.proto().is_at_least(0, 11) {
let flags = buf.get_u32();
let cardinality = TryFrom::try_from(buf.get_u8())?;
(flags, Some(cardinality))
} else {
(buf.get_u8() as u32, None)
};
let name = String::decode(buf)?;
ensure!(buf.remaining() >= 2, errors::Underflow);
let type_pos = TypePos(buf.get_u16());
Ok(ShapeElement {
flag_implicit: flags & 0b001 != 0,
flag_link_property: flags & 0b010 != 0,
flag_link: flags & 0b100 != 0,
cardinality,
name,
type_pos,
})
}
}
impl Decode for BaseScalarTypeDescriptor {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
assert!(buf.get_u8() == 2);
let id = Uuid::decode(buf)?;
Ok(BaseScalarTypeDescriptor { id })
}
}
impl Decode for ScalarTypeDescriptor {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 19, errors::Underflow);
assert!(buf.get_u8() == 3);
let id = Uuid::decode(buf)?;
let base_type_pos = TypePos(buf.get_u16());
Ok(ScalarTypeDescriptor { id, base_type_pos })
}
}
impl Decode for TupleTypeDescriptor {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 19, errors::Underflow);
assert!(buf.get_u8() == 4);
let id = Uuid::decode(buf)?;
let el_count = buf.get_u16();
ensure!(buf.remaining() >= 2*el_count as usize, errors::Underflow);
let mut element_types = Vec::with_capacity(el_count as usize);
for _ in 0..el_count {
element_types.push(TypePos(buf.get_u16()));
}
Ok(TupleTypeDescriptor { id, element_types })
}
}
impl Decode for NamedTupleTypeDescriptor {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 19, errors::Underflow);
assert!(buf.get_u8() == 5);
let id = Uuid::decode(buf)?;
let element_count = buf.get_u16();
let mut elements = Vec::with_capacity(element_count as usize);
for _ in 0..element_count {
elements.push(TupleElement::decode(buf)?);
}
Ok(NamedTupleTypeDescriptor { id, elements })
}
}
impl Decode for TupleElement {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
let name = String::decode(buf)?;
ensure!(buf.remaining() >= 2, errors::Underflow);
let type_pos = TypePos(buf.get_u16());
Ok(TupleElement {
name,
type_pos,
})
}
}
impl Decode for ArrayTypeDescriptor {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 21, errors::Underflow);
assert!(buf.get_u8() == 6);
let id = Uuid::decode(buf)?;
let type_pos = TypePos(buf.get_u16());
let dim_count = buf.get_u16();
ensure!(buf.remaining() >= 4*dim_count as usize, errors::Underflow);
let mut dimensions = Vec::with_capacity(dim_count as usize);
for _ in 0..dim_count {
dimensions.push(match buf.get_i32() {
-1 => None,
n if n > 0 => Some(n as u32),
_ => errors::InvalidArrayShape.fail()?,
});
}
Ok(ArrayTypeDescriptor { id, type_pos, dimensions })
}
}
impl Decode for RangeTypeDescriptor {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 19, errors::Underflow);
assert!(buf.get_u8() == 9);
let id = Uuid::decode(buf)?;
let type_pos = TypePos(buf.get_u16());
Ok(RangeTypeDescriptor { id, type_pos })
}
}
impl Decode for EnumerationTypeDescriptor {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 19, errors::Underflow);
assert!(buf.get_u8() == 7);
let id = Uuid::decode(buf)?;
let member_count = buf.get_u16();
let mut members = Vec::with_capacity(member_count as usize);
for _ in 0..member_count {
members.push(String::decode(buf)?);
}
Ok(EnumerationTypeDescriptor { id, members })
}
}
impl Decode for TypeAnnotationDescriptor {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 21, errors::Underflow);
let annotated_type = buf.get_u8();
assert!(annotated_type >= 0x7F);
let id = Uuid::decode(buf)?;
let annotation = String::decode(buf)?;
Ok(TypeAnnotationDescriptor { annotated_type, id, annotation })
}
}