daoxide 0.1.0

High-performance Rust library for DAOS (Distributed Asynchronous Object Storage)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
//! High-level ergonomic API facade for DAOS operations.
//!
//! This module provides a streamlined API that reduces boilerplate for common
//! DAOS workflows while preserving full access to lower-level APIs when needed.
//!
//! # Design Philosophy
//!
//! The facade follows these principles:
//! - **Sensible defaults**: Common operations work with minimal configuration
//! - **Escape hatches**: Lower-level APIs remain accessible for advanced use
//! - **Type safety**: All safety boundaries from lower layers preserved
//! - **No hidden state**: No global mutable singletons introduced
//!
//! # Architecture
//!
//! The facade is organized around the natural DAOS hierarchy:
//!
//! ```text
//! DaosClient (runtime + pool + container)
//!   ├── pool()      -> &Pool (escape hatch)
//!   ├── container() -> &Container (escape hatch)
//!   ├── ObjectBuilder (oid generation + open)
//!   │     └── open() -> Object (escape hatch to object API)
//!   └── kv()        -> KvClient (dkey/akey/sgl management)
//! ```
//!
//! # Example: Simple KV Operations
//!
//! ```ignore
//! use daoxide::facade::DaosClient;
//!
//! // Connect to DAOS with minimal boilerplate
//! let client = DaosClient::builder()
//!     .pool_label("mypool")
//!     .container_label("mycontainer")
//!     .build()?;
//!
//! // Store and retrieve values
//! client.put(b"my_dkey", b"my_akey", b"hello world")?;
//! let value = client.get::<_, _, Vec<u8>>(b"my_dkey", b"my_akey")?;
//!
//! // Or use transactions for atomic operations
//! client.put_tx(b"my_dkey", b"my_akey", b"atomic value")?;
//! ```
//!
//! # Escape Hatches
//!
//! All facade types provide paths to lower-level APIs:
//!
//! - `DaosClient::pool()` returns `&Pool` for direct pool operations
//! - `DaosClient::container()` returns `&Container` for container operations
//! - `ObjectBuilder::open()` returns `Object` for object operations
//! - `KvClient` methods accept `Tx` for transaction control

use crate::container::{Container, ContainerOpen};
use crate::error::{DaosError, Result};
use crate::io::{AKey, DKey, IoBuffer, Iod, IodSingleBuilder, Sgl};
use crate::object::{
    Object, ObjectClass, ObjectClassHints, ObjectId, ObjectOpenMode, ObjectType, generate_oid,
};
use crate::pool::{Pool, PoolBuilder};
use crate::runtime::DaosRuntime;
use crate::tx::Tx;

/// Error type for facade-specific errors that don't map to DAOS errors.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FacadeError {
    /// Client not yet built or already disconnected.
    NotConnected,
    /// Invalid configuration provided.
    InvalidConfig(String),
    /// Underlying DAOS error.
    Daos(DaosError),
}

impl std::fmt::Display for FacadeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FacadeError::NotConnected => write!(f, "client not connected"),
            FacadeError::InvalidConfig(msg) => write!(f, "invalid configuration: {}", msg),
            FacadeError::Daos(e) => write!(f, "DAOS error: {}", e),
        }
    }
}

impl std::error::Error for FacadeError {}

impl From<DaosError> for FacadeError {
    fn from(err: DaosError) -> Self {
        FacadeError::Daos(err)
    }
}

impl From<FacadeError> for DaosError {
    fn from(err: FacadeError) -> Self {
        match err {
            FacadeError::Daos(e) => e,
            FacadeError::NotConnected => DaosError::InvalidArg,
            FacadeError::InvalidConfig(_) => DaosError::InvalidArg,
        }
    }
}

/// Builder for configuring a [`DaosClient`] connection.
///
/// # Example
///
/// ```ignore
/// use daoxide::facade::DaosClientBuilder;
///
/// let client = DaosClientBuilder::new()
///     .pool_label("mypool")
///     .container_label("mycontainer")
///     .object_type(daoxide::object::ObjectType::KvHashed)
///     .build()?;
/// ```
#[derive(Debug)]
pub struct DaosClientBuilder {
    pool_label: Option<String>,
    pool_uuid: Option<String>,
    pool_sys: Option<String>,
    pool_flags: u32,
    container_label: Option<String>,
    container_uuid: Option<String>,
    container_flags: u32,
    object_type: ObjectType,
    object_class: ObjectClass,
    object_hints: ObjectClassHints,
}

impl Default for DaosClientBuilder {
    fn default() -> Self {
        Self {
            pool_label: None,
            pool_uuid: None,
            pool_sys: None,
            pool_flags: 0,
            container_label: None,
            container_uuid: None,
            container_flags: 0,
            object_type: ObjectType::KvHashed,
            object_class: ObjectClass::UNKNOWN,
            object_hints: ObjectClassHints::NONE,
        }
    }
}

impl DaosClientBuilder {
    /// Creates a new builder with default settings.
    #[inline]
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the pool label for connection.
    ///
    /// Either pool_label or pool_uuid must be provided, but not both.
    #[inline]
    pub fn pool_label(mut self, label: impl Into<String>) -> Self {
        self.pool_label = Some(label.into());
        self
    }

    /// Sets the pool UUID for connection.
    ///
    /// Either pool_label or pool_uuid must be provided, but not both.
    #[inline]
    pub fn pool_uuid(mut self, uuid: impl Into<String>) -> Self {
        self.pool_uuid = Some(uuid.into());
        self
    }

    /// Sets the DAOS system name.
    #[inline]
    pub fn pool_system(mut self, sys: impl Into<String>) -> Self {
        self.pool_sys = Some(sys.into());
        self
    }

    /// Sets the pool connection flags.
    #[inline]
    pub fn pool_flags(mut self, flags: u32) -> Self {
        self.pool_flags = flags;
        self
    }

    /// Sets the container label for creation/opening.
    ///
    /// Either container_label or container_uuid must be provided, but not both.
    #[inline]
    pub fn container_label(mut self, label: impl Into<String>) -> Self {
        self.container_label = Some(label.into());
        self
    }

    /// Sets the container UUID for opening.
    ///
    /// Either container_label or container_uuid must be provided, but not both.
    #[inline]
    pub fn container_uuid(mut self, uuid: impl Into<String>) -> Self {
        self.container_uuid = Some(uuid.into());
        self
    }

    /// Sets the container open flags.
    #[inline]
    pub fn container_flags(mut self, flags: u32) -> Self {
        self.container_flags = flags;
        self
    }

    /// Sets the default object type for objects created through this client.
    ///
    /// Defaults to [`ObjectType::KvHashed`].
    #[inline]
    pub fn object_type(mut self, ty: ObjectType) -> Self {
        self.object_type = ty;
        self
    }

    /// Sets the default object class for objects created through this client.
    ///
    /// Defaults to [`ObjectClass::UNKNOWN`] (DAOS selects based on hints/container properties).
    #[inline]
    pub fn object_class(mut self, oc: ObjectClass) -> Self {
        self.object_class = oc;
        self
    }

    /// Sets the default object class hints for objects created through this client.
    ///
    /// Defaults to [`ObjectClassHints::NONE`].
    #[inline]
    pub fn object_hints(mut self, hints: ObjectClassHints) -> Self {
        self.object_hints = hints;
        self
    }

    /// Validates the builder configuration.
    fn validate(&self) -> std::result::Result<(), FacadeError> {
        match (&self.pool_label, &self.pool_uuid) {
            (Some(_), Some(_)) => Err(FacadeError::InvalidConfig(
                "cannot specify both pool_label and pool_uuid".into(),
            )),
            (None, None) => Err(FacadeError::InvalidConfig(
                "must specify either pool_label or pool_uuid".into(),
            )),
            _ => Ok(()),
        }?;

        match (&self.container_label, &self.container_uuid) {
            (Some(_), Some(_)) => Err(FacadeError::InvalidConfig(
                "cannot specify both container_label and container_uuid".into(),
            )),
            (None, None) => Err(FacadeError::InvalidConfig(
                "must specify either container_label or container_uuid".into(),
            )),
            _ => Ok(()),
        }?;

        Ok(())
    }

    /// Builds a connected [`DaosClient`] from this builder.
    ///
    /// This initializes the DAOS runtime, connects to the pool, and opens/creates
    /// the container in one step.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Configuration is invalid
    /// - DAOS runtime cannot be initialized
    /// - Pool connection fails
    /// - Container creation/opening fails
    pub fn build(&self) -> Result<DaosClient> {
        self.validate()?;

        let runtime = DaosRuntime::new()?;

        let pool = {
            let mut builder = PoolBuilder::new();
            if let Some(ref label) = self.pool_label {
                builder = builder.label(label);
            }
            if let Some(ref uuid) = self.pool_uuid {
                builder = builder.uuid(uuid);
            }
            if let Some(ref sys) = self.pool_sys {
                builder = builder.system(sys);
            }
            builder.flags(self.pool_flags).build()?
        };

        let (container_identifier, open_by) = if let Some(label) = self.container_label.as_deref() {
            (label.to_string(), ContainerOpen::ByLabel)
        } else if let Some(uuid) = self.container_uuid.as_deref() {
            (uuid.to_string(), ContainerOpen::ByUuid)
        } else {
            unreachable!("validated above");
        };

        // Validate accessibility of target container and fail fast on mismatch.
        let _ = pool.open_container(&container_identifier, open_by, self.container_flags)?;

        Ok(DaosClient {
            runtime,
            pool,
            container_label: container_identifier,
            container_flags: self.container_flags,
            default_object_type: self.object_type,
            default_object_class: self.object_class,
            default_object_hints: self.object_hints,
        })
    }
}

/// High-level DAOS client that encapsulates runtime, pool, and container.
///
/// `DaosClient` provides a streamlined interface for common DAOS operations
/// while preserving access to lower-level APIs through escape hatches.
///
/// # Lifecycle
///
/// A `DaosClient` manages:
/// - DAOS runtime initialization (reference-counted)
/// - Pool connection (automatically disconnected on drop)
/// - Container lifecycle (automatically closed on drop)
///
/// # Escape Hatches
///
/// While `DaosClient` provides convenience methods, lower-level APIs remain accessible:
///
/// - [`DaosClient::pool`] - Direct pool operations
/// - [`DaosClient::container`] - Direct container operations
/// - [`DaosClient::object_builder`] - Object creation with full control
///
/// # Example
///
/// ```ignore
/// use daoxide::facade::DaosClient;
///
/// let client = DaosClient::builder()
///     .pool_label("mypool")
///     .container_label("mycontainer")
///     .build()?;
///
/// // Use the client for operations...
///
/// // Or access lower-level APIs:
/// let pool = client.pool();
/// let container = client.container();
/// ```
#[derive(Debug)]
pub struct DaosClient {
    #[allow(dead_code)]
    runtime: DaosRuntime,
    pool: Pool,
    container_label: String,
    container_flags: u32,
    default_object_type: ObjectType,
    default_object_class: ObjectClass,
    default_object_hints: ObjectClassHints,
}

impl DaosClient {
    /// Creates a new `DaosClientBuilder` for configuring a connection.
    #[inline]
    pub fn builder() -> DaosClientBuilder {
        DaosClientBuilder::new()
    }

    /// Returns a reference to the underlying pool.
    ///
    /// This is an escape hatch for advanced pool operations not exposed
    /// by the facade.
    #[inline]
    pub fn pool(&self) -> &Pool {
        &self.pool
    }

    /// Returns a newly opened container reference.
    ///
    /// This is an escape hatch for advanced container operations not exposed
    /// by the facade. Note that each call creates a new container handle;
    /// the container is automatically closed when the handle is dropped.
    pub fn container(&self) -> Result<Container<'_>> {
        self.pool.open_container(
            &self.container_label,
            ContainerOpen::ByLabel,
            self.container_flags,
        )
    }

    /// Returns the default object type for this client.
    #[inline]
    pub fn default_object_type(&self) -> ObjectType {
        self.default_object_type
    }

    /// Returns the default object class for this client.
    #[inline]
    pub fn default_object_class(&self) -> ObjectClass {
        self.default_object_class
    }

    /// Returns the default object hints for this client.
    #[inline]
    pub fn default_object_hints(&self) -> ObjectClassHints {
        self.default_object_hints
    }

    /// Allocates a new object ID in the container.
    ///
    /// This is a convenience method that combines container.alloc_oids()
    /// with OID encoding via generate_oid().
    ///
    /// # Arguments
    ///
    /// * `object_type` - The type of object to create
    /// * `oclass` - Object class (use `ObjectClass::UNKNOWN` for default)
    /// * `hints` - Object class hints
    ///
    /// # Errors
    ///
    /// Returns an error if OID allocation or encoding fails.
    pub fn alloc_oid(
        &self,
        object_type: ObjectType,
        oclass: ObjectClass,
        hints: ObjectClassHints,
    ) -> Result<ObjectId> {
        let container = self.container()?;
        let coh = container.as_handle()?;
        // DAOS requires caller-provided low bits to be unique in the container.
        // Allocate one OID slot first, then encode type/class/hints into it.
        let lo = container.alloc_oids(1)?;
        let mut oid = ObjectId::from_parts(0, lo);
        generate_oid(coh, &mut oid, object_type, oclass, hints)?;
        Ok(oid)
    }

    /// Creates a new [`ObjectBuilder`] for opening/creating an object.
    ///
    /// This is the primary entry point for object operations through the facade.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use daoxide::object::ObjectType;
    ///
    /// let object = client
    ///     .object_builder()
    ///     .object_type(ObjectType::KvHashed)
    ///     .open(ObjectOpenMode::ReadWrite)?;
    /// ```
    #[inline]
    pub fn object_builder(&self) -> ObjectBuilder<'_> {
        ObjectBuilder::new(self)
    }

    /// Opens an object with the default type/class/hints.
    ///
    /// This is a convenience method for simple use cases where the default
    /// object configuration is sufficient.
    ///
    /// # Arguments
    ///
    /// * `oid` - The object ID to open
    /// * `mode` - The open mode (read/write/exclusive)
    ///
    /// # Errors
    ///
    /// Returns an error if the object cannot be opened.
    #[inline]
    pub fn open_object(&self, oid: ObjectId, mode: ObjectOpenMode) -> Result<Object> {
        let container = self.container()?;
        Object::open_in(&container, oid, mode)
    }

    /// Stores a value in an object without a transaction.
    ///
    /// This is a convenience method that:
    /// 1. Opens the object (or creates if needed)
    /// 2. Creates dkey/akey/iod from the key arguments
    /// 3. Calls update
    ///
    /// # Type Parameters
    ///
    /// * `D` - DKey type (auto-converted from bytes)
    /// * `A` - AKey type (auto-converted from bytes)
    /// * `V` - Value type (must be `AsRef<[u8]>`)
    ///
    /// # Arguments
    ///
    /// * `oid` - Object ID to write to
    /// * `dkey` - Distribution key (e.g., `b"my_dkey"`)
    /// * `akey` - Attribute key (e.g., `b"my_akey"`)
    /// * `value` - Value to store (any type implementing `AsRef<[u8]>`)
    ///
    /// # Errors
    ///
    /// Returns an error if the operation fails.
    pub fn put<D, A, V>(&self, oid: ObjectId, dkey: D, akey: A, value: V) -> Result<()>
    where
        D: AsRef<[u8]>,
        A: AsRef<[u8]>,
        V: AsRef<[u8]>,
    {
        self.put_tx(oid, Tx::none(), dkey, akey, value)
    }

    /// Stores a value in an object with explicit transaction control.
    ///
    /// # Type Parameters
    ///
    /// * `D` - DKey type (auto-converted from bytes)
    /// * `A` - AKey type (auto-converted from bytes)
    /// * `V` - Value type (must be `AsRef<[u8]>`)
    ///
    /// # Arguments
    ///
    /// * `oid` - Object ID to write to
    /// * `tx` - Transaction to use (or `Tx::none()` for no transaction)
    /// * `dkey` - Distribution key
    /// * `akey` - Attribute key
    /// * `value` - Value to store
    pub fn put_tx<D, A, V>(
        &self,
        oid: ObjectId,
        tx: Tx<'_>,
        dkey: D,
        akey: A,
        value: V,
    ) -> Result<()>
    where
        D: AsRef<[u8]>,
        A: AsRef<[u8]>,
        V: AsRef<[u8]>,
    {
        let container = self.container()?;
        let object = Object::open_in(&container, oid, ObjectOpenMode::ReadWrite)?;

        let dkey = DKey::new(dkey.as_ref().to_vec())?;
        let akey = AKey::new(akey.as_ref().to_vec())?;
        let value_bytes = value.as_ref();

        let iod = Iod::Single(
            IodSingleBuilder::new(akey)
                .value_len(value_bytes.len())
                .build()?,
        );
        let sgl = Sgl::builder()
            .push(IoBuffer::from_vec(value_bytes.to_vec()))
            .build()?;

        object.update(&tx, &dkey, &iod, &sgl)
    }

    /// Retrieves a value from an object without a transaction.
    ///
    /// # Type Parameters
    ///
    /// * `D` - DKey type (auto-converted from bytes)
    /// * `A` - AKey type (auto-converted from bytes)
    /// * `V` - Value buffer type (must be `AsRef<[u8]>` for input, `Vec<u8>` for output)
    ///
    /// # Arguments
    ///
    /// * `oid` - Object ID to read from
    /// * `dkey` - Distribution key
    /// * `akey` - Attribute key
    /// * `buffer` - Buffer to read into (will be filled with data)
    ///
    /// # Errors
    ///
    /// Returns an error if the operation fails.
    pub fn get<D, A>(&self, oid: ObjectId, dkey: D, akey: A, buffer: &mut [u8]) -> Result<()>
    where
        D: AsRef<[u8]>,
        A: AsRef<[u8]>,
    {
        self.get_tx(oid, Tx::none(), dkey, akey, buffer)
    }

    /// Retrieves a value from an object with explicit transaction control.
    ///
    /// # Type Parameters
    ///
    /// * `D` - DKey type
    /// * `A` - AKey type
    /// * `V` - Value buffer type
    ///
    /// # Arguments
    ///
    /// * `oid` - Object ID to read from
    /// * `tx` - Transaction to use
    /// * `dkey` - Distribution key
    /// * `akey` - Attribute key
    /// * `buffer` - Buffer to read into
    pub fn get_tx<D, A>(
        &self,
        oid: ObjectId,
        tx: Tx<'_>,
        dkey: D,
        akey: A,
        buffer: &mut [u8],
    ) -> Result<()>
    where
        D: AsRef<[u8]>,
        A: AsRef<[u8]>,
    {
        let container = self.container()?;
        let object = Object::open_in(&container, oid, ObjectOpenMode::ReadWrite)?;

        let dkey = DKey::new(dkey.as_ref().to_vec())?;
        let akey = AKey::new(akey.as_ref().to_vec())?;
        let iod = Iod::Single(
            IodSingleBuilder::new(akey)
                .value_len(buffer.len())
                .build()?,
        );
        let mut sgl = Sgl::builder()
            .push(IoBuffer::from_vec(buffer.to_vec()))
            .build()?;

        object.fetch(&tx, &dkey, &iod, &mut sgl)?;
        let fetched = sgl
            .buffers()
            .first()
            .ok_or(DaosError::Internal("empty SGL after fetch".into()))?;
        if fetched.len() != buffer.len() {
            return Err(DaosError::Internal("fetch buffer length mismatch".into()));
        }
        buffer.copy_from_slice(fetched.as_slice());
        Ok(())
    }

    /// Deletes keys from an object.
    ///
    /// # Arguments
    ///
    /// * `oid` - Object ID
    /// * `dkey` - Distribution key to delete (if `Some`)
    /// * `akeys` - Attribute keys to delete within the dkey (if `Some`)
    /// * `tx` - Transaction to use
    pub fn delete(
        &self,
        oid: ObjectId,
        dkey: Option<&[u8]>,
        akeys: Option<&[&[u8]]>,
        tx: Tx<'_>,
    ) -> Result<()> {
        let container = self.container()?;
        let object = Object::open_in(&container, oid, ObjectOpenMode::ReadWrite)?;

        match (dkey, akeys) {
            (Some(dk), Some(aks)) => {
                let dkey = DKey::new(dk)?;
                let mut akeys_vec: Vec<AKey> = Vec::new();
                for a in aks {
                    akeys_vec.push(AKey::new(*a)?);
                }
                object.punch_akeys(&tx, &dkey, &akeys_vec)
            }
            (Some(dk), None) => {
                let dkey = DKey::new(dk)?;
                object.punch_dkeys(&tx, &[dkey])
            }
            (None, _) => object.punch(&tx),
        }
    }
}

/// Builder for configuring and opening a DAOS object.
///
/// Provides fine-grained control over object creation parameters while
/// reducing the boilerplate of the open sequence.
///
/// # Example
///
/// ```ignore
/// use daoxide::object::{ObjectOpenMode, ObjectType};
///
/// let object = client
///     .object_builder()
///     .object_type(ObjectType::KvHashed)
///     .object_class(my_class)
///     .open(ObjectOpenMode::ReadWrite)?;
/// ```
#[derive(Debug)]
pub struct ObjectBuilder<'c> {
    client: &'c DaosClient,
    object_type: ObjectType,
    object_class: ObjectClass,
    object_hints: ObjectClassHints,
}

impl<'c> ObjectBuilder<'c> {
    /// Creates a new `ObjectBuilder` bound to the given client.
    fn new(client: &'c DaosClient) -> Self {
        Self {
            client,
            object_type: client.default_object_type,
            object_class: client.default_object_class,
            object_hints: client.default_object_hints,
        }
    }

    /// Sets the object type.
    ///
    /// Defaults to the client's default object type.
    #[inline]
    pub fn object_type(mut self, ty: ObjectType) -> Self {
        self.object_type = ty;
        self
    }

    /// Sets the object class.
    ///
    /// Defaults to the client's default object class.
    #[inline]
    pub fn object_class(mut self, oc: ObjectClass) -> Self {
        self.object_class = oc;
        self
    }

    /// Sets the object class hints.
    ///
    /// Defaults to the client's default object hints.
    #[inline]
    pub fn object_hints(mut self, hints: ObjectClassHints) -> Self {
        self.object_hints = hints;
        self
    }

    /// Allocates a new object ID with the configured type/class/hints.
    ///
    /// This is separate from open() to allow inspecting the OID before opening.
    #[inline]
    pub fn alloc(&self) -> Result<ObjectId> {
        self.client
            .alloc_oid(self.object_type, self.object_class, self.object_hints)
    }

    /// Allocates a new object ID and opens it with the given mode.
    ///
    /// This combines alloc() and open() into a single call for convenience.
    ///
    /// # Arguments
    ///
    /// * `mode` - The open mode (read/write/exclusive)
    pub fn create(self, mode: ObjectOpenMode) -> Result<Object> {
        let oid = self.alloc()?;
        let container = self.client.container()?;
        Object::open_in(&container, oid, mode)
    }

    /// Opens an existing object by ID with the given mode.
    ///
    /// Use this when you have a pre-existing OID (e.g., from enumeration
    /// or previous allocation).
    ///
    /// # Arguments
    ///
    /// * `oid` - The object ID to open
    /// * `mode` - The open mode
    #[inline]
    pub fn open(&self, oid: ObjectId, mode: ObjectOpenMode) -> Result<Object> {
        let container = self.client.container()?;
        Object::open_in(&container, oid, mode)
    }

    /// Opens an object with the given ID, or creates a new one if it doesn't exist.
    ///
    /// This is useful for "get or create" semantics where you want to ensure
    /// an object exists before operating on it.
    ///
    /// # Arguments
    ///
    /// * `oid` - The object ID (existing or newly allocated)
    /// * `mode` - The open mode
    #[inline]
    pub fn open_or_create(self, oid: ObjectId, mode: ObjectOpenMode) -> Result<Object> {
        let _ = self.alloc();
        self.open(oid, mode)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::container::flags::CONT_OPEN_RW;
    use crate::pool::flags::POOL_CONNECT_NONE;

    #[test]
    fn test_client_builder_default() {
        let builder = DaosClientBuilder::new();
        assert!(builder.pool_label.is_none());
        assert!(builder.pool_uuid.is_none());
        assert!(builder.container_label.is_none());
        assert!(builder.container_uuid.is_none());
    }

    #[test]
    fn test_client_builder_pool_chaining() {
        let builder = DaosClientBuilder::new()
            .pool_label("mypool")
            .pool_system("daos_server")
            .pool_flags(POOL_CONNECT_NONE);
        assert_eq!(builder.pool_label.as_deref(), Some("mypool"));
        assert_eq!(builder.pool_sys.as_deref(), Some("daos_server"));
        assert_eq!(builder.pool_flags, POOL_CONNECT_NONE);
    }

    #[test]
    fn test_client_builder_container_chaining() {
        let builder = DaosClientBuilder::new()
            .container_label("mycontainer")
            .container_flags(CONT_OPEN_RW);
        assert_eq!(builder.container_label.as_deref(), Some("mycontainer"));
        assert_eq!(builder.container_flags, CONT_OPEN_RW);
    }

    #[test]
    fn test_client_builder_object_config() {
        let builder = DaosClientBuilder::new()
            .object_type(ObjectType::KvHashed)
            .object_class(ObjectClass::UNKNOWN)
            .object_hints(ObjectClassHints::NONE);
        assert_eq!(builder.object_type, ObjectType::KvHashed);
    }

    #[test]
    fn test_client_builder_validate_pool_both_label_and_uuid() {
        let builder = DaosClientBuilder::new()
            .pool_label("mypool")
            .pool_uuid("12345678-1234-1234-1234-123456789012");
        let result = builder.validate();
        assert!(result.is_err());
    }

    #[test]
    fn test_client_builder_validate_container_both_label_and_uuid() {
        let builder = DaosClientBuilder::new()
            .container_label("mycontainer")
            .container_uuid("12345678-1234-1234-1234-123456789012");
        let result = builder.validate();
        assert!(result.is_err());
    }

    #[test]
    fn test_client_builder_validate_neither_pool() {
        let builder = DaosClientBuilder::new().container_label("mycontainer");
        let result = builder.validate();
        assert!(result.is_err());
    }

    #[test]
    fn test_client_builder_validate_neither_container() {
        let builder = DaosClientBuilder::new().pool_label("mypool");
        let result = builder.validate();
        assert!(result.is_err());
    }

    #[test]
    fn test_object_builder_default_type() {
        let builder = DaosClientBuilder::new();
        assert_eq!(builder.object_type, ObjectType::KvHashed);
    }

    #[test]
    fn test_facade_error_display() {
        let err = FacadeError::NotConnected;
        assert_eq!(format!("{}", err), "client not connected");

        let err = FacadeError::InvalidConfig("test".into());
        assert_eq!(format!("{}", err), "invalid configuration: test");

        let err = FacadeError::Daos(DaosError::NotFound);
        assert_eq!(format!("{}", err), "DAOS error: Entity not found");
    }

    #[test]
    fn test_facade_error_from_daos_error() {
        let facade_err: FacadeError = DaosError::NotFound.into();
        assert!(matches!(facade_err, FacadeError::Daos(DaosError::NotFound)));
    }

    #[test]
    fn test_daos_error_from_facade_error() {
        let daos_err: DaosError = FacadeError::Daos(DaosError::Busy).into();
        assert!(matches!(daos_err, DaosError::Busy));

        let daos_err: DaosError = FacadeError::NotConnected.into();
        assert!(matches!(daos_err, DaosError::InvalidArg));
    }

    #[test]
    fn test_object_builder_debug() {
        let builder = DaosClientBuilder::new()
            .pool_label("mypool")
            .container_label("mycontainer");
        let debug_str = format!("{:?}", builder);
        assert!(debug_str.contains("DaosClientBuilder"));
    }

    #[test]
    fn test_daos_client_builder_all_options() {
        let builder = DaosClientBuilder::new()
            .pool_label("mypool")
            .pool_system("daos_server")
            .pool_flags(1)
            .container_label("mycontainer")
            .container_flags(2)
            .object_type(ObjectType::Array)
            .object_class(ObjectClass::UNKNOWN)
            .object_hints(ObjectClassHints::NONE);

        assert_eq!(builder.pool_label.as_deref(), Some("mypool"));
        assert_eq!(builder.pool_sys.as_deref(), Some("daos_server"));
        assert_eq!(builder.pool_flags, 1);
        assert_eq!(builder.container_label.as_deref(), Some("mycontainer"));
        assert_eq!(builder.container_flags, 2);
        assert_eq!(builder.object_type, ObjectType::Array);
    }

    #[test]
    fn test_daos_client_builder_uuid_based() {
        let builder = DaosClientBuilder::new()
            .pool_uuid("12345678-1234-1234-1234-123456789012")
            .container_uuid("87654321-4321-4321-4321-210987654321");

        assert!(builder.pool_label.is_none());
        assert!(builder.pool_uuid.is_some());
        assert!(builder.container_label.is_none());
        assert!(builder.container_uuid.is_some());
    }

    #[test]
    fn test_object_builder_chaining() {
        let builder = DaosClientBuilder::new()
            .pool_label("mypool")
            .container_label("mycontainer")
            .object_type(ObjectType::MultiHashed)
            .object_class(ObjectClass::UNKNOWN)
            .object_hints(ObjectClassHints::RDD_RP);

        assert_eq!(builder.object_type, ObjectType::MultiHashed);
        assert_eq!(builder.object_class, ObjectClass::UNKNOWN);
        assert!(builder.object_hints.as_raw() & ObjectClassHints::RDD_RP.as_raw() != 0);
    }

    #[test]
    fn test_facade_error_not_connected_display() {
        let err = FacadeError::NotConnected;
        assert!(format!("{:?}", err).contains("NotConnected"));
    }

    #[test]
    fn test_facade_error_invalid_config_display() {
        let err = FacadeError::InvalidConfig("missing pool".into());
        assert!(format!("{}", err).contains("missing pool"));
    }

    #[test]
    fn test_daos_client_debug() {
        // Can't build a real client without DAOS, but we can verify the debug impl exists
        let builder = DaosClientBuilder::new()
            .pool_label("mypool")
            .container_label("mycontainer");
        // Debug impl should compile
        let debug_str = format!("{:?}", builder);
        assert!(!debug_str.is_empty());
    }
}