Skip to main content

rouchdb_core/
adapter.rs

1use std::collections::HashMap;
2
3use async_trait::async_trait;
4
5use crate::document::*;
6use crate::error::Result;
7
8/// The trait all storage adapters must implement.
9///
10/// This mirrors PouchDB's internal adapter interface (underscore-prefixed
11/// methods in JavaScript). Each method corresponds to a CouchDB API endpoint.
12///
13/// Adapters are responsible for:
14/// - Storing and retrieving documents with full revision tree support
15/// - Tracking sequence numbers for the changes feed
16/// - Managing local (non-replicated) documents for checkpoints
17/// - Attachment storage and retrieval
18#[async_trait]
19pub trait Adapter: Send + Sync {
20    /// Get database information: name, document count, update sequence.
21    async fn info(&self) -> Result<DbInfo>;
22
23    /// Retrieve a single document by ID.
24    ///
25    /// Supports fetching specific revisions, open revisions (all leaves),
26    /// and including conflict information.
27    async fn get(&self, id: &str, opts: GetOptions) -> Result<crate::document::Document>;
28
29    /// Write multiple documents atomically.
30    ///
31    /// When `opts.new_edits` is `true` (default), the adapter generates new
32    /// revision IDs and checks for conflicts.
33    ///
34    /// When `opts.new_edits` is `false` (replication mode), the adapter
35    /// accepts revision IDs as-is and merges them into the existing revision
36    /// tree without conflict checks.
37    async fn bulk_docs(
38        &self,
39        docs: Vec<crate::document::Document>,
40        opts: BulkDocsOptions,
41    ) -> Result<Vec<DocResult>>;
42
43    /// Query all documents, optionally filtered by key range.
44    async fn all_docs(&self, opts: AllDocsOptions) -> Result<AllDocsResponse>;
45
46    /// Get changes since a given sequence number.
47    async fn changes(&self, opts: ChangesOptions) -> Result<ChangesResponse>;
48
49    /// Compare sets of document revisions to find which ones the adapter
50    /// is missing. Used during replication to avoid transferring data the
51    /// target already has.
52    async fn revs_diff(
53        &self,
54        revs: HashMap<String, Vec<String>>,
55    ) -> Result<RevsDiffResponse>;
56
57    /// Fetch multiple documents by ID and revision in a single request.
58    /// Used during replication to efficiently retrieve missing documents.
59    async fn bulk_get(&self, docs: Vec<BulkGetItem>) -> Result<BulkGetResponse>;
60
61    /// Store an attachment on a document.
62    async fn put_attachment(
63        &self,
64        doc_id: &str,
65        att_id: &str,
66        rev: &str,
67        data: Vec<u8>,
68        content_type: &str,
69    ) -> Result<DocResult>;
70
71    /// Retrieve raw attachment data.
72    async fn get_attachment(
73        &self,
74        doc_id: &str,
75        att_id: &str,
76        opts: GetAttachmentOptions,
77    ) -> Result<Vec<u8>>;
78
79    /// Retrieve a local document (not replicated, used for checkpoints).
80    async fn get_local(&self, id: &str) -> Result<serde_json::Value>;
81
82    /// Write a local document (not replicated, used for checkpoints).
83    async fn put_local(&self, id: &str, doc: serde_json::Value) -> Result<()>;
84
85    /// Remove a local document.
86    async fn remove_local(&self, id: &str) -> Result<()>;
87
88    /// Compact the database: remove old revisions, clean up unreferenced
89    /// attachment data.
90    async fn compact(&self) -> Result<()>;
91
92    /// Destroy the database and all its data.
93    async fn destroy(&self) -> Result<()>;
94}