
com.apple.foundationdb.clientlog.TupleKeyCountTree Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fdb-extensions Show documentation
Show all versions of fdb-extensions Show documentation
Extensions to the FoundationDB Java API.
The newest version!
/*
* TupleKeyCountTree.java
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2015-2020 Apple Inc. and the FoundationDB project authors
*
* 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.apple.foundationdb.clientlog;
import com.apple.foundationdb.annotation.API;
import com.apple.foundationdb.annotation.SpotBugsSuppressWarnings;
import com.apple.foundationdb.tuple.ByteArrayUtil;
import com.apple.foundationdb.tuple.Tuple;
import com.apple.foundationdb.tuple.TupleHelpers;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
/**
* A tree of occurrence counts tuple-encoded keys.
*/
@API(API.Status.EXPERIMENTAL)
@SpotBugsSuppressWarnings({"EI_EXPOSE_REP", "EI_EXPOSE_REP2"})
public class TupleKeyCountTree {
@Nonnull
private final byte[] bytes;
@Nullable
private final Object object;
private int count;
@Nullable
private final TupleKeyCountTree parent;
@Nonnull
private final Map children;
private boolean visible = true;
// Internal standin object for entries that don't actually have an object, just bytes that don't parse as a tuple.
private static final Object UNPARSEABLE = new Object() {
@Override
public String toString() {
return "*unparseable*";
}
};
private static final Comparator BYTES_COMPARATOR = ByteArrayUtil::compareUnsigned;
public TupleKeyCountTree() {
this(null, new byte[0], null);
}
public TupleKeyCountTree(@Nullable TupleKeyCountTree parent, @Nonnull byte[] bytes, @Nullable Object object) {
this.bytes = bytes;
this.object = object;
this.count = 0;
this.parent = parent;
this.children = new TreeMap<>(BYTES_COMPARATOR);
}
@Nonnull
public byte[] getBytes() {
return bytes;
}
@SuppressWarnings("PMD.CompareObjectsWithEquals")
public boolean hasObject() {
return object != UNPARSEABLE;
}
@Nullable
@SuppressWarnings("PMD.CompareObjectsWithEquals")
public Object getObject() {
if (object == UNPARSEABLE) {
throw new IllegalStateException("node does not have a parseable object");
}
return object;
}
/**
* Add the given tuple to the tree.
*
* Each element is added to the next deeper level in the tree, incrementing the count as it goes.
* @param tuple the tuple to add
*/
public void add(@Nonnull Tuple tuple) {
addInternal(tuple.getItems(), 0, tuple.pack(), 0);
}
/**
* Add encoded tuple bytes to the tree.
*
* @param packed the packed form of a tuple to be parsed and added to the tree
*/
public void add(@Nonnull byte[] packed) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy