All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.datastax.oss.driver.internal.core.adminrequest.AdminResult Maven / Gradle / Ivy

The newest version!
/*
 * Copyright DataStax, Inc.
 *
 * Licensed 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.datastax.oss.driver.internal.core.adminrequest;

import com.datastax.oss.driver.api.core.ProtocolVersion;
import com.datastax.oss.driver.internal.core.util.concurrent.CompletableFutures;
import com.datastax.oss.driver.shaded.guava.common.collect.AbstractIterator;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap;
import com.datastax.oss.protocol.internal.response.result.ColumnSpec;
import com.datastax.oss.protocol.internal.response.result.Rows;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.CompletionStage;
import net.jcip.annotations.NotThreadSafe;

@NotThreadSafe // wraps a mutable queue
public class AdminResult implements Iterable {

  private final Queue> data;
  private final Map columnSpecs;
  private final AdminRequestHandler nextHandler;
  private final ProtocolVersion protocolVersion;

  public AdminResult(
      Rows rows, AdminRequestHandler nextHandler, ProtocolVersion protocolVersion) {
    this.data = rows.getData();

    ImmutableMap.Builder columnSpecsBuilder = ImmutableMap.builder();
    for (ColumnSpec spec : rows.getMetadata().columnSpecs) {
      columnSpecsBuilder.put(spec.name, spec);
    }
    // Admin queries are simple selects only, so there are no duplicate names (if that ever
    // changes, build() will fail and we'll have to do things differently)
    this.columnSpecs = columnSpecsBuilder.build();

    this.nextHandler = nextHandler;
    this.protocolVersion = protocolVersion;
  }

  /** This consumes the result's data and can be called only once. */
  @NonNull
  @Override
  public Iterator iterator() {
    return new AbstractIterator() {
      @Override
      protected AdminRow computeNext() {
        List rowData = data.poll();
        return (rowData == null)
            ? endOfData()
            : new AdminRow(columnSpecs, rowData, protocolVersion);
      }
    };
  }

  public boolean hasNextPage() {
    return nextHandler != null;
  }

  public CompletionStage nextPage() {
    return (nextHandler == null)
        ? CompletableFutures.failedFuture(
            new AssertionError("No next page, use hasNextPage() before you call this method"))
        : nextHandler.start();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy