1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//! Functions for the various authorities to handle queries

use self::get_agent_activity_query::hashes::GetAgentActivityQuery;
use self::get_agent_activity_query::must_get_agent_activity::must_get_agent_activity;
use self::get_entry_ops_query::GetEntryOpsQuery;
use self::get_links_ops_query::GetLinksOpsQuery;
use self::{
    get_agent_activity_query::deterministic::DeterministicGetAgentActivityQuery,
    get_record_query::GetRecordOpsQuery,
};

use super::error::CascadeResult;
use holo_hash::ActionHash;
use holo_hash::AgentPubKey;
use holochain_state::query::link::GetLinksQuery;
use holochain_state::query::Query;
use holochain_state::query::Txn;
use holochain_types::prelude::*;
use holochain_zome_types::agent_activity::DeterministicGetAgentActivityFilter;
use tracing::*;

#[cfg(test)]
mod test;

pub(crate) mod get_agent_activity_query;
pub(crate) mod get_entry_ops_query;
pub(crate) mod get_links_ops_query;
pub(crate) mod get_record_query;

/// Handler for get_entry query to an Entry authority
#[instrument(skip(db))]
pub async fn handle_get_entry(
    db: DbRead<DbKindDht>,
    hash: EntryHash,
    _options: holochain_p2p::event::GetOptions,
) -> CascadeResult<WireEntryOps> {
    let query = GetEntryOpsQuery::new(hash);
    let results = db.read_async(move |txn| query.run(Txn::from(&txn))).await?;
    Ok(results)
}

/// Handler for get_record query to a Record authority
#[tracing::instrument(skip(env))]
pub async fn handle_get_record(
    env: DbRead<DbKindDht>,
    hash: ActionHash,
    options: holochain_p2p::event::GetOptions,
) -> CascadeResult<WireRecordOps> {
    let query = GetRecordOpsQuery::new(hash, options);
    let results = env
        .read_async(move |txn| query.run(Txn::from(&txn)))
        .await?;
    Ok(results)
}

/// Handler for get_agent_activity query to an Activity authority
#[instrument(skip(env))]
pub async fn handle_get_agent_activity(
    env: DbRead<DbKindDht>,
    agent: AgentPubKey,
    query: ChainQueryFilter,
    options: holochain_p2p::event::GetActivityOptions,
) -> CascadeResult<AgentActivityResponse<ActionHash>> {
    let query = GetAgentActivityQuery::new(agent, query, options);
    let results = env
        .read_async(move |txn| query.run(Txn::from(&txn)))
        .await?;
    Ok(results)
}

/// Handler for must_get_agent_activity query to an Activity authority
#[instrument(skip(env))]
pub async fn handle_must_get_agent_activity(
    env: DbRead<DbKindDht>,
    author: AgentPubKey,
    filter: ChainFilter,
) -> CascadeResult<MustGetAgentActivityResponse> {
    Ok(must_get_agent_activity(env, author, filter).await?)
}

/// Handler for get_agent_activity_deterministic query to an Activity authority
#[instrument(skip(env))]
pub async fn handle_get_agent_activity_deterministic(
    env: DbRead<DbKindDht>,
    agent: AgentPubKey,
    filter: DeterministicGetAgentActivityFilter,
    options: holochain_p2p::event::GetActivityOptions,
) -> CascadeResult<DeterministicGetAgentActivityResponse> {
    let query = DeterministicGetAgentActivityQuery::new(agent, filter, options);
    let results = env
        .read_async(move |txn| query.run(Txn::from(&txn)))
        .await?;
    Ok(results)
}

/// Handler for get_links query to a Record/Entry authority
#[instrument(skip(env, _options))]
pub async fn handle_get_links(
    env: DbRead<DbKindDht>,
    link_key: WireLinkKey,
    _options: holochain_p2p::event::GetLinksOptions,
) -> CascadeResult<WireLinkOps> {
    let query = GetLinksOpsQuery::new(link_key);
    let results = env
        .read_async(move |txn| query.run(Txn::from(&txn)))
        .await?;
    Ok(results)
}

/// Handler for querying links
#[instrument(skip(db))]
pub async fn handle_get_links_query(
    db: DbRead<DbKindDht>,
    query: WireLinkQuery,
) -> CascadeResult<Vec<Link>> {
    let get_links_query = GetLinksQuery::new(
        query.base.clone(),
        query.link_type.clone(),
        query.tag_prefix.clone(),
        query.into(),
    );
    Ok(db
        .read_async(move |txn| get_links_query.run(Txn::from(&txn)))
        .await?)
}