Skip to content
Draft
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
19 changes: 18 additions & 1 deletion pagefind/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{cmp::Ordering, path::PathBuf};

use anyhow::{bail, Result};
use anyhow::{bail, Result, anyhow};
use fossick::{FossickedData, Fossicker};
use futures::future::join_all;
use hashbrown::HashMap;
Expand All @@ -27,6 +27,10 @@ mod utils;

const PAGEFIND_VERSION: &str = env!("CARGO_PKG_VERSION");

pub struct IndexCatalogueRepresentation {
entries: Vec<(String, String)>
}

struct SearchState {
options: SearchOptions,
fossicked_pages: Vec<FossickedData>,
Expand Down Expand Up @@ -312,6 +316,19 @@ impl SearchState {
outdir
}

pub async fn get_index_catalogue(&self) -> Result<IndexCatalogueRepresentation> {
if self.built_indexes.is_empty() {
return Err(anyhow!("Indexes empty, are they built yet? (run get_files/write_files)"));
}
let entries: Vec<(String, String)> = self
.built_indexes
.iter()
.map(|indexes| indexes.fragments.clone()
.into_iter()
).flatten().collect();
Ok(IndexCatalogueRepresentation { entries })
}

pub async fn get_files(&self) -> Vec<SyntheticFile> {
let outdir = &self.options.bundle_output;

Expand Down
10 changes: 9 additions & 1 deletion pagefind/src/service/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use std::{collections::BTreeMap, path::PathBuf};
use crate::{
fossick::{parser::DomParserResult, Fossicker},
options::PagefindServiceConfig,
PagefindInboundConfig, SearchOptions, SearchState,
PagefindInboundConfig, SearchOptions, SearchState, IndexCatalogueRepresentation,
};

#[derive(Debug)]
Expand Down Expand Up @@ -208,6 +208,14 @@ impl PagefindIndex {
self.search_index.build_indexes().await?;
Ok(self.search_index.get_files().await)
}

/// Get the catalogue mappings from hashes to encoded data.
///
/// # Returns
/// An IndexCatalogueRepresentation containing the hash and content of each fragment.
pub async fn get_index_catalogue(&mut self) -> Result<IndexCatalogueRepresentation> {
self.search_index.get_index_catalogue().await
}
}

#[cfg(test)]
Expand Down
13 changes: 13 additions & 0 deletions pagefind/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,19 @@ pub async fn run_service() {
}
}
}
RequestAction::GetIndexCatalogue { index_id } => {
if let Some(index) = get_index(&mut indexes, index_id, err) {
match index.get_index_catalogue().await {
Ok(index_catalogue) => {
send(ResponseAction::GetIndexCatalogue {
entry_count: index_catalogue.entries.len(),
entries: index_catalogue.entries,
})
}
Err(e) => err(&e.to_string()),
}
}
}
RequestAction::GetFiles { index_id } => {
if let Some(index) = get_index(&mut indexes, index_id, err) {
match index.build_indexes().await {
Expand Down
3 changes: 3 additions & 0 deletions pagefind/src/service/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ pub(super) enum RequestAction {
GetFiles {
index_id: u32,
},
GetIndexCatalogue {
index_id: u32,
},
DeleteIndex {
index_id: u32,
},
Expand Down
4 changes: 4 additions & 0 deletions pagefind/src/service/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub(super) enum ResponseAction {
GetFiles {
files: Vec<SyntheticFileResponse>,
},
GetIndexCatalogue {
entries: Vec<(String, String)>,
entry_count: usize,
},
DeleteIndex {},
}

Expand Down
31 changes: 31 additions & 0 deletions wrappers/node/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const indexFns = (indexId) => {
addDirectory: (dir) => addDirectory(indexId, dir),
writeFiles: (options) => writeFiles(indexId, options),
getFiles: () => getFiles(indexId),
getIndexCatalogue: () => getIndexCatalogue(indexId),
deleteIndex: () => deleteIndex(indexId)
}
}
Expand Down Expand Up @@ -276,6 +277,36 @@ const getFiles = (indexId) => new Promise((resolve, reject) => {
);
});

/**
* @typedef {import ('pagefindService').GetIndexCatalogueResponse} GetIndexCatalogueResponse
*
* @param {number} indexId
* @returns {Promise<GetIndexCatalogueResponse>}
*/
const getIndexCatalogue = (indexId) => new Promise((resolve, reject) => {
const action = 'GetIndexCatalogue';
launch().sendMessage(
{
type: action,
index_id: indexId,
}, (response) => {
/** @type {function(InternalResponsePayload): Omit<GetIndexCatalogueResponse, 'errors'>?} */
const successCallback = (success) => {
if (success.type !== action) {
reject(`Message returned from backend should have been ${action}, but was ${success.type}`);
return null;
}

return {
entries: success.entries,
entryCount: success.entry_count
}
};
handleApiResponse(resolve, reject, response, successCallback);
}
);
});

/**
* @param {number} indexId
* @returns {Promise<null>}
Expand Down
Loading