2 * Copyright 2011 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.ipc.invalidation.ticl;
19 import com.google.ipc.invalidation.common.DigestFunction;
20 import com.google.ipc.invalidation.common.ObjectIdDigestUtils;
21 import com.google.ipc.invalidation.ticl.proto.ClientProtocol.ObjectIdP;
22 import com.google.ipc.invalidation.util.Bytes;
23 import com.google.ipc.invalidation.util.InternalBase;
24 import com.google.ipc.invalidation.util.TextBuilder;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.SortedMap;
29 import java.util.TreeMap;
32 * Simple, map-based implementation of {@link DigestStore}.
35 class SimpleRegistrationStore extends InternalBase implements DigestStore<ObjectIdP> {
37 /** All the registrations in the store mapped from the digest to the Object Id. */
38 private final SortedMap<Bytes, ObjectIdP> registrations = new TreeMap<Bytes, ObjectIdP>();
40 /** The function used to compute digests of objects. */
41 private final DigestFunction digestFunction;
43 /** The memoized digest of all objects in registrations. */
46 SimpleRegistrationStore(DigestFunction digestFunction) {
47 this.digestFunction = digestFunction;
52 public boolean add(ObjectIdP oid) {
53 if (registrations.put(ObjectIdDigestUtils.getDigest(oid.getSource(),
54 oid.getName().getByteArray(), digestFunction), oid) == null) {
62 public Collection<ObjectIdP> add(Collection<ObjectIdP> oids) {
63 Collection<ObjectIdP> addedOids = new ArrayList<ObjectIdP>();
64 for (ObjectIdP oid : oids) {
65 if (registrations.put(ObjectIdDigestUtils.getDigest(oid.getSource(),
66 oid.getName().getByteArray(), digestFunction), oid) == null) {
67 // There was no previous value, so this is a new item.
71 if (!addedOids.isEmpty()) {
72 // Only recompute the digest if we made changes.
79 public boolean remove(ObjectIdP oid) {
80 if (registrations.remove(ObjectIdDigestUtils.getDigest(oid.getSource(),
81 oid.getName().getByteArray(), digestFunction)) != null) {
89 public Collection<ObjectIdP> remove(Collection<ObjectIdP> oids) {
90 Collection<ObjectIdP> removedOids = new ArrayList<ObjectIdP>();
91 for (ObjectIdP oid : oids) {
92 if (registrations.remove(ObjectIdDigestUtils.getDigest(oid.getSource(),
93 oid.getName().getByteArray(), digestFunction)) != null) {
97 if (!removedOids.isEmpty()) {
98 // Only recompute the digest if we made changes.
105 public Collection<ObjectIdP> removeAll() {
106 Collection<ObjectIdP> result = new ArrayList<ObjectIdP>(registrations.values());
107 registrations.clear();
113 public boolean contains(ObjectIdP oid) {
114 return registrations.containsKey(ObjectIdDigestUtils.getDigest(oid.getSource(),
115 oid.getName().getByteArray(), digestFunction));
120 return registrations.size();
124 public byte[] getDigest() {
125 return digest.getByteArray();
129 public Collection<ObjectIdP> getElements(byte[] oidDigestPrefix, int prefixLen) {
130 // We always return all the registrations and let the Ticl sort it out.
131 return registrations.values();
134 /** Recomputes the digests over all objects and sets {@code this.digest}. */
135 private void recomputeDigest() {
136 this.digest = ObjectIdDigestUtils.getDigest(registrations.keySet(), digestFunction);
140 public void toCompactString(TextBuilder builder) {
142 .append("<SimpleRegistrationStore: registrations=")
143 .append(registrations.values())