Skip to content

Commit

Permalink
feat(homeserver): add DEFAULT_LIST_LIMIT and DEFAULT_MAX_LIST_LIMIT
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuhvi committed Sep 27, 2024
1 parent 560972c commit f029171
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 50 deletions.
1 change: 0 additions & 1 deletion pubky-homeserver/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub mod tables;

use tables::{Tables, TABLES_COUNT};

pub const MAX_LIST_LIMIT: u16 = 100;
pub const DEFAULT_MAP_SIZE: usize = 10995116277760; // 10TB (not = disk-space used)

#[derive(Debug, Clone)]
Expand Down
50 changes: 50 additions & 0 deletions pubky-homeserver/src/database/tables/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use heed::{
use postcard::{from_bytes, to_allocvec};
use serde::{Deserialize, Serialize};

use crate::database::DB;

/// Event [Timestamp] base32 => Encoded event.
pub type EventsTable = Database<Str, Bytes>;

Expand Down Expand Up @@ -56,3 +58,51 @@ impl Event {
}
}
}

const MAX_LIST_LIMIT: u16 = 1000;
const DEFAULT_LIST_LIMIT: u16 = 100;

impl DB {
pub fn list_events(
&self,
limit: Option<u16>,
cursor: Option<&str>,
) -> anyhow::Result<Vec<String>> {
let txn = self.env.read_txn()?;

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

let mut cursor = cursor.unwrap_or("0000000000000");

// Cursor smaller than 13 character is invalid
// TODO: should we send an error instead?
if cursor.len() < 13 {
cursor = "0000000000000"
}

let mut result: Vec<String> = vec![];
let mut next_cursor = cursor.to_string();

for _ in 0..limit {
match self.tables.events.get_greater_than(&txn, &next_cursor)? {
Some((timestamp, event_bytes)) => {
let event = Event::deserialize(event_bytes)?;

let line = format!("{} {}", event.operation(), event.url());
next_cursor = timestamp.to_string();

result.push(line);
}
None => break,
};
}

if !result.is_empty() {
result.push(format!("cursor: {next_cursor}"))
}

txn.commit()?;

Ok(result)
}
}
53 changes: 4 additions & 49 deletions pubky-homeserver/src/routes/feed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,16 @@ use axum::{
response::IntoResponse,
};

use crate::{
database::{tables::events::Event, MAX_LIST_LIMIT},
error::Result,
server::AppState,
};
use crate::{error::Result, server::AppState};

pub async fn feed(
State(state): State<AppState>,
Query(params): Query<HashMap<String, String>>,
) -> Result<impl IntoResponse> {
let txn = state.db.env.read_txn()?;

let limit = params
.get("limit")
.and_then(|l| l.parse::<u16>().ok())
.unwrap_or(MAX_LIST_LIMIT)
.min(MAX_LIST_LIMIT);

let mut cursor = params
.get("cursor")
.map(|c| c.as_str())
.unwrap_or("0000000000000");

// Guard against bad cursor
if cursor.len() < 13 {
cursor = "0000000000000"
}

let mut result: Vec<String> = vec![];
let mut next_cursor = cursor.to_string();

for _ in 0..limit {
match state
.db
.tables
.events
.get_greater_than(&txn, &next_cursor)?
{
Some((timestamp, event_bytes)) => {
let event = Event::deserialize(event_bytes)?;

let line = format!("{} {}", event.operation(), event.url());
next_cursor = timestamp.to_string();

result.push(line);
}
None => break,
};
}

if !result.is_empty() {
result.push(format!("cursor: {next_cursor}"))
}
let limit = params.get("limit").and_then(|l| l.parse::<u16>().ok());
let cursor = params.get("cursor").map(|c| c.as_str());

txn.commit()?;
let result = state.db.list_events(limit, cursor)?;

Ok(Response::builder()
.status(StatusCode::OK)
Expand Down

0 comments on commit f029171

Please sign in to comment.