Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runes pagination #3215

Merged
merged 5 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,31 @@ impl Index {
Ok(entries)
}

pub(crate) fn runes_paginated(
&self,
page_size: usize,
page_index: usize,
) -> Result<(Vec<(RuneId, RuneEntry)>, bool)> {
let mut entries = Vec::new();

for result in self
.database
.begin_read()?
.open_table(RUNE_ID_TO_RUNE_ENTRY)?
.iter()?
lugondev marked this conversation as resolved.
Show resolved Hide resolved
.rev()
.skip(page_index.saturating_mul(page_size))
.take(page_size.saturating_add(1))
{
let (id, entry) = result?;
entries.push((RuneId::load(id.value()), RuneEntry::load(entry.value())));
}

let more = entries.len() > page_size;

Ok((entries, more))
}

pub(crate) fn encode_rune_balance(id: RuneId, balance: u128, buffer: &mut Vec<u8>) {
varint::encode_to_vec(id.block.into(), buffer);
varint::encode_to_vec(id.tx.into(), buffer);
Expand Down
36 changes: 32 additions & 4 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ impl Server {
.route("/rare.txt", get(Self::rare_txt))
.route("/rune/:rune", get(Self::rune))
.route("/runes", get(Self::runes))
.route("/runes/:page", get(Self::runes_paginated))
.route("/runes/balances", get(Self::runes_balances))
.route("/sat/:sat", get(Self::sat))
.route("/search", get(Self::search_by_query))
Expand Down Expand Up @@ -693,17 +694,44 @@ impl Server {
async fn runes(
Extension(server_config): Extension<Arc<ServerConfig>>,
Extension(index): Extension<Arc<Index>>,
accept_json: AcceptJson,
) -> ServerResult<Response> {
Self::runes_paginated(
Extension(server_config),
Extension(index),
Path(0),
accept_json,
)
.await
}

async fn runes_paginated(
Extension(server_config): Extension<Arc<ServerConfig>>,
Extension(index): Extension<Arc<Index>>,
Path(page_index): Path<usize>,
AcceptJson(accept_json): AcceptJson,
) -> ServerResult {
task::block_in_place(|| {
let (entries, more) = index.runes_paginated(50, page_index)?;

let prev = page_index.checked_sub(1);

let next = more.then_some(page_index + 1);

Ok(if accept_json {
Json(api::Runes {
entries: index.runes()?,
Json(RunesHtml {
entries,
more,
prev,
next,
})
.into_response()
} else {
RunesHtml {
entries: index.runes()?,
entries,
more,
prev,
next,
}
.page(server_config)
.into_response()
Expand Down Expand Up @@ -2634,7 +2662,7 @@ mod tests {
server.assert_response_regex(
"/runes",
StatusCode::OK,
".*<title>Runes</title>.*<h1>Runes</h1>\n<ul>\n</ul>.*",
".*<title>Runes</title>.*<h1>Runes</h1>\n<ul>\n</ul>\n<div class=center>\n prev\n next\n </div>.*",
);

let (txid, id) = server.etch(
Expand Down
54 changes: 53 additions & 1 deletion src/templates/runes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use super::*;
#[derive(Boilerplate, Debug, PartialEq, Serialize, Deserialize)]
pub struct RunesHtml {
pub entries: Vec<(RuneId, RuneEntry)>,
pub more: bool,
pub prev: Option<usize>,
pub next: Option<usize>,
}

impl PageContent for RunesHtml {
Expand All @@ -29,13 +32,62 @@ mod tests {
..default()
}
)],
more: false,
prev: None,
next: None,
}
.to_string(),
"<h1>Runes</h1>
<ul>
<li><a href=/rune/A•A>A•A</a></li>
</ul>
"
<div class=center>
prev
next
</div>"
);
}

#[test]
fn with_prev_and_next() {
assert_eq!(
RunesHtml {
entries: vec![
(
RuneId { block: 0, tx: 0 },
RuneEntry {
spaced_rune: SpacedRune {
rune: Rune(0),
spacers: 0
},
..Default::default()
}
),
(
RuneId { block: 0, tx: 1 },
RuneEntry {
spaced_rune: SpacedRune {
rune: Rune(2),
spacers: 0
},
..Default::default()
}
)
],
prev: Some(1),
next: Some(2),
more: true,
}
.to_string(),
"<h1>Runes</h1>
<ul>
<li><a href=/rune/A>A</a></li>
<li><a href=/rune/C>C</a></li>
</ul>
<div class=center>
<a class=prev href=/runes/1>prev</a>
<a class=next href=/runes/2>next</a>
</div>"
);
}
}
12 changes: 12 additions & 0 deletions templates/runes.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,15 @@ <h1>Runes</h1>
<li><a href=/rune/{{ entry.spaced_rune }}>{{ entry.spaced_rune }}</a></li>
%% }
</ul>
<div class=center>
%% if let Some(prev) = self.prev {
<a class=prev href=/runes/{{prev}}>prev</a>
%% } else {
prev
%% }
%% if let Some(next) = self.next {
<a class=next href=/runes/{{next}}>next</a>
%% } else {
next
%% }
</div>
29 changes: 16 additions & 13 deletions tests/json_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,22 +568,22 @@ fn get_runes() {
api::Runes {
entries: vec![
(
RuneId { block: 10, tx: 1 },
RuneId { block: 24, tx: 1 },
RuneEntry {
block: a.id.block,
block: c.id.block,
burned: 0,
terms: None,
divisibility: 0,
etching: a.output.reveal,
etching: c.output.reveal,
mints: 0,
number: 0,
number: 2,
premine: 1000,
spaced_rune: SpacedRune {
rune: Rune(RUNE),
rune: Rune(RUNE + 2),
spacers: 0
},
symbol: Some('¢'),
timestamp: 10,
timestamp: 24,
turbo: false,
}
),
Expand All @@ -608,26 +608,29 @@ fn get_runes() {
}
),
(
RuneId { block: 24, tx: 1 },
RuneId { block: 10, tx: 1 },
RuneEntry {
block: c.id.block,
block: a.id.block,
burned: 0,
terms: None,
divisibility: 0,
etching: c.output.reveal,
etching: a.output.reveal,
mints: 0,
number: 2,
number: 0,
premine: 1000,
spaced_rune: SpacedRune {
rune: Rune(RUNE + 2),
rune: Rune(RUNE),
spacers: 0
},
symbol: Some('¢'),
timestamp: 24,
timestamp: 10,
turbo: false,
}
)
]
],
more: false,
next: None,
prev: None,
}
);
}
Expand Down
Loading