18
18
* @author thaivc
19
19
* @since 2024
20
20
*/
21
- public class LogRepository
22
- {
21
+ public class LogRepository {
23
22
/**
24
23
* The first segment number.
25
24
*/
@@ -38,13 +37,15 @@ public class LogRepository
38
37
FileChannel indexChannel ;
39
38
FileChannel logChannel ;
40
39
40
+ long logOffset ;
41
+ long indexOffset ;
42
+
41
43
/**
42
44
* Constructs a `LogRepository` with the specified base log directory.
43
45
*
44
46
* @param baseLogDir the base directory for log files
45
47
*/
46
- public LogRepository (String baseLogDir )
47
- {
48
+ public LogRepository (String baseLogDir ) {
48
49
LogUtil .createDirectoryNX (baseLogDir );
49
50
this .baseLogDir = baseLogDir ;
50
51
}
@@ -59,21 +60,21 @@ public LogRepository(String baseLogDir)
59
60
* @return the segment number of the latest log file, or 0 if no log files are found
60
61
* @throws IOException if an I/O error occurs while accessing the log directory
61
62
*/
62
- public long getLatestSegment () throws IOException
63
- {
63
+ public long getLatestSegment () throws IOException {
64
64
Optional <Path > lastFile = Files .list (Paths .get (baseLogDir ))
65
65
.filter (Files ::isRegularFile )
66
66
.max (Comparator .comparing (Path ::getFileName ));
67
67
return lastFile .map (path -> LogUtil .segment (path .getFileName ().toString ())).orElse (-1L );
68
68
}
69
69
70
- private void generateFiles (long segment ) throws IOException
71
- {
70
+ private void generateFiles (long segment ) throws IOException {
72
71
indexFile = new RandomAccessFile (indexPath (segment ), "rw" );
73
72
logFile = new RandomAccessFile (logPath (segment ), "rw" );
74
73
currentIndex = totalRecords (currentSegment );
75
74
indexChannel = indexFile .getChannel ();
76
75
logChannel = logFile .getChannel ();
76
+ logOffset = logFile .length ();
77
+ indexOffset = indexFile .length ();
77
78
}
78
79
79
80
/**
@@ -86,11 +87,9 @@ private void generateFiles(long segment) throws IOException
86
87
* @throws IOException if an I/O error occurs during the file operations
87
88
* @throws IllegalArgumentException if the specified segment is older than the current segment
88
89
*/
89
- public void switchToSegment (long segment ) throws IOException
90
- {
90
+ public void switchToSegment (long segment ) throws IOException {
91
91
long latestSegment = getLatestSegment ();
92
- if (segment < latestSegment )
93
- {
92
+ if (segment < latestSegment ) {
94
93
throw new IllegalArgumentException ("Cannot switch to an older segment" );
95
94
}
96
95
currentSegment = segment ;
@@ -106,8 +105,7 @@ public void switchToSegment(long segment) throws IOException
106
105
* @param readerBuffer the buffer to read the log entry into
107
106
* @throws IOException if an I/O error occurs
108
107
*/
109
- public void read (long segment , long index , ByteBuffer readerBuffer ) throws IOException
110
- {
108
+ public void read (long segment , long index , ByteBuffer readerBuffer ) throws IOException {
111
109
RandomAccessFile indexFileRead = indexFile != null && segment == currentSegment ? indexFile : new RandomAccessFile (indexPath (segment ), "r" );
112
110
RandomAccessFile logFileRead = logFile != null && segment == currentSegment ? logFile : new RandomAccessFile (logPath (segment ), "r" );
113
111
@@ -134,17 +132,16 @@ public void read(long segment, long index, ByteBuffer readerBuffer) throws IOExc
134
132
* @param logs the buffer containing the log entries to append
135
133
* @throws IOException if an I/O error occurs during the write operation
136
134
*/
137
- public void append (ByteBuffer logs ) throws IOException
138
- {
135
+ public void append (ByteBuffer logs ) throws IOException {
139
136
indexBufferWriter .clear ();
140
137
indexBufferWriter .putLong (logFile .length ());
141
138
indexBufferWriter .putInt (logs .limit ());
142
139
indexBufferWriter .flip ();
143
140
144
- var logOffset = logFile .length ();
145
- var indexOffset = indexFile .length ();
146
141
logChannel .write (logs , logOffset );
147
142
indexChannel .write (indexBufferWriter , indexOffset );
143
+ logOffset += logs .limit ();
144
+ indexOffset += LogIndex .SIZE ;
148
145
currentIndex ++;
149
146
}
150
147
@@ -157,19 +154,16 @@ public void append(ByteBuffer logs) throws IOException
157
154
* @param fromIndex the index from which to truncate
158
155
* @throws IOException if an I/O error occurs
159
156
*/
160
- public void truncate (long segment , long fromIndex ) throws IOException
161
- {
157
+ public void truncate (long segment , long fromIndex ) throws IOException {
162
158
File indexFileSys = new File (indexPath (segment ));
163
- if (!indexFileSys .exists ())
164
- {
159
+ if (!indexFileSys .exists ()) {
165
160
return ;
166
161
}
167
162
168
163
try (
169
164
RandomAccessFile indexFile = new RandomAccessFile (indexPath (segment ), "rw" );
170
165
RandomAccessFile logFile = new RandomAccessFile (logPath (segment ), "rw" )
171
- )
172
- {
166
+ ) {
173
167
indexBufferReader .clear ();
174
168
var indexChannel = indexFile .getChannel ();
175
169
indexChannel .read (indexBufferReader , fromIndex * LogIndex .SIZE );
@@ -187,19 +181,16 @@ public void truncate(long segment, long fromIndex) throws IOException
187
181
* @return the total number of records
188
182
* @throws IOException if an I/O error occurs
189
183
*/
190
- public long totalRecords (long segment ) throws IOException
191
- {
184
+ public long totalRecords (long segment ) throws IOException {
192
185
RandomAccessFile indexFileRead = segment == currentSegment ? indexFile : new RandomAccessFile (indexPath (segment ), "r" );
193
186
return indexFileRead .length () / LogIndex .SIZE ;
194
187
}
195
188
196
- private String indexPath (long segment )
197
- {
189
+ private String indexPath (long segment ) {
198
190
return baseLogDir + "/" + LogUtil .indexName (segment );
199
191
}
200
192
201
- private String logPath (long segment )
202
- {
193
+ private String logPath (long segment ) {
203
194
return baseLogDir + "/" + LogUtil .logName (segment );
204
195
}
205
196
}
0 commit comments