Skip to content

Commit

Permalink
support paging for task (#68)
Browse files Browse the repository at this point in the history
related: #718

Change-Id: I9c5adcfe3e3d486ed832d98a923287a6f97f84bb
  • Loading branch information
zhoney authored and Linary committed Nov 7, 2019
1 parent cf72286 commit f4b523f
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 14 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-client</artifactId>
<version>1.7.10</version>
<version>1.8.0</version>
<packaging>jar</packaging>

<name>hugegraph-client</name>
Expand Down Expand Up @@ -113,7 +113,7 @@
<manifestEntries>
<!-- Must be on one line, otherwise the automatic
upgrade script cannot replace the version number -->
<Implementation-Version>1.7.10.0</Implementation-Version>
<Implementation-Version>1.8.0.0</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
Expand Down
23 changes: 15 additions & 8 deletions src/main/java/com/baidu/hugegraph/api/task/TaskAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ public List<Task> list(String status, long limit) {
return result.readList(TASKS, Task.class);
}

public TasksWithPage list(String status, String page, long limit) {
E.checkArgument(page != null, "The page can not be null");
this.client.checkApiVersion("0.48", "getting tasks by paging");
Map<String, Object> params = new LinkedHashMap<>();
params.put("limit", limit);
params.put("page", page);
if (status != null) {
params.put("status", status);
}
RestResult result = this.client.get(this.path(), params);
return result.readObject(TasksWithPage.class);
}

public List<Task> list(List<Long> ids) {
Map<String, Object> params = new LinkedHashMap<>();
params.put("ids", ids);
Expand All @@ -83,17 +96,11 @@ public void delete(long id) {
this.client.delete(path(), String.valueOf(id));
}

public boolean cancel(long id) {
public Task cancel(long id) {
Map<String, Object> params = ImmutableMap.of("action", "cancel");
RestResult result = this.client.put(path(), String.valueOf(id),
ImmutableMap.of(), params);
@SuppressWarnings("unchecked")
Map<String, Object> response = result.readObject(Map.class);
Object cancelled = response.get("cancelled");
E.checkState(cancelled instanceof Boolean,
"Invalid task-cancel response, expect format is " +
"{\"cancelled\": [true|false]}, but got '%s'", response);
return (Boolean) cancelled;
return result.readObject(Task.class);
}

public Task waitUntilTaskSuccess(long taskId, long seconds) {
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/com/baidu/hugegraph/api/task/TasksWithPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2017 HugeGraph Authors
*
* 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 com.baidu.hugegraph.api.task;

import java.util.List;

import com.baidu.hugegraph.structure.Task;
import com.fasterxml.jackson.annotation.JsonProperty;

public class TasksWithPage {

@JsonProperty
private String page;
@JsonProperty
private List<Task> tasks;

public String page() {
return this.page;
}

public List<Task> tasks() {
return this.tasks;
}
}
11 changes: 10 additions & 1 deletion src/main/java/com/baidu/hugegraph/driver/TaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;

import com.baidu.hugegraph.api.task.TaskAPI;
import com.baidu.hugegraph.api.task.TasksWithPage;
import com.baidu.hugegraph.client.RestClient;
import com.baidu.hugegraph.structure.Task;

Expand All @@ -41,6 +42,10 @@ public List<Task> list(long limit) {
return this.taskAPI.list(null, limit);
}

public List<Task> list(List<Long> ids) {
return this.taskAPI.list(ids);
}

public List<Task> list(String status) {
return this.list(status, -1L);
}
Expand All @@ -49,6 +54,10 @@ public List<Task> list(String status, long limit) {
return this.taskAPI.list(status, limit);
}

public TasksWithPage list(String status, String page, long limit) {
return this.taskAPI.list(status, page, limit);
}

public Task get(long id) {
return this.taskAPI.get(id);
}
Expand All @@ -57,7 +66,7 @@ public void delete(long id) {
this.taskAPI.delete(id);
}

public boolean cancel(long id) {
public Task cancel(long id) {
return this.taskAPI.cancel(id);
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/baidu/hugegraph/structure/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ public boolean completed() {
.contains(this.status);
}

public boolean cancelled() {
return "cancelled".equals(this.status);
}

public boolean success() {
return "success".equals(this.status);
}
Expand Down
108 changes: 105 additions & 3 deletions src/test/java/com/baidu/hugegraph/api/TaskApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package com.baidu.hugegraph.api;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -28,6 +29,7 @@
import org.junit.Test;

import com.baidu.hugegraph.api.gremlin.GremlinRequest;
import com.baidu.hugegraph.api.task.TasksWithPage;
import com.baidu.hugegraph.structure.Task;
import com.baidu.hugegraph.structure.gremlin.ResultSet;
import com.baidu.hugegraph.structure.schema.IndexLabel;
Expand All @@ -50,7 +52,7 @@ public void teardown() throws Exception {
}

@Test
public void testList() {
public void testListAll() {
IndexLabel personByCity = schema().getIndexLabel("personByCity");
IndexLabel personByAge = schema().getIndexLabel("personByAge");
IndexLabel knowsByDate = schema().getIndexLabel("knowsByDate");
Expand All @@ -63,6 +65,7 @@ public void testList() {
taskIds.add(rebuildAPI.rebuild(createdByDate));

List<Task> tasks = taskAPI.list(null, -1);

Assert.assertEquals(4, tasks.size());

Set<Long> listedTaskIds = new HashSet<>();
Expand All @@ -73,12 +76,111 @@ public void testList() {
Assert.assertTrue(taskIds.containsAll(listedTaskIds));

taskIds.forEach(BaseApiTest::waitUntilTaskCompleted);

taskIds.forEach(id -> taskAPI.delete(id));

tasks = taskAPI.list(null, -1);
Assert.assertEquals(0, tasks.size());
}

@Test
public void testListByIds() {
IndexLabel personByCity = schema().getIndexLabel("personByCity");
IndexLabel personByAge = schema().getIndexLabel("personByAge");
IndexLabel knowsByDate = schema().getIndexLabel("knowsByDate");
IndexLabel createdByDate = schema().getIndexLabel("createdByDate");

List<Long> taskIds = new ArrayList<>();
taskIds.add(rebuildAPI.rebuild(personByCity));
taskIds.add(rebuildAPI.rebuild(personByAge));
taskIds.add(rebuildAPI.rebuild(knowsByDate));
taskIds.add(rebuildAPI.rebuild(createdByDate));

taskIds.forEach(BaseApiTest::waitUntilTaskCompleted);

List<Task> tasks = taskAPI.list(taskIds);

Assert.assertEquals(4, tasks.size());
Set<Long> listedTaskIds = new HashSet<>();
for (Task task : tasks) {
listedTaskIds.add(task.id());
}
Assert.assertEquals(taskIds.size(), listedTaskIds.size());
Assert.assertTrue(taskIds.containsAll(listedTaskIds));
}

@Test
public void testListByStatus() {
IndexLabel personByCity = schema().getIndexLabel("personByCity");
IndexLabel personByAge = schema().getIndexLabel("personByAge");
IndexLabel knowsByDate = schema().getIndexLabel("knowsByDate");
IndexLabel createdByDate = schema().getIndexLabel("createdByDate");

Set<Long> taskIds = new HashSet<>();
taskIds.add(rebuildAPI.rebuild(personByCity));
taskIds.add(rebuildAPI.rebuild(personByAge));
taskIds.add(rebuildAPI.rebuild(knowsByDate));
taskIds.add(rebuildAPI.rebuild(createdByDate));

taskIds.forEach(BaseApiTest::waitUntilTaskCompleted);

List<Task> tasks = taskAPI.list("SUCCESS", -1);

Assert.assertEquals(4, tasks.size());
Set<Long> listedTaskIds = new HashSet<>();
for (Task task : tasks) {
listedTaskIds.add(task.id());
}
Assert.assertEquals(taskIds.size(), listedTaskIds.size());
Assert.assertTrue(taskIds.containsAll(listedTaskIds));

tasks = taskAPI.list("SUCCESS", 3);

Assert.assertEquals(3, tasks.size());
Set<Long> listedTaskIds1 = new HashSet<>();
for (Task task : tasks) {
listedTaskIds1.add(task.id());
}
Assert.assertEquals(3, listedTaskIds1.size());
Assert.assertTrue(taskIds.containsAll(listedTaskIds));
}

@Test
public void testListByStatusAndPage() {
IndexLabel personByCity = schema().getIndexLabel("personByCity");
IndexLabel personByAge = schema().getIndexLabel("personByAge");
IndexLabel knowsByDate = schema().getIndexLabel("knowsByDate");
IndexLabel createdByDate = schema().getIndexLabel("createdByDate");

Set<Long> taskIds = new HashSet<>();
taskIds.add(rebuildAPI.rebuild(personByCity));
taskIds.add(rebuildAPI.rebuild(personByAge));
taskIds.add(rebuildAPI.rebuild(knowsByDate));
taskIds.add(rebuildAPI.rebuild(createdByDate));

taskIds.forEach(BaseApiTest::waitUntilTaskCompleted);

TasksWithPage tasksWithPage = taskAPI.list("SUCCESS", "", 2);

List<Task> tasks = tasksWithPage.tasks();
Assert.assertEquals(2, tasks.size());
Set<Long> listedTaskIds = new HashSet<>();
for (Task task : tasks) {
listedTaskIds.add(task.id());
}

tasksWithPage = taskAPI.list("SUCCESS", tasksWithPage.page(), 2);

List<Task> tasks1 = tasksWithPage.tasks();
Assert.assertEquals(2, tasks1.size());
Assert.assertNull(tasksWithPage.page());
for (Task task : tasks1) {
listedTaskIds.add(task.id());
}
Assert.assertEquals(taskIds.size(), listedTaskIds.size());
Assert.assertTrue(taskIds.containsAll(listedTaskIds));
}

@Test
public void testGet() {
IndexLabel personByCity = schema().getIndexLabel("personByCity");
Expand Down Expand Up @@ -155,8 +257,8 @@ public void testCancel() {
}
}
// Cancel async task
boolean cancelled = taskAPI.cancel(taskId);
Assert.assertTrue(cancelled);
Task task = taskAPI.cancel(taskId);
Assert.assertTrue(task.cancelled());

resultSet = gremlin().execute(request);
Assert.assertTrue(resultSet.size() < 10);
Expand Down

0 comments on commit f4b523f

Please sign in to comment.