Skip to content

Commit

Permalink
test(pubky): thorough testing for list options
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuhvi committed Aug 8, 2024
1 parent 5eb5440 commit e163e7f
Show file tree
Hide file tree
Showing 2 changed files with 263 additions and 90 deletions.
154 changes: 81 additions & 73 deletions pubky-homeserver/src/database/tables/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,85 +103,93 @@ impl DB {

let limit = limit.unwrap_or(MAX_LIST_LIMIT).min(MAX_LIST_LIMIT);

// Remove directories from the cursor;
let cursor = cursor
// TODO: make this more performant than split and allocations?
let mut threshold = cursor
.as_deref()
.and_then(|mut cursor| cursor.rsplit('/').next())
.unwrap_or(if reverse { "~" } else { "" });

let mut cursor = format!("{path}{cursor}");

// Fetch data based on direction
if reverse {
for _ in 0..limit {
if let Some((key, _)) = self.tables.entries.get_lower_than(txn, &cursor)? {
if !key.starts_with(path) {
break;
}

if shallow {
let mut split = key[path.len()..].split('/');
let item = split.next().expect("should not be reachable");

let is_directory = split.next().is_some();

cursor = format!(
"{}{}",
&key[..(path.len() + item.len())],
// `.` is immediately lower than `/`
if is_directory { "." } else { "" }
);

let url = format!(
"pubky://{path}{item}{}",
if is_directory { "/" } else { "" }
);

results.push(url);
} else {
cursor = key.to_string();
results.push(format!("pubky://{}", key))
}
};
}
} else {
for _ in 0..limit {
if let Some((key, _)) = self.tables.entries.get_greater_than(txn, &cursor)? {
if !key.starts_with(path) {
break;
}

if shallow {
let mut split = key[path.len()..].split('/');
let item = split.next().expect("should not be reachable");

let is_directory = split.next().is_some();

cursor = format!(
"{}{}",
&key[..(path.len() + item.len())],
// `0` is immediately higher than `/`
if is_directory { "0" } else { "" }
);

let url = format!(
"pubky://{path}{item}{}",
if is_directory { "/" } else { "" }
);

results.push(url);
} else {
cursor = key.to_string();
results.push(format!("pubky://{}", key))
}
};
}
};
.map(|mut cursor| {
// Get the name of the file or directory
// Similar to Path::new(cursor).file_name
let is_directory = cursor.ends_with('/');

let mut split = cursor.rsplit('/');

if is_directory {
// Move one step back
split.next();
}

let file_or_directory = split.next().expect("should not be reachable");

next_threshold(path, file_or_directory, is_directory, reverse, shallow)
})
.unwrap_or(next_threshold(path, "", false, reverse, shallow));

for _ in 0..limit {
if let Some((key, _)) = (if reverse {
self.tables.entries.get_lower_than(txn, &threshold)?
} else {
self.tables.entries.get_greater_than(txn, &threshold)?
}) {
if !key.starts_with(path) {
break;
}

if shallow {
let mut split = key[path.len()..].split('/');
let file_or_directory = split.next().expect("should not be reachable");

let is_directory = split.next().is_some();

threshold =
next_threshold(path, file_or_directory, is_directory, reverse, shallow);

results.push(format!(
"pubky://{path}{file_or_directory}{}",
if is_directory { "/" } else { "" }
));
} else {
threshold = key.to_string();
results.push(format!("pubky://{}", key))
}
};
}

Ok(results)
}
}

/// Calculate the next threshold, only for flat (non-`shallow`) listing
fn next_threshold(
path: &str,
file_or_directory: &str,
is_directory: bool,
reverse: bool,
shallow: bool,
) -> String {
format!(
"{path}{file_or_directory}{}",
if file_or_directory.is_empty() {
// No file_or_directory, early return
if reverse {
// `path/to/dir/\x7f` to catch all paths than `path/to/dir/`
"\x7f"
} else {
""
}
} else if shallow & is_directory {
if reverse {
// threshold = `path/to/dir\x2e`, since `\x2e` is lower than `/`
"\x2e"
} else {
//threshold = `path/to/dir\x7f`, since `\x7f` is greater than `/`
"\x7f"
}
} else {
""
}
)
}

#[derive(Clone, Default, Serialize, Deserialize, Debug, Eq, PartialEq)]
pub struct Entry {
/// Encoding version
Expand Down
Loading

0 comments on commit e163e7f

Please sign in to comment.