Line data Source code
1 : //! Catalog object ids.
2 :
3 : /// The id of a catalog entry (a script or a descriptor pool)
4 : pub type CatalogEntryId = u32;
5 : /// A database id
6 : pub type CatalogDatabaseId = u32;
7 : /// A schema id
8 : pub type CatalogSchemaId = u32;
9 : /// The catalog version, bumped by every modification
10 : pub type CatalogVersion = u32;
11 : /// The layer of a catalog entry: names resolve against the session, then the executed statements,
12 : /// then the connection, and within a layer against older entries first
13 : pub use dashql_buffers::catalog::CatalogLayer;
14 : /// The lowest layer in range queries of the catalog indexes
15 : pub const MIN_LAYER: CatalogLayer = CatalogLayer(u8::MIN);
16 : /// The highest layer in range queries of the catalog indexes
17 : pub const MAX_LAYER: CatalogLayer = CatalogLayer(u8::MAX);
18 :
19 : /// The null value of u32 fields in FlatBuffers
20 : pub const PROTO_NULL_U32: u32 = u32::MAX;
21 : /// The first database id
22 : pub const INITIAL_DATABASE_ID: CatalogDatabaseId = 1 << 8;
23 : /// The first schema id
24 : pub const INITIAL_SCHEMA_ID: CatalogSchemaId = 1 << 16;
25 : /// The first entry id
26 : pub const INITIAL_ENTRY_ID: CatalogEntryId = 1;
27 :
28 : /// An object id annotated with the catalog entry it belongs to
29 : #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
30 : pub struct ExternalObjectId {
31 : /// The catalog entry
32 : pub origin: CatalogEntryId,
33 : /// The index within the entry
34 : pub object: u32,
35 : }
36 :
37 : impl Default for ExternalObjectId {
38 1 : fn default() -> Self {
39 1 : Self { origin: u32::MAX, object: u32::MAX }
40 1 : }
41 : }
42 :
43 : impl ExternalObjectId {
44 4578 : pub const fn new(origin: CatalogEntryId, object: u32) -> Self {
45 4578 : Self { origin, object }
46 4578 : }
47 :
48 : /// Is a null id?
49 2 : pub fn is_null(&self) -> bool {
50 2 : self.object == u32::MAX
51 2 : }
52 :
53 10541 : pub fn pack(&self) -> u64 {
54 10541 : ((self.origin as u64) << 32) | self.object as u64
55 10541 : }
56 :
57 11021 : pub fn unpack(packed: u64) -> Self {
58 11021 : Self {
59 11021 : origin: (packed >> 32) as u32,
60 11021 : object: packed as u32,
61 11021 : }
62 11021 : }
63 : }
64 :
65 : /// A table id
66 : pub type CatalogTableId = ExternalObjectId;
67 : /// A function id
68 : pub type CatalogFunctionId = ExternalObjectId;
69 :
70 : /// The type of a catalog object, numbered like `CompletionCandidateObjectType`
71 : #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
72 : #[repr(u8)]
73 : pub enum CatalogObjectType {
74 : #[default]
75 : Deferred = 0,
76 : DatabaseReference = 1,
77 : SchemaReference = 2,
78 : TableDeclaration = 3,
79 : ColumnDeclaration = 4,
80 : FunctionDeclaration = 5,
81 : }
82 :
83 : /// The id of a catalog object, ordered by type, part 0, part 1
84 : #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
85 : pub struct QualifiedCatalogObjectId {
86 : /// The type of the object
87 : object_type: CatalogObjectType,
88 : /// The database id, or the packed table or function id
89 : part0: u64,
90 : /// The schema id or the column index, 0 for other types
91 : part1: u32,
92 : }
93 :
94 : impl QualifiedCatalogObjectId {
95 1 : pub const fn deferred() -> Self {
96 1 : Self {
97 1 : object_type: CatalogObjectType::Deferred,
98 1 : part0: 0,
99 1 : part1: 0,
100 1 : }
101 1 : }
102 2327 : pub const fn database(database_id: CatalogDatabaseId) -> Self {
103 2327 : Self {
104 2327 : object_type: CatalogObjectType::DatabaseReference,
105 2327 : part0: database_id as u64,
106 2327 : part1: 0,
107 2327 : }
108 2327 : }
109 887 : pub const fn schema(database_id: CatalogDatabaseId, schema_id: CatalogSchemaId) -> Self {
110 887 : Self {
111 887 : object_type: CatalogObjectType::SchemaReference,
112 887 : part0: database_id as u64,
113 887 : part1: schema_id,
114 887 : }
115 887 : }
116 2126 : pub fn table(table_id: CatalogTableId) -> Self {
117 2126 : Self {
118 2126 : object_type: CatalogObjectType::TableDeclaration,
119 2126 : part0: table_id.pack(),
120 2126 : part1: 0,
121 2126 : }
122 2126 : }
123 4306 : pub fn table_column(table_id: CatalogTableId, column_id: u32) -> Self {
124 4306 : Self {
125 4306 : object_type: CatalogObjectType::ColumnDeclaration,
126 4306 : part0: table_id.pack(),
127 4306 : part1: column_id,
128 4306 : }
129 4306 : }
130 35 : pub fn function(function_id: CatalogFunctionId) -> Self {
131 35 : Self {
132 35 : object_type: CatalogObjectType::FunctionDeclaration,
133 35 : part0: function_id.pack(),
134 35 : part1: 0,
135 35 : }
136 35 : }
137 :
138 6 : pub fn object_type(&self) -> CatalogObjectType {
139 6 : self.object_type
140 6 : }
141 861 : pub fn unpack_database_id(&self) -> CatalogDatabaseId {
142 861 : debug_assert_eq!(self.object_type, CatalogObjectType::DatabaseReference);
143 861 : self.part0 as u32
144 861 : }
145 2653 : pub fn unpack_schema_id(&self) -> (CatalogDatabaseId, CatalogSchemaId) {
146 2653 : debug_assert_eq!(self.object_type, CatalogObjectType::SchemaReference);
147 2653 : (self.part0 as u32, self.part1)
148 2653 : }
149 7310 : pub fn unpack_table_id(&self) -> CatalogTableId {
150 7310 : debug_assert_eq!(self.object_type, CatalogObjectType::TableDeclaration);
151 7310 : ExternalObjectId::unpack(self.part0)
152 7310 : }
153 3687 : pub fn unpack_table_column_id(&self) -> (CatalogTableId, u32) {
154 3687 : debug_assert_eq!(self.object_type, CatalogObjectType::ColumnDeclaration);
155 3687 : (ExternalObjectId::unpack(self.part0), self.part1)
156 3687 : }
157 10 : pub fn unpack_function_id(&self) -> CatalogFunctionId {
158 10 : debug_assert_eq!(self.object_type, CatalogObjectType::FunctionDeclaration);
159 10 : ExternalObjectId::unpack(self.part0)
160 10 : }
161 : }
|