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
use std::sync::Arc;
use std::{mem, ptr::copy_nonoverlapping};

use nando_support::{ImageValue, ObjectVersion};

use crate::{
    collections::pmap::PHashMap,
    object::ObjectHeader,
    tls::{IntoObjectMapping, ObjectMapping},
};
use crate::{IPtr, Object, ObjectId, Persistable};

#[derive(Clone, Debug)]
pub struct MaterializedObjectVersion {
    id: ObjectId,
    version: ObjectVersion,
    is_cacheable: bool,
    source_bytes: Vec<u8>,
}

impl MaterializedObjectVersion {
    fn get_constraint_set(&self) -> &'static PHashMap<ObjectId, ObjectVersion> {
        let map_offset = mem::offset_of!(ObjectHeader, object_version_constraints);
        let pmap_size = mem::size_of::<PHashMap<ObjectId, ObjectVersion>>();
        let bytes = &self.source_bytes.as_slice()[map_offset..(map_offset + pmap_size)];
        PHashMap::from_bytes_ref(bytes as *const [u8])
    }

    pub fn read_into<T: Persistable>(&self) -> &'static T {
        let bytes = &self.source_bytes.as_slice()[Object::header_size()..];
        return T::from_bytes_ref(bytes as *const [u8]);
    }

    pub fn apply_changes(
        &mut self,
        new_version: ObjectVersion,
        changes: &Vec<(IPtr, &ImageValue)>,
    ) {
        self.version = new_version;

        for (iptr, change) in changes {
            let offset: usize = iptr.offset.try_into().unwrap();
            let count = iptr.size.try_into().unwrap();
            if offset + count > self.source_bytes.len() {
                let extra_len = offset + count - self.source_bytes.len();
                self.source_bytes
                    .resize(self.source_bytes.len() + extra_len, 0);
            }

            let source_bytes = self.source_bytes.as_mut_ptr();
            unsafe {
                let target_slice = source_bytes.byte_offset(offset.try_into().unwrap());
                copy_nonoverlapping(change.data.as_ptr(), target_slice as *mut u8, count);
            }
        }
    }

    pub fn set_cacheable(&mut self, cacheable: bool) {
        self.is_cacheable = cacheable;
    }

    pub fn is_cacheable(&self) -> bool {
        self.is_cacheable
    }

    pub fn get_id(&self) -> ObjectId {
        self.id
    }

    pub fn get_version(&self) -> ObjectVersion {
        self.version
    }

    pub fn iptr_of(&self) -> IPtr {
        IPtr {
            object_id: self.id,
            offset: 0,
            size: 0,
        }
    }

    pub fn get_mapping_bounds(&self) -> (usize, usize) {
        let slice = &self.source_bytes.as_slice()[Object::header_size()..];
        let start = slice as *const _ as *const () as usize;
        (start, start + self.source_bytes.len())
    }

    pub fn get_version_constraint(&self, foreign_object: ObjectId) -> Option<ObjectVersion> {
        self.get_constraint_set().get(&foreign_object).copied()
    }

    pub fn from_version(other: &MaterializedObjectVersion) -> Self {
        let original_len = other.source_bytes.len();
        let mut source_bytes = Vec::with_capacity(original_len);

        unsafe {
            other
                .source_bytes
                .as_ptr()
                .copy_to_nonoverlapping(source_bytes.as_mut_ptr(), original_len);
            source_bytes.set_len(original_len);
        }

        Self {
            id: other.id,
            version: other.version,
            is_cacheable: other.is_cacheable,
            source_bytes,
        }
    }
}

impl From<&Arc<Object>> for MaterializedObjectVersion {
    fn from(obj: &Arc<Object>) -> Self {
        MaterializedObjectVersion {
            id: obj.id,
            version: obj.get_version(),
            is_cacheable: obj.object_is_cacheable(),
            source_bytes: unsafe { (&*obj.as_bytes()).to_vec() },
        }
    }
}

impl From<&Object> for MaterializedObjectVersion {
    fn from(obj: &Object) -> Self {
        MaterializedObjectVersion {
            id: obj.id,
            version: obj.get_version(),
            is_cacheable: obj.object_is_cacheable(),
            source_bytes: unsafe { (&*obj.as_bytes()).to_vec() },
        }
    }
}

impl IntoObjectMapping for Arc<MaterializedObjectVersion> {
    fn into_mapping(&self) -> ObjectMapping {
        let (start, end) = self.get_mapping_bounds();

        ObjectMapping {
            id: self.id,
            start,
            end,
        }
    }
}