Mercurial > hg > Database > Alice
annotate src/main/java/alice/datasegment/DataSegmentKey.java @ 650:4289b232b3fd
nulValue
author | suruga |
---|---|
date | Fri, 02 Feb 2018 18:26:49 +0900 |
parents | 646f705e65b1 |
children |
rev | line source |
---|---|
345 | 1 package alice.datasegment; |
2 | |
575
fe55be1ce12d
add ids compress local
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents:
531
diff
changeset
|
3 import java.io.IOException; |
345 | 4 import java.util.ArrayList; |
5 import java.util.Iterator; | |
6 | |
467 | 7 import alice.datasegment.Command; |
345 | 8 |
9 /** | |
523
145c425db88d
add CompressedLDSM
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents:
467
diff
changeset
|
10 * ここがコマンドの中身部分 |
145c425db88d
add CompressedLDSM
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents:
467
diff
changeset
|
11 * |
345 | 12 * Synchronized DataSegment for each DataSegment key |
13 * @author kazz | |
14 * | |
15 */ | |
16 public class DataSegmentKey { | |
419 | 17 |
18 private ArrayList<DataSegmentValue> dataList = new ArrayList<DataSegmentValue>(); | |
19 private ArrayList<Command> waitList = new ArrayList<Command>(); | |
20 private int tailIndex = 1; | |
345 | 21 |
419 | 22 public synchronized void runCommand(Command cmd) { |
23 switch (cmd.type) { | |
24 case UPDATE: | |
25 if (dataList.size() != 0) { | |
26 dataList.remove(0); | |
27 } | |
28 case PUT: | |
29 int index = tailIndex; | |
30 tailIndex++; | |
467 | 31 DataSegmentValue dsv = new DataSegmentValue(index, cmd.rData, cmd.reverseKey); |
419 | 32 dataList.add(dsv); |
33 // Process waiting peek and take commands | |
34 for (Iterator<Command> iter = waitList.iterator(); iter.hasNext(); ) { | |
35 Command waitCmd = iter.next(); | |
36 if (waitCmd.index < index) { | |
527 | 37 replyValue(waitCmd, dsv, cmd.getCompressFlag()); |
419 | 38 iter.remove(); |
39 if (waitCmd.type == CommandType.TAKE) { // someone is waiting for this put or update command | |
40 dataList.remove(dsv); | |
41 break; | |
42 } | |
43 } | |
44 } | |
45 break; | |
46 case PEEK: | |
47 if (cmd.index >= tailIndex) { | |
48 waitList.add(cmd); | |
49 break; | |
50 } | |
51 boolean waitFlag2 = true; | |
52 for (DataSegmentValue data : dataList) { | |
53 if (data.index > cmd.index) { | |
530 | 54 replyValue(cmd, data, cmd.getCompressFlag()); |
419 | 55 waitFlag2 = false; |
56 break; | |
57 } | |
58 } | |
59 if (waitFlag2) | |
60 waitList.add(cmd); | |
61 break; | |
62 case TAKE: | |
63 if (cmd.index >= tailIndex) { | |
64 waitList.add(cmd); | |
65 break; | |
66 } | |
67 boolean waitFlag = true; | |
68 for (Iterator<DataSegmentValue> iter = dataList.iterator(); iter.hasNext(); ) { | |
69 DataSegmentValue data = iter.next(); | |
70 if (data.index > cmd.index) { | |
527 | 71 replyValue(cmd, data, cmd.getCompressFlag()); |
419 | 72 iter.remove(); |
73 waitFlag = false; | |
74 break; | |
75 } | |
76 } | |
77 if (waitFlag) | |
78 waitList.add(cmd); | |
79 break; | |
80 case REMOVE: | |
81 // TODO: implements later | |
82 break; | |
83 default: | |
84 } | |
85 | |
86 } | |
87 | |
527 | 88 public void replyValue(Command cmd, DataSegmentValue data, boolean cFlag){ |
576 | 89 if (cFlag && !data.rData.compressed()){ |
575
fe55be1ce12d
add ids compress local
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents:
531
diff
changeset
|
90 try { |
fe55be1ce12d
add ids compress local
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents:
531
diff
changeset
|
91 data.rData.zip(); |
641 | 92 // System.out.println("in reply zip"); |
575
fe55be1ce12d
add ids compress local
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents:
531
diff
changeset
|
93 } catch (IOException e) { |
fe55be1ce12d
add ids compress local
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents:
531
diff
changeset
|
94 e.printStackTrace(); |
fe55be1ce12d
add ids compress local
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents:
531
diff
changeset
|
95 } |
fe55be1ce12d
add ids compress local
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents:
531
diff
changeset
|
96 } |
527 | 97 Command rCmd = new Command(CommandType.REPLY, null, null, data.rData, data.index, cmd.seq, null, null, data.from); |
98 rCmd.setCompressFlag(cFlag); | |
530 | 99 |
419 | 100 if (cmd.cs!=null){ // if cmd has cs-instance, it means Command from local. |
452 | 101 cmd.cs.ids.reply(cmd.receiver, rCmd); |
419 | 102 } else { |
103 try { | |
458 | 104 if (!cmd.getQuickFlag()) { |
452 | 105 cmd.connection.sendQueue.put(rCmd); |
106 } else { | |
107 cmd.connection.write(rCmd); | |
419 | 108 } |
109 } catch (InterruptedException e) { | |
110 e.printStackTrace(); | |
111 } | |
112 } | |
113 } | |
114 | |
345 | 115 } |