annotate src/alice/datasegment/LocalDataSegmentManager.java @ 12:c4d6ff56b9bf

unite Command and Reply and add Network outline
author one
date Fri, 13 Jan 2012 07:04:38 +0900
parents 78b415d019de
children 30f97d776a3e
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.concurrent.atomic.AtomicInteger;
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
4
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
5 import org.msgpack.type.Value;
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
6
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
7 import alice.codesegment.CodeSegment;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
8 import alice.datasegment.CommandType;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
9
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
10 public class LocalDataSegmentManager extends DataSegmentManager {
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
11
6
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
12 private AtomicInteger seq = new AtomicInteger(1);
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
13 private Runnable replyThread = new Runnable() {
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
14
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
15 @Override
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
16 public void run() {
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
17 while (true) {
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
18 try {
12
c4d6ff56b9bf unite Command and Reply
one
parents: 8
diff changeset
19 Command reply = replyQueue.take();
7
352eb19d837d implements reply of LocalDataSegment
one
parents: 6
diff changeset
20 Command cmd = seqHash.get(reply.seq);
352eb19d837d implements reply of LocalDataSegment
one
parents: 6
diff changeset
21 cmd.cs.ids.reply(cmd.argKey, new DataSegmentValue(reply.index, reply.val));
6
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
22 } catch (InterruptedException e) {
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
23 e.printStackTrace();
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
24 }
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
25 }
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
26 }
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
27
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
28 };
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
29 public LocalDataSegmentManager() {
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
30 new Thread(replyThread).start();
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
31 }
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
32
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
33 private DataSegmentKey getDataSegmentKey(String key) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
34 DataSegmentKey newDataSegmentKey = new DataSegmentKey();
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
35 DataSegmentKey dataSegmentKey = dataSegments.putIfAbsent(key, newDataSegmentKey);
8
78b415d019de Local DS and CS work! maybe...
one
parents: 7
diff changeset
36 if (dataSegmentKey == null) {
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
37 newDataSegmentKey.runKeyThread();
8
78b415d019de Local DS and CS work! maybe...
one
parents: 7
diff changeset
38 dataSegmentKey = newDataSegmentKey;
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
39 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
40 return dataSegmentKey;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
41 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
42
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
43 @Override
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
44 public void put(String key, Value val) {
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
45 DataSegmentKey dataSegmentKey = getDataSegmentKey(key);
7
352eb19d837d implements reply of LocalDataSegment
one
parents: 6
diff changeset
46 dataSegmentKey.addCommand(new Command(CommandType.PUT, null, val, 0, 0, this, null));
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
47 }
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
48
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
49 @Override
5
80375ae09a1f add update api
one
parents: 3
diff changeset
50 public void update(String key, Value val) {
80375ae09a1f add update api
one
parents: 3
diff changeset
51 DataSegmentKey dataSegmentKey = getDataSegmentKey(key);
7
352eb19d837d implements reply of LocalDataSegment
one
parents: 6
diff changeset
52 dataSegmentKey.addCommand(new Command(CommandType.UPDATE, null, val, 0, 0, this, null));
5
80375ae09a1f add update api
one
parents: 3
diff changeset
53 }
80375ae09a1f add update api
one
parents: 3
diff changeset
54
80375ae09a1f add update api
one
parents: 3
diff changeset
55 @Override
7
352eb19d837d implements reply of LocalDataSegment
one
parents: 6
diff changeset
56 public void take(String argKey, String key, int index, CodeSegment cs) {
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
57 DataSegmentKey dataSegmentKey = getDataSegmentKey(key);
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
58 int seq = this.seq.getAndIncrement();
7
352eb19d837d implements reply of LocalDataSegment
one
parents: 6
diff changeset
59 Command cmd = new Command(CommandType.TAKE, argKey, null, index, seq, this, cs);
6
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
60 seqHash.put(seq, cmd);
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
61 dataSegmentKey.addCommand(cmd);
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
62 }
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
63
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
64 @Override
7
352eb19d837d implements reply of LocalDataSegment
one
parents: 6
diff changeset
65 public void peek(String argKey, String key, int index, CodeSegment cs) {
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
66 DataSegmentKey dataSegmentKey = getDataSegmentKey(key);
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
67 int seq = this.seq.getAndIncrement();
7
352eb19d837d implements reply of LocalDataSegment
one
parents: 6
diff changeset
68 Command cmd = new Command(CommandType.PEEK, argKey, null, index, seq, this, cs);
6
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
69 seqHash.put(seq, cmd);
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
70 dataSegmentKey.addCommand(cmd);
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
71 }
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
72
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
73 @Override
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
74 public void remove(String key) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
75 DataSegmentKey dataSegmentKey = getDataSegmentKey(key);
7
352eb19d837d implements reply of LocalDataSegment
one
parents: 6
diff changeset
76 dataSegmentKey.addCommand(new Command(CommandType.REMOVE, null, null, 0, 0, this, null));
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
77 }
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
78
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
79 }