annotate src/alice/datasegment/LocalDataSegmentManager.java @ 5:80375ae09a1f

add update api
author one
date Wed, 11 Jan 2012 23:28:02 +0900
parents 91057e15065f
children c78a1cc2cd8f
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
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
12 private AtomicInteger seq = new AtomicInteger(1);
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
13
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
14 private DataSegmentKey getDataSegmentKey(String key) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
15 DataSegmentKey newDataSegmentKey = new DataSegmentKey();
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
16 DataSegmentKey dataSegmentKey = dataSegments.putIfAbsent(key, newDataSegmentKey);
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
17 if (dataSegmentKey == newDataSegmentKey) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
18 newDataSegmentKey.runKeyThread();
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
19 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
20 return dataSegmentKey;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
21 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
22
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
23 @Override
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
24 public void put(String key, Value val) {
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
25 DataSegmentKey dataSegmentKey = getDataSegmentKey(key);
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
26 dataSegmentKey.addCommand(new Command(CommandType.PUT, val, 0, null, 0));
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
27 }
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
28
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
29 @Override
5
80375ae09a1f add update api
one
parents: 3
diff changeset
30 public void update(String key, Value val) {
80375ae09a1f add update api
one
parents: 3
diff changeset
31 DataSegmentKey dataSegmentKey = getDataSegmentKey(key);
80375ae09a1f add update api
one
parents: 3
diff changeset
32 dataSegmentKey.addCommand(new Command(CommandType.UPDATE, val, 0, null, 0));
80375ae09a1f add update api
one
parents: 3
diff changeset
33 }
80375ae09a1f add update api
one
parents: 3
diff changeset
34
80375ae09a1f add update api
one
parents: 3
diff changeset
35 @Override
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
36 public void take(String key, int index, CodeSegment cs) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
37 DataSegmentKey dataSegmentKey = getDataSegmentKey(key);
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
38 int seq = this.seq.getAndIncrement();
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
39 dataSegmentKey.addCommand(new Command(CommandType.TAKE, null, index, cs, seq));
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
40 }
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
41
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
42 @Override
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
43 public void peek(String key, int index, CodeSegment cs) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
44 DataSegmentKey dataSegmentKey = getDataSegmentKey(key);
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
45 int seq = this.seq.getAndIncrement();
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
46 dataSegmentKey.addCommand(new Command(CommandType.PEEK, null, index, cs, seq));
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
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
50 public void remove(String key) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
51 DataSegmentKey dataSegmentKey = getDataSegmentKey(key);
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
52 dataSegmentKey.addCommand(new Command(CommandType.REMOVE, null, 0, null, 0));
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
53 }
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
54
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
55 }