-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimaryReplica.java
99 lines (95 loc) · 2.7 KB
/
PrimaryReplica.java
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
import java.lang.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class PrimaryReplica extends Replica
{
PrimaryReplica(int id,String ip)
{
super(id, ip);
}
//private int CSNCount=0;
//private ArrayList<Write> committedLog;
public void mergeWithDatabase()
{
for(Write w: writeLog)
{
//-------------ADD---------------//
if(w.getOp().equals("add"))
{ System.out.println("Song has been added to database");
database.add(w.getSongEntry());
int i =writeLog.indexOf(w);
w.setCSN(CSN);
CSN++;
writeLog.set(i,w);
committedLog.add(w);
//increment CSN, ADD CSN TO WRITE IN WRITE LOG
}
//-------------DELETE--------------//
if(w.getOp().equals("delete"))
{System.out.println("Song has been deleted from database");
for(SongEntry s: database)
{
if(s.getSongName().equals(w.getSongEntry().getSongName()))
{
database.remove(s);
}
}
int i =writeLog.indexOf(w);
w.setCSN(CSN);
CSN++;
writeLog.set(i,w);
committedLog.add(w);
//increment CSN, ADD CSN TO WRITE IN WRITE LOG
}
//-------------MODIFY--------------//
if(w.getOp().equals("modify"))
{
for(SongEntry s : database)
{
if(s.getSongName().equals(w.getSongEntry().getSongName()))
{
database.remove(s);
database.add(w.getSongEntry());
//store w with updated CSN in writelog for further propogation.
int i =writeLog.indexOf(w);
w.setCSN(CSN);
CSN++;
writeLog.set(i,w);
committedLog.add(w);
}
}
//increment CSN, ADD CSN TO WRITE IN WRITE LOG
}
}
}
/*
public void selectToSend()
{
ArrayList<Integer> V=receiveVersionVector(); //Version vector received from replica server
int RCSN = receiveCSN(); //highest CSN from the replica server
Thread.sleep(1000); //wait for Other server to start receiving
//-----------------------//
//SEND COMMITTED WRITES
if(RCSN<CSN)
{
for(int i=RCSN; i<CSN; i++) //Send over all the committed writes after RCSN
{
Write w = committedWriteLog.get(i);
sendWrite(w);
}
}
//-------------------------//
//SEND TENTATIVE WRITES
for(int i=0;i<writeLog.size();i++)
{
Write w = writeLog.get(i);
System.out.println("Song that is being sent: "+(w.getSongEntry()).getSongName());
if(V.get(w.getReplicaId())<=w.getAcceptTime()) //possible error comparing 2 Integers
{
System.out.println("sent");
sendWrite(w); //send new write to the server
}
}
}*/
}