Skip to content

Commit

Permalink
Refactor IdHolder, QueryHolder and other page related classes
Browse files Browse the repository at this point in the history
Change-Id: I06369fc3ab8e4ce52b9c68ac8c343d2bfea79a9d
  • Loading branch information
Linary committed Mar 15, 2019
1 parent b2223c7 commit 4a25e2c
Show file tree
Hide file tree
Showing 15 changed files with 551 additions and 698 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,90 +17,81 @@
* under the License.
*/

package com.baidu.hugegraph.backend.tx;
package com.baidu.hugegraph.backend.page;

import java.util.Set;
import java.util.function.Supplier;

import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.backend.query.Query;
import com.baidu.hugegraph.exception.NotSupportException;
import com.baidu.hugegraph.iterator.Metadatable;
import com.baidu.hugegraph.util.E;

public final class PagedIdHolder implements IdHolder, Metadatable {
public final class IdHolder {

private final Query query;
private final Supplier<PagedIds> idsFetcher;
private final Supplier<PageIds> idsFetcher;
private boolean exhausted;

private int limit;
private Set<Id> ids;
private String page;
private boolean finished;

public PagedIdHolder(Query query, Supplier<PagedIds> idsFetcher) {
/**
* For non-paged situation
*/
public IdHolder(Set<Id> ids) {
this.query = null;
this.idsFetcher = null;
this.exhausted = false;
this.ids = ids;
}

/**
* For paged situation
*/
public IdHolder(Query query, Supplier<PageIds> idsFetcher) {
E.checkArgument(query.paging(),
"Query '%s' must carry the page info in paged mode",
query);
this.query = query;
this.idsFetcher = idsFetcher;
this.exhausted = false;
this.ids = null;
this.page = query.page();
this.finished = false;
}

public int limit() {
return this.limit;
}

public void limit(int limit) {
this.limit = limit;
public void merge(Set<Id> ids) {
this.ids.addAll(ids);
}

public String page() {
return this.page;
public Set<Id> ids() {
E.checkNotNull(this.ids, "ids");
return this.ids;
}

public void page(String page) {
this.page = page;
public int size() {
if (this.ids == null) {
return 0;
}
return this.ids.size();
}

public void resetPage() {
this.page = "";
public boolean paging() {
return this.idsFetcher != null;
}

public Set<Id> fetchFromBackend() {
if (this.finished) {
public PageIds fetchNext(String page, long pageSize) {
if (this.exhausted) {
return null;
}

this.query.page(this.page);
this.query.limit(this.limit);
this.query.page(page);
this.query.limit(pageSize);

PagedIds result = this.idsFetcher.get();
PageIds result = this.idsFetcher.get();
this.ids = result.ids();
this.page = result.page();

assert this.ids != null;
if (this.ids.size() != this.query.limit() || this.page == null) {
this.finished = true;
}
return this.ids;
}

@Override
public int size() {
if (this.ids == null) {
return 0;
}
return this.ids.size();
}

@Override
public Object metadata(String meta, Object... args) {
if ("page".equals(meta)) {
return this.page;
if (this.ids.size() != this.query.limit() || result.page() == null) {
this.exhausted = true;
}
throw new NotSupportException("Invalid meta '%s'", meta);
return result;
}
}
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();
}
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
* under the License.
*/

package com.baidu.hugegraph.backend.tx;
package com.baidu.hugegraph.backend.page;

import java.util.Set;

import com.baidu.hugegraph.backend.id.Id;

public final class PagedIds {
public final class PageIds {

private final Set<Id> ids;
private final String page;

public PagedIds(Set<Id> ids, String page) {
public PageIds(Set<Id> ids, String page) {
this.ids = ids;
this.page = page;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package com.baidu.hugegraph.backend.tx;
package com.baidu.hugegraph.backend.page;

import java.util.Base64;

Expand All @@ -26,22 +26,33 @@
import com.baidu.hugegraph.util.Bytes;
import com.baidu.hugegraph.util.E;

public class PageState {
public final class PageState {

private final int offset;
private final String page;
public static final String PAGE_NONE = "";

private int offset;
private String page;

public PageState(int offset, String page) {
E.checkArgument(offset >= 0, "The offset must >= 0");
E.checkArgument(offset >= 0, "The offset must be >= 0");
E.checkNotNull(page, "page");
this.offset = offset;
this.page = page;
}

public void increase() {
this.offset++;
this.page = PAGE_NONE;
}

public int offset() {
return this.offset;
}

public void page(String page) {
this.page = page;
}

public String page() {
return this.page;
}
Expand Down
Loading

0 comments on commit 4a25e2c

Please sign in to comment.