-
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.
Refactor IdHolder, QueryHolder and other page related classes
Change-Id: I06369fc3ab8e4ce52b9c68ac8c343d2bfea79a9d
- Loading branch information
Showing
15 changed files
with
551 additions
and
698 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
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 " + | ||
"IdHolder in 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(); | ||
} | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
hugegraph-core/src/main/java/com/baidu/hugegraph/backend/page/PageEntryIterator.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,107 @@ | ||
/* | ||
* 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.Iterator; | ||
import java.util.NoSuchElementException; | ||
|
||
import com.baidu.hugegraph.backend.store.BackendEntry; | ||
import com.baidu.hugegraph.exception.NotSupportException; | ||
import com.baidu.hugegraph.iterator.Metadatable; | ||
import com.baidu.hugegraph.util.E; | ||
|
||
public class PageEntryIterator implements Iterator<BackendEntry>, Metadatable { | ||
|
||
private final QueryList queries; | ||
private final long pageSize; | ||
private QueryList.PageIterator results; | ||
private PageState pageState; | ||
private long remaining; | ||
|
||
public PageEntryIterator(QueryList queries, long pageSize) { | ||
this.queries = queries; | ||
this.pageSize = pageSize; | ||
this.results = QueryList.PageIterator.EMPTY; | ||
this.pageState = this.parsePageState(); | ||
this.remaining = queries.parent().limit(); | ||
} | ||
|
||
private PageState parsePageState() { | ||
String page = this.queries.parent().page(); | ||
PageState pageState = PageState.fromString(page); | ||
E.checkState(pageState.offset() < this.queries.total(), | ||
"Invalid page '%s' with an offset '%s' exceeds " + | ||
"the size of IdHolderList", page, pageState.offset()); | ||
return pageState; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if (this.results.iterator().hasNext()) { | ||
return true; | ||
} | ||
return this.fetch(); | ||
} | ||
|
||
private boolean fetch() { | ||
if (this.remaining <= 0 || | ||
this.pageState.offset() >= this.queries.total()) { | ||
return false; | ||
} | ||
|
||
long pageSize = this.remaining < this.pageSize ? | ||
this.remaining : this.pageSize; | ||
this.results = this.queries.fetchNext(this.pageState, pageSize); | ||
assert this.results != null; | ||
|
||
if (this.results.iterator().hasNext()) { | ||
if (results.page() == null) { | ||
this.pageState.increase(); | ||
} else { | ||
this.pageState.page(this.results.page()); | ||
} | ||
return true; | ||
} else { | ||
this.pageState.increase(); | ||
return this.fetch(); | ||
} | ||
} | ||
|
||
@Override | ||
public BackendEntry next() { | ||
if (!this.results.iterator().hasNext() && !this.hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
BackendEntry entry = this.results.iterator().next(); | ||
this.remaining--; | ||
return entry; | ||
} | ||
|
||
@Override | ||
public Object metadata(String meta, Object... args) { | ||
if ("page".equals(meta)) { | ||
if (this.pageState.offset() >= this.queries.total()) { | ||
return null; | ||
} | ||
return this.pageState.toString(); | ||
} | ||
throw new NotSupportException("Invalid meta '%s'", meta); | ||
} | ||
} |
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
Oops, something went wrong.