Skip to content

Commit 7d9c9f6

Browse files
ldanilekConvex, Inc.
authored andcommitted
[virtual table cleanup] stop mapping _id fields between table numbers (#28688)
now that virtual ids match the system ids, we no longer need to map IDs between table numbers when an index range or filter includes an `_id` GitOrigin-RevId: 28ad4c8c1e58236a8cbc62d0fb18c1bd56092cc4
1 parent 26d4699 commit 7d9c9f6

File tree

5 files changed

+5
-400
lines changed

5 files changed

+5
-400
lines changed

crates/common/src/query.rs

Lines changed: 3 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ use sha2::{
2424
};
2525
use value::{
2626
heap_size::HeapSize,
27-
id_v6::{
28-
DeveloperDocumentId,
29-
VirtualTableNumberMap,
30-
},
27+
id_v6::DeveloperDocumentId,
3128
utils::display_sequence,
3229
val,
3330
ConvexObject,
@@ -180,22 +177,12 @@ pub struct IndexRange {
180177
}
181178

182179
impl IndexRange {
183-
pub fn compile(
184-
self,
185-
indexed_fields: IndexedFields,
186-
virtual_table_number_map: Option<VirtualTableNumberMap>,
187-
) -> anyhow::Result<Interval> {
180+
pub fn compile(self, indexed_fields: IndexedFields) -> anyhow::Result<Interval> {
188181
let index_name = self.index_name.clone();
189182
let SplitIndexRange {
190183
equalities,
191184
inequality,
192-
} = self.split()?.map_values(|field, v| {
193-
if field == &*ID_FIELD_PATH {
194-
map_id_value_to_tablet(v, virtual_table_number_map)
195-
} else {
196-
Ok(v)
197-
}
198-
})?;
185+
} = self.split()?;
199186

200187
// Check that some permutation of the equality field paths + the (optional)
201188
// inequality field path is a prefix of the indexed paths.
@@ -397,49 +384,6 @@ struct SplitIndexRange {
397384
inequality: Option<IndexInequality>,
398385
}
399386

400-
impl SplitIndexRange {
401-
pub fn map_values(
402-
self,
403-
f: impl Fn(&FieldPath, ConvexValue) -> anyhow::Result<ConvexValue>,
404-
) -> anyhow::Result<SplitIndexRange> {
405-
let equalities = self
406-
.equalities
407-
.into_iter()
408-
.map(|(field, value)| {
409-
let new_value = match value.0 {
410-
Some(value) => MaybeValue(Some(f(&field, value)?)),
411-
None => MaybeValue(None),
412-
};
413-
anyhow::Ok((field, new_value))
414-
})
415-
.try_collect()?;
416-
let inequality = self
417-
.inequality
418-
.map(|inequality| {
419-
let start = match inequality.start {
420-
Bound::Unbounded => Bound::Unbounded,
421-
Bound::Included(value) => Bound::Included(f(&inequality.field_path, value)?),
422-
Bound::Excluded(value) => Bound::Excluded(f(&inequality.field_path, value)?),
423-
};
424-
let end = match inequality.end {
425-
Bound::Unbounded => Bound::Unbounded,
426-
Bound::Included(value) => Bound::Included(f(&inequality.field_path, value)?),
427-
Bound::Excluded(value) => Bound::Excluded(f(&inequality.field_path, value)?),
428-
};
429-
anyhow::Ok(IndexInequality {
430-
field_path: inequality.field_path,
431-
start,
432-
end,
433-
})
434-
})
435-
.transpose()?;
436-
Ok(SplitIndexRange {
437-
equalities,
438-
inequality,
439-
})
440-
}
441-
}
442-
443387
struct IndexInequality {
444388
field_path: FieldPath,
445389
start: Bound<ConvexValue>,
@@ -456,21 +400,6 @@ impl Display for QueryFields {
456400
}
457401
}
458402

459-
fn map_id_value_to_tablet(
460-
value: ConvexValue,
461-
virtual_table_number_map: Option<VirtualTableNumberMap>,
462-
) -> anyhow::Result<ConvexValue> {
463-
let val = match (&value, virtual_table_number_map) {
464-
(ConvexValue::String(id), Some(virtual_table_number_map)) => {
465-
let mapped =
466-
DeveloperDocumentId::map_string_between_table_numbers(id, virtual_table_number_map);
467-
val!(mapped)
468-
},
469-
_ => value,
470-
};
471-
Ok(val)
472-
}
473-
474403
fn already_defined_bound_error(
475404
bound_type: &str,
476405
field_path: &FieldPath,

crates/common/src/types/index.rs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@ use sync_types::identifier::{
1515
};
1616
use value::{
1717
heap_size::HeapSize,
18-
id_v6::VirtualTableNumberMap,
1918
FieldName,
2019
InternalId,
2120
ResolvedDocumentId,
22-
TableMapping,
2321
TableName,
2422
TabletId,
25-
VirtualTableMapping,
2623
};
2724

2825
use crate::{
@@ -147,33 +144,6 @@ impl StableIndexName {
147144
StableIndexName::Missing(index_name) => Err(index_name),
148145
}
149146
}
150-
151-
pub fn virtual_table_number_map(
152-
&self,
153-
table_mapping: &TableMapping,
154-
virtual_table_mapping: &VirtualTableMapping,
155-
) -> anyhow::Result<Option<VirtualTableNumberMap>> {
156-
match self {
157-
StableIndexName::Physical(index_name) => {
158-
let table_number = table_mapping.tablet_number(*index_name.table())?;
159-
Ok(Some(VirtualTableNumberMap {
160-
virtual_table_number: table_number,
161-
physical_table_number: table_number,
162-
}))
163-
},
164-
StableIndexName::Virtual(index_name, tablet_index_name) => {
165-
let namespace = table_mapping.tablet_namespace(*tablet_index_name.table())?;
166-
Ok(Some(VirtualTableNumberMap {
167-
virtual_table_number: virtual_table_mapping
168-
.namespace(namespace)
169-
.number(index_name.table())?,
170-
physical_table_number: table_mapping
171-
.tablet_number(*tablet_index_name.table())?,
172-
}))
173-
},
174-
StableIndexName::Missing(_) => Ok(None),
175-
}
176-
}
177147
}
178148

179149
impl HeapSize for TabletIndexName {

crates/database/src/query/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,7 @@ impl<RT: Runtime> DeveloperQuery<RT> {
366366
)),
367367
QuerySource::IndexRange(index_range) => {
368368
let order = index_range.order;
369-
let virtual_table_mapping = tx.virtual_table_mapping().clone();
370-
let virtual_table_number_map = stable_index_name
371-
.virtual_table_number_map(tx.table_mapping(), &virtual_table_mapping)?;
372-
let interval =
373-
index_range.compile(indexed_fields.clone(), virtual_table_number_map)?;
369+
let interval = index_range.compile(indexed_fields.clone())?;
374370
QueryNode::IndexRange(IndexRange::new(
375371
namespace,
376372
stable_index_name,

crates/value/src/base32.rs

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::cmp;
2-
31
// Forked from https://github.com/andreasots/base32 @ 58909ac.
42
//
53
// Copyright (c) 2015 The base32 Developers - MIT License
@@ -123,72 +121,6 @@ pub fn decode(data: &str) -> Result<Vec<u8>, InvalidBase32Error> {
123121
Ok(out)
124122
}
125123

126-
fn clamp_char_to_alphabet(c: char) -> char {
127-
match c {
128-
..'0' => '0',
129-
'0'..='9' => c,
130-
':'..'a' => 'a',
131-
'a'..='h' => c,
132-
'i' => 'j',
133-
'j'..='k' => c,
134-
'l' => 'm',
135-
'm'..='n' => c,
136-
'o' => 'p',
137-
'p'..='t' => c,
138-
'u' => 'v',
139-
'v'..='z' => c,
140-
'{'.. => 'z',
141-
}
142-
}
143-
144-
/// Returns a string that can be base32 decoded, and is the closest such string
145-
/// to the input string in lexicographic order.
146-
/// i.e. for every `t` which is valid base32 of length <= `target_len`,
147-
/// if s = t, then clamp_to_alphabet(s) = t,
148-
/// if s < t, then clamp_to_alphabet(s) <= t.
149-
/// if s > t, then clamp_to_alphabet(s) >= t.
150-
///
151-
/// How does it work?
152-
/// Each character is clamped to the closest character in the base32 alphabet.
153-
/// If a character has to be rounded up, following characters all become '0'.
154-
/// If a character has to be rounded down, following characters all become 'z'.
155-
/// And then we pad to a multiple of 5 characters to avoid dropping chars when
156-
/// decoding.
157-
///
158-
/// e.g. "azi" is between "azhzz" and "azj00", so it is clamped to "azj00".
159-
/// e.g. "abcd~" is between "abcdz" and "abce0", so it is clamped to "abcdz".
160-
pub fn clamp_to_alphabet(s: &str, target_len: usize) -> String {
161-
let mut out = String::with_capacity(s.len());
162-
let mut order = cmp::Ordering::Equal;
163-
for c in s.chars() {
164-
match order {
165-
cmp::Ordering::Equal => {
166-
let clamped = clamp_char_to_alphabet(c);
167-
out.push(clamped);
168-
order = c.cmp(&clamped);
169-
},
170-
cmp::Ordering::Less => {
171-
out.push('0');
172-
},
173-
cmp::Ordering::Greater => {
174-
out.push('z');
175-
},
176-
}
177-
}
178-
// Pad the output to a multiple of 5 with 0s so no characters are lost.
179-
while out.len() < target_len || out.len() % 5 != 0 {
180-
match order {
181-
cmp::Ordering::Equal | cmp::Ordering::Less => {
182-
out.push('0');
183-
},
184-
cmp::Ordering::Greater => {
185-
out.push('z');
186-
},
187-
}
188-
}
189-
out
190-
}
191-
192124
#[cfg(test)]
193125
mod tests {
194126
use proptest::prelude::*;

0 commit comments

Comments
 (0)