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

org.apache.cassandra.distributed.api.SimpleQueryResult Maven / Gradle / Ivy

The newest version!
/*
 * 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.cassandra.distributed.api;

import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * A table of data representing a complete query result.
 * 

* A QueryResult is different from {@link java.sql.ResultSet} in several key ways: * *

    *
  • represents a complete result rather than a cursor
  • *
  • returns a {@link Row} to access the current row of data
  • *
  • relies on object pooling; {@link #hasNext()} may return the same object just with different data, accessing a * {@link Row} from a previous {@link #hasNext()} call has undefined behavior.
  • *
  • includes {@link #filter(Predicate)}, this will do client side filtering since Apache Cassandra is more * restrictive on server side filtering
  • *
* *

Unsafe patterns

*

* Below are a few unsafe patterns which may lead to unexpected results * * {@code * while (rs.hasNext()) { * list.add(rs.next()); * } * } * * {@code * rs.forEach(list::add) * } *

* Both cases have the same issue; reference to a row from a previous call to {@link #hasNext()}. Since the same {@link Row} * object can be used accross different calls to {@link #hasNext()} this would mean any attempt to access after the fact * points to newer data. If this behavior is not desirable and access is needed between calls, then {@link Row#copy()} * should be used; this will clone the {@link Row} and return a new object pointing to the same data. */ public class SimpleQueryResult implements QueryResult, Iterable { private final String[] names; private final Object[][] results; private final List warnings; private final Predicate filter; private final Row row; private int offset = -1; public SimpleQueryResult(String[] names, Object[][] results) { this(names, results, Collections.emptyList()); } public SimpleQueryResult(String[] names, Object[][] results, List warnings) { this.names = Objects.requireNonNull(names, "names"); this.results = results; this.warnings = Objects.requireNonNull(warnings, "warnings"); this.row = new Row(names); this.filter = ignore -> true; } private SimpleQueryResult(String[] names, Object[][] results, Predicate filter, int offset) { this.names = names; this.results = results; this.warnings = Collections.emptyList(); this.filter = filter; this.offset = offset; this.row = new Row(names); } public List names() { return Collections.unmodifiableList(Arrays.asList(names)); } @Override public List warnings() { return Collections.unmodifiableList(warnings); } public SimpleQueryResult filter(Predicate fn) { return new SimpleQueryResult(names, results, filter.and(fn), offset); } @Override public Iterator iterator() { return new SimpleQueryResult(names, results, filter, offset); } /** * Reset the cursor to the start of the query result; if the query result has not been iterated, this has no effect. */ public void reset() { offset = -1; row.unsafeSetResults(null); } /** * Get all rows as a 2d array. Any calls to {@link #filter(Predicate)} will be ignored and the array returned will * be the full set from the query. */ public Object[][] toObjectArrays() { return results; } @Override public boolean hasNext() { if (results == null) return false; while ((offset += 1) < results.length) { row.unsafeSetResults(results[offset]); if (filter.test(row)) { return true; } } row.unsafeSetResults(null); return false; } @Override public Row next() { // no null check needed for results since offset only increments IFF results is not null if (offset < 0 || offset >= results.length) throw new NoSuchElementException(); return row; } @Override public String toString() { if (results == null) return "[]"; return Stream.of(results) .map(Arrays::toString) .collect(Collectors.joining(",", "[", "]")); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy