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
mod data;
mod operation;
mod relation;
mod search;
mod sequence;
mod sort;
mod temporary_data;

pub use data::SessionData;
pub use operation::{Depends, Pend, SessionOperation, SessionRecord};
pub use search::SessionSearchResult;
pub use sort::{SessionCustomOrder, SessionOrder, SessionOrderKey};
pub use temporary_data::{TemporaryData, TemporaryDataEntity};

use std::{io::Write, path::Path};

use hashbrown::HashMap;

use crate::{CollectionRow, Depend, Field, IdxFile, SessionDatabase};

use relation::SessionRelation;
use sequence::SequenceNumber;
use serde::Serialize;

use self::sequence::SequenceCursor;

#[derive(Serialize)]
pub struct SessionInfo {
    pub(super) name: String,
    pub(super) access_at: u64,
    pub(super) expire: i64,
}

impl SessionInfo {
    pub fn name(&self) -> &str {
        &self.name
    }
    pub fn access_at(&self) -> u64 {
        self.access_at
    }
    pub fn expire(&self) -> i64 {
        self.expire
    }
}

pub struct Session {
    name: String,
    pub(super) session_data: Option<SessionData>,
    pub(super) temporary_data: TemporaryData,
}
impl Session {
    pub(super) fn new(
        main_database: &SessionDatabase,
        name: impl Into<String>,
        expire_interval_sec: Option<i64>,
    ) -> Self {
        let mut name: String = name.into();
        assert!(name != "");
        if name == "" {
            name = "untitiled".to_owned();
        }
        let session_dir = main_database.session_dir(&name);
        if !session_dir.exists() {
            std::fs::create_dir_all(&session_dir).unwrap();
        }
        let session_data = Self::new_data(&session_dir, expire_interval_sec);
        let temporary_data = session_data.init_temporary_data();
        Self {
            name,
            session_data: Some(session_data),
            temporary_data,
        }
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn set_sequence_cursor(&mut self, current: usize) {
        if let Some(session_data) = &mut self.session_data {
            session_data.sequence_number.set_current(current);
        }
    }

    pub fn sequence_cursor(&self) -> Option<SequenceCursor> {
        self.session_data
            .as_ref()
            .map(|session_data| SequenceCursor {
                max: session_data.sequence_number.max(),
                current: session_data.sequence_number.current(),
            })
    }

    pub fn new_data(session_dir: &Path, expire_interval_sec: Option<i64>) -> SessionData {
        let mut access = session_dir.to_path_buf();
        access.push("expire");
        let mut file = std::fs::OpenOptions::new()
            .create(true)
            .write(true)
            .open(access)
            .unwrap();
        let expire = expire_interval_sec.unwrap_or(-1);
        file.write(&expire.to_be_bytes()).unwrap();

        let mut fields = HashMap::new();
        let mut fields_dir = session_dir.to_path_buf();
        fields_dir.push("fields");
        if !fields_dir.exists() {
            std::fs::create_dir_all(&fields_dir.to_owned()).unwrap();
        }
        for p in fields_dir.read_dir().unwrap().into_iter() {
            let p = p.unwrap();
            let path = p.path();
            if path.is_dir() {
                if let Some(fname) = p.file_name().to_str() {
                    let field = Field::new(path, 1);
                    fields.insert(fname.to_owned(), field);
                }
            }
        }

        SessionData {
            sequence_number: SequenceNumber::new({
                let mut path = session_dir.to_path_buf();
                path.push("sequence_number.i");
                path
            }),
            sequence: IdxFile::new(
                {
                    let mut path = session_dir.to_path_buf();
                    path.push("sequence.i");
                    path
                },
                1,
            ),
            collection_id: IdxFile::new(
                {
                    let mut path = session_dir.to_path_buf();
                    path.push("collection_id.i");
                    path
                },
                1,
            ),
            row: IdxFile::new(
                {
                    let mut path = session_dir.to_path_buf();
                    path.push("row.i");
                    path
                },
                1,
            ),
            operation: IdxFile::new(
                {
                    let mut path = session_dir.to_path_buf();
                    path.push("operation.i");
                    path
                },
                1,
            ),
            activity: IdxFile::new(
                {
                    let mut path = session_dir.to_path_buf();
                    path.push("activity.i");
                    path
                },
                1,
            ),
            term_begin: IdxFile::new(
                {
                    let mut path = session_dir.to_path_buf();
                    path.push("term_begin.i");
                    path
                },
                1,
            ),
            term_end: IdxFile::new(
                {
                    let mut path = session_dir.to_path_buf();
                    path.push("term_end.i");
                    path
                },
                1,
            ),
            uuid: IdxFile::new(
                {
                    let mut path = session_dir.to_path_buf();
                    path.push("uuid.i");
                    path
                },
                1,
            ),
            fields,
            relation: SessionRelation::new(session_dir, 1),
        }
    }
}