Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 86CB8200C1A for ; Mon, 13 Feb 2017 08:54:36 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 837CD160B60; Mon, 13 Feb 2017 07:54:36 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id CD356160B4D for ; Mon, 13 Feb 2017 08:54:35 +0100 (CET) Received: (qmail 76341 invoked by uid 500); 13 Feb 2017 07:54:35 -0000 Mailing-List: contact dev-help@phoenix.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@phoenix.apache.org Delivered-To: mailing list dev@phoenix.apache.org Received: (qmail 76330 invoked by uid 99); 13 Feb 2017 07:54:34 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 13 Feb 2017 07:54:34 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 9C23CDFC31; Mon, 13 Feb 2017 07:54:34 +0000 (UTC) From: ankitsinghal To: dev@phoenix.apache.org Reply-To: dev@phoenix.apache.org References: In-Reply-To: Subject: [GitHub] phoenix pull request #229: PHOENIX-3572 Support FETCH NEXT|n ROWS query on c... Content-Type: text/plain Message-Id: <20170213075434.9C23CDFC31@git1-us-west.apache.org> Date: Mon, 13 Feb 2017 07:54:34 +0000 (UTC) archived-at: Mon, 13 Feb 2017 07:54:36 -0000 Github user ankitsinghal commented on a diff in the pull request: https://github.com/apache/phoenix/pull/229#discussion_r100738871 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/util/CursorUtil.java --- @@ -0,0 +1,203 @@ +/* + * 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 org.apache.phoenix.util; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +import org.apache.hadoop.hbase.client.Scan; +import org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.phoenix.compile.QueryPlan; +import org.apache.phoenix.compile.OrderByCompiler.OrderBy; +import org.apache.phoenix.execute.CursorFetchPlan; +import org.apache.phoenix.iterate.CursorResultIterator; +import org.apache.phoenix.parse.CloseStatement; +import org.apache.phoenix.parse.DeclareCursorStatement; +import org.apache.phoenix.parse.OpenStatement; +import org.apache.phoenix.schema.tuple.Tuple; + +public final class CursorUtil { + + private static class CursorWrapper { + private final String cursorName; + private final String selectSQL; + private boolean isOpen = false; + QueryPlan queryPlan; + ImmutableBytesWritable row; + ImmutableBytesWritable previousRow; + private Scan scan; + private boolean moreValues=true; + private boolean isReversed; + private boolean islastCallNext; + private CursorFetchPlan fetchPlan; + private int offset = -1; + + private CursorWrapper(String cursorName, String selectSQL, QueryPlan queryPlan){ + this.cursorName = cursorName; + this.selectSQL = selectSQL; + this.queryPlan = queryPlan; + this.islastCallNext = true; + this.fetchPlan = new CursorFetchPlan(queryPlan); + } + + private synchronized void openCursor(Connection conn) throws SQLException { + if(isOpen){ + return; + } + this.scan = this.queryPlan.getContext().getScan(); + isReversed=OrderBy.REV_ROW_KEY_ORDER_BY.equals(this.queryPlan.getOrderBy()); + isOpen = true; + } + + private void closeCursor() throws SQLException { + isOpen = false; + ((CursorResultIterator) fetchPlan.iterator()).closeCursor(); + //TODO: Determine if the cursor should be removed from the HashMap at this point. + //Semantically it makes sense that something which is 'Closed' one should be able to 'Open' again. + mapCursorIDQuery.remove(this.cursorName); + } + + private QueryPlan getFetchPlan(boolean isNext, int fetchSize) throws SQLException { + if (!isOpen) + throw new SQLException("Fetch call on closed cursor '" + this.cursorName + "'!"); + ((CursorResultIterator)fetchPlan.iterator()).setFetchSize(fetchSize); + if (!queryPlan.getStatement().isAggregate() || !queryPlan.getStatement().isDistinct()) { + if (islastCallNext != isNext) { + if (islastCallNext && !isReversed){ + ScanUtil.setReversed(scan); + } else { + ScanUtil.unsetReversed(scan); + } --- End diff -- this code seems to be for reverse/prior and belongs to another JIRA. can we remove this if it can affect the functionality? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---