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

[Pine] Support PineTopper as a component of distkv ecosystem. #520

Merged
merged 7 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions pine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,12 @@

<artifactId>distkv-pine</artifactId>

<dependencies>
<dependency>
<groupId>com.distkv</groupId>
<artifactId>distkv-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
17 changes: 15 additions & 2 deletions pine/src/main/java/com/distkv/pine/api/Pine.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,31 @@
import com.distkv.pine.components.PineTopper;
import com.distkv.pine.runtime.PineRuntime;

/**
* The API class of Pine.
*/
public class Pine {

/// The Pine runtime.
private static PineRuntime runtime;

public static void init() {
/**
* Initial the Pine.
*
* @param address The address of distkv server to connect.
*/
public static void init(String address) {
runtime = new PineRuntime();
runtime.init();
runtime.init(address);
}

/**
* Shutdown the Pine.
*/
public static void shutdown() {
jovany-wang marked this conversation as resolved.
Show resolved Hide resolved
if (runtime != null) {
runtime.shutdown();
runtime = null;
}
}

Expand Down
12 changes: 12 additions & 0 deletions pine/src/main/java/com/distkv/pine/components/PineHandle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.distkv.pine.components;

public abstract class PineHandle {
jovany-wang marked this conversation as resolved.
Show resolved Hide resolved

private PineHandleId handleId;

protected String getKey() {
return String.format("%s_%s", getComponentType(), handleId.toString());
}

jovany-wang marked this conversation as resolved.
Show resolved Hide resolved
protected abstract String getComponentType();
}
30 changes: 30 additions & 0 deletions pine/src/main/java/com/distkv/pine/components/PineTopper.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
package com.distkv.pine.components;

import javafx.util.Pair;
import java.util.List;

/**
* The `PineTopper` component is used to do a xxxx.
*/
public interface PineTopper {

/**
* Add a member to this topper.
*
* @param memberName The name of the member that will be added.
* @param memberScore The score of the member.
*/
public void addMember(String memberName, int memberScore);

/**
* Remove a member from this topper.
*
* @param memberName The name of the member that will be removed.
*/
public void removeMember(String memberName);

/**
* Get the topper members.
*
* @param num The top number that will be returned.
*/
// TODO(qwang): The `Pair` should be refined with our implementation.
public List<Pair<String, Integer>> top(int num);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@
package com.distkv.pine.components;

public class PineTopperImpl implements PineTopper {
import com.distkv.client.DistkvClient;
import com.distkv.common.entity.sortedList.SortedListEntity;
import com.google.protobuf.InvalidProtocolBufferException;
import javafx.util.Pair;
import java.util.LinkedList;
import java.util.List;

public class PineTopperImpl extends PineHandle implements PineTopper {


private static final String COMPONENT_TYPE = "TOPPER";

private DistkvClient distkvClient;

public PineTopperImpl(DistkvClient distkvClient) {
this.distkvClient = distkvClient;
// Construct an empty sorted-list to the store.
distkvClient.sortedLists().put(getKey(), new LinkedList<>());
}

@Override
public void addMember(String memberName, int memberScore) {
distkvClient.sortedLists().putMember(getKey(), new SortedListEntity(memberName, memberScore));
}

public void removeMember(String memberName) {
distkvClient.sortedLists().removeMember(getKey(), memberName);
}

public List<Pair<String, Integer>> top(int num) {
try {
LinkedList<SortedListEntity> result = distkvClient.sortedLists().top(getKey(), num);
// Covert the result type.

} catch (InvalidProtocolBufferException e) {
// TODO(qwang): Refine this.
throw new RuntimeException(e);
}
}

@Override
protected String getComponentType() {
return COMPONENT_TYPE;
}

}
10 changes: 9 additions & 1 deletion pine/src/main/java/com/distkv/pine/runtime/PineRuntime.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package com.distkv.pine.runtime;

import com.distkv.client.DefaultDistkvClient;
import com.distkv.client.DistkvClient;
import com.distkv.pine.components.PineTopper;
import com.distkv.pine.components.PineTopperImpl;

public class PineRuntime {

public void init() {

/**
* The distkv sync client.
*/
private DistkvClient distkvClient;

public void init(String address) {
distkvClient = new DefaultDistkvClient(address);
}

public void shutdown() {
Expand Down