Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "specifications"]
path = driver-core/src/test/resources/specifications
path = testing/resources/specifications
url = https://github.com/mongodb/specifications
5 changes: 5 additions & 0 deletions bson/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ plugins {

base.archivesName.set("bson")

tasks.processTestResources {
from("${rootProject.projectDir}/testing/resources")
into("${layout.buildDirectory.get()}/resources/test")
}

configureMavenPublication {
pom {
name.set("BSON")
Expand Down
11 changes: 3 additions & 8 deletions bson/src/main/org/bson/BinaryVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@

import org.bson.annotations.Beta;
import org.bson.annotations.Reason;

import static org.bson.assertions.Assertions.isTrueArgument;
import static org.bson.assertions.Assertions.notNull;
import org.bson.diagnostics.Logger;
import org.bson.diagnostics.Loggers;

/**
* Binary Vectors are densely packed arrays of numbers, all the same type, which are stored and retrieved efficiently using the BSON Binary
Expand All @@ -33,6 +32,7 @@
* @since 5.3
*/
public abstract class BinaryVector {
protected static final Logger LOGGER = Loggers.getLogger("BinaryVector");
private final DataType dataType;

BinaryVector(final DataType dataType) {
Expand Down Expand Up @@ -64,9 +64,6 @@ public abstract class BinaryVector {
*/
@Beta(Reason.SERVER)
public static PackedBitBinaryVector packedBitVector(final byte[] data, final byte padding) {
notNull("data", data);
isTrueArgument("Padding must be between 0 and 7 bits. Provided padding: " + padding, padding >= 0 && padding <= 7);
isTrueArgument("Padding must be 0 if vector is empty. Provided padding: " + padding, padding == 0 || data.length > 0);
return new PackedBitBinaryVector(data, padding);
}

Expand All @@ -83,7 +80,6 @@ public static PackedBitBinaryVector packedBitVector(final byte[] data, final byt
* @return A {@link Int8BinaryVector} instance with the {@link DataType#INT8} data type.
*/
public static Int8BinaryVector int8Vector(final byte[] data) {
notNull("data", data);
return new Int8BinaryVector(data);
}

Expand All @@ -99,7 +95,6 @@ public static Int8BinaryVector int8Vector(final byte[] data) {
* @return A {@link Float32BinaryVector} instance with the {@link DataType#FLOAT32} data type.
*/
public static Float32BinaryVector floatVector(final float[] data) {
notNull("data", data);
return new Float32BinaryVector(data);
}

Expand Down
8 changes: 4 additions & 4 deletions bson/src/main/org/bson/Float32BinaryVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.util.Arrays;

import static org.bson.assertions.Assertions.assertNotNull;
import static org.bson.assertions.Assertions.notNull;

/**
* Represents a vector of 32-bit floating-point numbers, where each element in the vector is a float.
Expand All @@ -35,9 +35,9 @@ public final class Float32BinaryVector extends BinaryVector {

private final float[] data;

Float32BinaryVector(final float[] vectorData) {
Float32BinaryVector(final float[] data) {
super(DataType.FLOAT32);
this.data = assertNotNull(vectorData);
this.data = notNull("data", data);
}

/**
Expand All @@ -49,7 +49,7 @@ public final class Float32BinaryVector extends BinaryVector {
* @return the underlying float array representing this {@link Float32BinaryVector} vector.
*/
public float[] getData() {
return assertNotNull(data);
return data;
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions bson/src/main/org/bson/Int8BinaryVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.Arrays;
import java.util.Objects;

import static org.bson.assertions.Assertions.assertNotNull;
import static org.bson.assertions.Assertions.notNull;

/**
* Represents a vector of 8-bit signed integers, where each element in the vector is a byte.
Expand All @@ -38,7 +38,7 @@ public final class Int8BinaryVector extends BinaryVector {

Int8BinaryVector(final byte[] data) {
super(DataType.INT8);
this.data = assertNotNull(data);
this.data = notNull("data", data);
}

/**
Expand All @@ -50,7 +50,7 @@ public final class Int8BinaryVector extends BinaryVector {
* @return the underlying byte array representing this {@link Int8BinaryVector} vector.
*/
public byte[] getData() {
return assertNotNull(data);
return data;
}

@Override
Expand Down
13 changes: 12 additions & 1 deletion bson/src/main/org/bson/PackedBitBinaryVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.Objects;

import static org.bson.assertions.Assertions.assertNotNull;
import static org.bson.assertions.Assertions.isTrueArgument;
import static org.bson.assertions.Assertions.notNull;

/**
* Represents a packed bit vector, where each element of the vector is represented by a single bit (0 or 1).
Expand All @@ -43,8 +45,17 @@ public final class PackedBitBinaryVector extends BinaryVector {

PackedBitBinaryVector(final byte[] data, final byte padding) {
super(DataType.PACKED_BIT);
this.data = assertNotNull(data);
this.data = notNull("data", data);
this.padding = padding;
isTrueArgument("Padding must be between 0 and 7 bits. Provided padding: " + padding, padding >= 0 && padding <= 7);
isTrueArgument("Padding must be 0 if vector is empty. Provided padding: " + padding, padding == 0 || data.length > 0);
if (padding > 0) {
int mask = (1 << padding) - 1;
if ((data[data.length - 1] & mask) != 0) {
// JAVA-5848 in version 6.0.0 will convert this logging into an IllegalArgumentException
LOGGER.warn("The last " + padding + " padded bits should be zero in the final byte.");
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ExtendedJsonDoubleConverter implements Converter<Double> {
public void convert(final Double value, final StrictJsonWriter writer) {
writer.writeStartObject();
writer.writeName("$numberDouble");
writer.writeString(Double.toString(value));
writer.writeString(JsonDoubleHelper.toString(value));
writer.writeEndObject();

}
Expand Down
2 changes: 1 addition & 1 deletion bson/src/main/org/bson/json/JsonDoubleConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
class JsonDoubleConverter implements Converter<Double> {
@Override
public void convert(final Double value, final StrictJsonWriter writer) {
writer.writeNumber(Double.toString(value));
writer.writeNumber(JsonDoubleHelper.toString(value));
}
}
32 changes: 32 additions & 0 deletions bson/src/main/org/bson/json/JsonDoubleHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* 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 org.bson.json;

import java.util.regex.Pattern;

final class JsonDoubleHelper {

private static final Pattern POSITIVE_EXPONENT_PATTERN = Pattern.compile("E(\\d+)");
private static final String POSITIVE_EXPONENT_REPLACER = "E+$1";

static String toString(final double value) {
String doubleString = Double.toString(value);
return POSITIVE_EXPONENT_PATTERN.matcher(doubleString).replaceAll(POSITIVE_EXPONENT_REPLACER);
}

private JsonDoubleHelper() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void convert(final Double value, final StrictJsonWriter writer) {
if (value.isNaN() || value.isInfinite()) {
FALLBACK_CONVERTER.convert(value, writer);
} else {
writer.writeNumber(Double.toString(value));
writer.writeNumber(JsonDoubleHelper.toString(value));
}
}
}
50 changes: 0 additions & 50 deletions bson/src/test/resources/bson-binary-vector/float32.json

This file was deleted.

56 changes: 0 additions & 56 deletions bson/src/test/resources/bson-binary-vector/int8.json

This file was deleted.

Loading