Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IGNITE-17855 Implement inline size calculation for B+tree #1179

Merged
merged 14 commits into from
Oct 11, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public class BinaryTupleCommon {
public static final int PREFIX_FLAG = 1 << 3;

/**
* Flag, which indicates how to interpret situations when Binary Tuple Prefix columns are equal to
* first N columns of a Binary Tuple (where N is the length of the prefix).
* Flag, which indicates how to interpret situations when Binary Tuple Prefix columns are equal to first N columns of a Binary Tuple
* (where N is the length of the prefix).
*
* <p>This flag is used by some index implementations for internal optimizations.
*/
Expand Down Expand Up @@ -133,4 +133,26 @@ public static int nullOffset(int index) {
public static byte nullMask(int index) {
return (byte) (1 << (index % 8));
}

/**
* Calculates the size of entry in variable-length offset table.
*
* @param size Variable-length area size.
* @return Size in bytes.
*/
public static int valueSizeToEntrySize(long size) {
if (size <= 0xff) {
return 1;
}

if (size <= 0xffff) {
return 2;
}

if (size <= Integer.MAX_VALUE) {
return 4;
}

throw new IgniteInternalException("Too big binary tuple size");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.ignite.internal.binarytuple;

import static org.apache.ignite.internal.binarytuple.BinaryTupleCommon.valueSizeToEntrySize;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.apache.ignite.lang.IgniteInternalException;
import org.junit.jupiter.api.Test;

/**
* For {@link BinaryTupleCommon} testing.
*/
public class BinaryTupleCommonTest {
@Test
void testTableOffsetEntrySize() {
assertEquals(1, valueSizeToEntrySize(10));
assertEquals(1, valueSizeToEntrySize(Byte.MAX_VALUE));
assertEquals(1, valueSizeToEntrySize(0xff));

assertEquals(2, valueSizeToEntrySize(0x1ff));
assertEquals(2, valueSizeToEntrySize(Short.MAX_VALUE));
assertEquals(2, valueSizeToEntrySize(0xffff));

assertEquals(4, valueSizeToEntrySize(0x1ffff));
assertEquals(4, valueSizeToEntrySize(Integer.MAX_VALUE));

assertThrows(IgniteInternalException.class, () -> valueSizeToEntrySize(0xffffffffL));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public abstract class BplusInnerIo<L> extends BplusIo<L> {
/** Offset of the link. */
private static final int SHIFT_LINK = SHIFT_LEFT + PARTITIONLESS_LINK_SIZE_BYTES;

/** Number of bytes a child link takes in storage. */
public static final int CHILD_LINK_SIZE = PARTITIONLESS_LINK_SIZE_BYTES;
tkalkirill marked this conversation as resolved.
Show resolved Hide resolved

/** Offset of the right page ID of the item. */
private final int shiftRight = SHIFT_LINK + itemSize;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static NativeType numberOf(int precision) {
/**
* Creates a STRING type with maximal length is <code>len</code>.
*
* @param len Maximum length of the string.
* @param len Maximum length of the string, {@link Integer#MAX_VALUE} if not defined.
* @return Native type.
*/
public static NativeType stringOf(int len) {
Expand All @@ -111,7 +111,7 @@ public static NativeType stringOf(int len) {
/**
* Creates a BYTES type with maximal length is <code>len</code>.
*
* @param len Maximum length of the byte array.
* @param len Maximum length of the byte array, {@link Integer#MAX_VALUE} if not defined.
* @return Native type.
*/
public static NativeType blobOf(int len) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,33 @@
* Variable-length native type.
*/
public class VarlenNativeType extends NativeType {
/** Length of the type. */
/** Length of the type, {@link Integer#MAX_VALUE} if not defined. */
private final int len;

/**
* Constructor.
*
* @param typeSpec Type spec.
* @param len Type length.
* @param len Type length.
*/
protected VarlenNativeType(NativeTypeSpec typeSpec, int len) {
super(typeSpec);

this.len = len;
}

/** {@inheritDoc} */
@Override
public boolean mismatch(NativeType type) {
return super.mismatch(type) || len < ((VarlenNativeType) type).len;
}

/**
* Get length of the type.
* Get length of the type, {@link Integer#MAX_VALUE} if not defined.
*/
public int length() {
return len;
}

/** {@inheritDoc} */
@Override
public String toString() {
return S.toString(VarlenNativeType.class.getSimpleName(), "name", spec(), "len", len);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.apache.ignite.internal.schema.BinaryTupleSchema;
import org.apache.ignite.internal.schema.NativeTypeSpec;
import org.apache.ignite.internal.schema.row.InternalTuple;
import org.apache.ignite.internal.storage.index.SortedIndexDescriptor.ColumnDescriptor;
import org.apache.ignite.internal.storage.index.SortedIndexDescriptor.SortedIndexColumnDescriptor;

/**
* Comparator implementation for comparing {@link BinaryTuple}s on a per-column basis.
Expand Down Expand Up @@ -63,10 +63,10 @@ public int compare(ByteBuffer buffer1, ByteBuffer buffer2) {

int columnsToCompare = Math.min(tuple1.count(), tuple2.count());

assert columnsToCompare <= descriptor.indexColumns().size();
assert columnsToCompare <= descriptor.columns().size();

for (int i = 0; i < columnsToCompare; i++) {
ColumnDescriptor columnDescriptor = descriptor.indexColumns().get(i);
SortedIndexColumnDescriptor columnDescriptor = descriptor.columns().get(i);

int compare = compareField(tuple1, tuple2, i);

Expand Down Expand Up @@ -102,7 +102,7 @@ private int compareField(InternalTuple tuple1, InternalTuple tuple2, int index)
return 1;
}

ColumnDescriptor columnDescriptor = descriptor.indexColumns().get(index);
SortedIndexColumnDescriptor columnDescriptor = descriptor.columns().get(index);

NativeTypeSpec typeSpec = columnDescriptor.type().spec();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,34 @@
*
* @see HashIndexStorage
*/
public class HashIndexDescriptor {
public class HashIndexDescriptor implements IndexDescriptor {
/**
* Descriptor of a Hash Index column.
*/
public static class ColumnDescriptor {
public static class HashIndexColumnDescriptor implements ColumnDescriptor {
private final String name;

private final NativeType type;

private final boolean nullable;

ColumnDescriptor(ColumnView tableColumnView) {
HashIndexColumnDescriptor(ColumnView tableColumnView) {
this.name = tableColumnView.name();
this.type = ConfigurationToSchemaDescriptorConverter.convert(tableColumnView.type());
this.nullable = tableColumnView.nullable();
}

/**
* Returns the name of an index column.
*/
@Override
public String name() {
return name;
}

/**
* Returns a column type.
*/
@Override
public NativeType type() {
return type;
}

/**
* Returns {@code true} if this column can contain null values or {@code false} otherwise.
*/
@Override
public boolean nullable() {
return nullable;
}
Expand All @@ -84,7 +78,7 @@ public String toString() {

private final UUID id;

private final List<ColumnDescriptor> columns;
private final List<HashIndexColumnDescriptor> columns;

/**
* Creates an Index Descriptor from a given Table Configuration.
Expand Down Expand Up @@ -122,22 +116,18 @@ public HashIndexDescriptor(UUID indexId, TablesView tablesConfig) {

assert columnView != null : "Incorrect index column configuration. " + columnName + " column does not exist";

return new ColumnDescriptor(columnView);
return new HashIndexColumnDescriptor(columnView);
})
.collect(toUnmodifiableList());
}

/**
* Returns the ID of this Index.
*/
@Override
public UUID id() {
return id;
}

/**
* Returns the Column Descriptors that comprise a row of this index.
*/
public List<ColumnDescriptor> indexColumns() {
@Override
public List<HashIndexColumnDescriptor> columns() {
return columns;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.ignite.internal.storage.index;

import java.util.List;
import java.util.UUID;
import org.apache.ignite.internal.schema.NativeType;

/**
* Index descriptor.
*/
public interface IndexDescriptor {
/**
* Index column descriptor.
*/
interface ColumnDescriptor {
/**
* Returns the name of an index column.
*/
String name();

/**
* Returns a column type.
*/
NativeType type();

/**
* Returns {@code true} if this column can contain null values or {@code false} otherwise.
*/
boolean nullable();
}

/**
* Returns the index ID.
*/
UUID id();

/**
* Returns index column descriptions.
*/
List<? extends ColumnDescriptor> columns();
}
Loading