Project

General

Profile

Statistics
| Revision:

root / DistributedBackupService / src / chunk / Chunk.java @ 2

History | View | Annotate | Download (2.88 KB)

1 1 up20130859
package chunk;
2
3
import java.util.HashSet;
4
import java.util.Set;
5
import java.util.concurrent.ConcurrentHashMap;
6
import java.util.concurrent.ConcurrentMap;
7
import java.util.concurrent.atomic.AtomicIntegerArray;
8
9
public class Chunk {
10
11
        private ConcurrentMap<String, AtomicIntegerArray> chunkReplication;
12
13
        private String fileID;
14
        private int chunkNo;
15
        private int replication;
16
        private byte[] data;
17
        private int size;
18
        private Set< Integer > chunks;
19
20
        public Chunk(String fileID, int chunkNo, byte[] data) {
21
                this.fileID = fileID;
22
                this.chunkNo = chunkNo;
23
                this.data = data;
24
        }
25
26
        public Chunk(String fileID, int chunkNo, int replication, byte[] data) {
27
                this(fileID, chunkNo, data);
28
                this.replication = replication;
29
        }
30
31
        public Chunk(Chunk chunkData, byte[] data) {
32
                this(chunkData.getFileID(), chunkData.getChunkNo(), chunkData.getReplication(), data);
33
        }
34
35
        public Chunk(int chunkNo, int replication) {
36
                this.chunkNo = chunkNo;
37
                this.replication = replication;
38
                this.chunks = new HashSet<>();
39
        }
40
41
        public Chunk(String fileID, int chunkNo, int replication, int size) {
42
                this(chunkNo, replication);
43
                this.fileID = fileID;
44
                this.size = size;
45
        }
46
47
        // Peer information
48
        public Chunk() {
49
                chunkReplication = new ConcurrentHashMap<>();
50
        }
51
52
        public void resetChunkReplication(String fileID) {
53
                chunkReplication.remove(fileID);
54
        }
55
56
        public void startChunkReplication(String fileID, int numChunks) {
57
                chunkReplication.putIfAbsent(fileID, new AtomicIntegerArray(numChunks));
58
        }
59
60
        public Integer addChunkReplication(String fileID, int chunkNo) {
61
                if (!chunkReplication.containsKey(fileID))
62
                        return null;
63
64
                int replication = chunkReplication.get(fileID).addAndGet(chunkNo, 1);
65
                System.out.println("Incrementing replication of " + fileID + "/" + chunkNo + " to " + replication);
66
                return replication;
67
        }
68
69
        public int getChunkReplication(String fileID, int chunkNo) {
70
                return chunkReplication.get(fileID).get(chunkNo);
71
        }
72
73
        public AtomicIntegerArray getChunkReplication(String fileID) {
74
                return chunkReplication.get(fileID);
75
        }
76
77
        // Getters and setters
78
79
        public String getFileID() { return fileID; }
80
81
        public int getChunkNo() { return chunkNo; }
82
83
        public int getReplication() { return replication; }
84
85
        public byte[] getData() { return data; }
86
87
        public boolean removeChunk(Integer peerID) { return chunks.remove(peerID); }
88
89
        public boolean addChunk(Integer peerID) { return chunks.add(peerID); }
90
91
        public int getNumChunks() { return chunks.size(); }
92
93
        public int getSize() { return size; }
94
95
        public Set<Integer> getChunks() { return chunks; }
96
97
        public void setFileID(String fileID) { this.fileID = fileID; }
98
99
        public void setChunkNo(int chunkNo) { this.chunkNo = chunkNo; }
100
101
        public void setReplication(int replication) { this.replication = replication; }
102
103
        public void setData(byte[] data) { this.data = data; }
104
105
        public void setSize(int size) { this.size = size; }
106
107
        public void setChunks(Set<Integer> mirrors) { this.chunks = mirrors; }
108
109
}