Author: eevans
Date: Sun Oct 31 14:36:56 2010
New Revision: 1029360
URL: http://svn.apache.org/viewvc?rev=1029360&view=rev
Log:
(mostly )implemented key ranges in SELECT
Patch by eevans
Modified:
cassandra/trunk/drivers/java/src/org/apache/cassandra/cql/driver/Connection.java
cassandra/trunk/src/java/org/apache/cassandra/avro/CassandraServer.java
cassandra/trunk/src/java/org/apache/cassandra/cql/CQLStatement.java
cassandra/trunk/src/java/org/apache/cassandra/cql/Column.java
cassandra/trunk/src/java/org/apache/cassandra/cql/QueryProcessor.java
cassandra/trunk/src/java/org/apache/cassandra/cql/Relation.java
cassandra/trunk/src/java/org/apache/cassandra/cql/Row.java
cassandra/trunk/src/java/org/apache/cassandra/cql/SelectExpression.java
cassandra/trunk/src/java/org/apache/cassandra/cql/SelectStatement.java
cassandra/trunk/src/java/org/apache/cassandra/cql/StatementType.java
cassandra/trunk/src/java/org/apache/cassandra/cql/Term.java
cassandra/trunk/src/java/org/apache/cassandra/cql/UpdateStatement.java
Modified:
cassandra/trunk/drivers/java/src/org/apache/cassandra/cql/driver/Connection.java
URL:
http://svn.apache.org/viewvc/cassandra/trunk/drivers/java/src/org/apache/cassandra/cql/driver/Connection.java?rev=1029360&r1=1029359&r2=1029360&view=diff
==============================================================================
---
cassandra/trunk/drivers/java/src/org/apache/cassandra/cql/driver/Connection.java
(original)
+++
cassandra/trunk/drivers/java/src/org/apache/cassandra/cql/driver/Connection.java
Sun Oct 31 14:36:56 2010
@@ -69,17 +69,25 @@ public class Connection
private ByteBuffer compressQuery(String queryStr, Compression compression)
{
+ byte[] data = queryStr.getBytes();
Deflater compressor = new Deflater();
- compressor.setInput(queryStr.getBytes());
+ compressor.setInput(data);
compressor.finish();
- byte[] compressedQuery = new byte[100];
- int compressedSize = compressor.deflate(compressedQuery);
+
+ ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
+ byte[] buffer = new byte[1024];
+
+ while (!compressor.finished())
+ {
+ int size = compressor.deflate(buffer);
+ byteArray.write(buffer, 0, size);
+ }
logger.trace("Compressed query statement {} bytes in length to {}
bytes",
- queryStr.length(),
- compressedSize);
+ data.length,
+ byteArray.size());
- return ByteBuffer.wrap(compressedQuery);
+ return ByteBuffer.wrap(byteArray.toByteArray());
}
public CqlResult execute(String queryStr)
@@ -116,7 +124,11 @@ public class Connection
case ROWS:
for (CqlRow row : result.rows)
{
- System.out.println("Date: " + new
String(row.columns.get(0).value.array()));
+ System.out.println("KEY: " + new String(row.key.array()));
+ for (org.apache.cassandra.avro.Column col : row.columns)
+ {
+ System.out.println(" COL: " + new
String(col.name.array()) + ":" + new String(col.value.array()));
+ }
}
}
}
Modified:
cassandra/trunk/src/java/org/apache/cassandra/avro/CassandraServer.java
URL:
http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/avro/CassandraServer.java?rev=1029360&r1=1029359&r2=1029360&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/avro/CassandraServer.java
(original)
+++ cassandra/trunk/src/java/org/apache/cassandra/avro/CassandraServer.java Sun
Oct 31 14:36:56 2010
@@ -20,6 +20,7 @@ package org.apache.cassandra.avro;
*
*/
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
@@ -1182,11 +1183,18 @@ public class CassandraServer implements
Inflater decompressor = new Inflater();
decompressor.setInput(query.array(), 0,
query.array().length);
- byte[] decompressedBytes = new byte[100];
- int length = decompressor.inflate(decompressedBytes);
+ ByteArrayOutputStream byteArray = new
ByteArrayOutputStream();
+ byte[] buffer = new byte[1024];
+
+ while (!decompressor.finished())
+ {
+ int size = decompressor.inflate(buffer);
+ byteArray.write(buffer, 0, size);
+ }
+
decompressor.end();
- queryString = new String(decompressedBytes, 0, length,
"UTF-8");
+ queryString = new String(byteArray.toByteArray(), 0,
byteArray.size(), "UTF-8");
}
}
catch (DataFormatException e)
Modified: cassandra/trunk/src/java/org/apache/cassandra/cql/CQLStatement.java
URL:
http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/cql/CQLStatement.java?rev=1029360&r1=1029359&r2=1029360&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/cql/CQLStatement.java
(original)
+++ cassandra/trunk/src/java/org/apache/cassandra/cql/CQLStatement.java Sun Oct
31 14:36:56 2010
@@ -1,4 +1,25 @@
package org.apache.cassandra.cql;
+/*
+ *
+ * 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.
+ *
+ */
+
public class CQLStatement
{
Modified: cassandra/trunk/src/java/org/apache/cassandra/cql/Column.java
URL:
http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/cql/Column.java?rev=1029360&r1=1029359&r2=1029360&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/cql/Column.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/cql/Column.java Sun Oct 31
14:36:56 2010
@@ -1,4 +1,25 @@
package org.apache.cassandra.cql;
+/*
+ *
+ * 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.
+ *
+ */
+
public class Column
{
Modified: cassandra/trunk/src/java/org/apache/cassandra/cql/QueryProcessor.java
URL:
http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/cql/QueryProcessor.java?rev=1029360&r1=1029359&r2=1029360&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/cql/QueryProcessor.java
(original)
+++ cassandra/trunk/src/java/org/apache/cassandra/cql/QueryProcessor.java Sun
Oct 31 14:36:56 2010
@@ -1,3 +1,23 @@
+/*
+ *
+ * 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.cql;
@@ -25,20 +45,29 @@ import org.apache.cassandra.avro.Unavail
import org.apache.cassandra.db.ColumnFamily;
import org.apache.cassandra.db.DecoratedKey;
import org.apache.cassandra.db.IColumn;
+import org.apache.cassandra.db.RangeSliceCommand;
import org.apache.cassandra.db.ReadCommand;
import org.apache.cassandra.db.RowMutation;
import org.apache.cassandra.db.SliceByNamesReadCommand;
import org.apache.cassandra.db.SliceFromReadCommand;
import org.apache.cassandra.db.filter.QueryPath;
+import org.apache.cassandra.dht.AbstractBounds;
+import org.apache.cassandra.dht.Bounds;
+import org.apache.cassandra.dht.IPartitioner;
import org.apache.cassandra.service.StorageProxy;
import org.apache.cassandra.service.StorageService;
import org.apache.cassandra.thrift.ConsistencyLevel;
+import org.apache.cassandra.thrift.SlicePredicate;
+import org.apache.cassandra.thrift.SliceRange;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import static org.apache.cassandra.avro.AvroValidation.validateKey;
public class QueryProcessor
{
-
+ private static final Logger logger =
LoggerFactory.getLogger(QueryProcessor.class);
+
public static Map<DecoratedKey<?>, ColumnFamily>
readColumnFamily(List<ReadCommand> commands, ConsistencyLevel cLevel)
throws UnavailableException, InvalidRequestException, TimedOutException
{
@@ -79,6 +108,8 @@ public class QueryProcessor
public static CqlResult process(String queryString, String keyspace)
throws RecognitionException, UnavailableException,
InvalidRequestException, TimedOutException
{
+ logger.debug("CQL QUERY: {}", queryString);
+
CqlParser parser = getParser(queryString);
CQLStatement statement = parser.query();
@@ -109,7 +140,7 @@ public class QueryProcessor
{
Collection<byte[]> columnNames = new
ArrayList<byte[]>();
for (Term column :
select.getColumnPredicates().getTerms())
- columnNames.add(column.getBytes()); //
FIXME: surely not good enough
+ columnNames.add(column.getBytes());
commands.add(new SliceByNamesReadCommand(keyspace,
key, queryPath, columnNames));
}
@@ -151,7 +182,70 @@ public class QueryProcessor
}
else // It is a range query (range of keys).
{
+ // FIXME: ranges can be open-ended, but a start must
exist. Assert so here.
+
+ List<org.apache.cassandra.db.Row> rows = null;
+ IPartitioner<?> p = StorageService.getPartitioner();
+ AbstractBounds bounds = new
Bounds(p.getToken(select.getKeyPredicates().getStart().getBytes()),
+
p.getToken(select.getKeyPredicates().getFinish().getBytes()));
+
+ // XXX: Our use of Thrift structs internally makes me Sad.
:(
+ SlicePredicate thriftSlicePredicate = new SlicePredicate();
+ if (select.getColumnPredicates().isRange() ||
select.getColumnPredicates().getTerms().size() == 0)
+ {
+ SliceRange sliceRange = new SliceRange();
+ sliceRange.start =
select.getColumnPredicates().getStart().getBytes();
+ sliceRange.finish =
select.getColumnPredicates().getFinish().getBytes();
+ sliceRange.reversed = false; // FIXME: hard-coded
+ sliceRange.count = select.getNumColumns();
+ thriftSlicePredicate.slice_range = sliceRange;
+ }
+ else
+ {
+ List<byte[]> columnNames = new ArrayList<byte[]>();
+ for (Term column :
select.getColumnPredicates().getTerms())
+ columnNames.add(column.getBytes());
+ thriftSlicePredicate.column_names = columnNames;
+ }
+
+ try
+ {
+ rows = StorageProxy.getRangeSlice(new
RangeSliceCommand(keyspace,
+
select.getColumnFamily(),
+
null,
+
thriftSlicePredicate,
+
bounds,
+
select.getNumRecords()),
+
select.getConsistencyLevel());
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e);
+ }
+ catch (org.apache.cassandra.thrift.UnavailableException e)
+ {
+ throw new UnavailableException();
+ }
+ catch (TimeoutException e)
+ {
+ throw new TimedOutException();
+ }
+ for (org.apache.cassandra.db.Row row : rows)
+ {
+ CqlRow avroRow = new CqlRow();
+ avroRow.key = ByteBuffer.wrap(row.key.key);
+ avroRow.columns = new ArrayList<Column>();
+
+ for (IColumn column : row.cf.getSortedColumns())
+ {
+ Column avroColumn = new Column();
+ avroColumn.name = ByteBuffer.wrap(column.name());
+ avroColumn.value = ByteBuffer.wrap(column.value());
+ avroRow.columns.add(avroColumn);
+ }
+ avroRows.add(avroRow);
+ }
}
avroResult.rows = avroRows;
@@ -202,21 +296,4 @@ public class QueryProcessor
TokenStream tokenStream = new CommonTokenStream(lexer);
return new CqlParser(tokenStream);
}
-
- public static void main(String[] args) throws RecognitionException
- {
- CqlParser parser = getParser("SElecT FRoM Standard1 where KEY >
\"foo\" and key < \"fnord\" and COLUMN=\"bar\";");
- CQLStatement statement = parser.query();
-
- switch (statement.type)
- {
- case SELECT:
- SelectStatement st = (SelectStatement)statement.statement;
- System.out.println(st.getColumnFamily() + " " +
st.getKeyPredicates().getStart().getText() +
- " " + st.getColumnPredicates().getTerms() + " " +
st.getKeyPredicates().isRange());
- case UPDATE:
- return;
- }
- }
-
}
Modified: cassandra/trunk/src/java/org/apache/cassandra/cql/Relation.java
URL:
http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/cql/Relation.java?rev=1029360&r1=1029359&r2=1029360&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/cql/Relation.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/cql/Relation.java Sun Oct 31
14:36:56 2010
@@ -1,4 +1,25 @@
package org.apache.cassandra.cql;
+/*
+ *
+ * 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.
+ *
+ */
+
/**
* Relations encapsulate the relationship between an entity and a value. For
@@ -64,4 +85,4 @@ enum RelationType
return null;
}
-}
\ No newline at end of file
+}
Modified: cassandra/trunk/src/java/org/apache/cassandra/cql/Row.java
URL:
http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/cql/Row.java?rev=1029360&r1=1029359&r2=1029360&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/cql/Row.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/cql/Row.java Sun Oct 31
14:36:56 2010
@@ -1,4 +1,25 @@
package org.apache.cassandra.cql;
+/*
+ *
+ * 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.
+ *
+ */
+
import java.util.ArrayList;
import java.util.List;
Modified:
cassandra/trunk/src/java/org/apache/cassandra/cql/SelectExpression.java
URL:
http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/cql/SelectExpression.java?rev=1029360&r1=1029359&r2=1029360&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/cql/SelectExpression.java
(original)
+++ cassandra/trunk/src/java/org/apache/cassandra/cql/SelectExpression.java Sun
Oct 31 14:36:56 2010
@@ -1,4 +1,25 @@
package org.apache.cassandra.cql;
+/*
+ *
+ * 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.
+ *
+ */
+
import java.util.ArrayList;
import java.util.List;
Modified: cassandra/trunk/src/java/org/apache/cassandra/cql/SelectStatement.java
URL:
http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/cql/SelectStatement.java?rev=1029360&r1=1029359&r2=1029360&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/cql/SelectStatement.java
(original)
+++ cassandra/trunk/src/java/org/apache/cassandra/cql/SelectStatement.java Sun
Oct 31 14:36:56 2010
@@ -1,4 +1,25 @@
package org.apache.cassandra.cql;
+/*
+ *
+ * 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.
+ *
+ */
+
import org.apache.cassandra.thrift.ConsistencyLevel;
Modified: cassandra/trunk/src/java/org/apache/cassandra/cql/StatementType.java
URL:
http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/cql/StatementType.java?rev=1029360&r1=1029359&r2=1029360&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/cql/StatementType.java
(original)
+++ cassandra/trunk/src/java/org/apache/cassandra/cql/StatementType.java Sun
Oct 31 14:36:56 2010
@@ -1,4 +1,25 @@
package org.apache.cassandra.cql;
+/*
+ *
+ * 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.
+ *
+ */
+
public enum StatementType
{
Modified: cassandra/trunk/src/java/org/apache/cassandra/cql/Term.java
URL:
http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/cql/Term.java?rev=1029360&r1=1029359&r2=1029360&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/cql/Term.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/cql/Term.java Sun Oct 31
14:36:56 2010
@@ -1,4 +1,25 @@
package org.apache.cassandra.cql;
+/*
+ *
+ * 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.
+ *
+ */
+
import org.apache.cassandra.utils.FBUtilities;
Modified: cassandra/trunk/src/java/org/apache/cassandra/cql/UpdateStatement.java
URL:
http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/cql/UpdateStatement.java?rev=1029360&r1=1029359&r2=1029360&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/cql/UpdateStatement.java
(original)
+++ cassandra/trunk/src/java/org/apache/cassandra/cql/UpdateStatement.java Sun
Oct 31 14:36:56 2010
@@ -1,4 +1,25 @@
package org.apache.cassandra.cql;
+/*
+ *
+ * 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.
+ *
+ */
+
import java.util.ArrayList;
import java.util.List;