Mercurial > hg > Members > tatsuki > Alice
annotate src/alice/datasegment/DataSegmentKey.java @ 14:e3f1b21718b0
implements RemoteDataSegment
author | kazz <kazz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 15 Jan 2012 00:56:25 +0900 |
parents | 30f97d776a3e |
children | bb075e103cd3 |
rev | line source |
---|---|
2 | 1 package alice.datasegment; |
2 | |
3 | 3 import java.util.ArrayList; |
10 | 4 import java.util.LinkedList; |
3 | 5 import java.util.concurrent.LinkedBlockingQueue; |
6 import java.util.concurrent.atomic.AtomicInteger; | |
7 | |
8 import alice.datasegment.Command; | |
9 | |
2 | 10 public class DataSegmentKey { |
11 | |
3 | 12 private LinkedBlockingQueue<Command> cmdQueue = new LinkedBlockingQueue<Command>(); |
13 private ArrayList<DataSegmentValue> dataList = new ArrayList<DataSegmentValue>(); | |
14 private ArrayList<Command> waitList = new ArrayList<Command>(); | |
15 private AtomicInteger tailIndex = new AtomicInteger(1); | |
16 private Runnable keyThread; | |
17 | |
18 public DataSegmentKey() { | |
19 | |
20 } | |
21 | |
22 public void addCommand(Command cmd) { | |
23 cmdQueue.add(cmd); | |
24 } | |
25 | |
26 public void runKeyThread() { | |
27 keyThread = new Runnable() { | |
28 @Override | |
29 public void run() { | |
30 while (true) { | |
31 try { | |
32 Command cmd = cmdQueue.take(); | |
13 | 33 switch (cmd.type) { |
5 | 34 case UPDATE: |
35 if (dataList.size() != 0) { | |
36 dataList.remove(0); | |
37 } | |
3 | 38 case PUT: |
39 int index = tailIndex.getAndIncrement(); | |
6 | 40 DataSegmentValue dsv = new DataSegmentValue(index, cmd.val); |
41 dataList.add(dsv); | |
3 | 42 // run waiting peek and take |
10 | 43 LinkedList<Command> removeList = new LinkedList<Command>(); |
3 | 44 for (Command waitCmd : waitList) { |
45 if (waitCmd.index < index) { | |
14
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
46 waitCmd.replyQueue.put(new Command(CommandType.REPLY, null, null, cmd.val, index, waitCmd.seq, null, null)); |
10 | 47 removeList.add(waitCmd); |
13 | 48 if (waitCmd.type == CommandType.TAKE) { // delete data, if it run take cmd. |
6 | 49 dataList.remove(dsv); |
50 break; | |
51 } | |
3 | 52 } |
53 } | |
10 | 54 for (Command rmCmd : removeList) { |
55 waitList.remove(rmCmd); | |
56 } | |
3 | 57 break; |
58 case PEEK: | |
59 if (cmd.index >= tailIndex.get()) { | |
60 waitList.add(cmd); | |
61 break; | |
62 } | |
63 for (DataSegmentValue data : dataList) { | |
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 | 66 break; |
67 } | |
68 } | |
6 | 69 waitList.add(cmd); |
3 | 70 break; |
71 case TAKE: | |
72 if (cmd.index >= tailIndex.get()) { | |
73 waitList.add(cmd); | |
74 break; | |
75 } | |
76 for (DataSegmentValue data : dataList) { | |
77 if (data.index > cmd.index) { | |
14
e3f1b21718b0
implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
78 cmd.replyQueue.put(new Command(CommandType.REPLY, null, null, data.val, data.index, cmd.seq, null, null)); |
3 | 79 dataList.remove(data); |
80 break; | |
81 } | |
82 } | |
6 | 83 waitList.add(cmd); |
3 | 84 break; |
85 case REMOVE: | |
86 // TODO: implements later | |
87 break; | |
88 default: | |
89 } | |
90 } catch (InterruptedException e) { | |
91 e.printStackTrace(); | |
92 } | |
93 } | |
94 } | |
95 }; | |
6 | 96 new Thread(keyThread).start(); |
3 | 97 }; |
98 | |
2 | 99 } |