summaryrefslogtreecommitdiff
path: root/ci-wasm-frontend/src/main.rs
blob: 8524eea75bbbcf85b4981b96be91730adda48230 (plain)
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
use fastly::{Request, Response};
use fastly::kv_store::KVStore;
use fastly::mime::Mime;

use std::time::{Duration, Instant};
use std::io::ErrorKind;

use sqlite_vfs::{DatabaseHandle, LockKind, OpenKind, OpenOptions, Vfs, WalDisabled};

pub fn main() {
    eprintln!("version: {}", std::env::var("FASTLY_SERVICE_VERSION").unwrap());
    let mut r = Request::from_client();
    let start = std::time::Instant::now();

    let qs = r.get_query_str();
    let mut registered = false;
    let mut vfs = "none?";

    let mut resp = Response::new();

    let register_start = std::time::Instant::now();

    if let Some(qs) = qs {
        resp.set_header("qs", qs);
        if qs.ends_with("vfs=fd") {
            sqlite_vfs::register("memvfs", FdVfs {}, true).unwrap();
            vfs = "fd";
            registered = true;
        }
    }

    if !registered {
        sqlite_vfs::register("memvfs", InMemVfs {}, true).unwrap();
        vfs = "inmem";
    }

    let register_done = register_start.elapsed();

    let db_start = std::time::Instant::now();

    let db = match rusqlite::Connection::open_with_flags_and_vfs(
        "state.db",
        rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY,
        "memvfs",
    ) {
        Ok(db) => db,
        Err(e) => {
            eprintln!("got error {} at {}",
                e,
                std::env::var("FASTLY_HOSTNAME").unwrap()
            );
            panic!();
            return;
        }
    };

    let db_done = db_start.elapsed();

    let handle_start = std::time::Instant::now();

    let resp_body: String = handle_req(db, &r).unwrap();

    resp.set_body(resp_body);
//    resp.set_content_type(Mime::HTML);
// associated item not found in `Mime` ???
    resp.set_header("content-type", "text/html");
    resp.set_header("version", std::env::var("FASTLY_SERVICE_VERSION").unwrap());
    resp.set_header("host", std::env::var("FASTLY_HOSTNAME").unwrap());
    resp.set_header("total-time", start.elapsed().as_micros().to_string());
    resp.set_header("render-time", handle_start.elapsed().as_micros().to_string());
    resp.set_header("register-time", register_done.as_micros().to_string());
    resp.set_header("db-time", db_done.as_micros().to_string());
    resp.set_header("vfs", vfs);

    resp.send_to_client()
}

use fastly_shared::FastlyStatus;
#[link(wasm_import_module = "fastly_object_store")]
extern "C" {
    #[link_name = "lookup_as_fd"]
    pub fn lookup_as_fd(
        kv_store_handle: fastly_sys::KVStoreHandle,
        key_ptr: *const u8,
        key_len: usize,
        fd_out: *mut std::ffi::c_int,
    ) -> FastlyStatus;
}

use std::os::wasi::prelude::RawFd;
fn lookup_fd(key: &str) -> Result<RawFd, String> {
    let store = KVStore::open("ci-state")
        .map_err(|e| format!("could not open store ci-state: {:?}", e))?
        .ok_or_else(|| "ci-state is not an existing store")?;
    let mut db_fd: std::ffi::c_int = -1;
    let res = unsafe { lookup_as_fd(store.as_handle().as_u32(), key.as_bytes().as_ptr(), key.as_bytes().len(), &mut db_fd) };
    if res != FastlyStatus::OK {
        return Err(format!("bad fastly status: {:?}", res));
    }

    Ok(db_fd as RawFd)
}

struct InMemVfsHandle {
    bytes: Vec<u8>
}

impl DatabaseHandle for InMemVfsHandle {
    type WalIndex = WalDisabled;

    fn size(&self) -> Result<u64, std::io::Error> {
        Ok(self.bytes.len() as u64)
    }

    fn read_exact_at(&mut self, buf: &mut [u8], offset: u64) -> Result<(), std::io::Error> {
        buf.copy_from_slice(&self.bytes[offset as usize..][..buf.len()]);
        Ok(())
    }

    fn write_all_at(&mut self, buf: &[u8], offset: u64) -> Result<(), std::io::Error> {
        eprintln!("write_all_at {}", offset);
        Err(std::io::Error::new(ErrorKind::Other, "no can do"))
    }

    fn sync(&mut self, data_only: bool) -> Result<(), std::io::Error> {
        Ok(())
    }

    fn set_len(&mut self, size: u64) -> Result<(), std::io::Error> {
        eprintln!("set_len {}", size);
        Err(std::io::Error::new(ErrorKind::Other, "no can do"))
    }

    fn lock(&mut self, lock: sqlite_vfs::LockKind) -> Result<bool, std::io::Error> {
        eprintln!("lock {:?}", lock);
        Ok(true)
    }

    fn reserved(&mut self) -> Result<bool, std::io::Error> {
        eprintln!("reserved?");
        Ok(false)
    }

    fn current_lock(&self) -> Result<sqlite_vfs::LockKind, std::io::Error> {
        eprintln!("current_lock");
        Ok(sqlite_vfs::LockKind::None)

    }

    fn wal_index(&self, readonly: bool) -> Result<WalDisabled, std::io::Error> {
        eprintln!("wal");
        Ok(WalDisabled::default())
    }

    fn set_chunk_size(&self, chunk_size: usize) -> Result<(), std::io::Error> {
        eprintln!("set chunk size {}", chunk_size);
        Err(std::io::Error::new(ErrorKind::Other, "no can do"))
    }
}

struct FdVfsHandle {
    fd: RawFd,
}

impl DatabaseHandle for FdVfsHandle {
    type WalIndex = WalDisabled;

    fn size(&self) -> Result<u64, std::io::Error> {
        use std::os::fd::{FromRawFd, IntoRawFd};
        let f = unsafe { std::fs::File::from_raw_fd(self.fd) };
        let len = f.metadata().unwrap().len();
        f.into_raw_fd();
        Ok(len)
    }

    fn read_exact_at(&mut self, buf: &mut [u8], offset: u64) -> Result<(), std::io::Error> {
        let mut remaining = buf;
        let mut total_read = 0;
        let mut to_read = remaining.len();
        let mut file_offs = offset;
        while total_read < to_read {
            let n_read = unsafe {
                libc::pread(
                    self.fd,
                    remaining.as_mut_ptr() as *mut std::ffi::c_void,
                    remaining.len(),
                    file_offs as i64,
                )
            };
            if n_read == 0 {
                panic!("stopped reading?");
            }
            if n_read < 0 {
                panic!("read err: {}", n_read);
            }
            let n_read = n_read as usize;
            total_read += n_read;
            remaining = &mut remaining[(n_read as usize)..];
        }

        Ok(())

    }

    fn write_all_at(&mut self, buf: &[u8], offset: u64) -> Result<(), std::io::Error> {
        eprintln!("write requested");
        Err(std::io::Error::new(ErrorKind::Other, "no can do"))
    }

    fn sync(&mut self, data_only: bool) -> Result<(), std::io::Error> {
        Ok(())
    }

    fn set_len(&mut self, size: u64) -> Result<(), std::io::Error> {
        eprintln!("set len {}", size);
        Err(std::io::Error::new(ErrorKind::Other, "no can do"))
    }

    fn lock(&mut self, lock: sqlite_vfs::LockKind) -> Result<bool, std::io::Error> {

        eprintln!("lock {:?}", lock);
        Ok(true)
    }

    fn reserved(&mut self) -> Result<bool, std::io::Error> {
        eprintln!("reserved requested");
        Ok(false)
    }

    fn current_lock(&self) -> Result<sqlite_vfs::LockKind, std::io::Error> {
        Ok(sqlite_vfs::LockKind::None)

    }

    fn wal_index(&self, readonly: bool) -> Result<WalDisabled, std::io::Error> {
        Ok(WalDisabled::default())
    }

    fn set_chunk_size(&self, chunk_size: usize) -> Result<(), std::io::Error> {
        Err(std::io::Error::new(ErrorKind::Other, "no can do"))
    }
}

struct InMemVfs;

impl Vfs for InMemVfs {
    type Handle = InMemVfsHandle;

    fn open(&self, db: &str, opts: OpenOptions) -> Result<Self::Handle, std::io::Error> {
        if opts.kind != OpenKind::MainDb {
            return Err(std::io::Error::new(ErrorKind::Other, "no"));
        }

        let store = KVStore::open("ci-state")
            .expect("can open ci-state")
            .expect("ci-state exists");

        let bytes = store.lookup_bytes(db).expect("lookup_works").expect("bytes exist");

        Ok(InMemVfsHandle { bytes })
//         Ok(FdVfsHandle { fd })
    }

    fn delete(&self, _db: &str) -> Result<(), std::io::Error> {
        // do nothing for deletes
        Err(std::io::Error::new(ErrorKind::Other, "no can do"))
    }

    fn exists(&self, db: &str) -> Result<bool, std::io::Error> {
        Ok(false)
    }

    fn temporary_name(&self) -> String {
        "temp name".to_string()
    }

    fn random(&self, buffer: &mut [i8]) {
        rand::Rng::fill(&mut rand::thread_rng(), buffer);
    }

    fn sleep(&self, duration: Duration) -> Duration {
        let now = Instant::now();
        std::thread::sleep(duration);
        now.elapsed()
    }
}
struct FdVfs;

impl Vfs for FdVfs {
    type Handle = FdVfsHandle;

    fn open(&self, db: &str, opts: OpenOptions) -> Result<Self::Handle, std::io::Error> {
        if opts.kind != OpenKind::MainDb {
            return Err(std::io::Error::new(ErrorKind::Other, "no"));
        }

        let store = KVStore::open("ci-state")
            .expect("can open ci-state")
            .expect("ci-state exists");

        let fd = lookup_fd(db).map_err(|msg| {
            std::io::Error::new(
                std::io::ErrorKind::Other,
                msg,
            )
        })?;

//         Ok(InMemVfsHandle { fd })
         Ok(FdVfsHandle { fd })
    }

    fn delete(&self, _db: &str) -> Result<(), std::io::Error> {
        // do nothing for deletes
        Err(std::io::Error::new(ErrorKind::Other, "no can do"))
    }

    fn exists(&self, db: &str) -> Result<bool, std::io::Error> {
        Ok(false)
    }

    fn temporary_name(&self) -> String {
        "temp name".to_string()
    }

    fn random(&self, buffer: &mut [i8]) {
        rand::Rng::fill(&mut rand::thread_rng(), buffer);
    }

    fn sleep(&self, duration: Duration) -> Duration {
        let now = Instant::now();
        std::thread::sleep(duration);
        now.elapsed()
    }
}

pub fn handle_req(db: rusqlite::Connection, req: &Request) -> Result<String, String> {
    println!("hi");

    use ci_lib_core::dbctx::DbCtx;
    use std::sync::Arc;
    use std::sync::Mutex;

    let ctx = Arc::new(DbCtx {
        config_path: "/".into(),
        conn: Mutex::new(db),
    });

    if req.get_url().path() == "/" {
        ci_lib_web::build_repo_index(&ctx)
    } else {
        Ok("a".to_string())
    }
}