1 package org.apache.helix.controller.restlet;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.I0Itec.zkclient.DataUpdater;
23 import org.apache.helix.ZNRecord;
24 import org.apache.helix.ZNRecordUpdater;
25 import org.codehaus.jackson.annotate.JsonCreator;
26 import org.codehaus.jackson.annotate.JsonIgnore;
27 import org.codehaus.jackson.annotate.JsonProperty;
28
29 /**
30 * Unit of transfered ZNRecord updates. Contains the ZNRecord Value, zkPath
31 * to store the update value, and the property type (used to merge the ZNRecord)
32 * For ZNRecord subtraction, it is currently not supported yet.
33 * */
34 public class ZNRecordUpdate
35 {
36 public enum OpCode
37 {
38 // TODO: create is not supported; but update will create if not exist
39 CREATE,
40 UPDATE,
41 SET
42 }
43 final String _path;
44 ZNRecord _record;
45 final OpCode _code;
46
47 @JsonCreator
48 public ZNRecordUpdate(@JsonProperty("path")String path,
49 @JsonProperty("opcode")OpCode code,
50 @JsonProperty("record")ZNRecord record)
51 {
52 _path = path;
53 _record = record;
54 _code = code;
55 }
56
57 public String getPath()
58 {
59 return _path;
60 }
61
62 public ZNRecord getRecord()
63 {
64 return _record;
65 }
66
67 public OpCode getOpcode()
68 {
69 return _code;
70 }
71
72 @JsonIgnore(true)
73 public DataUpdater<ZNRecord> getZNRecordUpdater()
74 {
75 if(_code == OpCode.SET)
76
77 {
78 return new ZNRecordUpdater(_record)
79 {
80 @Override
81 public ZNRecord update(ZNRecord current)
82 {
83 return _record;
84 }
85 };
86 }
87 else if ((_code == OpCode.UPDATE))
88 {
89 return new ZNRecordUpdater(_record);
90 }
91 else
92 {
93 throw new UnsupportedOperationException("Not supported : " + _code);
94 }
95 }
96 }