annotate src/alice/datasegment/DataSegmentKey.java @ 69:1d4f2b72fb31

delete KeyThread
author kazz <kazz@cr.ie.u-ryukyu.ac.jp>
date Tue, 21 Feb 2012 19:44:33 +0900
parents eb1714f41caf
children 4bfd81352cfa 8d3cb7e5fa57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
1 package alice.datasegment;
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
2
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
3 import java.util.ArrayList;
17
bb075e103cd3 bug fix for take()
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
4 import java.util.Iterator;
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
5 import java.util.concurrent.atomic.AtomicInteger;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
6
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
7 import alice.datasegment.Command;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
8
57
7fa9ddb31f64 add comment
one
parents: 44
diff changeset
9 /**
7fa9ddb31f64 add comment
one
parents: 44
diff changeset
10 * Synchronized DataSegment for each DataSegment key
7fa9ddb31f64 add comment
one
parents: 44
diff changeset
11 * @author kazz
7fa9ddb31f64 add comment
one
parents: 44
diff changeset
12 *
7fa9ddb31f64 add comment
one
parents: 44
diff changeset
13 */
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
14 public class DataSegmentKey {
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
15
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
16 private ArrayList<DataSegmentValue> dataList = new ArrayList<DataSegmentValue>();
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
17 private ArrayList<Command> waitList = new ArrayList<Command>();
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
18 private AtomicInteger tailIndex = new AtomicInteger(1);
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
19
69
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
20 public void runCommand(Command cmd) {
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
21 switch (cmd.type) {
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
22 case UPDATE:
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
23 if (dataList.size() != 0) {
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
24 dataList.remove(0);
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
25 }
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
26 case PUT:
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
27 int index = tailIndex.getAndIncrement();
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
28 DataSegmentValue dsv = new DataSegmentValue(index, cmd.val, cmd.reverseKey);
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
29 dataList.add(dsv);
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
30 // Process waiting peek and take commands
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
31 for (Iterator<Command> iter = waitList.iterator(); iter.hasNext(); ) {
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
32 Command waitCmd = iter.next();
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
33 if (waitCmd.index < index) {
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
34 try {
69
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
35 waitCmd.replyQueue.put(new Command(CommandType.REPLY, null, null, cmd.val, index, waitCmd.seq, null, null, cmd.reverseKey));
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
36 } catch (InterruptedException e) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
37 e.printStackTrace();
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
38 }
69
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
39 iter.remove();
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
40 if (waitCmd.type == CommandType.TAKE) { // someone is waiting for this put or update command
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
41 dataList.remove(dsv);
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
42 break;
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
43 }
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
44 }
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
45 }
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
46 break;
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
47 case PEEK:
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
48 if (cmd.index >= tailIndex.get()) {
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
49 waitList.add(cmd);
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
50 break;
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
51 }
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
52 boolean waitFlag2 = true;
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
53 for (DataSegmentValue data : dataList) {
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
54 if (data.index > cmd.index) {
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
55 try {
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
56 cmd.replyQueue.put(new Command(CommandType.REPLY, null, null, data.val, data.index, cmd.seq, null, null, data.from));
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
57 } catch (InterruptedException e) {
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
58 e.printStackTrace();
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
59 }
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
60 waitFlag2 = false;
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
61 break;
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
62 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
63 }
69
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
64 if (waitFlag2)
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
65 waitList.add(cmd);
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
66 break;
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
67 case TAKE:
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
68 if (cmd.index >= tailIndex.get()) {
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
69 waitList.add(cmd);
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
70 break;
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
71 }
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
72 boolean waitFlag = true;
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
73 for (Iterator<DataSegmentValue> iter = dataList.iterator(); iter.hasNext(); ) {
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
74 DataSegmentValue data = iter.next();
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
75 if (data.index > cmd.index) {
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
76 try {
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
77 cmd.replyQueue.put(new Command(CommandType.REPLY, null, null, data.val, data.index, cmd.seq, null, null, data.from));
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
78 } catch (InterruptedException e) {
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
79 e.printStackTrace();
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
80 }
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
81 iter.remove();
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
82 waitFlag = false;
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
83 break;
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
84 }
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
85 }
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
86 if (waitFlag)
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
87 waitList.add(cmd);
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
88 break;
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
89 case REMOVE:
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
90 // TODO: implements later
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
91 break;
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
92 default:
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
93 }
1d4f2b72fb31 delete KeyThread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 59
diff changeset
94
20
0bb03861b5cd set name to Thread
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 18
diff changeset
95 }
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
96
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
97 }