Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / HI.java
1 /*
2  * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.access.concepts;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import org.opendaylight.yangtools.concepts.WritableObjects;
18
19 /**
20  * Serialization proxy for {@link LocalHistoryIdentifier}.
21  *
22  * @implNote
23  *     cookie is currently required only for module-based sharding, which is implemented as part of normal
24  *     DataBroker interfaces. For DOMDataTreeProducer cookie will always be zero, hence we may end up not needing
25  *     cookie at all.
26  *     We use WritableObjects.writeLongs() to output historyId and cookie (in that order). If we end up not needing
27  *     the cookie at all, we can switch to writeLong() and use zero flags for compatibility.
28  */
29 final class HI implements Externalizable {
30     @java.io.Serial
31     private static final long serialVersionUID = 1L;
32
33     private LocalHistoryIdentifier identifier;
34
35     @SuppressWarnings("checkstyle:RedundantModifier")
36     public HI() {
37         // for Externalizable
38     }
39
40     HI(final LocalHistoryIdentifier identifier) {
41         this.identifier = requireNonNull(identifier);
42     }
43
44     @Override
45     public void writeExternal(final ObjectOutput out) throws IOException {
46         identifier.getClientId().writeTo(out);
47         WritableObjects.writeLongs(out, identifier.getHistoryId(), identifier.getCookie());
48     }
49
50     @Override
51     public void readExternal(final ObjectInput in) throws IOException {
52         final var clientId = ClientIdentifier.readFrom(in);
53         final byte header = WritableObjects.readLongHeader(in);
54         final var historyId = WritableObjects.readFirstLong(in, header);
55         final var cookie = WritableObjects.readSecondLong(in, header);
56         identifier = new LocalHistoryIdentifier(clientId, historyId, cookie);
57     }
58
59     @java.io.Serial
60     private Object readResolve() {
61         return verifyNotNull(identifier);
62     }
63 }