11use crate :: {
2- engine:: profile:: EngineProfile , engine:: target_state:: TargetStateProviderRegistry ,
3- engine:: txn_batcher:: TxnBatcher , prelude:: * ,
2+ engine:: profile:: EngineProfile ,
3+ engine:: target_state:: TargetStateProviderRegistry ,
4+ prelude:: * ,
5+ state_store:: { ReadTxn , TxnBatcher } ,
46} ;
57
68use cocoindex_utils:: fingerprint:: Fingerprint ;
@@ -133,13 +135,26 @@ impl<Prof: EngineProfile> Environment<Prof> {
133135 & self . inner . db_env
134136 }
135137
138+ /// Create the per-app sub-database for this environment and wrap it in a
139+ /// `Store`. Hides the underlying LMDB sub-database creation from
140+ /// `App::new`.
141+ pub fn create_app_store ( & self , app_name : & str ) -> Result < crate :: state_store:: Store > {
142+ let mut wtxn = self . inner . db_env . write_txn ( ) ?;
143+ let db = self
144+ . inner
145+ . db_env
146+ . create_database ( & mut wtxn, Some ( app_name) ) ?;
147+ wtxn. commit ( ) ?;
148+ Ok ( crate :: state_store:: Store :: new ( db) )
149+ }
150+
136151 /// Open an LMDB read transaction with automatic retry on `MDB_READERS_FULL`.
137152 ///
138153 /// Two-phase strategy:
139154 /// 1. Retry with a short timeout — handles transient reader slot contention.
140155 /// 2. If phase 1 times out, call `clear_stale_readers()` to reclaim slots
141156 /// from dead processes, then retry indefinitely.
142- pub async fn read_txn ( & self ) -> Result < heed :: RoTxn < ' _ , heed :: WithoutTls > > {
157+ pub async fn read_txn ( & self ) -> Result < ReadTxn < ' _ > > {
143158 let db_env = self . db_env ( ) ;
144159 let try_read_txn = || async {
145160 match db_env. read_txn ( ) {
@@ -156,7 +171,7 @@ impl<Prof: EngineProfile> Environment<Prof> {
156171
157172 // Phase 1: short timeout for transient concurrency.
158173 match retryable:: run ( & try_read_txn, & LMDB_READ_TXN_RETRY_PHASE1 ) . await {
159- Ok ( txn) => return Ok ( txn) ,
174+ Ok ( txn) => return Ok ( ReadTxn :: new ( txn) ) ,
160175 Err ( e) if !e. is_retryable => return Err ( e. into ( ) ) ,
161176 Err ( _) => { }
162177 }
@@ -168,6 +183,7 @@ impl<Prof: EngineProfile> Environment<Prof> {
168183 }
169184 retryable:: run ( & try_read_txn, & LMDB_READ_TXN_RETRY_PHASE2 )
170185 . await
186+ . map ( ReadTxn :: new)
171187 . map_err ( Into :: into)
172188 }
173189
0 commit comments