aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetStore.java
blob: 9ad55efee5a6dac72b8e1c84c17961df366a7cf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2018 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.collect.nestedset;

import com.google.common.base.Preconditions;
import com.google.common.collect.MapMaker;
import com.google.common.hash.Hashing;
import com.google.devtools.build.lib.skyframe.serialization.DeserializationContext;
import com.google.devtools.build.lib.skyframe.serialization.SerializationConstants;
import com.google.devtools.build.lib.skyframe.serialization.SerializationContext;
import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
import com.google.protobuf.ByteString;
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.CodedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nullable;

/**
 * Supports association between fingerprints and NestedSet contents. A single NestedSetStore
 * instance should be globally available across a single process.
 *
 * <p>Maintains the fingerprint -> contents side of the bimap by decomposing nested Object[]'s.
 *
 * <p>For example, suppose the NestedSet A can be drawn as:
 *
 * <pre>
 *         A
 *       /  \
 *      B   C
 *     / \
 *    D  E
 * </pre>
 *
 * <p>Then, in memory, A = [[D, E], C]. To store the NestedSet, we would rely on the fingerprint
 * value FPb = fingerprint([D, E]) and write
 *
 * <pre>A -> fingerprint(FPb, C)</pre>
 *
 * <p>On retrieval, A will be reconstructed by first retrieving A using its fingerprint, and then
 * recursively retrieving B using its fingerprint.
 */
public class NestedSetStore {

  /** Stores fingerprint -> NestedSet associations. */
  interface NestedSetStorageEndpoint {
    /** Associates a fingerprint with the serialized representation of some NestedSet contents. */
    void put(ByteString fingerprint, byte[] serializedBytes);

    /**
     * Retrieves the serialized bytes for the NestedSet contents associated with this fingerprint.
     */
    byte[] get(ByteString fingerprint);
  }

  /** An in-memory {@link NestedSetStorageEndpoint} */
  private static class InMemoryNestedSetStorageEndpoint implements NestedSetStorageEndpoint {
    private final ConcurrentHashMap<ByteString, byte[]> fingerprintToContents =
        new ConcurrentHashMap<>();

    @Override
    public void put(ByteString fingerprint, byte[] serializedBytes) {
      fingerprintToContents.put(fingerprint, serializedBytes);
    }

    @Override
    public byte[] get(ByteString fingerprint) {
      return fingerprintToContents.get(fingerprint);
    }
  }

  /** An in-memory cache for fingerprint <-> NestedSet associations. */
  private static class NestedSetCache {
    private final Map<ByteString, Object> fingerprintToContents =
        new MapMaker()
            .concurrencyLevel(SerializationConstants.DESERIALIZATION_POOL_SIZE)
            .weakValues()
            .makeMap();

    /** Object/Object[] contents to fingerprint. Maintained for fast fingerprinting. */
    private final Map<Object, ByteString> contentsToFingerprint =
        new MapMaker()
            .concurrencyLevel(SerializationConstants.DESERIALIZATION_POOL_SIZE)
            .weakKeys()
            .makeMap();

    /**
     * Returns the NestedSet contents associated with the given fingerprint. Returns null if the
     * fingerprint is not known.
     */
    @Nullable
    public Object contentsForFingerprint(ByteString fingerprint) {
      return fingerprintToContents.get(fingerprint);
    }

    /**
     * Retrieves the fingerprint associated with the given NestedSet contents, or null if the given
     * contents are not known.
     */
    @Nullable
    public ByteString fingerprintForContents(Object contents) {
      return contentsToFingerprint.get(contents);
    }

    /** Associates the provided fingerprint and NestedSet contents. */
    public void put(ByteString fingerprint, Object contents) {
      contentsToFingerprint.put(contents, fingerprint);
      fingerprintToContents.put(fingerprint, contents);
    }
  }

  private final NestedSetCache nestedSetCache = new NestedSetCache();
  private final NestedSetStorageEndpoint nestedSetStorageEndpoint =
      new InMemoryNestedSetStorageEndpoint();

  /**
   * Computes and returns the fingerprint for the given NestedSet contents using the given {@link
   * SerializationContext}, while also associating the contents with the computed fingerprint in the
   * store. Recursively does the same for all transitive members (i.e. Object[] members) of the
   * provided contents.
   */
  ByteString computeFingerprintAndStore(Object contents, SerializationContext serializationContext)
      throws SerializationException {
    ByteString priorFingerprint = nestedSetCache.fingerprintForContents(contents);
    if (priorFingerprint != null) {
      return priorFingerprint;
    }

    // For every fingerprint computation, we need to use a new memoization table.  This is required
    // to guarantee that the same child will always have the same fingerprint - otherwise,
    // differences in memoization context could cause part of a child to be memoized in one
    // fingerprinting but not in the other.  We expect this clearing of memoization state to be a
    // major source of extra work over the naive serialization approach.  The same value may have to
    // be serialized many times across separate fingerprintings.
    SerializationContext newSerializationContext = serializationContext.getNewMemoizingContext();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(byteArrayOutputStream);

    try {
      if (contents instanceof Object[]) {
        Object[] contentsArray = (Object[]) contents;
        codedOutputStream.writeInt32NoTag(contentsArray.length);
        for (Object child : contentsArray) {
          if (child instanceof Object[]) {
            ByteString fingerprint = computeFingerprintAndStore(child, serializationContext);
            newSerializationContext.serialize(fingerprint, codedOutputStream);
          } else {
            newSerializationContext.serialize(child, codedOutputStream);
          }
        }
      } else {
        codedOutputStream.writeInt32NoTag(1);
        newSerializationContext.serialize(contents, codedOutputStream);
      }
      codedOutputStream.flush();
    } catch (IOException e) {
      throw new SerializationException("Could not serialize " + contents, e);
    }

    byte[] serializedBytes = byteArrayOutputStream.toByteArray();
    ByteString fingerprint =
        ByteString.copyFrom(Hashing.md5().hashBytes(serializedBytes).asBytes());

    nestedSetCache.put(fingerprint, contents);
    nestedSetStorageEndpoint.put(fingerprint, serializedBytes);

    return fingerprint;
  }

  /** Retrieves and deserializes the NestedSet contents associated with the given fingerprint. */
  public Object getContentsAndDeserialize(
      ByteString fingerprint, DeserializationContext deserializationContext)
      throws IOException, SerializationException {
    Object contents = nestedSetCache.contentsForFingerprint(fingerprint);
    if (contents != null) {
      return contents;
    }

    byte[] retrieved =
        Preconditions.checkNotNull(
            nestedSetStorageEndpoint.get(fingerprint),
            "Fingerprint %s not found in NestedSetStore",
            fingerprint);
    CodedInputStream codedIn = CodedInputStream.newInstance(retrieved);
    DeserializationContext newDeserializationContext =
        deserializationContext.getNewMemoizingContext();

    int numberOfElements = codedIn.readInt32();
    Object dereferencedContents;
    if (numberOfElements > 1) {
      Object[] dereferencedContentsArray = new Object[numberOfElements];
      for (int i = 0; i < numberOfElements; i++) {
        Object deserializedElement = newDeserializationContext.deserialize(codedIn);
        dereferencedContentsArray[i] =
            deserializedElement instanceof ByteString
                ? getContentsAndDeserialize(
                    (ByteString) deserializedElement, deserializationContext)
                : deserializedElement;
      }
      dereferencedContents = dereferencedContentsArray;
    } else {
      dereferencedContents = newDeserializationContext.deserialize(codedIn);
    }

    nestedSetCache.put(fingerprint, dereferencedContents);
    return dereferencedContents;
  }
}