-
Notifications
You must be signed in to change notification settings - Fork 2
/
mmap_file.clj
47 lines (36 loc) · 1.54 KB
/
mmap_file.clj
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
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at https://mozilla.org/MPL/2.0/.
(ns helins.binf.example.mmap-file
"Using BinF for writing and reading to a memory-mapped file on the JVM.
BinF protocols are implemented for ByteBuffer, parent of MappedByteBuffer."
;; We shall reuse our functions for writing and reading a date.
;;
(:require [helins.binf :as binf]
[helins.binf.example :as binf.example])
(:import java.io.RandomAccessFile
java.nio.channels.FileChannel$MapMode))
;;;;;;;;;; Eval interactively
(comment
;; Writing a date to '/tmp/binf-example.dat' and reading it back.
;;
(= [2021 3 16]
(with-open [file (RandomAccessFile. "/tmp/binf-example.dat"
"rw")]
(let [view (-> file
.getChannel
(.map FileChannel$MapMode/READ_WRITE
;; From byte 0 in the file
0
;; A size in bytes, we know a date is 4 bytes
4))]
(-> view
;; Writing date
(binf.example/wr-date 2021
3
16)
;; Ensuring changes are persisted on disk
.force
;; Reading it back from the start of the file
(binf/seek 0)
binf.example/rr-date)))))