-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support pagination when single index query (#328)
Support pagination when single index query Change-Id: Iab355a9f68cebd8ebc666cf126842eb19fa40240
- Loading branch information
Showing
46 changed files
with
1,584 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/IdHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* 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.backend.page; | ||
|
||
import java.util.Set; | ||
import java.util.function.Function; | ||
|
||
import com.baidu.hugegraph.backend.id.Id; | ||
import com.baidu.hugegraph.backend.query.ConditionQuery; | ||
import com.baidu.hugegraph.util.E; | ||
import com.baidu.hugegraph.util.InsertionOrderUtil; | ||
import com.google.common.collect.ImmutableSet; | ||
|
||
public final class IdHolder { | ||
|
||
private final ConditionQuery query; | ||
private final Function<ConditionQuery, PageIds> idsFetcher; | ||
private boolean exhausted; | ||
|
||
private Set<Id> ids; | ||
|
||
/** | ||
* For non-paging situation | ||
*/ | ||
public IdHolder(Set<Id> ids) { | ||
this.query = null; | ||
this.idsFetcher = null; | ||
this.exhausted = false; | ||
if (ids instanceof ImmutableSet) { | ||
this.ids = InsertionOrderUtil.newSet(ids); | ||
} else { | ||
this.ids = ids; | ||
} | ||
} | ||
|
||
/** | ||
* For paging situation | ||
*/ | ||
public IdHolder(ConditionQuery query, | ||
Function<ConditionQuery, PageIds> idsFetcher) { | ||
E.checkArgument(query.paging(), | ||
"Query '%s' must include page info", query); | ||
this.query = query.copy(); | ||
this.idsFetcher = idsFetcher; | ||
this.exhausted = false; | ||
this.ids = null; | ||
} | ||
|
||
public void merge(Set<Id> ids) { | ||
this.ids.addAll(ids); | ||
} | ||
|
||
public Set<Id> ids() { | ||
E.checkNotNull(this.ids, "ids"); | ||
return this.ids; | ||
} | ||
|
||
public int size() { | ||
if (this.ids == null) { | ||
return 0; | ||
} | ||
return this.ids.size(); | ||
} | ||
|
||
public boolean paging() { | ||
return this.idsFetcher != null; | ||
} | ||
|
||
public PageIds fetchNext(String page, long pageSize) { | ||
if (this.exhausted) { | ||
return PageIds.EMPTY; | ||
} | ||
|
||
this.query.page(page); | ||
this.query.limit(pageSize); | ||
|
||
PageIds result = this.idsFetcher.apply(this.query); | ||
|
||
assert result != null; | ||
this.ids = result.ids(); | ||
if (this.ids.size() != this.query.limit() || result.page() == null) { | ||
this.exhausted = true; | ||
} | ||
return result; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/IdHolderList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* 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.backend.page; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
|
||
import com.baidu.hugegraph.util.E; | ||
|
||
public final class IdHolderList extends ArrayList<IdHolder> { | ||
|
||
private final boolean paging; | ||
|
||
public IdHolderList(boolean paging) { | ||
this.paging = paging; | ||
} | ||
|
||
public boolean paging() { | ||
return this.paging; | ||
} | ||
|
||
@Override | ||
public boolean add(IdHolder holder) { | ||
E.checkArgument(this.paging == holder.paging(), | ||
"The IdHolder to be linked must be " + | ||
"in same paging mode"); | ||
if (this.paging || this.isEmpty()) { | ||
super.add(holder); | ||
} else { | ||
assert this.size() == 1; | ||
IdHolder self = this.get(0); | ||
assert !self.paging(); | ||
self.merge(holder.ids()); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean addAll(Collection<? extends IdHolder> idHolders) { | ||
for (IdHolder idHolder : idHolders) { | ||
this.add(idHolder); | ||
} | ||
return true; | ||
} | ||
|
||
public int idsSize() { | ||
if (this.paging || this.isEmpty()) { | ||
return 0; | ||
} else { | ||
assert this.size() == 1; | ||
return this.get(0).size(); | ||
} | ||
} | ||
} |
Oops, something went wrong.