Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Madan Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 16 | package org.onosproject.store.primitives.impl; |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 17 | |
| 18 | import org.onosproject.store.Timestamp; |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 19 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 20 | import com.google.common.base.MoreObjects; |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 21 | import com.google.common.base.Objects; |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 22 | |
Brian O'Connor | 777b0ae | 2015-12-08 19:38:18 -0800 | [diff] [blame] | 23 | import static com.google.common.base.Preconditions.checkNotNull; |
| 24 | |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Representation of a value in EventuallyConsistentMap. |
| 27 | * |
| 28 | * @param <V> value type |
| 29 | */ |
| 30 | public class MapValue<V> implements Comparable<MapValue<V>> { |
| 31 | private final Timestamp timestamp; |
| 32 | private final V value; |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame] | 33 | private long creationTime; |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 34 | |
Madan Jampani | 43f3795 | 2015-07-02 12:54:08 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Creates a tombstone value with the specified timestamp. |
sangyun-han | 72fd0e9 | 2016-04-26 20:50:08 +0900 | [diff] [blame] | 37 | * |
Madan Jampani | 43f3795 | 2015-07-02 12:54:08 -0700 | [diff] [blame] | 38 | * @param timestamp timestamp for tombstone |
| 39 | * @return tombstone MapValue |
| 40 | * |
| 41 | * @param <U> value type |
| 42 | */ |
| 43 | public static <U> MapValue<U> tombstone(Timestamp timestamp) { |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame] | 44 | return new MapValue<>(null, timestamp, System.currentTimeMillis()); |
Madan Jampani | 43f3795 | 2015-07-02 12:54:08 -0700 | [diff] [blame] | 45 | } |
| 46 | |
sangyun-han | 72fd0e9 | 2016-04-26 20:50:08 +0900 | [diff] [blame] | 47 | /** |
| 48 | * Constructor automatically to create the system time of construction. |
| 49 | * |
| 50 | * @param value value |
| 51 | * @param timestamp value timestamp |
| 52 | */ |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 53 | public MapValue(V value, Timestamp timestamp) { |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame] | 54 | this(value, timestamp, System.currentTimeMillis()); |
| 55 | } |
| 56 | |
| 57 | /** |
sangyun-han | 72fd0e9 | 2016-04-26 20:50:08 +0900 | [diff] [blame] | 58 | * Creates a map value using value, timestamp, and creation time. |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame] | 59 | * |
| 60 | * @param value value |
| 61 | * @param timestamp value timestamp. |
| 62 | * @param creationTime the system time (on local instance) of construction |
| 63 | */ |
| 64 | public MapValue(V value, Timestamp timestamp, long creationTime) { |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 65 | this.value = value; |
Brian O'Connor | 777b0ae | 2015-12-08 19:38:18 -0800 | [diff] [blame] | 66 | this.timestamp = checkNotNull(timestamp, "Timestamp cannot be null"); |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame] | 67 | this.creationTime = creationTime; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Creates a copy of MapValue. |
| 72 | * <p> |
| 73 | * The copy will have an updated creation time corresponding to when the copy was constructed. |
| 74 | * |
| 75 | * @return MapValue copy |
| 76 | */ |
| 77 | public MapValue<V> copy() { |
| 78 | return new MapValue<>(this.value, this.timestamp, System.currentTimeMillis()); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 79 | } |
| 80 | |
sangyun-han | 72fd0e9 | 2016-04-26 20:50:08 +0900 | [diff] [blame] | 81 | /** |
| 82 | * Tests if this value is tombstone value with the specified timestamp. |
| 83 | * |
| 84 | * @return true if this value is null, otherwise false |
| 85 | */ |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 86 | public boolean isTombstone() { |
| 87 | return value == null; |
| 88 | } |
| 89 | |
sangyun-han | 72fd0e9 | 2016-04-26 20:50:08 +0900 | [diff] [blame] | 90 | /** |
| 91 | * Tests if this value is alive. |
| 92 | * |
| 93 | * @return true if this value is not null, otherwise false |
| 94 | */ |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 95 | public boolean isAlive() { |
| 96 | return value != null; |
| 97 | } |
| 98 | |
sangyun-han | 72fd0e9 | 2016-04-26 20:50:08 +0900 | [diff] [blame] | 99 | /** |
| 100 | * Returns the timestamp of this value. |
| 101 | * |
| 102 | * @return timestamp |
| 103 | */ |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 104 | public Timestamp timestamp() { |
| 105 | return timestamp; |
| 106 | } |
| 107 | |
sangyun-han | 72fd0e9 | 2016-04-26 20:50:08 +0900 | [diff] [blame] | 108 | /** |
| 109 | * Returns this value. |
| 110 | * |
| 111 | * @return value |
| 112 | */ |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 113 | public V get() { |
| 114 | return value; |
| 115 | } |
| 116 | |
sangyun-han | 72fd0e9 | 2016-04-26 20:50:08 +0900 | [diff] [blame] | 117 | /** |
| 118 | * Returns the creation time of this value. |
| 119 | * |
| 120 | * @return creationTime |
| 121 | */ |
Madan Jampani | 29f52a3 | 2016-04-18 15:20:52 -0700 | [diff] [blame] | 122 | public long creationTime() { |
| 123 | return creationTime; |
| 124 | } |
| 125 | |
sangyun-han | 72fd0e9 | 2016-04-26 20:50:08 +0900 | [diff] [blame] | 126 | /** |
| 127 | * Tests if this value is newer than the specified MapValue. |
| 128 | * |
| 129 | * @param other the value to be compared |
| 130 | * @return true if this value is newer than other |
| 131 | */ |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 132 | public boolean isNewerThan(MapValue<V> other) { |
Brian O'Connor | 777b0ae | 2015-12-08 19:38:18 -0800 | [diff] [blame] | 133 | if (other == null) { |
| 134 | return true; |
| 135 | } |
| 136 | return this.timestamp.isNewerThan(other.timestamp); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 137 | } |
| 138 | |
sangyun-han | 72fd0e9 | 2016-04-26 20:50:08 +0900 | [diff] [blame] | 139 | /** |
| 140 | * Tests if this timestamp is newer than the specified timestamp. |
| 141 | * |
| 142 | * @param timestamp timestamp to be compared |
| 143 | * @return true if this instance is newer |
| 144 | */ |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 145 | public boolean isNewerThan(Timestamp timestamp) { |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 146 | return this.timestamp.isNewerThan(timestamp); |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 147 | } |
| 148 | |
sangyun-han | 72fd0e9 | 2016-04-26 20:50:08 +0900 | [diff] [blame] | 149 | /** |
| 150 | * Returns summary of a MapValue for use during Anti-Entropy exchanges. |
| 151 | * |
| 152 | * @return Digest with timestamp and whether this value is null or not |
| 153 | */ |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 154 | public Digest digest() { |
| 155 | return new Digest(timestamp, isTombstone()); |
| 156 | } |
| 157 | |
| 158 | @Override |
sangyun-han | 72fd0e9 | 2016-04-26 20:50:08 +0900 | [diff] [blame] | 159 | public int compareTo(MapValue<V> o) { |
| 160 | return this.timestamp.compareTo(o.timestamp); |
| 161 | } |
| 162 | |
| 163 | @Override |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 164 | public int hashCode() { |
| 165 | return Objects.hashCode(timestamp, value); |
| 166 | } |
| 167 | |
Madan Jampani | 92c64eb | 2015-07-23 15:37:07 -0700 | [diff] [blame] | 168 | @SuppressWarnings("unchecked") |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 169 | @Override |
| 170 | public boolean equals(Object other) { |
| 171 | if (other instanceof MapValue) { |
Madan Jampani | 92c64eb | 2015-07-23 15:37:07 -0700 | [diff] [blame] | 172 | MapValue<V> that = (MapValue<V>) other; |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 173 | return Objects.equal(this.timestamp, that.timestamp) && |
| 174 | Objects.equal(this.value, that.value); |
| 175 | } |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | @Override |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 180 | public String toString() { |
| 181 | return MoreObjects.toStringHelper(getClass()) |
| 182 | .add("timestamp", timestamp) |
| 183 | .add("value", value) |
| 184 | .toString(); |
| 185 | } |
| 186 | |
| 187 | @SuppressWarnings("unused") |
| 188 | private MapValue() { |
| 189 | this.timestamp = null; |
| 190 | this.value = null; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Digest or summary of a MapValue for use during Anti-Entropy exchanges. |
| 195 | */ |
| 196 | public static class Digest { |
| 197 | private final Timestamp timestamp; |
| 198 | private final boolean isTombstone; |
| 199 | |
| 200 | public Digest(Timestamp timestamp, boolean isTombstone) { |
| 201 | this.timestamp = timestamp; |
| 202 | this.isTombstone = isTombstone; |
| 203 | } |
| 204 | |
| 205 | public Timestamp timestamp() { |
| 206 | return timestamp; |
| 207 | } |
| 208 | |
| 209 | public boolean isTombstone() { |
| 210 | return isTombstone; |
| 211 | } |
| 212 | |
| 213 | public boolean isNewerThan(Digest other) { |
| 214 | return timestamp.isNewerThan(other.timestamp); |
| 215 | } |
| 216 | |
| 217 | @Override |
Madan Jampani | dbe6703 | 2015-07-08 22:48:09 -0700 | [diff] [blame] | 218 | public int hashCode() { |
| 219 | return Objects.hashCode(timestamp, isTombstone); |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public boolean equals(Object other) { |
| 224 | if (other instanceof Digest) { |
| 225 | Digest that = (Digest) other; |
| 226 | return Objects.equal(this.timestamp, that.timestamp) && |
| 227 | Objects.equal(this.isTombstone, that.isTombstone); |
| 228 | } |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | @Override |
Madan Jampani | 3d76c94 | 2015-06-29 23:37:10 -0700 | [diff] [blame] | 233 | public String toString() { |
| 234 | return MoreObjects.toStringHelper(getClass()) |
| 235 | .add("timestamp", timestamp) |
| 236 | .add("isTombstone", isTombstone) |
| 237 | .toString(); |
| 238 | } |
| 239 | } |
| 240 | } |