annotate src/alice/datasegment/DataSegmentKey.java @ 17:bb075e103cd3

bug fix for take()
author kazz <kazz@cr.ie.u-ryukyu.ac.jp>
date Sun, 15 Jan 2012 15:18:01 +0900
parents e3f1b21718b0
children 72dd27d952b0
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;
10
5f7cce38b25c bug fix for wait command
one
parents: 7
diff changeset
5 import java.util.LinkedList;
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
6 import java.util.concurrent.LinkedBlockingQueue;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
7 import java.util.concurrent.atomic.AtomicInteger;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
8
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
9 import alice.datasegment.Command;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
10
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
11 public class DataSegmentKey {
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
12
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
13 private LinkedBlockingQueue<Command> cmdQueue = new LinkedBlockingQueue<Command>();
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
14 private ArrayList<DataSegmentValue> dataList = new ArrayList<DataSegmentValue>();
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
15 private ArrayList<Command> waitList = new ArrayList<Command>();
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
16 private AtomicInteger tailIndex = new AtomicInteger(1);
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
17 private Runnable keyThread;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
18
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
19 public DataSegmentKey() {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
20
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
21 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
22
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
23 public void addCommand(Command cmd) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
24 cmdQueue.add(cmd);
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
25 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
26
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
27 public void runKeyThread() {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
28 keyThread = new Runnable() {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
29 @Override
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
30 public void run() {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
31 while (true) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
32 try {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
33 Command cmd = cmdQueue.take();
13
30f97d776a3e implements Alice daemon
one
parents: 12
diff changeset
34 switch (cmd.type) {
5
80375ae09a1f add update api
one
parents: 3
diff changeset
35 case UPDATE:
80375ae09a1f add update api
one
parents: 3
diff changeset
36 if (dataList.size() != 0) {
80375ae09a1f add update api
one
parents: 3
diff changeset
37 dataList.remove(0);
80375ae09a1f add update api
one
parents: 3
diff changeset
38 }
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
39 case PUT:
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
40 int index = tailIndex.getAndIncrement();
6
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
41 DataSegmentValue dsv = new DataSegmentValue(index, cmd.val);
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
42 dataList.add(dsv);
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
43 // run waiting peek and take
17
bb075e103cd3 bug fix for take()
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
44 boolean takeFlag = true;
bb075e103cd3 bug fix for take()
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
45 for (Iterator<Command> iter = waitList.iterator(); iter.hasNext() && takeFlag; ) {
bb075e103cd3 bug fix for take()
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
46 Command waitCmd = iter.next();
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
47 if (waitCmd.index < index) {
14
e3f1b21718b0 implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
48 waitCmd.replyQueue.put(new Command(CommandType.REPLY, null, null, cmd.val, index, waitCmd.seq, null, null));
17
bb075e103cd3 bug fix for take()
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
49 iter.remove();
13
30f97d776a3e implements Alice daemon
one
parents: 12
diff changeset
50 if (waitCmd.type == CommandType.TAKE) { // delete data, if it run take cmd.
6
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
51 dataList.remove(dsv);
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
52 break;
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
53 }
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
54 }
17
bb075e103cd3 bug fix for take()
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
55
10
5f7cce38b25c bug fix for wait command
one
parents: 7
diff changeset
56 }
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
57 break;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
58 case PEEK:
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
59 if (cmd.index >= tailIndex.get()) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
60 waitList.add(cmd);
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
61 break;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
62 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
63 for (DataSegmentValue data : dataList) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
64 if (data.index > cmd.index) {
14
e3f1b21718b0 implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
65 cmd.replyQueue.put(new Command(CommandType.REPLY, null, null, data.val, data.index, cmd.seq, null, null));
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
66 break;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
67 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
68 }
6
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
69 waitList.add(cmd);
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
70 break;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
71 case TAKE:
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
72 if (cmd.index >= tailIndex.get()) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
73 waitList.add(cmd);
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
74 break;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
75 }
17
bb075e103cd3 bug fix for take()
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
76 boolean waitFlag = true;
bb075e103cd3 bug fix for take()
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
77 for (Iterator<DataSegmentValue> iter = dataList.iterator(); iter.hasNext(); ) {
bb075e103cd3 bug fix for take()
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
78 DataSegmentValue data = iter.next();
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
79 if (data.index > cmd.index) {
14
e3f1b21718b0 implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
80 cmd.replyQueue.put(new Command(CommandType.REPLY, null, null, data.val, data.index, cmd.seq, null, null));
17
bb075e103cd3 bug fix for take()
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
81 iter.remove();
bb075e103cd3 bug fix for take()
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
82 waitFlag = false;
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
83 break;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
84 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
85 }
17
bb075e103cd3 bug fix for take()
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
86 if (waitFlag)
bb075e103cd3 bug fix for take()
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
87 waitList.add(cmd);
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
88 break;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
89 case REMOVE:
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
90 // TODO: implements later
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
91 break;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
92 default:
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
93 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
94 } catch (InterruptedException e) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
95 e.printStackTrace();
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
96 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
97 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
98 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
99 };
6
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
100 new Thread(keyThread).start();
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
101 };
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
102
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
103 }