Mercurial > hg > Members > tatsuki > Alice
diff 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 |
line wrap: on
line diff
--- a/src/alice/datasegment/DataSegmentKey.java Sun Dec 11 06:37:05 2011 +0900 +++ b/src/alice/datasegment/DataSegmentKey.java Wed Jan 11 00:17:27 2012 +0900 @@ -1,5 +1,86 @@ package alice.datasegment; +import java.util.ArrayList; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.atomic.AtomicInteger; + +import alice.datasegment.Command; + public class DataSegmentKey { + private LinkedBlockingQueue<Command> cmdQueue = new LinkedBlockingQueue<Command>(); + private ArrayList<DataSegmentValue> dataList = new ArrayList<DataSegmentValue>(); + private ArrayList<Command> waitList = new ArrayList<Command>(); + private AtomicInteger tailIndex = new AtomicInteger(1); + private Runnable keyThread; + + public DataSegmentKey() { + + } + + public void addCommand(Command cmd) { + cmdQueue.add(cmd); + } + + public void runKeyThread() { + keyThread = new Runnable() { + @Override + public void run() { + while (true) { + try { + Command cmd = cmdQueue.take(); + switch (cmd.cmdType) { + case PUT: + int index = tailIndex.getAndIncrement(); + dataList.add(new DataSegmentValue(index, cmd.val)); + // run waiting peek and take + for (Command waitCmd : waitList) { + if (waitCmd.index < index) { + // TODO: make and send reply msg + + } + } + break; + case PEEK: + if (cmd.index >= tailIndex.get()) { + waitList.add(cmd); + break; + } + for (DataSegmentValue data : dataList) { + if (data.index > cmd.index) { + // TODO: make and send reply msg + + break; + } + } + break; + case TAKE: + if (cmd.index >= tailIndex.get()) { + waitList.add(cmd); + break; + } + for (DataSegmentValue data : dataList) { + if (data.index > cmd.index) { + // TODO: make and send reply msg + + dataList.remove(data); + break; + } + } + break; + case REMOVE: + // TODO: implements later + break; + default: + } + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + }; + keyThread.run(); + }; + }