Skip to content

Commit

Permalink
Woohoo! First successful replication.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hellblazer committed Dec 20, 2023
1 parent ae531bf commit 03a7325
Show file tree
Hide file tree
Showing 14 changed files with 668 additions and 408 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
*/
package com.salesforce.apollo.choam.support;

import java.nio.ByteBuffer;

import com.salesforce.apollo.cryptography.Digest;
import org.h2.mvstore.WriteBuffer;
import org.h2.mvstore.type.BasicDataType;

import com.salesforce.apollo.cryptography.Digest;
import java.nio.ByteBuffer;

/**
* @author hal.hildebrand
*
*/

public class DigestType extends BasicDataType<Digest> {
Expand All @@ -41,8 +39,7 @@ public Digest read(ByteBuffer buff) {
}

@Override
public void write(WriteBuffer buff, Digest obj) {
Digest digest = (Digest) obj;
public void write(WriteBuffer buff, Digest digest) {
buff.put(digest.getAlgorithm().digestCode());
for (long l : digest.getLongs()) {
buff.putLong(l);
Expand Down
10 changes: 4 additions & 6 deletions grpc/src/main/proto/leyden.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ service Reconciliation {
}

message KeyAndToken {
int32 ring = 1;
bytes key = 2;
bytes token = 3;
bytes key = 1;
bytes token = 2;
}

message Update {
Expand All @@ -54,9 +53,8 @@ message Interval {
}

message Binding {
int32 ring = 1;
Bound bound = 2;
bytes token = 3;
Bound bound = 1;
bytes token = 2;
}

message Bound {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,48 @@
*/
package com.salesforce.apollo.leyden;

import com.google.protobuf.Message;
import com.google.protobuf.InvalidProtocolBufferException;
import com.salesforce.apollo.leyden.proto.Bound;
import org.h2.mvstore.DataUtils;
import org.h2.mvstore.WriteBuffer;
import org.h2.mvstore.type.BasicDataType;

import java.nio.ByteBuffer;
import java.util.function.Function;

/**
* @author hal.hildebrand
*/
public final class ProtobufDatatype<Type extends Message> extends BasicDataType<Type> {
private Function<byte[], Type> factory;
public final class BoundDatatype extends BasicDataType<Bound> {

public ProtobufDatatype(Function<byte[], Type> factory) {
this.factory = factory;
@Override
public int compare(Bound a, Bound b) {
return super.compare(a, b);
}

@Override
public Type[] createStorage(int size) {
@SuppressWarnings("unchecked")
final var storage = (Type[]) new Object[size];
return storage;
public Bound[] createStorage(int size) {
return new Bound[size];
}

@Override
public int getMemory(Type data) {
public int getMemory(Bound data) {
return data.getSerializedSize();
}

@Override
public Type read(ByteBuffer buff) {
public Bound read(ByteBuffer buff) {
int size = DataUtils.readVarInt(buff);
byte[] data = new byte[size];
buff.get(data);
return factory.apply(data);
try {
return Bound.parseFrom(buff);
} catch (InvalidProtocolBufferException e) {
throw new IllegalArgumentException(e);
}
}

@Override
public void write(WriteBuffer buff, Type data) {
public void write(WriteBuffer buff, Bound data) {
buff.putVarInt(data.getSerializedSize());
buff.put(data.toByteString().toByteArray());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2022, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
package com.salesforce.apollo.leyden;

import com.salesforce.apollo.cryptography.Digest;
import com.salesforce.apollo.cryptography.DigestAlgorithm;
import org.h2.mvstore.WriteBuffer;
import org.h2.mvstore.type.BasicDataType;

import java.nio.ByteBuffer;

/**
* @author hal.hildebrand
*/
public final class DigestDatatype extends BasicDataType<Digest> {
private final DigestAlgorithm algorithm;

public DigestDatatype(DigestAlgorithm algorithm) {
this.algorithm = algorithm;
}

@Override
public int compare(Digest a, Digest b) {
return a.compareTo(b);
}

@Override
public Digest[] createStorage(int size) {
return new Digest[size];
}

@Override
public int getMemory(Digest data) {
return algorithm.longLength() * 8;
}

@Override
public Digest read(ByteBuffer buff) {
byte[] data = new byte[algorithm.longLength() * 8];
buff.get(data);
return new Digest(algorithm, data);
}

@Override
public void write(WriteBuffer buff, Digest data) {
buff.put(data.getBytes());
}
}
Loading

0 comments on commit 03a7325

Please sign in to comment.