Skip to content

Commit a483834

Browse files
committed
refactor: use IndexSet
1 parent e265be0 commit a483834

7 files changed

Lines changed: 11 additions & 19 deletions

File tree

schema-engine/sql-schema-describer/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ either.workspace = true
3030
async-trait.workspace = true
3131
bigdecimal.workspace = true
3232
enumflags2 = { workspace = true, features = ["serde"] }
33-
indexmap.workspace = true
33+
indexmap = { workspace = true, features = ["serde"] }
3434
indoc.workspace = true
3535
itertools.workspace = true
3636
regex.workspace = true

schema-engine/sql-schema-describer/src/lib.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub use self::{
2424
walkers::*,
2525
};
2626
pub use either::Either;
27+
use indexmap::IndexSet;
2728
pub use prisma_value::PrismaValue;
2829

2930
use enumflags2::{BitFlag, BitFlags};
@@ -57,7 +58,7 @@ pub struct SqlMetadata {
5758
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
5859
pub struct SqlSchema {
5960
/// Namespaces (schemas)
60-
namespaces: Vec<String>,
61+
namespaces: IndexSet<String>,
6162
/// The schema's tables.
6263
tables: Vec<Table>,
6364
/// The schema's enums.
@@ -185,10 +186,7 @@ impl SqlSchema {
185186

186187
/// Find a namespace by name.
187188
pub fn get_namespace_id(&self, name: &str) -> Option<NamespaceId> {
188-
self.namespaces
189-
.binary_search_by(|ns_name| ns_name.as_str().cmp(name))
190-
.ok()
191-
.map(|pos| NamespaceId(pos as u32))
189+
self.namespaces.get_index_of(name).map(|pos| NamespaceId(pos as u32))
192190
}
193191

194192
/// The total number of indexes in the schema.
@@ -333,9 +331,8 @@ impl SqlSchema {
333331
}
334332

335333
pub fn push_namespace(&mut self, name: String) -> NamespaceId {
336-
let id = NamespaceId(self.namespaces.len() as u32);
337-
self.namespaces.push(name);
338-
id
334+
let (id, _) = self.namespaces.insert_full(name);
335+
NamespaceId(id as u32)
339336
}
340337

341338
pub fn push_table(&mut self, name: String, namespace_id: NamespaceId, description: Option<String>) -> TableId {

schema-engine/sql-schema-describer/src/mssql.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use either::Either;
99
use enumflags2::BitFlags;
1010
use indexmap::IndexMap;
1111
use indoc::indoc;
12-
use itertools::Itertools;
1312
use prisma_value::PrismaValue;
1413
use psl::{
1514
builtin_connectors::{MsSqlType, MsSqlTypeParameter},
@@ -547,11 +546,7 @@ impl<'a> SqlSchemaDescriber<'a> {
547546
let names = rows
548547
.into_iter()
549548
.map(|row| row.get_expect_string("name"))
550-
.filter(|name| namespaces.contains(&name.as_str()))
551-
// SqlSchema uses binary search, namespaces must be added in sorted order.
552-
// We have to sort regardless of the ORDER BY since Rust ordering could
553-
// be different then the one used by SQL Server.
554-
.sorted();
549+
.filter(|name| namespaces.contains(&name.as_str()));
555550

556551
for name in names {
557552
sql_schema.push_namespace(name);

schema-engine/sql-schema-describer/src/walkers/enum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl<'a> EnumWalker<'a> {
1515
pub fn namespace(self) -> Option<&'a str> {
1616
self.schema
1717
.namespaces
18-
.get(self.get().namespace_id.0 as usize)
18+
.get_index(self.get().namespace_id.0 as usize)
1919
.map(|s| s.as_str())
2020
}
2121

schema-engine/sql-schema-describer/src/walkers/table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl<'a> TableWalker<'a> {
7171
pub fn namespace(self) -> Option<&'a str> {
7272
self.schema
7373
.namespaces
74-
.get(self.table().namespace_id.0 as usize)
74+
.get_index(self.table().namespace_id.0 as usize)
7575
.map(|s| s.as_str())
7676
}
7777

schema-engine/sql-schema-describer/src/walkers/user_defined_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl<'a> UserDefinedTypeWalker<'a> {
1818
pub fn namespace(self) -> Option<&'a str> {
1919
self.schema
2020
.namespaces
21-
.get(self.get().namespace_id.0 as usize)
21+
.get_index(self.get().namespace_id.0 as usize)
2222
.map(|s| s.as_str())
2323
}
2424

schema-engine/sql-schema-describer/src/walkers/view.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl<'a> ViewWalker<'a> {
2020
pub fn namespace(self) -> Option<&'a str> {
2121
self.schema
2222
.namespaces
23-
.get(self.get().namespace_id.0 as usize)
23+
.get_index(self.get().namespace_id.0 as usize)
2424
.map(|s| s.as_str())
2525
}
2626

0 commit comments

Comments
 (0)