Line data Source code
1 : // Copyright (C) 2020 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.comment; 16 : 17 : import com.google.auto.value.AutoValue; 18 : import com.google.gerrit.entities.Change; 19 : import com.google.gerrit.entities.Project; 20 : import com.google.gerrit.proto.Protos; 21 : import com.google.gerrit.server.cache.proto.Cache; 22 : import com.google.gerrit.server.cache.serialize.CacheSerializer; 23 : 24 : /** 25 : * An identifier of a comment that should be used to load the comment context using {@link 26 : * CommentContextCache#get(CommentContextKey)}, or {@link CommentContextCache#getAll(Iterable)}. 27 : * 28 : * <p>The {@link CommentContextCacheImpl} implementation uses this class as the cache key, while 29 : * replacing the {@link #path()} field with the hashed path. 30 : */ 31 : @AutoValue 32 2 : public abstract class CommentContextKey { 33 : abstract Project.NameKey project(); 34 : 35 : abstract Change.Id changeId(); 36 : 37 : /** The unique comment ID. */ 38 : abstract String id(); 39 : 40 : /** File path at which the comment was written. */ 41 : abstract String path(); 42 : 43 : abstract Integer patchset(); 44 : 45 : /** Number of extra lines of context that should be added before and after the comment range. */ 46 : abstract int contextPadding(); 47 : 48 : abstract Builder toBuilder(); 49 : 50 : public static Builder builder() { 51 2 : return new AutoValue_CommentContextKey.Builder(); 52 : } 53 : 54 : @AutoValue.Builder 55 2 : public abstract static class Builder { 56 : 57 : public abstract Builder project(Project.NameKey nameKey); 58 : 59 : public abstract Builder changeId(Change.Id changeId); 60 : 61 : public abstract Builder id(String id); 62 : 63 : public abstract Builder path(String path); 64 : 65 : public abstract Builder patchset(Integer patchset); 66 : 67 : public abstract Builder contextPadding(Integer numLines); 68 : 69 : public abstract CommentContextKey build(); 70 : } 71 : 72 153 : public enum Serializer implements CacheSerializer<CommentContextKey> { 73 153 : INSTANCE; 74 : 75 : @Override 76 : public byte[] serialize(CommentContextKey key) { 77 1 : return Protos.toByteArray( 78 1 : Cache.CommentContextKeyProto.newBuilder() 79 1 : .setProject(key.project().get()) 80 1 : .setChangeId(key.changeId().toString()) 81 1 : .setPatchset(key.patchset()) 82 1 : .setPathHash(key.path()) 83 1 : .setCommentId(key.id()) 84 1 : .setContextPadding(key.contextPadding()) 85 1 : .build()); 86 : } 87 : 88 : @Override 89 : public CommentContextKey deserialize(byte[] in) { 90 : Cache.CommentContextKeyProto proto = 91 1 : Protos.parseUnchecked(Cache.CommentContextKeyProto.parser(), in); 92 1 : return CommentContextKey.builder() 93 1 : .project(Project.NameKey.parse(proto.getProject())) 94 1 : .changeId(Change.Id.tryParse(proto.getChangeId()).get()) 95 1 : .patchset(proto.getPatchset()) 96 1 : .id(proto.getCommentId()) 97 1 : .path(proto.getPathHash()) 98 1 : .contextPadding(proto.getContextPadding()) 99 1 : .build(); 100 : } 101 : } 102 : }