-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathlog.rs
More file actions
238 lines (226 loc) · 7.08 KB
/
Copy pathlog.rs
File metadata and controls
238 lines (226 loc) · 7.08 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//! Core Log implementation with read and write APIs.
//!
//! This module provides the [`Log`] struct, the primary entry point for
//! interacting with OpenData Log. It exposes both write operations ([`append`])
//! and read operations ([`scan`], [`count`]) via the [`LogRead`] trait.
use std::ops::RangeBounds;
use bytes::Bytes;
use crate::config::{CountOptions, ScanOptions, WriteOptions};
use crate::error::Result;
use crate::model::{LogEntry, Record};
use crate::reader::{LogRead, LogReader};
/// An iterator over log entries for a specific key.
///
/// Created by [`LogRead::scan`] or [`LogRead::scan_with_options`]. Yields
/// entries in sequence number order within the specified range.
///
/// # Streaming Behavior
///
/// The iterator fetches entries lazily as they are consumed. Large scans
/// do not load all entries into memory at once.
///
/// # Example
///
/// ```ignore
/// let mut iter = log.scan(Bytes::from("orders"), 100..);
/// while let Some(entry) = iter.next().await? {
/// process_entry(entry);
/// }
/// ```
pub struct LogIterator {
// Implementation details will be added later
_private: (),
}
impl LogIterator {
/// Advances the iterator and returns the next log entry.
///
/// Returns `Ok(Some(entry))` if there is another entry in the range,
/// `Ok(None)` if the iteration is complete, or `Err` if an error occurred.
///
/// # Errors
///
/// Returns an error if there is a storage failure while reading entries.
pub async fn next(&mut self) -> Result<Option<LogEntry>> {
todo!()
}
}
/// The main log interface providing read and write operations.
///
/// `Log` is the primary entry point for interacting with OpenData Log.
/// It provides methods to append records, scan entries, and count records
/// within a key's log.
///
/// # Read Operations
///
/// Read operations are provided via the [`LogRead`] trait, which `Log`
/// implements. This allows generic code to work with either `Log` or
/// [`LogReader`].
///
/// # Thread Safety
///
/// `Log` is designed to be shared across threads. All methods take `&self`
/// and internal synchronization is handled automatically.
///
/// # Writer Semantics
///
/// Currently, each log supports a single writer. Multi-writer support may
/// be added in the future, but would require each key to have a single
/// writer to maintain monotonic ordering within that key's log.
///
/// # Example
///
/// ```ignore
/// use log::{Log, LogRead, Record, WriteOptions};
/// use bytes::Bytes;
///
/// // Open a log (implementation details TBD)
/// let log = Log::open(path, options).await?;
///
/// // Append records
/// let records = vec![
/// Record { key: Bytes::from("user:123"), value: Bytes::from("event-a") },
/// Record { key: Bytes::from("user:456"), value: Bytes::from("event-b") },
/// ];
/// log.append(records).await?;
///
/// // Scan entries for a specific key
/// let mut iter = log.scan(Bytes::from("user:123"), ..);
/// while let Some(entry) = iter.next().await? {
/// println!("seq={}: {:?}", entry.sequence, entry.value);
/// }
///
/// // Get a read-only view
/// let reader = log.reader();
/// ```
pub struct Log {
// Implementation details will be added later
_private: (),
}
impl Log {
/// Opens or creates a log with the given configuration.
///
/// This is the primary entry point for creating a `Log` instance. The
/// configuration specifies the storage backend and other settings.
///
/// # Arguments
///
/// * `config` - Configuration specifying storage backend and settings.
///
/// # Errors
///
/// Returns an error if the storage backend cannot be initialized.
///
/// # Example
///
/// ```ignore
/// use log::{Log, Config};
///
/// let log = Log::open(Config::default()).await?;
/// ```
pub async fn open(_config: crate::config::Config) -> crate::error::Result<Self> {
todo!()
}
/// Appends records to the log.
///
/// Records are assigned sequence numbers in the order they appear in the
/// input vector. All records in a single append call are written atomically.
///
/// This method uses default write options. Use [`append_with_options`] for
/// custom durability settings.
///
/// # Arguments
///
/// * `records` - The records to append. Each record specifies its target
/// key and value.
///
/// # Errors
///
/// Returns an error if the write fails due to storage issues.
///
/// # Example
///
/// ```ignore
/// let records = vec![
/// Record { key: Bytes::from("events"), value: Bytes::from("event-1") },
/// Record { key: Bytes::from("events"), value: Bytes::from("event-2") },
/// ];
/// log.append(records).await?;
/// ```
///
/// [`append_with_options`]: Log::append_with_options
pub async fn append(&self, records: Vec<Record>) -> Result<()> {
self.append_with_options(records, WriteOptions::default())
.await
}
/// Appends records to the log with custom options.
///
/// Records are assigned sequence numbers in the order they appear in the
/// input vector. All records in a single append call are written atomically.
///
/// # Arguments
///
/// * `records` - The records to append. Each record specifies its target
/// key and value.
/// * `options` - Write options controlling durability behavior.
///
/// # Errors
///
/// Returns an error if the write fails due to storage issues.
///
/// # Example
///
/// ```ignore
/// let records = vec![
/// Record { key: Bytes::from("events"), value: Bytes::from("critical-event") },
/// ];
/// let options = WriteOptions { await_durable: true };
/// log.append_with_options(records, options).await?;
/// ```
pub async fn append_with_options(
&self,
_records: Vec<Record>,
_options: WriteOptions,
) -> Result<()> {
todo!()
}
/// Creates a read-only view of the log.
///
/// The returned [`LogReader`] provides access to all read operations
/// ([`scan`](LogRead::scan), [`count`](LogRead::count)) but not write
/// operations. This is useful for consumers that should not have write
/// access to the log.
///
/// # Example
///
/// ```ignore
/// let reader = log.reader();
///
/// // Reader can scan and count
/// let iter = reader.scan(Bytes::from("orders"), ..);
/// let count = reader.count(Bytes::from("orders"), ..).await?;
///
/// // But cannot append (this would be a compile error):
/// // reader.append(records).await?; // ERROR: no method `append`
/// ```
pub fn reader(&self) -> LogReader {
todo!()
}
}
impl LogRead for Log {
fn scan_with_options(
&self,
_key: Bytes,
_seq_range: impl RangeBounds<u64> + Send,
_options: ScanOptions,
) -> LogIterator {
todo!()
}
async fn count_with_options(
&self,
_key: Bytes,
_seq_range: impl RangeBounds<u64> + Send,
_options: CountOptions,
) -> Result<u64> {
todo!()
}
}