Mercurial > hg > Members > tatsuki > Alice
comparison src/alice/datasegment/DataSegmentKey.java @ 3:91057e15065f
add DataSegment API and CodeSegment
author | one |
---|---|
date | Wed, 11 Jan 2012 00:17:27 +0900 |
parents | f71eabb1df2a |
children | 80375ae09a1f |
comparison
equal
deleted
inserted
replaced
2:f71eabb1df2a | 3:91057e15065f |
---|---|
1 package alice.datasegment; | 1 package alice.datasegment; |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.concurrent.LinkedBlockingQueue; | |
5 import java.util.concurrent.atomic.AtomicInteger; | |
6 | |
7 import alice.datasegment.Command; | |
2 | 8 |
3 public class DataSegmentKey { | 9 public class DataSegmentKey { |
4 | 10 |
11 private LinkedBlockingQueue<Command> cmdQueue = new LinkedBlockingQueue<Command>(); | |
12 private ArrayList<DataSegmentValue> dataList = new ArrayList<DataSegmentValue>(); | |
13 private ArrayList<Command> waitList = new ArrayList<Command>(); | |
14 private AtomicInteger tailIndex = new AtomicInteger(1); | |
15 private Runnable keyThread; | |
16 | |
17 public DataSegmentKey() { | |
18 | |
19 } | |
20 | |
21 public void addCommand(Command cmd) { | |
22 cmdQueue.add(cmd); | |
23 } | |
24 | |
25 public void runKeyThread() { | |
26 keyThread = new Runnable() { | |
27 @Override | |
28 public void run() { | |
29 while (true) { | |
30 try { | |
31 Command cmd = cmdQueue.take(); | |
32 switch (cmd.cmdType) { | |
33 case PUT: | |
34 int index = tailIndex.getAndIncrement(); | |
35 dataList.add(new DataSegmentValue(index, cmd.val)); | |
36 // run waiting peek and take | |
37 for (Command waitCmd : waitList) { | |
38 if (waitCmd.index < index) { | |
39 // TODO: make and send reply msg | |
40 | |
41 } | |
42 } | |
43 break; | |
44 case PEEK: | |
45 if (cmd.index >= tailIndex.get()) { | |
46 waitList.add(cmd); | |
47 break; | |
48 } | |
49 for (DataSegmentValue data : dataList) { | |
50 if (data.index > cmd.index) { | |
51 // TODO: make and send reply msg | |
52 | |
53 break; | |
54 } | |
55 } | |
56 break; | |
57 case TAKE: | |
58 if (cmd.index >= tailIndex.get()) { | |
59 waitList.add(cmd); | |
60 break; | |
61 } | |
62 for (DataSegmentValue data : dataList) { | |
63 if (data.index > cmd.index) { | |
64 // TODO: make and send reply msg | |
65 | |
66 dataList.remove(data); | |
67 break; | |
68 } | |
69 } | |
70 break; | |
71 case REMOVE: | |
72 // TODO: implements later | |
73 break; | |
74 default: | |
75 } | |
76 } catch (InterruptedException e) { | |
77 // TODO Auto-generated catch block | |
78 e.printStackTrace(); | |
79 } | |
80 } | |
81 } | |
82 }; | |
83 keyThread.run(); | |
84 }; | |
85 | |
5 } | 86 } |