mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-25 18:55:28 -06:00
add UUID -> ToBase64String() -> String
This commit is contained in:
parent
339e507c5e
commit
17500b921c
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022 nosqlbench
|
||||||
|
*
|
||||||
|
* 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 io.nosqlbench.virtdata.library.basics.shared.from_uuid;
|
||||||
|
|
||||||
|
import io.nosqlbench.virtdata.api.annotations.Example;
|
||||||
|
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
@ThreadSafeMapper
|
||||||
|
public class ToBase64String implements Function<UUID,String> {
|
||||||
|
private ThreadLocal<Base64.Encoder> tl_encoder = ThreadLocal.withInitial(Base64::getEncoder);
|
||||||
|
|
||||||
|
@Example({"ToBase64String()","Encode the bits of a UUID into a Base64 String"})
|
||||||
|
public ToBase64String() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String apply(UUID uuid) {
|
||||||
|
ByteBuffer bytes = ByteBuffer.allocate(Long.BYTES << 1);
|
||||||
|
bytes.putLong(uuid.getMostSignificantBits());
|
||||||
|
bytes.putLong(uuid.getLeastSignificantBits());
|
||||||
|
Base64.Encoder encoder = tl_encoder.get();
|
||||||
|
byte[] encoded = encoder.encode(bytes.array());
|
||||||
|
return new String(encoded, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
}
|
@ -16,15 +16,19 @@
|
|||||||
|
|
||||||
package io.nosqlbench.virtdata.library.basics.shared.from_long.to_string;
|
package io.nosqlbench.virtdata.library.basics.shared.from_long.to_string;
|
||||||
|
|
||||||
|
import io.nosqlbench.virtdata.library.basics.shared.from_long.to_uuid.ToUUID;
|
||||||
|
import io.nosqlbench.virtdata.library.basics.shared.from_uuid.ToBase64String;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public class ToBase64StringTest {
|
public class ToBase64StringTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testApply() {
|
public void testApply() {
|
||||||
ToBase64String f = new ToBase64String();
|
io.nosqlbench.virtdata.library.basics.shared.from_long.to_string.ToBase64String f = new io.nosqlbench.virtdata.library.basics.shared.from_long.to_string.ToBase64String();
|
||||||
String apply = f.apply(32144123454345L);
|
String apply = f.apply(32144123454345L);
|
||||||
assertThat(apply).isEqualTo("AAAdPCMPY4k=");
|
assertThat(apply).isEqualTo("AAAdPCMPY4k=");
|
||||||
}
|
}
|
||||||
@ -35,7 +39,15 @@ public class ToBase64StringTest {
|
|||||||
new io.nosqlbench.virtdata.library.basics.shared.unary_string.ToBase64String();
|
new io.nosqlbench.virtdata.library.basics.shared.unary_string.ToBase64String();
|
||||||
String r = f.apply("four score and seven years ago");
|
String r = f.apply("four score and seven years ago");
|
||||||
assertThat(r).isEqualTo("Zm91ciBzY29yZSBhbmQgc2V2ZW4geWVhcnMgYWdv");
|
assertThat(r).isEqualTo("Zm91ciBzY29yZSBhbmQgc2V2ZW4geWVhcnMgYWdv");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUuidToBase64() {
|
||||||
|
ToUUID toUUID = new ToUUID();
|
||||||
|
UUID uuid1 = toUUID.apply(1);
|
||||||
|
ToBase64String toBase64 = new ToBase64String();
|
||||||
|
String string = toBase64.apply(uuid1);
|
||||||
|
assertThat(string).isEqualTo("ASNFZ4mrTe+AAAAAAAAAAQ==");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user