@@ -44,6 +44,14 @@ pub struct File {
44
44
45
45
/// The number of bytes that accumulate in this `File` per tick.
46
46
bytes_per_tick : u64 ,
47
+
48
+ /// Whether the file is read-only -- that is, no more "writes" will ever
49
+ /// happen -- or not.
50
+ read_only : bool ,
51
+
52
+ /// The ordinal number of this File. If the file is foo.log the ordinal
53
+ /// number is 0, if foo.log.1 then 1 etc.
54
+ ordinal : u8 ,
47
55
}
48
56
49
57
impl File {
@@ -70,9 +78,10 @@ impl File {
70
78
/// This function runs the clock forward to `now`, updating `modified_tick`
71
79
/// and `status_tick` as bytes are continuously "written" to the `File`.
72
80
///
73
- /// Will have no result if `now` <= `modified_tick`.
81
+ /// Will have no result if `now` <= `modified_tick`. Will have no result if
82
+ /// the file is read-only.
74
83
fn advance_time ( & mut self , now : Tick ) {
75
- if now <= self . modified_tick {
84
+ if now <= self . modified_tick || self . read_only {
76
85
return ;
77
86
}
78
87
@@ -83,6 +92,39 @@ impl File {
83
92
self . modified_tick = now;
84
93
self . status_tick = now;
85
94
}
95
+
96
+ /// Set this file to read-only
97
+ ///
98
+ /// This function flips the internal bool on this `File` stopping any future
99
+ /// byte accumulations.
100
+ pub fn set_read_only ( & mut self ) {
101
+ self . read_only = true ;
102
+ }
103
+
104
+ /// Return whether the file is read-only or not
105
+ #[ must_use]
106
+ pub fn read_only ( & self ) -> bool {
107
+ self . read_only
108
+ }
109
+
110
+ /// Return the ordinal number of this File
111
+ #[ must_use]
112
+ pub fn ordinal ( & self ) -> u8 {
113
+ self . ordinal
114
+ }
115
+
116
+ /// Increment the ordinal number of this File
117
+ pub fn incr_ordinal ( & mut self ) {
118
+ self . ordinal = self . ordinal . saturating_add ( 1 ) ;
119
+ }
120
+
121
+ /// Returns the current size in bytes of the File
122
+ ///
123
+ /// This function does not advance time.
124
+ #[ must_use]
125
+ pub fn size ( & self ) -> u64 {
126
+ self . bytes_written
127
+ }
86
128
}
87
129
88
130
/// Model representation of a `Directory`. Contains children are `Directory`
@@ -133,6 +175,7 @@ pub struct State {
133
175
root_inode : Inode ,
134
176
now : Tick ,
135
177
block_cache : block:: Cache ,
178
+ max_bytes_per_file : u64 ,
136
179
}
137
180
138
181
/// The attributes of a `Node`.
@@ -164,7 +207,12 @@ pub enum NodeType {
164
207
impl State {
165
208
/// Create a new instance of `State`.
166
209
#[ tracing:: instrument( skip( block_cache) ) ]
167
- pub fn new ( bytes_per_tick : u64 , block_cache : block:: Cache ) -> State {
210
+ pub fn new (
211
+ bytes_per_tick : u64 ,
212
+ max_rotations : u8 ,
213
+ max_bytes_per_file : u64 ,
214
+ block_cache : block:: Cache ,
215
+ ) -> State {
168
216
let root_inode: Inode = 1 ; // `/`
169
217
let logs_inode: Inode = 2 ; // `/logs`
170
218
let foo_log_inode: Inode = 3 ; // `/logs/foo.log`
@@ -210,6 +258,9 @@ impl State {
210
258
status_tick : 0 ,
211
259
212
260
bytes_per_tick,
261
+
262
+ read_only : false ,
263
+ ordinal : 0 ,
213
264
} ;
214
265
nodes. insert (
215
266
foo_log_inode,
@@ -229,6 +280,7 @@ impl State {
229
280
root_inode,
230
281
now : 0 ,
231
282
block_cache,
283
+ max_bytes_per_file,
232
284
}
233
285
}
234
286
@@ -256,7 +308,22 @@ impl State {
256
308
match node {
257
309
Node :: File { file, .. } => {
258
310
file. advance_time ( now) ;
259
- // TODO add rotation logic here
311
+ if file. read_only ( ) {
312
+ continue ;
313
+ } ;
314
+ if file. size ( ) >= self . max_bytes_per_file {
315
+ file. set_read_only ( ) ;
316
+ // Add rotation logic here. What I want to do is add a
317
+ // new File with the name of the current `File` bump the
318
+ // ordinal number of `file` and make the new File have
319
+ // `file` as its peer. I need to adjust `self.nodes` to
320
+ // include the new File and also modify the name of the
321
+ // current `file`. Or I should just make a pre-defined
322
+ // array of names and index into them with the ordinal.
323
+ //
324
+ // If the ordinal of the `file` is past
325
+ // self.max_rotations we delete it.
326
+ }
260
327
}
261
328
Node :: Directory { .. } => {
262
329
// directories are immutable though time flows around them
0 commit comments