Skip to content

Commit

Permalink
Remove intermediary vecs/btreemaps for ordering on index
Browse files Browse the repository at this point in the history
  • Loading branch information
w4 committed Nov 13, 2024
1 parent 3430b7c commit 6595e8c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 39 additions & 18 deletions src/methods/index.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use std::{collections::BTreeMap, sync::Arc};
use std::{cell::RefCell, sync::Arc};

use anyhow::Context;
use askama::Template;
use axum::{response::IntoResponse, Extension};
use axum::{
response::{IntoResponse, Response},
Extension,
};
use itertools::{Either, Itertools};

use super::filters;
use crate::{
Expand All @@ -12,28 +16,45 @@ use crate::{

#[derive(Template)]
#[template(path = "index.html")]
pub struct View {
pub repositories: BTreeMap<Option<String>, Vec<YokedRepository>>,
pub struct View<
'a,
Group: Iterator<Item = (&'a String, &'a YokedRepository)>,
GroupIter: Iterator<Item = (&'a str, Group)>,
> {
// this type sig is a necessary evil unfortunately, because askama takes a reference
// to the data for rendering.
pub repositories: RefCell<Either<GroupIter, std::iter::Empty<(&'a str, Group)>>>,
}

impl<'a, Group, GroupIter> View<'a, Group, GroupIter>
where
Group: Iterator<Item = (&'a String, &'a YokedRepository)>,
GroupIter: Iterator<Item = (&'a str, Group)>,
{
fn take_iter(&self) -> Either<GroupIter, std::iter::Empty<(&'a str, Group)>> {
self.repositories.replace(Either::Right(std::iter::empty()))
}
}

pub async fn handle(
Extension(db): Extension<Arc<rocksdb::DB>>,
) -> Result<impl IntoResponse, super::repo::Error> {
let mut repositories: BTreeMap<Option<String>, Vec<YokedRepository>> = BTreeMap::new();

) -> Result<Response, super::repo::Error> {
let fetched = tokio::task::spawn_blocking(move || Repository::fetch_all(&db))
.await
.context("Failed to join Tokio task")??;

for (k, v) in fetched {
// TODO: fixme
let mut split: Vec<_> = k.split('/').collect();
split.pop();
let key = Some(split.join("/")).filter(|v| !v.is_empty());

let k = repositories.entry(key).or_default();
k.push(v);
}

Ok(into_response(View { repositories }))
// rocksdb returned the keys already ordered for us so group_by is a nice
// operation we can use here to avoid writing into a map to group. though,
// now that i think about it it might act a little bit strangely when mixing
// root repositories and nested repositories. we're going to have to prefix
// root repositories with a null byte or something. i'll just leave this here
// as a TODO.
let repositories = fetched
.iter()
.group_by(|(k, _)| memchr::memrchr(b'/', k.as_bytes()).map_or("", |idx| &k[..idx]));

Ok(into_response(View {
repositories: Either::Left(repositories.into_iter()).into(),
})
.into_response())
}
16 changes: 8 additions & 8 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@
</thead>

<tbody>
{%- for (path, repositories) in repositories %}
{%- if let Some(path) = path %}
{%- for (path, repositories) in self.take_iter() %}
{%- if !path.is_empty() %}
<tr><td class="repo-section" colspan="4">{{ path }}</td></tr>
{%- endif -%}

{%- for repository in repositories %}
{% set repository = repository.get() %}
<tr class="{% if path.is_some() %}has-parent{% endif %}">
{% set repository = repository.1.get() %}
<tr class="{% if !path.is_empty() %}has-parent{% endif %}">
<td>
<a href="/{% if let Some(path) = path %}{{ path }}/{% endif %}{{ repository.name }}">
<a href="/{% if !path.is_empty() %}{{ path }}/{% endif %}{{ repository.name }}">
{{- repository.name -}}
</a>
</td>
<td>
<a href="/{% if let Some(path) = path %}{{ path }}/{% endif %}{{ repository.name }}">
<a href="/{% if !path.is_empty() %}{{ path }}/{% endif %}{{ repository.name }}">
{%- if let Some(description) = repository.description.as_ref() -%}
{{- description -}}
{%- else -%}
Expand All @@ -36,14 +36,14 @@
</a>
</td>
<td>
<a href="/{% if let Some(path) = path %}{{ path }}/{% endif %}{{ repository.name }}">
<a href="/{% if !path.is_empty() %}{{ path }}/{% endif %}{{ repository.name }}">
{%- if let Some(owner) = repository.owner.as_ref() -%}
{{- owner -}}
{%- endif -%}
</a>
</td>
<td>
<a href="/{% if let Some(path) = path %}{{ path }}/{% endif %}{{ repository.name }}">
<a href="/{% if !path.is_empty() %}{{ path }}/{% endif %}{{ repository.name }}">
<time datetime="{{ repository.last_modified|format_time }}" title="{{ repository.last_modified|format_time }}">
{{- repository.last_modified|timeago -}}
</time>
Expand Down

0 comments on commit 6595e8c

Please sign in to comment.