1818@ Component
1919public class AOFContainer {
2020 private ReentrantLock bufferLock ;
21- private List <Command > buffer ;
2221 private ObjectOutputStream output ;
22+ private FileDescriptor fd ;
23+ private boolean alwaysFlush ;
2324
2425 @ Autowired
2526 private KevaConfig kevaConfig ;
2627
2728 public void init () {
29+ alwaysFlush = kevaConfig .getAofInterval () == 0 ;
2830 bufferLock = new ReentrantLock ();
29- buffer = new ArrayList <>(64 );
3031
3132 try {
32- boolean isExists = new File (getWorkingDir () + "keva.aof" ).exists ();
3333 FileOutputStream fos = new FileOutputStream (getWorkingDir () + "keva.aof" , true );
34- output = isExists ? new AppendOnlyObjectOutputStream (fos ) : new ObjectOutputStream (fos );
34+ fd = fos .getFD ();
35+ output = new ObjectOutputStream (fos );
3536 } catch (IOException e ) {
3637 if (e instanceof FileNotFoundException ) {
3738 log .info ("AOF file not found, creating new file..." );
@@ -45,11 +46,11 @@ public void init() {
4546 }
4647 }
4748
48- if (kevaConfig . getAofInterval () != 0 ) {
49+ if (! alwaysFlush ) {
4950 ScheduledExecutorService executorService = Executors .newSingleThreadScheduledExecutor ();
5051 executorService .scheduleAtFixedRate (() -> {
5152 try {
52- sync ();
53+ flush ();
5354 } catch (IOException e ) {
5455 log .error ("Error writing AOF file" , e );
5556 }
@@ -61,51 +62,28 @@ public void init() {
6162 }
6263
6364 public void write (Command command ) {
64- if (kevaConfig .getAofInterval () == 0 ) {
65- syncPerMutation (command );
66- return ;
67- }
68-
69- bufferLock .lock ();
70- try {
71- buffer .add (command );
72- } finally {
73- bufferLock .unlock ();
74- }
75- }
76-
77- public void sync () throws IOException {
78- if (buffer .isEmpty ()) {
79- return ;
80- }
8165 bufferLock .lock ();
8266 try {
83- for (Command command : buffer ) {
84- output .writeObject (command .getObjects ());
67+ output .writeObject (command .getObjects ());
68+ if (alwaysFlush ) {
69+ flush ();
8570 }
71+ } catch (IOException e ) {
72+ log .error ("Error writing AOF file" , e );
8673 } finally {
87- output .flush ();
88- for (Command command : buffer ) {
89- command .recycle ();
90- }
91- buffer .clear ();
9274 bufferLock .unlock ();
9375 }
9476 }
9577
96- public void syncPerMutation (Command command ) {
97- try {
98- output .writeObject (command .getObjects ());
99- output .flush ();
100- } catch (IOException e ) {
101- log .error ("Error writing AOF file" , e );
102- }
78+ private void flush () throws IOException {
79+ fd .sync ();
10380 }
10481
10582 public List <Command > read () throws IOException {
10683 try {
10784 List <Command > commands = new ArrayList <>(100 );
10885 FileInputStream fis = new FileInputStream (getWorkingDir () + "keva.aof" );
86+ log .info ("AOF size is: {}" , fis .getChannel ().size ());
10987 ObjectInputStream input = new ObjectInputStream (fis );
11088 while (true ) {
11189 try {
@@ -129,15 +107,4 @@ private String getWorkingDir() {
129107 String workingDir = kevaConfig .getWorkDirectory ();
130108 return workingDir .equals ("./" ) ? "" : workingDir + "/" ;
131109 }
132-
133- private static class AppendOnlyObjectOutputStream extends ObjectOutputStream {
134- public AppendOnlyObjectOutputStream (OutputStream out ) throws IOException {
135- super (out );
136- }
137-
138- @ Override
139- protected void writeStreamHeader () throws IOException {
140- reset ();
141- }
142- }
143110}
0 commit comments