├── doc └── resources │ ├── oltp_olap.png │ ├── architecture.png │ ├── spark_architecture.png │ ├── spark_performance.png │ ├── geo_shape_mapper_example_2.png │ ├── geo_shape_mapper_example_3.png │ ├── geo_shape_mapper_example_4.png │ ├── geo_shape_mapper_example_5.png │ ├── geo_shape_mapper_example_6.png │ ├── geo_shape_condition_example_1.png │ └── geo_shape_condition_example_2.png ├── .mvn └── wrapper │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── plugin └── src │ ├── deb │ └── control │ │ └── control │ ├── main │ ├── java │ │ ├── org │ │ │ └── apache │ │ │ │ └── cassandra │ │ │ │ └── service │ │ │ │ └── reads │ │ │ │ └── range │ │ │ │ ├── LuceneReplicaPlanMerger.java │ │ │ │ └── LuceneReplicaPlanIterator.java │ │ └── com │ │ │ └── stratio │ │ │ └── cassandra │ │ │ └── lucene │ │ │ ├── common │ │ │ ├── Builder.java │ │ │ ├── JTSNotFoundException.java │ │ │ ├── GeoOperation.java │ │ │ └── GeospatialUtilsJTS.java │ │ │ ├── schema │ │ │ ├── mapping │ │ │ │ ├── builder │ │ │ │ │ ├── BlobMapperBuilder.java │ │ │ │ │ ├── InetMapperBuilder.java │ │ │ │ │ ├── UUIDMapperBuilder.java │ │ │ │ │ ├── BooleanMapperBuilder.java │ │ │ │ │ ├── SingleColumnMapperBuilder.java │ │ │ │ │ ├── LongMapperBuilder.java │ │ │ │ │ ├── FloatMapperBuilder.java │ │ │ │ │ ├── DoubleMapperBuilder.java │ │ │ │ │ ├── IntegerMapperBuilder.java │ │ │ │ │ ├── BigIntegerMapperBuilder.java │ │ │ │ │ ├── DateMapperBuilder.java │ │ │ │ │ ├── TextMapperBuilder.java │ │ │ │ │ ├── StringMapperBuilder.java │ │ │ │ │ ├── BigDecimalMapperBuilder.java │ │ │ │ │ ├── DateRangeMapperBuilder.java │ │ │ │ │ └── GeoPointMapperBuilder.java │ │ │ │ ├── StringMapper.java │ │ │ │ ├── BooleanMapper.java │ │ │ │ ├── BlobMapper.java │ │ │ │ ├── TextMapper.java │ │ │ │ ├── KeywordMapper.java │ │ │ │ └── DateMapper.java │ │ │ └── analysis │ │ │ │ ├── AnalyzerBuilder.java │ │ │ │ └── ClasspathAnalyzerBuilder.java │ │ │ └── search │ │ │ ├── condition │ │ │ ├── builder │ │ │ │ ├── AllConditionBuilder.java │ │ │ │ ├── NoneConditionBuilder.java │ │ │ │ ├── PrefixConditionBuilder.java │ │ │ │ ├── RegexpConditionBuilder.java │ │ │ │ ├── WildcardConditionBuilder.java │ │ │ │ ├── LuceneConditionBuilder.java │ │ │ │ ├── PhraseConditionBuilder.java │ │ │ │ ├── MatchConditionBuilder.java │ │ │ │ └── ContainsConditionBuilder.java │ │ │ ├── AllCondition.java │ │ │ ├── NoneCondition.java │ │ │ ├── SingleFieldCondition.java │ │ │ ├── PrefixCondition.java │ │ │ └── RegexpCondition.java │ │ │ └── sort │ │ │ ├── builder │ │ │ ├── SimpleSortFieldBuilder.java │ │ │ ├── SortFieldBuilder.java │ │ │ └── GeoDistanceSortFieldBuilder.java │ │ │ └── SortField.java │ └── scala │ │ └── com │ │ └── stratio │ │ └── cassandra │ │ └── lucene │ │ ├── util │ │ ├── Tracing.scala │ │ ├── Logging.scala │ │ ├── SimplePartitionIterator.scala │ │ ├── Tracer.scala │ │ ├── Locker.scala │ │ └── SingleRowIterator.scala │ │ ├── index │ │ ├── NoIDFSimilarity.scala │ │ ├── TokenLengthAnalyzer.scala │ │ └── RAMIndex.scala │ │ ├── partitioning │ │ └── PartitionerOnNone.scala │ │ ├── IndexReaderExcludingDataCenter.scala │ │ ├── IndexReaderSkinny.scala │ │ ├── IndexServiceMBean.scala │ │ └── IndexReader.scala │ └── test │ ├── resources │ └── logback-test.xml │ ├── scala │ └── com │ │ └── stratio │ │ └── cassandra │ │ └── lucene │ │ ├── partitioning │ │ ├── PartitionerOnNoneTest.scala │ │ ├── PartitionerTest.scala │ │ └── PartitionerOnTokenTest.scala │ │ ├── index │ │ └── NoIDFSimilarityTest.scala │ │ ├── mapping │ │ ├── ClusteringMapperTest.scala │ │ └── TokenMapperTest.scala │ │ ├── util │ │ ├── TimeCounterTest.scala │ │ ├── LockerTest.scala │ │ └── BytBufferUtilsTest.scala │ │ └── BaseScalaTest.scala │ └── java │ └── com │ └── stratio │ └── cassandra │ └── lucene │ ├── search │ └── condition │ │ ├── AbstractConditionTest.java │ │ ├── GeoOperationTest.java │ │ ├── AllConditionTest.java │ │ └── NoneConditionTest.java │ └── schema │ ├── mapping │ ├── AbstractMapperTest.java │ └── MapperTest.java │ └── analysis │ └── ClasspathAnalyzerBuilderTest.java ├── .gitignore ├── testsAT ├── README.rst └── src │ └── test │ ├── java │ └── com │ │ └── stratio │ │ └── cassandra │ │ └── lucene │ │ ├── search │ │ ├── AllSearchTest.java │ │ └── NoneSearchTest.java │ │ ├── BaseTest.java │ │ ├── issues │ │ ├── Issue94Test.java │ │ ├── Issue69Test.java │ │ ├── Issue177Test.java │ │ ├── Issue123Test.java │ │ ├── Issue18Test.java │ │ └── Issue64Test.java │ │ ├── util │ │ ├── monitoring │ │ │ ├── CassandraMonitoringClient.java │ │ │ ├── CassandraJolokiaClient.java │ │ │ └── CassandraJMXClient.java │ │ ├── CassandraUtilsUpdate.java │ │ └── CassandraUtilsDelete.java │ │ └── varia │ │ ├── LargeFieldTest.java │ │ ├── InOperatorWithSkinnyRowsTest.java │ │ └── SearchMatchingManyRowsTest.java │ └── resources │ └── logback.xml └── .circleci └── config.yml /doc/resources/oltp_olap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/instaclustr/cassandra-lucene-index/HEAD/doc/resources/oltp_olap.png -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/instaclustr/cassandra-lucene-index/HEAD/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /doc/resources/architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/instaclustr/cassandra-lucene-index/HEAD/doc/resources/architecture.png -------------------------------------------------------------------------------- /doc/resources/spark_architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/instaclustr/cassandra-lucene-index/HEAD/doc/resources/spark_architecture.png -------------------------------------------------------------------------------- /doc/resources/spark_performance.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/instaclustr/cassandra-lucene-index/HEAD/doc/resources/spark_performance.png -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.8.1/apache-maven-3.8.1-bin.zip 2 | -------------------------------------------------------------------------------- /doc/resources/geo_shape_mapper_example_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/instaclustr/cassandra-lucene-index/HEAD/doc/resources/geo_shape_mapper_example_2.png -------------------------------------------------------------------------------- /doc/resources/geo_shape_mapper_example_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/instaclustr/cassandra-lucene-index/HEAD/doc/resources/geo_shape_mapper_example_3.png -------------------------------------------------------------------------------- /doc/resources/geo_shape_mapper_example_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/instaclustr/cassandra-lucene-index/HEAD/doc/resources/geo_shape_mapper_example_4.png -------------------------------------------------------------------------------- /doc/resources/geo_shape_mapper_example_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/instaclustr/cassandra-lucene-index/HEAD/doc/resources/geo_shape_mapper_example_5.png -------------------------------------------------------------------------------- /doc/resources/geo_shape_mapper_example_6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/instaclustr/cassandra-lucene-index/HEAD/doc/resources/geo_shape_mapper_example_6.png -------------------------------------------------------------------------------- /doc/resources/geo_shape_condition_example_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/instaclustr/cassandra-lucene-index/HEAD/doc/resources/geo_shape_condition_example_1.png -------------------------------------------------------------------------------- /doc/resources/geo_shape_condition_example_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/instaclustr/cassandra-lucene-index/HEAD/doc/resources/geo_shape_condition_example_2.png -------------------------------------------------------------------------------- /plugin/src/deb/control/control: -------------------------------------------------------------------------------- 1 | Package: [[name]] 2 | Version: [[version]] 3 | Section: misc 4 | Priority: optional 5 | Architecture: all 6 | Maintainer: [[maintainer]] 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Maven 2 | log/ 3 | target/ 4 | hs_err_pid* 5 | 6 | # IntelliJ 7 | .idea/ 8 | *.iml 9 | *.iws 10 | 11 | # Eclipse 12 | .classpath 13 | .metadata 14 | .project 15 | .settings/ 16 | 17 | # Mac 18 | .DS_Store 19 | 20 | # Jar 21 | *.jar 22 | 23 | testsAT/jts-core.jar 24 | -------------------------------------------------------------------------------- /testsAT/README.rst: -------------------------------------------------------------------------------- 1 | ================================================= 2 | Stratio’s Cassandra Lucene Index acceptance tests 3 | ================================================= 4 | 5 | This project contains several functional tests (>800) for testing cassandra-lucene-index functionality. 6 | Tests can be executed against a patched Apache Cassandra cluster this way: 7 | 8 | .. code-block:: bash 9 | 10 | mvn -f testsAT/pom.xml -U verify -Dit.host=10.200.0.155 -Dit.monitor_service=jolokia -Dit.monitor_services_url=10.200.0.155:8000,10.200.0.157:8000 11 | 12 | where *monitor_service* could be JMX or Jolokia 13 | -------------------------------------------------------------------------------- /plugin/src/main/java/org/apache/cassandra/service/reads/range/LuceneReplicaPlanMerger.java: -------------------------------------------------------------------------------- 1 | package org.apache.cassandra.service.reads.range; 2 | 3 | import java.util.Iterator; 4 | 5 | import org.apache.cassandra.db.ConsistencyLevel; 6 | import org.apache.cassandra.db.Keyspace; 7 | import org.apache.cassandra.locator.ReplicaPlan; 8 | 9 | public class LuceneReplicaPlanMerger extends ReplicaPlanMerger { 10 | public LuceneReplicaPlanMerger(final Iterator iterator, 11 | final Keyspace keyspace, 12 | final ConsistencyLevel consistency) { 13 | super(iterator, keyspace, consistency); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /plugin/src/main/java/org/apache/cassandra/service/reads/range/LuceneReplicaPlanIterator.java: -------------------------------------------------------------------------------- 1 | package org.apache.cassandra.service.reads.range; 2 | 3 | import org.apache.cassandra.db.ConsistencyLevel; 4 | import org.apache.cassandra.db.Keyspace; 5 | import org.apache.cassandra.db.PartitionPosition; 6 | import org.apache.cassandra.dht.AbstractBounds; 7 | 8 | public class LuceneReplicaPlanIterator extends ReplicaPlanIterator { 9 | 10 | public LuceneReplicaPlanIterator(final AbstractBounds keyRange, 11 | final Keyspace keyspace, 12 | final ConsistencyLevel consistency) { 13 | super(keyRange, keyspace, consistency); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /plugin/src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /plugin/src/main/scala/com/stratio/cassandra/lucene/util/Tracing.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.stratio.cassandra.lucene.util 18 | 19 | /** Trait including a [[Tracer]] instance. 20 | * 21 | * @author Andres de la Pena `adelapena@stratio.com` 22 | */ 23 | trait Tracing { 24 | 25 | protected val tracer: Tracer = new Tracer 26 | 27 | } 28 | -------------------------------------------------------------------------------- /plugin/src/main/scala/com/stratio/cassandra/lucene/util/Logging.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.stratio.cassandra.lucene.util 18 | 19 | import com.typesafe.scalalogging.{Logger, StrictLogging} 20 | 21 | /** Trait including a [[Logger]] instance named `logger`. 22 | * 23 | * @author Andres de la Pena `adelapena@stratio.com` 24 | */ 25 | trait Logging extends StrictLogging {} 26 | -------------------------------------------------------------------------------- /testsAT/src/test/java/com/stratio/cassandra/lucene/search/AllSearchTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search; 17 | 18 | import static com.stratio.cassandra.lucene.builder.Builder.all; 19 | 20 | import org.junit.jupiter.api.Test; 21 | 22 | public class AllSearchTest extends AbstractSearchTest { 23 | 24 | @Test 25 | public void testAll() { 26 | filter(all()).check(5); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /testsAT/src/test/java/com/stratio/cassandra/lucene/search/NoneSearchTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search; 17 | 18 | import static com.stratio.cassandra.lucene.builder.Builder.none; 19 | 20 | import org.junit.jupiter.api.Test; 21 | 22 | public class NoneSearchTest extends AbstractSearchTest { 23 | 24 | @Test 25 | public void testNone() { 26 | filter(none()).check(0); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/common/Builder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.common; 17 | 18 | /** 19 | * Class for building complex objects. 20 | * 21 | * @param The type of the object to be built. 22 | * @author Andres de la Pena {@literal } 23 | */ 24 | public interface Builder { 25 | 26 | /** 27 | * Returns the object represented by this builder. 28 | * 29 | * @return the built object 30 | */ 31 | T build(); 32 | } 33 | -------------------------------------------------------------------------------- /testsAT/src/test/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 22 | 23 | [%level] [%logger{35}] %msg %n 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /plugin/src/main/scala/com/stratio/cassandra/lucene/index/NoIDFSimilarity.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.index 17 | 18 | import org.apache.lucene.search.similarities.{ClassicSimilarity, Similarity} 19 | 20 | /** [[Similarity]] that ignores the inverse document frequency, doing the similarity independent of the index 21 | * context. 22 | * 23 | * @author Andres de la Pena `adelapena@stratio.com` 24 | */ 25 | class NoIDFSimilarity extends ClassicSimilarity { 26 | 27 | /** Returns a constant neutral score value of `1.0`. 28 | * 29 | * @param docFreq the number of documents which contain the term 30 | * @param numDocs the total number of documents in the collection 31 | * @return a constant value 32 | */ 33 | override def idf(docFreq: Long, numDocs: Long): Float = 1.0f 34 | 35 | } 36 | -------------------------------------------------------------------------------- /plugin/src/main/scala/com/stratio/cassandra/lucene/util/SimplePartitionIterator.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.util 17 | 18 | import org.apache.cassandra.db.partitions.PartitionIterator 19 | import org.apache.cassandra.db.rows.RowIterator 20 | 21 | /** [[PartitionIterator]] composed by a list of [[SingleRowIterator]]s. 22 | * 23 | * @param rows the rows to be iterated 24 | * @author Andres de la Pena `adelapena@stratio.com` 25 | */ 26 | class SimplePartitionIterator(rows: Seq[SingleRowIterator]) extends PartitionIterator { 27 | 28 | private[this] val iterator = rows.iterator 29 | 30 | /** @inheritdoc */ 31 | def hasNext: Boolean = iterator.hasNext 32 | 33 | /** @inheritdoc */ 34 | def next(): RowIterator = iterator.next 35 | 36 | /** @inheritdoc */ 37 | def close() = iterator.foreach(_.close()) 38 | } 39 | -------------------------------------------------------------------------------- /plugin/src/main/scala/com/stratio/cassandra/lucene/util/Tracer.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.util 17 | 18 | import org.apache.cassandra.tracing.{Tracing => Tracer} 19 | 20 | /** Wrapper for [[Tracer]] avoiding test environment failures. 21 | * 22 | * @author Andres de la Pena `adelapena@stratio.com` 23 | */ 24 | final class Tracer extends Logging { 25 | 26 | /** If Cassandra tracing is enabled. */ 27 | lazy val canTrace: Boolean = try { 28 | Tracer.isTracing 29 | true 30 | } catch { 31 | case e: Error => 32 | logger.warn(s"Unable to trace: ${e.getMessage}", e) 33 | false 34 | } 35 | 36 | /** Traces the specified string message. 37 | * 38 | * @param message the message to be traced 39 | */ 40 | def trace(message: => String) = if (canTrace && Tracer.isTracing) Tracer.trace(message) 41 | } 42 | -------------------------------------------------------------------------------- /plugin/src/main/scala/com/stratio/cassandra/lucene/util/Locker.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.util 17 | 18 | /** Class to execute code in mutual exclusion based on the hashcode of an object. 19 | * 20 | * @param numLocks the number of underlying concurrent locks 21 | * @author Andres de la Pena `adelapena@stratio.com` 22 | */ 23 | class Locker(numLocks: Int) { 24 | 25 | if (numLocks <= 0) throw new IllegalArgumentException( 26 | s"The number of concurrent locks should be strictly positive but found $numLocks") 27 | 28 | private val locks = (1 to numLocks).map(_ => new Object()).toArray 29 | 30 | /** Runs the specified task in mutual exclusion based on the hashcode of the specified id */ 31 | def run[A](id: AnyRef, task: () => A): Unit = { 32 | locks(Math.abs(id.hashCode % numLocks)).synchronized {task.apply()} 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /plugin/src/test/scala/com/stratio/cassandra/lucene/partitioning/PartitionerOnNoneTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.partitioning 17 | 18 | import org.junit.runner.RunWith 19 | import org.scalatestplus.junit.JUnitRunner 20 | 21 | /** Tests for [[PartitionerOnNone]]. 22 | * 23 | * @author Andres de la Pena `adelapena@stratio.com` 24 | */ 25 | @RunWith(classOf[JUnitRunner]) 26 | class PartitionerOnNoneTest extends PartitionerTest { 27 | 28 | test("parse JSON") { 29 | val json = "{type:\"none\"}" 30 | Partitioner.fromJson(json) shouldBe PartitionerOnNone.Builder() 31 | } 32 | 33 | test("num partitions") { 34 | PartitionerOnNone().numPartitions shouldBe 1 35 | } 36 | 37 | test("key partition") { 38 | val partitioner = PartitionerOnNone() 39 | for (i <- 1 to 20) partitioner.partition(key(i)) shouldBe 0 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/builder/BlobMapperBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping.builder; 17 | 18 | import com.stratio.cassandra.lucene.schema.mapping.BlobMapper; 19 | 20 | /** 21 | * {@link SingleColumnMapperBuilder} to build a new {@link BlobMapper}. 22 | * 23 | * @author Andres de la Pena {@literal } 24 | */ 25 | public class BlobMapperBuilder extends SingleColumnMapperBuilder { 26 | 27 | /** 28 | * Returns the {@link BlobMapper} represented by this {@link MapperBuilder}. 29 | * 30 | * @param field the name of the field to be built 31 | * @return the {@link BlobMapper} represented by this 32 | */ 33 | @Override 34 | public BlobMapper build(String field) { 35 | return new BlobMapper(field, column, validated); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/builder/InetMapperBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping.builder; 17 | 18 | import com.stratio.cassandra.lucene.schema.mapping.InetMapper; 19 | 20 | /** 21 | * {@link SingleColumnMapperBuilder} to build a new {@link InetMapper}. 22 | * 23 | * @author Andres de la Pena {@literal } 24 | */ 25 | public class InetMapperBuilder extends SingleColumnMapperBuilder { 26 | 27 | /** 28 | * Returns the {@link InetMapper} represented by this {@link MapperBuilder}. 29 | * 30 | * @param field the name of the field to be built 31 | * @return the {@link InetMapper} represented by this 32 | */ 33 | @Override 34 | public InetMapper build(String field) { 35 | return new InetMapper(field, column, validated); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/builder/UUIDMapperBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping.builder; 17 | 18 | import com.stratio.cassandra.lucene.schema.mapping.UUIDMapper; 19 | 20 | /** 21 | * {@link SingleColumnMapperBuilder} to build a new {@link UUIDMapper}. 22 | * 23 | * @author Andres de la Pena {@literal } 24 | */ 25 | public class UUIDMapperBuilder extends SingleColumnMapperBuilder { 26 | 27 | /** 28 | * Returns the {@link UUIDMapper} represented by this {@link MapperBuilder}. 29 | * 30 | * @param field the name of the field to be built 31 | * @return the {@link UUIDMapper} represented by this 32 | */ 33 | @Override 34 | public UUIDMapper build(String field) { 35 | return new UUIDMapper(field, column, validated); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /plugin/src/test/scala/com/stratio/cassandra/lucene/index/NoIDFSimilarityTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.index 17 | 18 | import com.stratio.cassandra.lucene.BaseScalaTest 19 | import org.junit.runner.RunWith 20 | import org.scalatestplus.junit.JUnitRunner 21 | 22 | /** Tests for [[NoIDFSimilarity]]. 23 | * 24 | * @author Andres de la Pena `adelapena@stratio.com` 25 | */ 26 | @RunWith(classOf[JUnitRunner]) 27 | class NoIDFSimilarityTest extends BaseScalaTest { 28 | 29 | test("neutral IDF score") { 30 | val similarity = new NoIDFSimilarity 31 | similarity.idf(0l, 0l) shouldBe 1.0f 32 | similarity.idf(1l, 5l) shouldBe 1.0f 33 | similarity.idf(10000l, 10943l) shouldBe 1.0f 34 | similarity.idf(-45667l, 2132189l) shouldBe 1.0f 35 | similarity.idf(367423794l, -394612l) shouldBe 1.0f 36 | similarity.idf(-2147294213l, 15264214l) shouldBe 1.0f 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /plugin/src/test/scala/com/stratio/cassandra/lucene/mapping/ClusteringMapperTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.mapping 17 | 18 | import com.stratio.cassandra.lucene.BaseScalaTest 19 | import org.apache.cassandra.dht.Murmur3Partitioner 20 | import org.apache.lucene.util.BytesRef 21 | import org.junit.runner.RunWith 22 | import org.scalatestplus.junit.JUnitRunner 23 | 24 | /** Tests for [[ClusteringMapper]]. 25 | * 26 | * @author Andres de la Pena `adelapena@stratio.com` 27 | */ 28 | @RunWith(classOf[JUnitRunner]) 29 | class ClusteringMapperTest extends BaseScalaTest { 30 | 31 | test("collate prefix") { 32 | val values = List(Long.MinValue, -10L, -2L, -1L, 0L, 1L, 2L, 10L, Long.MaxValue) 33 | val tokens = values.map(new Murmur3Partitioner.LongToken(_)) 34 | val bytes = tokens.map(ClusteringMapper.prefix(_)).map(new BytesRef(_)) 35 | bytes shouldBe bytes.reverse.sorted 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/builder/BooleanMapperBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping.builder; 17 | 18 | import com.stratio.cassandra.lucene.schema.mapping.BooleanMapper; 19 | 20 | /** 21 | * {@link SingleColumnMapperBuilder} to build a new {@link BooleanMapper}. 22 | * 23 | * @author Andres de la Pena {@literal } 24 | */ 25 | public class BooleanMapperBuilder extends SingleColumnMapperBuilder { 26 | 27 | /** 28 | * Returns the {@link BooleanMapper} represented by this {@link MapperBuilder}. 29 | * 30 | * @param field the name of the field to be built 31 | * @return the {@link BooleanMapper} represented by this 32 | */ 33 | @Override 34 | public BooleanMapper build(String field) { 35 | return new BooleanMapper(field, column, validated); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/analysis/AnalyzerBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.analysis; 17 | 18 | import com.fasterxml.jackson.annotation.JsonSubTypes; 19 | import com.fasterxml.jackson.annotation.JsonTypeInfo; 20 | import org.apache.lucene.analysis.Analyzer; 21 | 22 | /** 23 | * An Lucene {@link Analyzer} builder. 24 | * 25 | * @author Andres de la Pena {@literal } 26 | */ 27 | @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type") 28 | @JsonSubTypes({@JsonSubTypes.Type(value = ClasspathAnalyzerBuilder.class, name = "classpath"), 29 | @JsonSubTypes.Type(value = SnowballAnalyzerBuilder.class, name = "snowball")}) 30 | public abstract class AnalyzerBuilder { 31 | 32 | /** 33 | * Gets or creates the Lucene {@link Analyzer}. 34 | * 35 | * @return the built analyzer 36 | */ 37 | public abstract Analyzer analyzer(); 38 | } 39 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/common/JTSNotFoundException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.common; 17 | 18 | import com.stratio.cassandra.lucene.IndexException; 19 | 20 | /** 21 | * {@link IndexException} to be thrown if Java Topology Suite (JTS) 22 | * library is not found in classpath 23 | * 24 | * @author Eduardo Alonso {@literal } 25 | */ 26 | public class JTSNotFoundException extends IndexException { 27 | 28 | private static final String MESSAGE = "JTS JAR is not provided due to license compatibility issues, please " + 29 | "include jts-core-1.14.0.jar in Cassandra lib directory in order to use " + 30 | "GeoShapeMapper or GeoShapeCondition"; 31 | 32 | /** 33 | * Default constructor. 34 | */ 35 | public JTSNotFoundException() { 36 | super(MESSAGE); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/search/condition/builder/AllConditionBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.condition.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonCreator; 19 | import com.stratio.cassandra.lucene.search.condition.AllCondition; 20 | 21 | /** 22 | * {@link ConditionBuilder} for building a new {@link AllCondition}. 23 | * 24 | * @author Andres de la Pena {@literal } 25 | */ 26 | public class AllConditionBuilder extends ConditionBuilder { 27 | 28 | /** 29 | * Creates a new {@link AllConditionBuilder}. 30 | */ 31 | @JsonCreator 32 | public AllConditionBuilder() { 33 | } 34 | 35 | /** 36 | * Returns the {@link AllCondition} represented by this builder. 37 | * 38 | * @return a new all condition 39 | */ 40 | @Override 41 | public AllCondition build() { 42 | return new AllCondition(boost); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/search/condition/builder/NoneConditionBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.condition.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonCreator; 19 | import com.stratio.cassandra.lucene.search.condition.NoneCondition; 20 | 21 | /** 22 | * {@link ConditionBuilder} for building a new {@link NoneCondition}. 23 | * 24 | * @author Andres de la Pena {@literal } 25 | */ 26 | public class NoneConditionBuilder extends ConditionBuilder { 27 | 28 | /** 29 | * Creates a new {@link NoneConditionBuilder}. 30 | */ 31 | @JsonCreator 32 | public NoneConditionBuilder() { 33 | } 34 | 35 | /** 36 | * Returns the {@link NoneCondition} represented by this builder. 37 | * 38 | * @return a new none condition 39 | */ 40 | @Override 41 | public NoneCondition build() { 42 | return new NoneCondition(boost); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /plugin/src/test/scala/com/stratio/cassandra/lucene/partitioning/PartitionerTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.partitioning 17 | 18 | import com.stratio.cassandra.lucene.BaseScalaTest 19 | import com.stratio.cassandra.lucene.BaseScalaTest.int32 20 | import org.apache.cassandra.db.DecoratedKey 21 | import org.apache.cassandra.dht.Murmur3Partitioner 22 | 23 | /** Tests for [[Partitioner]]. 24 | * 25 | * @author Andres de la Pena `adelapena@stratio.com` 26 | */ 27 | class PartitionerTest extends BaseScalaTest { 28 | 29 | test("parse default") { 30 | Partitioner.fromJson(null, "{}") shouldBe PartitionerOnNone() 31 | } 32 | 33 | test("num partitions with none partitioner") { 34 | PartitionerOnNone().allPartitions shouldBe List(0) 35 | } 36 | 37 | test("num partitions with token partitioner") { 38 | PartitionerOnToken(4).allPartitions shouldBe List(0, 1, 2, 3) 39 | } 40 | 41 | def key(n: Int): DecoratedKey = Murmur3Partitioner.instance.decorateKey(int32.decompose(n)) 42 | 43 | } 44 | -------------------------------------------------------------------------------- /plugin/src/main/scala/com/stratio/cassandra/lucene/partitioning/PartitionerOnNone.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.partitioning 17 | 18 | import org.apache.cassandra.db.{DecoratedKey, ReadCommand} 19 | import org.apache.cassandra.schema.TableMetadata 20 | 21 | /** [[Partitioner]] with no action, equivalent to just don't partitioning the index. 22 | * 23 | * @author Andres de la Pena `adelapena@stratio.com` 24 | */ 25 | case class PartitionerOnNone() extends Partitioner { 26 | 27 | /** @inheritdoc */ 28 | override def numPartitions: Int = 1 29 | 30 | /** @inheritdoc */ 31 | override def partition(key: DecoratedKey): Int = 0 32 | 33 | /** @inheritdoc */ 34 | override def partitions(command: ReadCommand): List[Int] = allPartitions 35 | 36 | } 37 | 38 | /** Companion object for [[PartitionerOnNone]]. */ 39 | object PartitionerOnNone { 40 | 41 | /** [[PartitionerOnNone]] builder. */ 42 | case class Builder() extends Partitioner.Builder { 43 | override def build(metadata: TableMetadata): PartitionerOnNone = PartitionerOnNone() 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /testsAT/src/test/java/com/stratio/cassandra/lucene/BaseTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene; 17 | 18 | import static org.junit.jupiter.api.Assertions.assertEquals; 19 | 20 | import java.util.concurrent.Callable; 21 | 22 | import com.github.nosan.embedded.cassandra.Cassandra; 23 | import org.slf4j.Logger; 24 | import org.slf4j.LoggerFactory; 25 | 26 | /** 27 | * @author Andres de la Pena {@literal } 28 | */ 29 | public abstract class BaseTest extends PluginTestFramework { 30 | 31 | public static final Logger logger = LoggerFactory.getLogger("TEST"); 32 | public static Cassandra cassandra; 33 | 34 | private void assertPure(String msg, int count, T expected, Callable callable) throws Exception { 35 | if (count > 0) { 36 | T actual = callable.call(); 37 | assertEquals(expected, actual); 38 | assertPure(msg, count - 1, actual, callable); 39 | } 40 | } 41 | 42 | protected void assertPure(String msg, Callable callable) throws Exception { 43 | assertPure(msg, 10, callable.call(), callable); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/search/sort/builder/SimpleSortFieldBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.sort.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonCreator; 19 | import com.fasterxml.jackson.annotation.JsonProperty; 20 | import com.stratio.cassandra.lucene.search.sort.SimpleSortField; 21 | 22 | /** 23 | * @author Eduardo Alonso {@literal } 24 | */ 25 | public class SimpleSortFieldBuilder extends SortFieldBuilder { 26 | 27 | /** The name of the field to be used for sort. */ 28 | @JsonProperty("field") 29 | final String field; 30 | 31 | /** 32 | * Creates a new {@link SimpleSortFieldBuilder} for the specified field. 33 | * 34 | * @param field The field to sort by. 35 | */ 36 | @JsonCreator 37 | public SimpleSortFieldBuilder(@JsonProperty("field") String field) { 38 | this.field = field; 39 | } 40 | 41 | /** {@inheritDoc} */ 42 | @Override 43 | public SimpleSortField build() { 44 | return new SimpleSortField(field, reverse); 45 | } 46 | } -------------------------------------------------------------------------------- /plugin/src/test/scala/com/stratio/cassandra/lucene/util/TimeCounterTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.util 17 | 18 | import com.stratio.cassandra.lucene.BaseScalaTest 19 | 20 | /** Class for testing [[TimeCounter]]. 21 | * 22 | * @author Andres de la Pena `adelapena@stratio.com` 23 | */ 24 | class TimeCounterTest extends BaseScalaTest { 25 | 26 | test("reusable") { 27 | val started = TimeCounter.create.start 28 | Thread.sleep(10) 29 | assert(Range(10, 1000).contains(started.time)) 30 | val stopped = started.stop 31 | assert(Range(10, 1000).contains(stopped.time)) 32 | stopped.toString shouldBe s"${stopped.time} ms" 33 | 34 | Thread.sleep(1000) 35 | 36 | val newStarted = stopped.start 37 | Thread.sleep(10) 38 | assert(Range(20, 1000).contains(newStarted.time)) 39 | val newStopped = newStarted.stop 40 | assert(Range(20, 1000).contains(newStopped.time)) 41 | newStopped.toString shouldBe s"${newStopped.time} ms" 42 | } 43 | 44 | test("immutable") { 45 | val tc = TimeCounter.create 46 | tc.start.stop.start 47 | Thread.sleep(10) 48 | tc.time shouldBe 0 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /plugin/src/test/java/com/stratio/cassandra/lucene/search/condition/AbstractConditionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.condition; 17 | 18 | import com.stratio.cassandra.lucene.search.condition.builder.ConditionBuilder; 19 | import com.stratio.cassandra.lucene.common.JsonSerializer; 20 | 21 | import java.io.IOException; 22 | 23 | import static org.junit.Assert.assertEquals; 24 | 25 | /** 26 | * Abstract class for {@link ConditionBuilder} tests. 27 | * 28 | * @author Andres de la Pena {@literal } 29 | */ 30 | public abstract class AbstractConditionTest { 31 | 32 | protected void testJsonSerialization(ConditionBuilder conditionBuilder, String json) { 33 | try { 34 | String json1 = JsonSerializer.toString(conditionBuilder); 35 | assertEquals("JSON serialization is wrong", json, json1); 36 | String json2 = JsonSerializer.toString(JsonSerializer.fromString(json1, ConditionBuilder.class)); 37 | assertEquals("JSON serialization is wrong", json1, json2); 38 | } catch (IOException e) { 39 | throw new RuntimeException(e); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /plugin/src/test/java/com/stratio/cassandra/lucene/schema/mapping/AbstractMapperTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping; 17 | 18 | import com.stratio.cassandra.lucene.schema.mapping.builder.MapperBuilder; 19 | import com.stratio.cassandra.lucene.common.JsonSerializer; 20 | import org.slf4j.Logger; 21 | import org.slf4j.LoggerFactory; 22 | 23 | import java.io.IOException; 24 | 25 | import static org.junit.Assert.assertEquals; 26 | 27 | /** 28 | * @author Andres de la Pena {@literal } 29 | */ 30 | public class AbstractMapperTest { 31 | 32 | public static final Logger logger = LoggerFactory.getLogger("TEST"); 33 | 34 | protected void testJson(MapperBuilder mapperBuilder, String json) { 35 | try { 36 | String json1 = JsonSerializer.toString(mapperBuilder); 37 | assertEquals("JSON serialization is wrong", json, json1); 38 | String json2 = JsonSerializer.toString(JsonSerializer.fromString(json1, MapperBuilder.class)); 39 | assertEquals("JSON serialization is wrong", json1, json2); 40 | } catch (IOException e) { 41 | logger.error("Error in JSON serialization", e); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /plugin/src/test/scala/com/stratio/cassandra/lucene/mapping/TokenMapperTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.mapping 17 | 18 | import com.stratio.cassandra.lucene.BaseScalaTest 19 | import com.stratio.cassandra.lucene.util.ByteBufferUtils.toHex 20 | import org.apache.cassandra.dht.Murmur3Partitioner 21 | import org.junit.runner.RunWith 22 | import org.scalatestplus.junit.JUnitRunner 23 | 24 | /** Tests for [[TokenMapper]]. 25 | * 26 | * @author Andres de la Pena `adelapena@stratio.com` 27 | */ 28 | @RunWith(classOf[JUnitRunner]) 29 | class TokenMapperTest extends BaseScalaTest { 30 | 31 | test("long value") { 32 | val v1 = List(Long.MinValue, -12345L, -123L, -2L, -1L, 0L, 1L, 2L, 123L, 12345L, Long.MaxValue) 33 | val v2 = v1.map(new Murmur3Partitioner.LongToken(_)).map(TokenMapper.longValue) 34 | v1 shouldBe v2 35 | } 36 | 37 | test("bytes ref") { 38 | def hex(n: Long) = toHex(TokenMapper.bytesRef(new Murmur3Partitioner.LongToken(n))) 39 | hex(Long.MinValue) shouldBe "2000000000000000000000" 40 | hex(Long.MaxValue) shouldBe "20017f7f7f7f7f7f7f7f7f" 41 | hex(-1) shouldBe "20007f7f7f7f7f7f7f7f7f" 42 | hex(0) shouldBe "2001000000000000000000" 43 | hex(1) shouldBe "2001000000000000000001" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/builder/SingleColumnMapperBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonProperty; 19 | import com.stratio.cassandra.lucene.schema.mapping.SingleColumnMapper; 20 | 21 | /** 22 | * Abstract {@link MapperBuilder} for creating new {@link SingleColumnMapper}s. 23 | * 24 | * @param The {@link SingleColumnMapper} to be built. 25 | * @param The specific {@link SingleColumnMapper}. 26 | * @author Andres de la Pena {@literal } 27 | */ 28 | public abstract class SingleColumnMapperBuilder, K extends SingleColumnMapperBuilder> 29 | extends MapperBuilder { 30 | 31 | /** The name of the column to be mapped. */ 32 | @JsonProperty("column") 33 | protected String column; 34 | 35 | /** 36 | * Sets the name of the Cassandra column to be mapped. 37 | * 38 | * @param column the name of the Cassandra column to be mapped 39 | * @return this 40 | */ 41 | @SuppressWarnings("unchecked") 42 | public final K column(String column) { 43 | this.column = column; 44 | return (K) this; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | jobs: 3 | build: 4 | 5 | working_directory: ~/cassandra-lucene-index 6 | 7 | docker: 8 | - image: cimg/openjdk:8.0 9 | 10 | steps: 11 | 12 | - checkout 13 | 14 | - restore_cache: 15 | keys: 16 | - m2-{{ checksum "pom.xml" }} 17 | - m2- 18 | 19 | - run: mvn clean install -DoutputDirectory=/tmp/artifacts -Pdeb,rpm -Dcassandra.version=4.1.10 20 | 21 | - save_cache: 22 | paths: 23 | - ~/.m2 24 | key: m2-{{ checksum "pom.xml" }} 25 | 26 | - store_test_results: 27 | path: ~/cassandra-lucene-index/testsAT/target/surefire-reports 28 | 29 | - store_artifacts: 30 | path: /tmp/artifacts 31 | 32 | - persist_to_workspace: 33 | root: /tmp/artifacts 34 | paths: 35 | - "cassandra-lucene-index-plugin-*.jar" 36 | - "cassandra-lucene-index-plugin_*.deb" 37 | - "cassandra-lucene-index-plugin-*.rpm" 38 | 39 | publish-github-release: 40 | docker: 41 | - image: cimg/go:1.22.11 42 | steps: 43 | - attach_workspace: 44 | at: ./artifacts 45 | - run: 46 | name: "Publish Release on GitHub" 47 | command: | 48 | set -xue 49 | go install github.com/tcnksm/ghr@latest 50 | ghr -t ${GITHUB_TOKEN} -u ${CIRCLE_PROJECT_USERNAME} -r ${CIRCLE_PROJECT_REPONAME} -c ${CIRCLE_SHA1} -delete ${CIRCLE_TAG} ./artifacts/ 51 | 52 | workflows: 53 | version: 2 54 | build: 55 | jobs: 56 | - build: 57 | filters: 58 | tags: 59 | only: /^cassandra-4\.1.\d+-\d+\.\d+\.\d+$/ 60 | - publish-github-release: 61 | requires: 62 | - build 63 | filters: 64 | branches: 65 | ignore: /.*/ 66 | tags: 67 | only: /^cassandra-4\.1.\d+-\d+\.\d+\.\d+$/ 68 | -------------------------------------------------------------------------------- /plugin/src/main/scala/com/stratio/cassandra/lucene/IndexReaderExcludingDataCenter.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene 17 | 18 | import com.stratio.cassandra.lucene.index.DocumentIterator 19 | import com.stratio.cassandra.lucene.util.Logging 20 | import org.apache.cassandra.db.{ColumnFamilyStore, ReadCommand} 21 | import org.apache.cassandra.db.partitions.UnfilteredPartitionIterator 22 | import org.apache.cassandra.db.rows.UnfilteredRowIterator 23 | import org.apache.cassandra.schema.TableMetadata 24 | 25 | /** [[UnfilteredPartitionIterator]] for retrieving rows from a [[DocumentIterator]]. 26 | * 27 | * @author Andres de la Pena `adelapena@stratio.com` 28 | */ 29 | class IndexReaderExcludingDataCenter(command: ReadCommand, 30 | table: ColumnFamilyStore) extends UnfilteredPartitionIterator with Logging { 31 | override def metadata(): TableMetadata = table.metadata.get() 32 | 33 | override def close(): Unit = {} 34 | 35 | override def next(): UnfilteredRowIterator = { 36 | logger.warn("You are executing a query against a excluded datacenter node") 37 | null 38 | } 39 | 40 | override def hasNext: Boolean = { 41 | logger.warn("You are executing a query against a excluded datacenter node") 42 | false 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/builder/LongMapperBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonProperty; 19 | import com.stratio.cassandra.lucene.schema.mapping.LongMapper; 20 | 21 | /** 22 | * {@link SingleColumnMapperBuilder} to build a new {@link LongMapper}. 23 | * 24 | * @author Andres de la Pena {@literal } 25 | */ 26 | public class LongMapperBuilder extends SingleColumnMapperBuilder { 27 | 28 | @JsonProperty("boost") 29 | private Float boost; 30 | 31 | /** 32 | * Sets the boost to be used. 33 | * 34 | * @param boost the boost to be used 35 | * @return this 36 | */ 37 | public LongMapperBuilder boost(Float boost) { 38 | this.boost = boost; 39 | return this; 40 | } 41 | 42 | /** 43 | * Returns the {@link LongMapper} represented by this {@link MapperBuilder}. 44 | * 45 | * @param field the name of the field to be built 46 | * @return the {@link LongMapper} represented by this 47 | */ 48 | @Override 49 | public LongMapper build(String field) { 50 | return new LongMapper(field, column, validated, boost); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/builder/FloatMapperBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonProperty; 19 | import com.stratio.cassandra.lucene.schema.mapping.FloatMapper; 20 | 21 | /** 22 | * {@link SingleColumnMapperBuilder} to build a new {@link FloatMapper}. 23 | * 24 | * @author Andres de la Pena {@literal } 25 | */ 26 | public class FloatMapperBuilder extends SingleColumnMapperBuilder { 27 | 28 | @JsonProperty("boost") 29 | private Float boost; 30 | 31 | /** 32 | * Sets the boost to be used. 33 | * 34 | * @param boost the boost to be used 35 | * @return this 36 | */ 37 | public FloatMapperBuilder boost(Float boost) { 38 | this.boost = boost; 39 | return this; 40 | } 41 | 42 | /** 43 | * Returns the {@link FloatMapper} represented by this {@link MapperBuilder}. 44 | * 45 | * @param field the name of the field to be built 46 | * @return the {@link FloatMapper} represented by this 47 | */ 48 | @Override 49 | public FloatMapper build(String field) { 50 | return new FloatMapper(field, column, validated, boost); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /plugin/src/main/scala/com/stratio/cassandra/lucene/IndexReaderSkinny.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene 17 | 18 | import com.stratio.cassandra.lucene.index.DocumentIterator 19 | import org.apache.cassandra.db._ 20 | 21 | /** [[IndexReader]] for skinny rows. 22 | * 23 | * @param service the index service 24 | * @param command the read command 25 | * @param table the base table 26 | * @param controller the read execution controller 27 | * @param documents the documents iterator 28 | * @author Andres de la Pena `adelapena@stratio.com` 29 | */ 30 | class IndexReaderSkinny( 31 | service: IndexServiceSkinny, 32 | command: ReadCommand, 33 | table: ColumnFamilyStore, 34 | controller: ReadExecutionController, 35 | documents: DocumentIterator) 36 | extends IndexReader(command, table, controller, documents) { 37 | 38 | /** @inheritdoc */ 39 | override protected def prepareNext(): Boolean = { 40 | while (nextData.isEmpty && documents.hasNext) { 41 | val nextDoc = documents.next 42 | val key = service.decoratedKey(nextDoc._1) 43 | val filter = command.clusteringIndexFilter(key) 44 | nextData = Some(read(key, filter)) 45 | nextData.foreach(d => if (d.isEmpty) d.close()) 46 | } 47 | nextData.isDefined 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/builder/DoubleMapperBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonProperty; 19 | import com.stratio.cassandra.lucene.schema.mapping.DoubleMapper; 20 | 21 | /** 22 | * {@link SingleColumnMapperBuilder} to build a new {@link DoubleMapper}. 23 | * 24 | * @author Andres de la Pena {@literal } 25 | */ 26 | public class DoubleMapperBuilder extends SingleColumnMapperBuilder { 27 | 28 | @JsonProperty("boost") 29 | private Float boost; 30 | 31 | /** 32 | * Sets the boost to be used. 33 | * 34 | * @param boost the boost to be used 35 | * @return this 36 | */ 37 | public DoubleMapperBuilder boost(Float boost) { 38 | this.boost = boost; 39 | return this; 40 | } 41 | 42 | /** 43 | * Returns the {@link DoubleMapper} represented by this {@link MapperBuilder}. 44 | * 45 | * @param field the name of the field to be built 46 | * @return the {@link DoubleMapper} represented by this 47 | */ 48 | @Override 49 | public DoubleMapper build(String field) { 50 | return new DoubleMapper(field, column, validated, boost); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/builder/IntegerMapperBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonProperty; 19 | import com.stratio.cassandra.lucene.schema.mapping.IntegerMapper; 20 | 21 | /** 22 | * {@link SingleColumnMapperBuilder} to build a new {@link IntegerMapper}. 23 | * 24 | * @author Andres de la Pena {@literal } 25 | */ 26 | public class IntegerMapperBuilder extends SingleColumnMapperBuilder { 27 | 28 | @JsonProperty("boost") 29 | private Float boost; 30 | 31 | /** 32 | * Sets the boost to be used. 33 | * 34 | * @param boost the boost to be used 35 | * @return this 36 | */ 37 | public IntegerMapperBuilder boost(Float boost) { 38 | this.boost = boost; 39 | return this; 40 | } 41 | 42 | /** 43 | * Returns the {@link IntegerMapper} represented by this {@link MapperBuilder}. 44 | * 45 | * @param field the name of the field to be built 46 | * @return the {@link IntegerMapper} represented by this 47 | */ 48 | @Override 49 | public IntegerMapper build(String field) { 50 | return new IntegerMapper(field, column, validated, boost); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /plugin/src/test/scala/com/stratio/cassandra/lucene/util/LockerTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.util 17 | 18 | import java.util.concurrent.{Executors, TimeUnit} 19 | 20 | import com.stratio.cassandra.lucene.BaseScalaTest 21 | 22 | /** Class for testing [[Locker]]. 23 | * 24 | * @author Andres de la Pena `adelapena@stratio.com` 25 | */ 26 | class LockerTest extends BaseScalaTest { 27 | 28 | test("test locker synchronization") { 29 | 30 | val executor = Executors.newFixedThreadPool(8) 31 | val locker = new Locker(4) 32 | 33 | val numIncrements = 10000 34 | val numCounters = 8 35 | val counters = (1 to numCounters).map(_ => 0).toArray 36 | 37 | (0 until numCounters).foreach { counter => 38 | (0 until numIncrements).foreach { _ => 39 | executor.submit[Unit](() => 40 | locker.run(counter.asInstanceOf[AnyRef], () => {counters(counter) += 1})) 41 | } 42 | } 43 | 44 | (0 until numIncrements).foreach { _ => 45 | (0 until numCounters).foreach { counter => 46 | executor.submit[Unit](() => 47 | locker.run(counter.asInstanceOf[AnyRef], () => {counters(counter) -= 1})) 48 | } 49 | } 50 | 51 | executor.shutdown() 52 | executor.awaitTermination(1, TimeUnit.DAYS) 53 | 54 | counters shouldBe (1 to numCounters).map(_ => 0).toArray 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /testsAT/src/test/java/com/stratio/cassandra/lucene/issues/Issue94Test.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.issues; 17 | 18 | import static com.stratio.cassandra.lucene.builder.Builder.integerMapper; 19 | import static com.stratio.cassandra.lucene.builder.Builder.stringMapper; 20 | 21 | import com.stratio.cassandra.lucene.BaseTest; 22 | import com.stratio.cassandra.lucene.util.CassandraUtils; 23 | import org.junit.jupiter.api.Test; 24 | 25 | /** 26 | * @author Andres de la Pena {@literal } 27 | */ 28 | 29 | public class Issue94Test extends BaseTest { 30 | 31 | @Test 32 | public void testInsertExplicitlyNullColumns() { 33 | CassandraUtils utils = CassandraUtils.builder("issue_94").withTable("test") 34 | .withIndexName("test") 35 | .withPartitionKey("a") 36 | .withColumn("a", "int", integerMapper()) 37 | .withColumn("b", "text", stringMapper()) 38 | .withColumn("c", "text", stringMapper()) 39 | .build() 40 | .createKeyspace() 41 | .createTable() 42 | .createIndex(); 43 | utils.insert(new String[]{"a", "b", "c"}, new Object[]{1, null, null}) 44 | .execute("INSERT INTO %s(a , b , c ) VALUES (1, null, null);", utils.getQualifiedTable()); 45 | CassandraUtils.dropKeyspaceIfNotNull(utils); 46 | } 47 | } -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/builder/BigIntegerMapperBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonProperty; 19 | import com.stratio.cassandra.lucene.schema.mapping.BigIntegerMapper; 20 | 21 | /** 22 | * {@link SingleColumnMapperBuilder} to build a new {@link BigIntegerMapper}. 23 | * 24 | * @author Andres de la Pena {@literal } 25 | */ 26 | public class BigIntegerMapperBuilder extends SingleColumnMapperBuilder { 27 | 28 | @JsonProperty("digits") 29 | private Integer digits; 30 | 31 | /** 32 | * Sets the max number of digits. 33 | * 34 | * @param digits The max number of digits. 35 | * @return this 36 | */ 37 | public BigIntegerMapperBuilder digits(Integer digits) { 38 | this.digits = digits; 39 | return this; 40 | } 41 | 42 | /** 43 | * Returns the {@link BigIntegerMapper} represented by this {@link MapperBuilder}. 44 | * 45 | * @param field the name of the field to be built 46 | * @return the {@link BigIntegerMapper} represented by this 47 | */ 48 | @Override 49 | public BigIntegerMapper build(String field) { 50 | return new BigIntegerMapper(field, column, validated, digits); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/builder/DateMapperBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonProperty; 19 | import com.stratio.cassandra.lucene.schema.mapping.DateMapper; 20 | 21 | /** 22 | * {@link SingleColumnMapperBuilder} to build a new {@link DateMapper}. 23 | * 24 | * @author Andres de la Pena {@literal } 25 | */ 26 | public class DateMapperBuilder extends SingleColumnMapperBuilder { 27 | 28 | /** The date pattern */ 29 | @JsonProperty("pattern") 30 | private String pattern; 31 | 32 | /** 33 | * Sets the default date format pattern. 34 | * 35 | * @param pattern a {@link java.text.SimpleDateFormat} date pattern 36 | * @return This. 37 | */ 38 | public DateMapperBuilder pattern(String pattern) { 39 | this.pattern = pattern; 40 | return this; 41 | } 42 | 43 | /** 44 | * Returns the {@link DateMapper} represented by this {@link MapperBuilder}. 45 | * 46 | * @param field the name of the field to be built 47 | * @return the {@link DateMapper} represented by this 48 | */ 49 | @Override 50 | public DateMapper build(String field) { 51 | return new DateMapper(field, column, validated, pattern); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /plugin/src/test/java/com/stratio/cassandra/lucene/schema/mapping/MapperTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping; 17 | 18 | import com.stratio.cassandra.lucene.column.Columns; 19 | import org.apache.lucene.index.IndexableField; 20 | import org.junit.Test; 21 | 22 | import java.util.List; 23 | 24 | import static com.stratio.cassandra.lucene.schema.SchemaBuilders.integerMapper; 25 | import static com.stratio.cassandra.lucene.schema.SchemaBuilders.stringMapper; 26 | import static org.junit.Assert.assertEquals; 27 | 28 | /** 29 | * {@link Mapper} tests. 30 | * 31 | * @author Andres de la Pena {@literal } 32 | */ 33 | public class MapperTest { 34 | 35 | @Test 36 | public void testIndexableFields() { 37 | Mapper mapper = stringMapper().build("f"); 38 | Columns columns = new Columns().add("f", "v"); 39 | List fields = mapper.indexableFields(columns); 40 | assertEquals("Expected 2 fields", 2, fields.size()); 41 | } 42 | 43 | @Test 44 | public void testBestEffortIndexableFields() { 45 | Mapper mapper = integerMapper().build("f"); 46 | Columns columns = new Columns().add("f", "1").add("f", "x").add("f", "2"); 47 | List fields = mapper.bestEffortIndexableFields(columns); 48 | assertEquals("Expected 4 fields", 4, fields.size()); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/builder/TextMapperBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonProperty; 19 | import com.stratio.cassandra.lucene.schema.mapping.TextMapper; 20 | 21 | /** 22 | * {@link SingleColumnMapperBuilder} to build a new {@link TextMapper}. 23 | * 24 | * @author Andres de la Pena {@literal } 25 | */ 26 | public class TextMapperBuilder extends SingleColumnMapperBuilder { 27 | 28 | @JsonProperty("analyzer") 29 | private String analyzer; 30 | 31 | /** 32 | * Sets the name of the {@link org.apache.lucene.analysis.Analyzer} to be used. 33 | * 34 | * @param analyzer the name of the {@link org.apache.lucene.analysis.Analyzer} to be used 35 | * @return this 36 | */ 37 | public TextMapperBuilder analyzer(String analyzer) { 38 | this.analyzer = analyzer; 39 | return this; 40 | } 41 | 42 | /** 43 | * Returns the {@link TextMapper} represented by this {@link MapperBuilder}. 44 | * 45 | * @param field the name of the field to be built 46 | * @return the {@link TextMapper} represented by this 47 | */ 48 | @Override 49 | public TextMapper build(String field) { 50 | return new TextMapper(field, column, validated, analyzer); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/search/condition/AllCondition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.condition; 17 | 18 | import com.google.common.base.MoreObjects; 19 | import com.stratio.cassandra.lucene.schema.Schema; 20 | import org.apache.lucene.search.MatchAllDocsQuery; 21 | import org.apache.lucene.search.Query; 22 | 23 | import java.util.Collections; 24 | import java.util.Set; 25 | 26 | /** 27 | * A {@link Condition} implementation that matches all documents. 28 | * 29 | * @author Andres de la Pena {@literal } 30 | */ 31 | public class AllCondition extends Condition { 32 | 33 | /** 34 | * Constructor without field arguments. 35 | * 36 | * @param boost The boost for this query clause. Documents matching this clause will (in addition to the normal 37 | * weightings) have their score multiplied by {@code boost}. 38 | */ 39 | public AllCondition(Float boost) { 40 | super(boost); 41 | } 42 | 43 | /** {@inheritDoc} */ 44 | @Override 45 | public Query doQuery(Schema schema) { 46 | return new MatchAllDocsQuery(); 47 | } 48 | 49 | /** {@inheritDoc} */ 50 | public Set postProcessingFields() { 51 | return Collections.emptySet(); 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override 56 | public MoreObjects.ToStringHelper toStringHelper() { 57 | return toStringHelper(this); 58 | } 59 | } -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/search/condition/NoneCondition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.condition; 17 | 18 | import com.google.common.base.MoreObjects; 19 | import com.stratio.cassandra.lucene.schema.Schema; 20 | import org.apache.lucene.search.BooleanQuery; 21 | import org.apache.lucene.search.Query; 22 | 23 | import java.util.Collections; 24 | import java.util.Set; 25 | 26 | /** 27 | * A {@link Condition} implementation that matches none documents. 28 | * 29 | * @author Andres de la Pena {@literal } 30 | */ 31 | public class NoneCondition extends Condition { 32 | 33 | /** 34 | * Constructor without field arguments. 35 | * 36 | * @param boost The boost for this query clause. Documents matching this clause will (in addition to the normal 37 | * weightings) have their score multiplied by {@code boost}. 38 | */ 39 | public NoneCondition(Float boost) { 40 | super(boost); 41 | } 42 | 43 | /** {@inheritDoc} */ 44 | @Override 45 | public Query doQuery(Schema schema) { 46 | return new BooleanQuery.Builder().build(); 47 | } 48 | 49 | /** {@inheritDoc} */ 50 | public Set postProcessingFields() { 51 | return Collections.emptySet(); 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override 56 | public MoreObjects.ToStringHelper toStringHelper() { 57 | return toStringHelper(this); 58 | } 59 | } -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/builder/StringMapperBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonProperty; 19 | import com.stratio.cassandra.lucene.schema.mapping.StringMapper; 20 | 21 | /** 22 | * {@link SingleColumnMapperBuilder} to build a new {@link StringMapper}. 23 | * 24 | * @author Andres de la Pena {@literal } 25 | */ 26 | public class StringMapperBuilder extends SingleColumnMapperBuilder { 27 | 28 | @JsonProperty("case_sensitive") 29 | private Boolean caseSensitive; 30 | 31 | /** 32 | * Sets if the {@link StringMapper} to be built must be case sensitive. 33 | * 34 | * @param caseSensitive if the {@link StringMapper} to be built must be case sensitive 35 | * @return this 36 | */ 37 | public StringMapperBuilder caseSensitive(Boolean caseSensitive) { 38 | this.caseSensitive = caseSensitive; 39 | return this; 40 | } 41 | 42 | /** 43 | * Returns the {@link StringMapper} represented by this {@link MapperBuilder}. 44 | * 45 | * @param field the name of the field to be built 46 | * @return the {@link StringMapper} represented by this 47 | */ 48 | @Override 49 | public StringMapper build(String field) { 50 | return new StringMapper(field, column, validated, caseSensitive); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/common/GeoOperation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.common; 17 | 18 | import com.fasterxml.jackson.annotation.JsonCreator; 19 | import com.stratio.cassandra.lucene.IndexException; 20 | import org.apache.lucene.spatial.query.SpatialOperation; 21 | 22 | /** 23 | * Enum representing a spatial operation. 24 | */ 25 | public enum GeoOperation { 26 | 27 | INTERSECTS("intersects", SpatialOperation.Intersects), 28 | IS_WITHIN("is_within", SpatialOperation.IsWithin), 29 | CONTAINS("contains", SpatialOperation.Contains); 30 | 31 | private final String name; 32 | private final SpatialOperation spatialOperation; 33 | 34 | GeoOperation(String name, SpatialOperation spatialOperation) { 35 | this.name = name; 36 | this.spatialOperation = spatialOperation; 37 | } 38 | 39 | /** 40 | * Returns the represented Lucene's {@link SpatialOperation}. 41 | * 42 | * @return the Lucene's spatial operation 43 | */ 44 | public SpatialOperation getSpatialOperation() { 45 | return spatialOperation; 46 | } 47 | 48 | @JsonCreator 49 | public static GeoOperation parse(String value) { 50 | for (GeoOperation geoOperation : values()) { 51 | String name = geoOperation.name; 52 | if (name.equalsIgnoreCase(value)) { 53 | return geoOperation; 54 | } 55 | } 56 | throw new IndexException("Invalid geographic operation {}", value); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/StringMapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping; 17 | 18 | /** 19 | * A {@link Mapper} to map a string, not tokenized field. 20 | * 21 | * @author Andres de la Pena {@literal } 22 | */ 23 | public class StringMapper extends KeywordMapper { 24 | 25 | /** The default case sensitive option. */ 26 | public static final boolean DEFAULT_CASE_SENSITIVE = true; 27 | 28 | /** If it must be case sensitive. */ 29 | public final boolean caseSensitive; 30 | 31 | /** 32 | * Builds a new {@link StringMapper}. 33 | * 34 | * @param field the name of the field 35 | * @param column the name of the column to be mapped 36 | * @param validated if the field must be validated 37 | * @param caseSensitive if the analyzer must be case sensitive 38 | */ 39 | public StringMapper(String field, String column, Boolean validated, Boolean caseSensitive) { 40 | super(field, column, validated, PRINTABLE_TYPES); 41 | this.caseSensitive = caseSensitive == null ? DEFAULT_CASE_SENSITIVE : caseSensitive; 42 | } 43 | 44 | /** {@inheritDoc} */ 45 | @Override 46 | protected String doBase(String name, Object value) { 47 | String string = value.toString(); 48 | return caseSensitive ? string : string.toLowerCase(); 49 | } 50 | 51 | /** {@inheritDoc} */ 52 | @Override 53 | public String toString() { 54 | return toStringHelper(this).add("caseSensitive", caseSensitive).toString(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/search/condition/builder/PrefixConditionBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.condition.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonCreator; 19 | import com.fasterxml.jackson.annotation.JsonProperty; 20 | import com.stratio.cassandra.lucene.search.condition.PrefixCondition; 21 | 22 | /** 23 | * {@link ConditionBuilder} for building a new {@link PrefixCondition}. 24 | * 25 | * @author Andres de la Pena {@literal } 26 | */ 27 | public class PrefixConditionBuilder extends ConditionBuilder { 28 | 29 | /** The name of the field to be matched. */ 30 | @JsonProperty("field") 31 | private final String field; 32 | 33 | /** The prefix to be matched. */ 34 | @JsonProperty("value") 35 | private final String value; 36 | 37 | /** 38 | * Creates a new {@link PrefixConditionBuilder}. 39 | * 40 | * @param field the name of the field to be matched 41 | * @param value the prefix to be matched 42 | */ 43 | @JsonCreator 44 | public PrefixConditionBuilder(@JsonProperty("field") String field, @JsonProperty("value") String value) { 45 | this.field = field; 46 | this.value = value; 47 | } 48 | 49 | /** 50 | * Returns the {@link PrefixCondition} represented by this builder. 51 | * 52 | * @return a new prefix condition 53 | */ 54 | @Override 55 | public PrefixCondition build() { 56 | return new PrefixCondition(boost, field, value); 57 | } 58 | } -------------------------------------------------------------------------------- /testsAT/src/test/java/com/stratio/cassandra/lucene/util/monitoring/CassandraMonitoringClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.util.monitoring; 17 | 18 | /** 19 | * Client for consuming the JMX monitoring services of a Cassandra node. 20 | * 21 | * @author Eduardo Alonso {@literal } 22 | */ 23 | public interface CassandraMonitoringClient { 24 | 25 | /** 26 | * Connects to the node. 27 | * 28 | * @return this 29 | */ 30 | CassandraMonitoringClient connect(); 31 | 32 | /** 33 | * Closes the connection to the node. 34 | */ 35 | void disconnect(); 36 | 37 | /** 38 | * Gets the value of a specific attribute of a named MBean. 39 | * 40 | * @param bean the name of the MBean from which the attribute is to be retrieved 41 | * @param attribute the name of the attribute to be retrieved 42 | * @return the value of the retrieved attribute 43 | */ 44 | Object read(String bean, String attribute); 45 | 46 | /** 47 | * Invokes an operation on an MBean. 48 | * 49 | * @param bean the name of the MBean from which the attribute is to be retrieved 50 | * @param operation the name of the operation to be invoked 51 | * @param params n array containing the parameters to be set when the operation is invoked 52 | * @param signature An array containing the signature of the operation, an array of class names in the format 53 | * returned by {@link Class#getName()}. 54 | */ 55 | void invoke(String bean, String operation, Object[] params, String[] signature); 56 | } 57 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/analysis/ClasspathAnalyzerBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.analysis; 17 | 18 | import com.fasterxml.jackson.annotation.JsonCreator; 19 | import com.fasterxml.jackson.annotation.JsonProperty; 20 | import com.stratio.cassandra.lucene.IndexException; 21 | import org.apache.lucene.analysis.Analyzer; 22 | 23 | import java.lang.reflect.Constructor; 24 | 25 | /** 26 | * {@link AnalyzerBuilder} for building {@link Analyzer}s in classpath using its default (no args) constructor. 27 | * 28 | * @author Andres de la Pena {@literal } 29 | */ 30 | public class ClasspathAnalyzerBuilder extends AnalyzerBuilder { 31 | 32 | @JsonProperty("class") 33 | private final String className; 34 | 35 | /** 36 | * Builds a new {@link AnalyzerBuilder} using the specified {@link Analyzer} full class name. 37 | * 38 | * @param className an {@link Analyzer} full qualified class name 39 | */ 40 | @JsonCreator 41 | public ClasspathAnalyzerBuilder(@JsonProperty("class") String className) { 42 | this.className = className; 43 | 44 | } 45 | 46 | /** {@inheritDoc} */ 47 | @Override 48 | public Analyzer analyzer() { 49 | try { 50 | Class analyzerClass = Class.forName(className); 51 | Constructor constructor = analyzerClass.getConstructor(); 52 | return (Analyzer) constructor.newInstance(); 53 | } catch (Exception e) { 54 | throw new IndexException(e, "Not found analyzer '{}'", className); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/search/condition/builder/RegexpConditionBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.condition.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonCreator; 19 | import com.fasterxml.jackson.annotation.JsonProperty; 20 | import com.stratio.cassandra.lucene.search.condition.RegexpCondition; 21 | 22 | /** 23 | * {@link ConditionBuilder} for building a new {@link RegexpCondition}. 24 | * 25 | * @author Andres de la Pena {@literal } 26 | */ 27 | public class RegexpConditionBuilder extends ConditionBuilder { 28 | 29 | /** The name of the field to be matched. */ 30 | @JsonProperty("field") 31 | private final String field; 32 | 33 | /** The wildcard expression to be matched. */ 34 | @JsonProperty("value") 35 | private final String value; 36 | 37 | /** 38 | * Creates a new {@link RegexpConditionBuilder} for the specified field and expression. 39 | * 40 | * @param field the name of the field to be matched 41 | * @param value the wildcard expression to be matched 42 | */ 43 | @JsonCreator 44 | public RegexpConditionBuilder(@JsonProperty("field") String field, @JsonProperty("value") String value) { 45 | this.field = field; 46 | this.value = value; 47 | } 48 | 49 | /** 50 | * Returns the {@link RegexpCondition} represented by this builder. 51 | * 52 | * @return a new regexp condition 53 | */ 54 | @Override 55 | public RegexpCondition build() { 56 | return new RegexpCondition(boost, field, value); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/search/condition/builder/WildcardConditionBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.condition.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonCreator; 19 | import com.fasterxml.jackson.annotation.JsonProperty; 20 | import com.stratio.cassandra.lucene.search.condition.WildcardCondition; 21 | 22 | /** 23 | * {@link ConditionBuilder} for building a new {@link WildcardCondition}. 24 | * 25 | * @author Andres de la Pena {@literal } 26 | */ 27 | public class WildcardConditionBuilder extends ConditionBuilder { 28 | 29 | /** The name of the field to be matched. */ 30 | @JsonProperty("field") 31 | private final String field; 32 | 33 | /** The wildcard expression to be matched. */ 34 | @JsonProperty("value") 35 | private final String value; 36 | 37 | /** 38 | * Creates a new {@link WildcardConditionBuilder} for the specified field and value. 39 | * 40 | * @param field the name of the field to be matched 41 | * @param value the wildcard expression to be matched 42 | */ 43 | @JsonCreator 44 | public WildcardConditionBuilder(@JsonProperty("field") String field, @JsonProperty("value") String value) { 45 | this.field = field; 46 | this.value = value; 47 | } 48 | 49 | /** 50 | * Returns the {@link WildcardCondition} represented by this builder. 51 | * 52 | * @return a new wildcard condition 53 | */ 54 | @Override 55 | public WildcardCondition build() { 56 | return new WildcardCondition(boost, field, value); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /plugin/src/test/scala/com/stratio/cassandra/lucene/partitioning/PartitionerOnTokenTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.partitioning 17 | 18 | import com.stratio.cassandra.lucene.IndexException 19 | import org.junit.runner.RunWith 20 | import org.scalatestplus.junit.JUnitRunner 21 | 22 | /** Tests for [[PartitionerOnToken]]. 23 | * 24 | * @author Andres de la Pena `adelapena@stratio.com` 25 | */ 26 | @RunWith(classOf[JUnitRunner]) 27 | class PartitionerOnTokenTest extends PartitionerTest { 28 | 29 | test("build with zero partitions") { 30 | assertThrows[IndexException] {PartitionerOnToken(0)} 31 | } 32 | 33 | test("build with negative partitions") { 34 | assertThrows[IndexException] {PartitionerOnToken(-1)} 35 | } 36 | 37 | test("parse JSON") { 38 | val json = "{type:\"token\", partitions: 10}" 39 | Partitioner.fromJson(json) shouldBe PartitionerOnToken.Builder(10) 40 | } 41 | 42 | test("num partitions") { 43 | PartitionerOnToken(4).numPartitions shouldBe 4 44 | } 45 | 46 | test("key partition with 1 partition") { 47 | for (i <- 1 to 10) { 48 | PartitionerOnToken(1).partition(key(i)) shouldBe 0 49 | } 50 | } 51 | 52 | test("key partition with n partitions") { 53 | val partitioner = PartitionerOnToken(10) 54 | partitioner.partition(key(0)) shouldBe 8 55 | partitioner.partition(key(1)) shouldBe 9 56 | partitioner.partition(key(2)) shouldBe 2 57 | partitioner.partition(key(3)) shouldBe 5 58 | partitioner.partition(key(4)) shouldBe 5 59 | partitioner.partition(key(5)) shouldBe 4 60 | partitioner.partition(key(6)) shouldBe 8 61 | partitioner.partition(key(7)) shouldBe 6 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/search/sort/builder/SortFieldBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.sort.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonProperty; 19 | import com.fasterxml.jackson.annotation.JsonSubTypes; 20 | import com.fasterxml.jackson.annotation.JsonTypeInfo; 21 | import com.stratio.cassandra.lucene.common.Builder; 22 | import com.stratio.cassandra.lucene.search.sort.SortField; 23 | 24 | /** 25 | * {@link Builder} for building a new {@link SortField}. 26 | * 27 | * @param the {@link SortField} 28 | * @param the {@link SortField} builder 29 | * @author Andres de la Pena {@literal } 30 | */ 31 | @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", defaultImpl = SimpleSortFieldBuilder.class) 32 | @JsonSubTypes({@JsonSubTypes.Type(value = SimpleSortFieldBuilder.class, name = "simple"), 33 | @JsonSubTypes.Type(value = GeoDistanceSortFieldBuilder.class, name = "geo_distance")}) 34 | public abstract class SortFieldBuilder implements Builder { 35 | 36 | /** If natural order should be reversed. */ 37 | @JsonProperty("reverse") 38 | boolean reverse; 39 | 40 | /** 41 | * Returns this {@link SortFieldBuilder} with the specified reverse option. 42 | * 43 | * @param reverse {@code true} if natural order should be reversed 44 | * @return This. 45 | */ 46 | @SuppressWarnings("unchecked") 47 | public K reverse(boolean reverse) { 48 | this.reverse = reverse; 49 | return (K) this; 50 | } 51 | 52 | /** {@inheritDoc} */ 53 | @Override 54 | public abstract T build(); 55 | } -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/search/sort/SortField.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.sort; 17 | 18 | import com.stratio.cassandra.lucene.schema.Schema; 19 | 20 | import java.util.Set; 21 | 22 | /** 23 | * A sorting for a field of a search. 24 | * 25 | * @author Andres de la Pena {@literal } 26 | */ 27 | public abstract class SortField { 28 | 29 | /** The default reverse option. */ 30 | public static final boolean DEFAULT_REVERSE = false; 31 | 32 | /** {@code true} if natural order should be reversed. */ 33 | public final boolean reverse; 34 | 35 | /** 36 | * Returns a new {@link SortField}. 37 | * 38 | * @param reverse {@code true} if natural order should be reversed. 39 | */ 40 | public SortField(Boolean reverse) { 41 | 42 | this.reverse = reverse == null ? DEFAULT_REVERSE : reverse; 43 | } 44 | 45 | /** 46 | * Returns the Lucene's {@link org.apache.lucene.search.SortField} representing this {@link SortField}. 47 | * 48 | * @param schema the {@link Schema} to be used 49 | * @return the Lucene's sort field 50 | */ 51 | public abstract org.apache.lucene.search.SortField sortField(Schema schema); 52 | 53 | /** 54 | * Returns the names of the involved fields. 55 | * 56 | * @return the names of the involved fields 57 | */ 58 | public abstract Set postProcessingFields(); 59 | 60 | /** {@inheritDoc} */ 61 | @Override 62 | public abstract String toString(); 63 | 64 | /** {@inheritDoc} */ 65 | @Override 66 | public abstract boolean equals(Object o); 67 | 68 | /** {@inheritDoc} */ 69 | @Override 70 | public abstract int hashCode(); 71 | } -------------------------------------------------------------------------------- /plugin/src/main/scala/com/stratio/cassandra/lucene/IndexServiceMBean.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene 17 | 18 | /** JMX methods to be exposed by [[IndexService]]. 19 | * 20 | * @author Andres de la Pena `adelapena@stratio.com` 21 | */ 22 | trait IndexServiceMBean { 23 | 24 | /** Commits the pending changes. */ 25 | def commit(): Unit 26 | 27 | /** Returns the total number of documents in this index. 28 | * 29 | * @return the number of documents 30 | */ 31 | def getNumDocs: Long 32 | 33 | /** Returns the total number of deleted documents in this index. 34 | * 35 | * @return the number of deleted documents 36 | */ 37 | def getNumDeletedDocs: Long 38 | 39 | /** 40 | * Get if it has deletions 41 | * @return 42 | */ 43 | def getHasDeletions: Boolean 44 | 45 | /** 46 | * Get max docs 47 | * @return 48 | */ 49 | def getMaxDocs: Int 50 | 51 | /** 52 | * Get ref counts 53 | * @return 54 | */ 55 | def getRefCount: Int 56 | 57 | /** Optimizes the index forcing merge segments leaving the specified number of segments. This 58 | * operation may block until all merging completes. 59 | * 60 | * @param maxNumSegments the maximum number of segments left in the index after merging finishes 61 | * @param doWait `true` if the call should block until the operation completes 62 | */ 63 | def forceMerge(maxNumSegments: Int, doWait: Boolean): Unit 64 | 65 | /** Optimizes the index forcing merge of all segments that have deleted documents. This operation 66 | * may block until all merging completes. 67 | * 68 | * @param doWait `true` if the call should block until the operation completes 69 | */ 70 | def forceMergeDeletes(doWait: Boolean): Unit 71 | 72 | /** Refreshes the index readers. */ 73 | def refresh(): Unit 74 | } 75 | -------------------------------------------------------------------------------- /plugin/src/test/java/com/stratio/cassandra/lucene/schema/analysis/ClasspathAnalyzerBuilderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.analysis; 17 | 18 | import com.stratio.cassandra.lucene.IndexException; 19 | import com.stratio.cassandra.lucene.common.JsonSerializer; 20 | import org.apache.lucene.analysis.Analyzer; 21 | import org.apache.lucene.analysis.en.EnglishAnalyzer; 22 | import org.junit.Test; 23 | 24 | import java.io.IOException; 25 | 26 | import static org.junit.Assert.assertEquals; 27 | 28 | /** 29 | * @author Andres de la Pena {@literal } 30 | */ 31 | public class ClasspathAnalyzerBuilderTest { 32 | 33 | @Test 34 | public void testBuild() { 35 | String className = "org.apache.lucene.analysis.en.EnglishAnalyzer"; 36 | ClasspathAnalyzerBuilder builder = new ClasspathAnalyzerBuilder(className); 37 | Analyzer analyzer = builder.analyzer(); 38 | assertEquals("Expected EnglishAnalyzer class", EnglishAnalyzer.class, analyzer.getClass()); 39 | } 40 | 41 | @Test(expected = IndexException.class) 42 | public void testBuildWithWrongClassName() { 43 | new ClasspathAnalyzerBuilder("abc").analyzer(); 44 | } 45 | 46 | @Test 47 | public void testParseJSON() throws IOException { 48 | String json = "{type:\"classpath\", class:\"org.apache.lucene.analysis.en.EnglishAnalyzer\"}"; 49 | AnalyzerBuilder builder = JsonSerializer.fromString(json, AnalyzerBuilder.class); 50 | Analyzer analyzer = builder.analyzer(); 51 | assertEquals("Expected EnglishAnalyzer class", EnglishAnalyzer.class, analyzer.getClass()); 52 | } 53 | 54 | @Test(expected = IOException.class) 55 | public void testParseJSONInvalid() throws IOException { 56 | String json = "{class:\"abc\"}"; 57 | JsonSerializer.fromString(json, AnalyzerBuilder.class); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /testsAT/src/test/java/com/stratio/cassandra/lucene/issues/Issue69Test.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.issues; 17 | 18 | import static org.junit.Assert.assertEquals; 19 | 20 | import com.stratio.cassandra.lucene.BaseTest; 21 | import com.stratio.cassandra.lucene.util.CassandraUtils; 22 | import org.junit.jupiter.api.AfterAll; 23 | import org.junit.jupiter.api.BeforeAll; 24 | import org.junit.jupiter.api.Test; 25 | 26 | /** 27 | * @author Andres de la Pena 28 | */ 29 | 30 | public class Issue69Test extends BaseTest { 31 | 32 | private static CassandraUtils utils; 33 | 34 | @BeforeAll 35 | public static void before() { 36 | utils = CassandraUtils.builder("distinct") 37 | .withPartitionKey("make") 38 | .withClusteringKey("model") 39 | .withColumn("make", "text") 40 | .withColumn("model", "text") 41 | .withColumn("color", "text") 42 | .build() 43 | .createKeyspace() 44 | .createTable() 45 | .createIndex() 46 | .insert(new String[]{"make", "model", "color"}, new Object[]{"Tesla", "Model X", "Red"}) 47 | .insert(new String[]{"make", "model", "color"}, new Object[]{"Tesla", "Model S", "Red"}) 48 | .insert(new String[]{"make", "model", "color"}, new Object[]{"Porsche", "Cayman S", "Red"}) 49 | .refresh(); 50 | } 51 | 52 | @AfterAll 53 | public static void after() { 54 | CassandraUtils.dropKeyspaceIfNotNull(utils); 55 | } 56 | 57 | @Test 58 | public void testUDF() { 59 | int n1 = utils.execute("SELECT make FROM %s;", utils.getQualifiedTable()).all().size(); 60 | assertEquals("Basic count is wrong", n1, 3); 61 | int n2 = utils.execute("SELECT DISTINCT make FROM %s;", utils.getQualifiedTable()).all().size(); 62 | assertEquals("Basic count is wrong", n2, 2); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/builder/BigDecimalMapperBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonProperty; 19 | import com.stratio.cassandra.lucene.schema.mapping.BigDecimalMapper; 20 | 21 | /** 22 | * {@link SingleColumnMapperBuilder} to build a new {@link BigDecimalMapper}. 23 | * 24 | * @author Andres de la Pena {@literal } 25 | */ 26 | public class BigDecimalMapperBuilder extends SingleColumnMapperBuilder { 27 | 28 | @JsonProperty("integer_digits") 29 | private Integer integerDigits; 30 | 31 | @JsonProperty("decimal_digits") 32 | private Integer decimalDigits; 33 | 34 | /** 35 | * Sets the max number of digits for the integer part. 36 | * 37 | * @param integerDigits the max number of digits for the integer part 38 | * @return this 39 | */ 40 | public BigDecimalMapperBuilder integerDigits(Integer integerDigits) { 41 | this.integerDigits = integerDigits; 42 | return this; 43 | } 44 | 45 | /** 46 | * Sets the max number of digits for the decimal part. 47 | * 48 | * @param decimalDigits the max number of digits for the decimal part 49 | * @return this 50 | */ 51 | public BigDecimalMapperBuilder decimalDigits(Integer decimalDigits) { 52 | this.decimalDigits = decimalDigits; 53 | return this; 54 | } 55 | 56 | /** 57 | * Returns the {@link BigDecimalMapper} represented by this {@link MapperBuilder}. 58 | * 59 | * @param field the name of the field to be built 60 | * @return the {@link BigDecimalMapper} represented by this 61 | */ 62 | @Override 63 | public BigDecimalMapper build(String field) { 64 | return new BigDecimalMapper(field, column, validated, integerDigits, decimalDigits); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /plugin/src/test/java/com/stratio/cassandra/lucene/search/condition/GeoOperationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.condition; 17 | 18 | import com.stratio.cassandra.lucene.IndexException; 19 | import com.stratio.cassandra.lucene.common.GeoOperation; 20 | import org.apache.lucene.spatial.query.SpatialOperation; 21 | import org.junit.Test; 22 | 23 | import static org.junit.Assert.assertEquals; 24 | 25 | /** 26 | * @author Eduardo Alonso {@literal } 27 | */ 28 | public class GeoOperationTest extends AbstractConditionTest { 29 | 30 | @Test(expected = IndexException.class) 31 | public void testParseNull() { 32 | GeoOperation.parse(null); 33 | } 34 | 35 | @Test(expected = IndexException.class) 36 | public void testParseEmpty() { 37 | GeoOperation.parse(""); 38 | } 39 | 40 | @Test(expected = IndexException.class) 41 | public void testParseBlank() { 42 | GeoOperation.parse("\t "); 43 | } 44 | 45 | @Test(expected = IndexException.class) 46 | public void testInvalid() { 47 | GeoOperation.parse("invalid_operation"); 48 | } 49 | 50 | @Test 51 | public void testParseIntersects() { 52 | GeoOperation distance = GeoOperation.parse("intersects"); 53 | check(distance, SpatialOperation.Intersects); 54 | } 55 | 56 | @Test 57 | public void testParseIsWithin() { 58 | GeoOperation distance = GeoOperation.parse("is_within"); 59 | check(distance, SpatialOperation.IsWithin); 60 | } 61 | 62 | @Test 63 | public void testParseContains() { 64 | GeoOperation distance = GeoOperation.parse("contains"); 65 | check(distance, SpatialOperation.Contains); 66 | } 67 | 68 | private void check(GeoOperation operation, SpatialOperation spatialOperation) { 69 | assertEquals("Parsed distance is wrong", spatialOperation, operation.getSpatialOperation()); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /testsAT/src/test/java/com/stratio/cassandra/lucene/varia/LargeFieldTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.varia; 17 | 18 | import static com.stratio.cassandra.lucene.builder.Builder.stringMapper; 19 | import static com.stratio.cassandra.lucene.builder.Builder.textMapper; 20 | import static com.stratio.cassandra.lucene.builder.Builder.wildcard; 21 | 22 | import java.io.IOException; 23 | import java.util.Arrays; 24 | import java.util.UUID; 25 | 26 | import com.stratio.cassandra.lucene.BaseTest; 27 | import com.stratio.cassandra.lucene.util.CassandraUtils; 28 | import org.junit.jupiter.api.Test; 29 | 30 | /** 31 | * @author Andres de la Pena 32 | */ 33 | 34 | public class LargeFieldTest extends BaseTest { 35 | 36 | @Test 37 | public void testLargeField() throws IOException { 38 | CassandraUtils utils = CassandraUtils.builder("large_field") 39 | .withPartitionKey("k") 40 | .withColumn("k", "int", null) 41 | .withColumn("t", "text", textMapper().analyzer("whitespace")) 42 | .withColumn("s", "text", stringMapper()) 43 | .build() 44 | .createKeyspace() 45 | .createTable() 46 | .createIndex(); 47 | 48 | int numNumbers = 5000; 49 | UUID[] numbers = new UUID[numNumbers]; 50 | for (int i = 0; i < numNumbers; i++) { 51 | numbers[i] = UUID.randomUUID(); 52 | } 53 | String largeString = Arrays.toString(numbers); 54 | 55 | utils.insert(new String[]{"k", "t", "s"}, new Object[]{1, "a", "b"}) 56 | .insert(new String[]{"k", "t", "s"}, new Object[]{2, largeString, "b"}) 57 | .insert(new String[]{"k", "t", "s"}, new Object[]{3, "a", largeString}) 58 | .refresh() 59 | .searchAll().check(3) 60 | .filter(wildcard("t", "*")).check(3) 61 | .filter(wildcard("s", "*")).check(2) 62 | .dropKeyspace(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/BooleanMapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping; 17 | 18 | import com.stratio.cassandra.lucene.IndexException; 19 | 20 | import java.util.Arrays; 21 | 22 | /** 23 | * A {@link Mapper} to map a boolean field. 24 | * 25 | * @author Andres de la Pena {@literal } 26 | */ 27 | public class BooleanMapper extends KeywordMapper { 28 | 29 | /** The {@code String} representation of a true value. */ 30 | private static final String TRUE = "true"; 31 | 32 | /** The {@code String} representation of a false value. */ 33 | private static final String FALSE = "false"; 34 | 35 | /** 36 | * Builds a new {@link BooleanMapper}. 37 | * 38 | * @param field the name of the field 39 | * @param column the name of the column to be mapped 40 | * @param validated if the field must be validated 41 | */ 42 | public BooleanMapper(String field, String column, Boolean validated) { 43 | super(field, column, validated, Arrays.asList(String.class, Boolean.class)); 44 | } 45 | 46 | /** {@inheritDoc} */ 47 | @Override 48 | protected String doBase(String name, Object value) { 49 | if (value instanceof Boolean) { 50 | return (Boolean) value ? TRUE : FALSE; 51 | } else if (value instanceof String) { 52 | return base(name, (String) value); 53 | } 54 | throw new IndexException("Field '{}' requires a boolean, but found '{}'", name, value); 55 | } 56 | 57 | private String base(String name, String value) { 58 | if (value.equalsIgnoreCase(TRUE)) { 59 | return TRUE; 60 | } else if (value.equalsIgnoreCase(FALSE)) { 61 | return FALSE; 62 | } else { 63 | throw new IndexException("Boolean field '{}' requires either '{}' or '{}', but found '{}'", 64 | name, TRUE, FALSE, value); 65 | } 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /plugin/src/test/scala/com/stratio/cassandra/lucene/BaseScalaTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene 17 | 18 | import com.google.common.collect.Lists 19 | import org.apache.cassandra.cql3.FieldIdentifier 20 | import org.apache.cassandra.db.marshal._ 21 | import org.scalatest.{FunSuite, Matchers} 22 | import scala.jdk.CollectionConverters._ 23 | 24 | /** Base test. 25 | * 26 | * @author Andres de la Pena `adelapena@stratio.com` 27 | */ 28 | class BaseScalaTest extends FunSuite with Matchers { 29 | 30 | 31 | } 32 | 33 | object BaseScalaTest { 34 | 35 | val utf8 = UTF8Type.instance 36 | val ascii = AsciiType.instance 37 | val int32 = Int32Type.instance 38 | val byte = ByteType.instance 39 | val short = ShortType.instance 40 | val long = LongType.instance 41 | val float = FloatType.instance 42 | val double = DoubleType.instance 43 | val date = SimpleDateType.instance 44 | val integer = IntegerType.instance 45 | val uuid = UUIDType.instance 46 | val lexicalUuid = LexicalUUIDType.instance 47 | val timeUuid = TimeUUIDType.instance 48 | val decimal = DecimalType.instance 49 | val timestamp = TimestampType.instance 50 | val boolean = BooleanType.instance 51 | 52 | def set[A](elements: AbstractType[A], multiCell: Boolean): SetType[A] = 53 | SetType.getInstance(elements, multiCell) 54 | 55 | def list[A](elements: AbstractType[A], multiCell: Boolean): ListType[A] = 56 | ListType.getInstance(elements, multiCell) 57 | 58 | def map[A, B](keys: AbstractType[A], values: AbstractType[B], multiCell: Boolean): MapType[A, B] = 59 | MapType.getInstance(keys, values, multiCell) 60 | 61 | def udt(names: List[String], types: List[AbstractType[_]]): UserType = 62 | new UserType( 63 | "ks", 64 | utf8.decompose("cell"), 65 | Lists.newArrayList(names.map(x => new FieldIdentifier(utf8.decompose(x))).asJava), 66 | Lists.newArrayList(types.asJava),false) 67 | 68 | def reversed[A](base: AbstractType[A]): ReversedType[A] = ReversedType.getInstance(base) 69 | } 70 | -------------------------------------------------------------------------------- /plugin/src/main/scala/com/stratio/cassandra/lucene/index/TokenLengthAnalyzer.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.index 17 | 18 | import com.google.common.base.MoreObjects 19 | import com.stratio.cassandra.lucene.util.Logging 20 | import org.apache.lucene.analysis.Analyzer.TokenStreamComponents 21 | import org.apache.lucene.analysis.tokenattributes.CharTermAttribute 22 | import org.apache.lucene.analysis.util.FilteringTokenFilter 23 | import org.apache.lucene.analysis.{Analyzer, AnalyzerWrapper} 24 | import org.apache.lucene.index.IndexWriter 25 | 26 | /** [[AnalyzerWrapper]] that discards too large tokens. 27 | * 28 | * @param analyzer the analyzer to be wrapped 29 | * @author Andres de la Pena `adelapena@stratio.com` 30 | */ 31 | class TokenLengthAnalyzer(val analyzer: Analyzer) 32 | extends AnalyzerWrapper(analyzer.getReuseStrategy) with Logging { 33 | 34 | /** inheritdoc */ 35 | override protected def getWrappedAnalyzer(fieldName: String): Analyzer = analyzer 36 | 37 | /** @inheritdoc */ 38 | override protected def wrapComponents( 39 | field: String, 40 | components: TokenStreamComponents): TokenStreamComponents = { 41 | val tokenFilter = new FilteringTokenFilter(components.getTokenStream) { 42 | 43 | val term = addAttribute(classOf[CharTermAttribute]) 44 | val maxSize = IndexWriter.MAX_TERM_LENGTH 45 | 46 | /** @inheritdoc */ 47 | override protected def accept: Boolean = { 48 | val size = term.length 49 | if (size <= maxSize) true 50 | else { 51 | logger.warn( 52 | s"Discarding immense term in field='$field', " + 53 | s"Lucene only allows terms with at most $maxSize bytes in length; got $size") 54 | false 55 | } 56 | } 57 | } 58 | new TokenStreamComponents(components.getTokenizer, tokenFilter) 59 | } 60 | 61 | /** @inheritdoc */ 62 | override def toString: String = { 63 | MoreObjects.toStringHelper(this).add("analyzer", analyzer).toString 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/search/sort/builder/GeoDistanceSortFieldBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.sort.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonCreator; 19 | import com.fasterxml.jackson.annotation.JsonProperty; 20 | import com.stratio.cassandra.lucene.search.sort.GeoDistanceSortField; 21 | 22 | /** 23 | * @author Eduardo Alonso {@literal } 24 | */ 25 | public class GeoDistanceSortFieldBuilder extends SortFieldBuilder { 26 | 27 | /** The name of the the geo point field mapper to use to calculate distance. */ 28 | @JsonProperty("field") 29 | private final String field; 30 | 31 | /** The latitude of the center point to sort by min distance to it. */ 32 | @JsonProperty("latitude") 33 | private final double latitude; 34 | 35 | /** The longitude of the center point to sort by min distance to it. */ 36 | @JsonProperty("longitude") 37 | private final double longitude; 38 | 39 | /** 40 | * Creates a new {@link GeoDistanceSortFieldBuilder} for the specified field. 41 | * 42 | * @param field the name of the geo point field mapper to use to calculate distance 43 | * @param latitude the latitude of the center point to sort by min distance to it 44 | * @param longitude the longitude of the center point to sort by min distance to it 45 | */ 46 | @JsonCreator 47 | public GeoDistanceSortFieldBuilder(@JsonProperty("field") String field, 48 | @JsonProperty("latitude") double latitude, 49 | @JsonProperty("longitude") double longitude) { 50 | 51 | this.field = field; 52 | this.latitude = latitude; 53 | this.longitude = longitude; 54 | } 55 | 56 | /** {@inheritDoc} */ 57 | @Override 58 | public GeoDistanceSortField build() { 59 | return new GeoDistanceSortField(field, reverse, latitude, longitude); 60 | } 61 | } -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/search/condition/builder/LuceneConditionBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.condition.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonCreator; 19 | import com.fasterxml.jackson.annotation.JsonProperty; 20 | import com.stratio.cassandra.lucene.search.condition.LuceneCondition; 21 | 22 | /** 23 | * {@link ConditionBuilder} for building a new {@link LuceneCondition}. 24 | * 25 | * @author Andres de la Pena {@literal } 26 | */ 27 | public class LuceneConditionBuilder extends ConditionBuilder { 28 | 29 | /** The Lucene query syntax expression. */ 30 | @JsonProperty("query") 31 | private final String query; 32 | 33 | /** The name of the field where the clauses will be applied by default. */ 34 | @JsonProperty("default_field") 35 | private String defaultField; 36 | 37 | /** 38 | * Returns a new {@link LuceneConditionBuilder} with the specified query. 39 | * 40 | * @param query the Lucene query syntax expression 41 | */ 42 | @JsonCreator 43 | public LuceneConditionBuilder(@JsonProperty("query") String query) { 44 | this.query = query; 45 | } 46 | 47 | /** 48 | * Returns this builder with the specified default field name. This is the field where the clauses will be applied 49 | * by default. 50 | * 51 | * @param defaultField the name of the field where the clauses will be applied by default 52 | * @return this with the specified name of the default field 53 | */ 54 | public LuceneConditionBuilder defaultField(String defaultField) { 55 | this.defaultField = defaultField; 56 | return this; 57 | } 58 | 59 | /** 60 | * Returns the {@link LuceneCondition} represented by this builder. 61 | * 62 | * @return a new Lucene syntax condition 63 | */ 64 | @Override 65 | public LuceneCondition build() { 66 | return new LuceneCondition(boost, defaultField, query); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /testsAT/src/test/java/com/stratio/cassandra/lucene/issues/Issue177Test.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.issues; 17 | 18 | import static com.stratio.cassandra.lucene.builder.Builder.integerMapper; 19 | import static com.stratio.cassandra.lucene.builder.Builder.longMapper; 20 | import static com.stratio.cassandra.lucene.builder.Builder.match; 21 | 22 | import com.stratio.cassandra.lucene.BaseTest; 23 | import com.stratio.cassandra.lucene.util.CassandraUtils; 24 | import org.junit.jupiter.api.Test; 25 | 26 | /** 27 | * Tests numeric mapping of date types (issue 28 | * 177) 29 | * 30 | * @author Andres de la Pena {@literal } 31 | */ 32 | 33 | public class Issue177Test extends BaseTest { 34 | 35 | @Test 36 | public void test() { 37 | CassandraUtils.builder("issue_177").withTable("test") 38 | .withIndexName("idx") 39 | .withColumn("id", "int") 40 | .withColumn("a_date", "date") 41 | .withColumn("a_timestamp", "timestamp") 42 | .withIndexColumn("lucene") 43 | .withPartitionKey("id") 44 | .withMapper("a_date_millis", longMapper().column("a_date")) 45 | .withMapper("a_timestamp_millis", longMapper().column("a_timestamp")) 46 | .withMapper("a_date_days", integerMapper().column("a_date")) 47 | .withMapper("a_timestamp_days", integerMapper().column("a_timestamp")) 48 | .build() 49 | .createKeyspace() 50 | .createTable() 51 | .createIndex() 52 | .insert(new String[]{"id", "a_date", "a_timestamp"}, new Object[]{1, 1000, 1000}) 53 | .refresh() 54 | .filter(match("a_timestamp_millis", 1000)).check(1) 55 | .filter(match("a_date_days", 1000)).check(1) 56 | .filter(match("a_timestamp_days", -2147483648)).check(1) 57 | .filter(match("a_date_millis", -185542500787200000L)).check(1) 58 | .dropKeyspace(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/builder/DateRangeMapperBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonProperty; 19 | import com.stratio.cassandra.lucene.schema.mapping.DateRangeMapper; 20 | 21 | /** 22 | * {@link MapperBuilder} to build a new {@link DateRangeMapper}. 23 | * 24 | * @author Andres de la Pena {@literal } 25 | */ 26 | public class DateRangeMapperBuilder extends MapperBuilder { 27 | 28 | /** The column containing the start date. */ 29 | @JsonProperty("from") 30 | private final String from; 31 | 32 | /** The column containing the stop date. */ 33 | @JsonProperty("to") 34 | private final String to; 35 | 36 | /** The date pattern */ 37 | @JsonProperty("pattern") 38 | private String pattern; 39 | 40 | /** 41 | * Returns a new {@link DateRangeMapperBuilder}. 42 | * 43 | * @param from the column containing the start date 44 | * @param to the column containing the stop date 45 | */ 46 | public DateRangeMapperBuilder(@JsonProperty("from") String from, @JsonProperty("to") String to) { 47 | this.from = from; 48 | this.to = to; 49 | } 50 | 51 | /** 52 | * Sets the date pattern to be used both for columns and fields. 53 | * 54 | * @param pattern a {@link java.text.SimpleDateFormat} date pattern 55 | * @return this 56 | */ 57 | public DateRangeMapperBuilder pattern(String pattern) { 58 | this.pattern = pattern; 59 | return this; 60 | } 61 | 62 | /** 63 | * Returns the {@link DateRangeMapper} represented by this {@link MapperBuilder}. 64 | * 65 | * @param field the name of the field to be built 66 | * @return the {@link DateRangeMapper} represented by this 67 | */ 68 | @Override 69 | public DateRangeMapper build(String field) { 70 | return new DateRangeMapper(field, validated, from, to, pattern); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/BlobMapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping; 17 | 18 | import com.stratio.cassandra.lucene.IndexException; 19 | import com.stratio.cassandra.lucene.util.ByteBufferUtils; 20 | import org.apache.cassandra.utils.Hex; 21 | 22 | import java.nio.ByteBuffer; 23 | import java.util.Arrays; 24 | 25 | /** 26 | * A {@link Mapper} to map blob values. 27 | * 28 | * @author Andres de la Pena {@literal } 29 | */ 30 | public class BlobMapper extends KeywordMapper { 31 | 32 | /** 33 | * Builds a new {@link BlobMapper}. 34 | * 35 | * @param field the name of the field 36 | * @param column the name of the column to be mapped 37 | * @param validated if the field must be validated 38 | */ 39 | public BlobMapper(String field, String column, Boolean validated) { 40 | super(field, column, validated, Arrays.asList(String.class, ByteBuffer.class)); 41 | } 42 | 43 | /** {@inheritDoc} */ 44 | @Override 45 | protected String doBase(String name, Object value) { 46 | if (value instanceof ByteBuffer) { 47 | return base((ByteBuffer) value); 48 | } else if (value instanceof byte[]) { 49 | return base((byte[]) value); 50 | } else if (value instanceof String) { 51 | return base((String) value); 52 | } 53 | throw new IndexException("Field '{}' requires a byte array, but found '{}'", field, value); 54 | } 55 | 56 | private String base(ByteBuffer value) { 57 | return ByteBufferUtils.toHex(value); 58 | } 59 | 60 | private String base(byte[] value) { 61 | return ByteBufferUtils.toHex(value); 62 | } 63 | 64 | private String base(String value) { 65 | try { 66 | byte[] bytes = Hex.hexToBytes(value.replaceFirst("0x", "")); 67 | return Hex.bytesToHex(bytes); 68 | } catch (NumberFormatException e) { 69 | throw new IndexException(e, "Field '{}' requires an hex string, but found '{}'", field, value); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/search/condition/SingleFieldCondition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.condition; 17 | 18 | import com.google.common.base.MoreObjects; 19 | import com.stratio.cassandra.lucene.IndexException; 20 | import org.apache.commons.lang3.StringUtils; 21 | 22 | import java.util.Collections; 23 | import java.util.Set; 24 | 25 | /** 26 | * The abstract base class for queries directed to a specific field which name should be specified. 27 | * 28 | * Known subclasses are:
  • {@link FuzzyCondition}
  • {@link MatchCondition}
  • {@link PhraseCondition}
  • 29 | * {@link PrefixCondition}
  • {@link RangeCondition}
  • {@link WildcardCondition}
  • {@link BitemporalCondition} 30 | *
  • {@link DateRangeCondition}
  • {@link GeoDistanceCondition}
  • {@link GeoBBoxCondition}
  • {@link 31 | * GeoShapeCondition}
32 | * 33 | * @author Andres de la Pena {@literal } 34 | */ 35 | public abstract class SingleFieldCondition extends Condition { 36 | 37 | /** The name of the field to be matched. */ 38 | public final String field; 39 | 40 | /** 41 | * Abstract {@link SingleFieldCondition} builder receiving the boost to be used. 42 | * 43 | * @param boost the boost for this query clause. Documents matching this clause will (in addition to the normal 44 | * weightings) have their score multiplied by {@code boost}. 45 | * @param field the name of the field to be matched 46 | */ 47 | public SingleFieldCondition(Float boost, String field) { 48 | super(boost); 49 | 50 | if (StringUtils.isBlank(field)) { 51 | throw new IndexException("Field name required"); 52 | } 53 | 54 | this.field = field; 55 | } 56 | 57 | /** {@inheritDoc} */ 58 | public Set postProcessingFields() { 59 | return Collections.singleton(field); 60 | } 61 | 62 | /** {@inheritDoc} */ 63 | @Override 64 | protected MoreObjects.ToStringHelper toStringHelper(Object o) { 65 | return super.toStringHelper(o).add("field", field); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /testsAT/src/test/java/com/stratio/cassandra/lucene/issues/Issue123Test.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.issues; 17 | 18 | import static com.stratio.cassandra.lucene.builder.Builder.match; 19 | 20 | import java.util.LinkedHashMap; 21 | import java.util.Map; 22 | 23 | import com.stratio.cassandra.lucene.BaseTest; 24 | import com.stratio.cassandra.lucene.util.CassandraUtils; 25 | import org.junit.jupiter.api.AfterAll; 26 | import org.junit.jupiter.api.BeforeAll; 27 | import org.junit.jupiter.api.Test; 28 | 29 | /** 30 | * @author Eduardo Alonso {@literal } 31 | */ 32 | 33 | public class Issue123Test extends BaseTest { 34 | 35 | private static CassandraUtils utils; 36 | 37 | @BeforeAll 38 | public static void before() { 39 | utils = CassandraUtils.builder("issue_123") 40 | .withPartitionKey("partition") 41 | .withClusteringKey("id") 42 | .withColumn("partition", "int") 43 | .withColumn("id", "int") 44 | .withColumn("ascii_1", "ascii") 45 | .withColumn("bigint_1", "bigint") 46 | .withColumn("blob_1", "blob") 47 | .build() 48 | .createKeyspace() 49 | .createTable() 50 | .createIndex(); 51 | for (Integer p = 0; p < 2; p++) { 52 | for (Integer i = 1; i <= 100; i++) { 53 | Map data = new LinkedHashMap<>(); 54 | data.put("partition", p.toString()); 55 | data.put("id", i.toString()); 56 | data.put("ascii_1", "'ascii_bis'"); 57 | data.put("bigint_1", "3000000000000000"); 58 | data.put("blob_1", "0x3E0A15"); 59 | utils.insert(data); 60 | } 61 | } 62 | utils.refresh(); 63 | } 64 | 65 | @Test 66 | public void testQueryWithFetchSizeToIntegerMaxValue() { 67 | utils.query(match("partition", 0)).fetchSize(Integer.MAX_VALUE).check(100); 68 | } 69 | 70 | @AfterAll 71 | public static void after() { 72 | CassandraUtils.dropKeyspaceIfNotNull(utils); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/search/condition/PrefixCondition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.condition; 17 | 18 | import com.google.common.base.MoreObjects; 19 | import com.stratio.cassandra.lucene.IndexException; 20 | import com.stratio.cassandra.lucene.schema.mapping.SingleColumnMapper; 21 | import org.apache.lucene.analysis.Analyzer; 22 | import org.apache.lucene.index.Term; 23 | import org.apache.lucene.search.PrefixQuery; 24 | import org.apache.lucene.search.Query; 25 | 26 | /** 27 | * A {@link Condition} implementation that matches documents containing terms with a specified prefix. 28 | * 29 | * @author Andres de la Pena {@literal } 30 | */ 31 | public class PrefixCondition extends SingleColumnCondition { 32 | 33 | /** The field prefix to be matched. */ 34 | public final String value; 35 | 36 | /** 37 | * Constructor using the field name and the value to be matched. 38 | * 39 | * @param boost the boost for this query clause. Documents matching this clause will (in addition to the normal 40 | * weightings) have their score multiplied by {@code boost}. 41 | * @param field the name of the field to be matched 42 | * @param value the field prefix to be matched 43 | */ 44 | public PrefixCondition(Float boost, String field, String value) { 45 | super(boost, field); 46 | if (value == null) { 47 | throw new IndexException("Field value required"); 48 | } 49 | this.value = value; 50 | } 51 | 52 | /** {@inheritDoc} */ 53 | @Override 54 | public Query doQuery(SingleColumnMapper mapper, Analyzer analyzer) { 55 | if (mapper.base == String.class) { 56 | Term term = new Term(field, value); 57 | return new PrefixQuery(term); 58 | } else { 59 | throw new IndexException("Prefix queries are not supported by mapper '{}'", mapper); 60 | } 61 | } 62 | 63 | /** {@inheritDoc} */ 64 | @Override 65 | public MoreObjects.ToStringHelper toStringHelper() { 66 | return toStringHelper(this).add("value", value); 67 | } 68 | } -------------------------------------------------------------------------------- /testsAT/src/test/java/com/stratio/cassandra/lucene/util/CassandraUtilsUpdate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.util; 17 | 18 | import com.datastax.driver.core.querybuilder.Clause; 19 | import com.datastax.driver.core.querybuilder.QueryBuilder; 20 | import com.datastax.driver.core.querybuilder.Update; 21 | import com.stratio.cassandra.lucene.builder.search.condition.Condition; 22 | import com.stratio.cassandra.lucene.builder.search.sort.SortField; 23 | 24 | /** 25 | * @author Andres de la Pena {@literal } 26 | */ 27 | public class CassandraUtilsUpdate { 28 | 29 | private CassandraUtils parent; 30 | private Update update; 31 | 32 | public CassandraUtilsUpdate(CassandraUtils parent) { 33 | this.parent = parent; 34 | update = QueryBuilder.update(parent.getKeyspace(), parent.getTable()); 35 | } 36 | 37 | public CassandraUtilsUpdate set(String name, Object value) { 38 | update.with(QueryBuilder.set(name, value)); 39 | return this; 40 | } 41 | 42 | public CassandraUtilsUpdate where(String name, Object value) { 43 | update.where(QueryBuilder.eq(name, value)); 44 | return this; 45 | } 46 | 47 | public CassandraUtilsUpdate and(String name, Object value) { 48 | update.where().and(QueryBuilder.eq(name, value)); 49 | return this; 50 | } 51 | 52 | public Update.Conditions onlyIf(Clause condition) { 53 | return update.onlyIf(condition); 54 | } 55 | 56 | private CassandraUtils execute() { 57 | parent.execute(update); 58 | return parent; 59 | } 60 | 61 | public Update asUpdate() { 62 | return update; 63 | } 64 | 65 | public CassandraUtilsSelect query(Condition query) { 66 | return execute().query(query); 67 | } 68 | 69 | public CassandraUtilsSelect filter(Condition filter) { 70 | return execute().filter(filter); 71 | } 72 | 73 | public CassandraUtilsSelect sort(SortField... sort) { 74 | return execute().sort(sort); 75 | } 76 | 77 | public CassandraUtils refresh() { 78 | return execute().commit().refresh(); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/TextMapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping; 17 | 18 | import com.stratio.cassandra.lucene.IndexException; 19 | import org.apache.lucene.document.Field; 20 | import org.apache.lucene.document.TextField; 21 | import org.apache.lucene.search.SortField; 22 | 23 | import java.util.Optional; 24 | 25 | /** 26 | * A {@link Mapper} to map a string, tokenized field. 27 | * 28 | * @author Andres de la Pena {@literal } 29 | */ 30 | public class TextMapper extends SingleColumnMapper.SingleFieldMapper { 31 | 32 | /** 33 | * Builds a new {@link TextMapper} using the specified Lucene {@link org.apache.lucene.analysis.Analyzer}. 34 | * 35 | * @param field the name of the field 36 | * @param column the name of the column to be mapped 37 | * @param validated if the field must be validated 38 | * @param analyzer the name of the Lucene {@link org.apache.lucene.analysis.Analyzer} to be used 39 | */ 40 | public TextMapper(String field, String column, Boolean validated, String analyzer) { 41 | super(field, column, false, validated, analyzer, String.class, PRINTABLE_TYPES); 42 | } 43 | 44 | /** {@inheritDoc} */ 45 | @Override 46 | protected String doBase(String name, Object value) { 47 | return value.toString(); 48 | } 49 | 50 | /** {@inheritDoc} */ 51 | @Override 52 | public Optional indexedField(String name, String value) { 53 | return Optional.of(new TextField(name, value, STORE)); 54 | } 55 | 56 | /** {@inheritDoc} */ 57 | @Override 58 | public Optional sortedField(String name, String value) { 59 | return Optional.empty(); 60 | } 61 | 62 | /** {@inheritDoc} */ 63 | @Override 64 | public SortField sortField(String name, boolean reverse) { 65 | throw new IndexException("Text mapper '{}' does not support sorting because it is analyzed", name); 66 | } 67 | 68 | /** {@inheritDoc} */ 69 | @Override 70 | public String toString() { 71 | return toStringHelper(this).add("analyzer", analyzer).toString(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /plugin/src/main/scala/com/stratio/cassandra/lucene/index/RAMIndex.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.index 17 | 18 | import org.apache.lucene.analysis.Analyzer 19 | import org.apache.lucene.document.Document 20 | import org.apache.lucene.index.{DirectoryReader, IndexWriter, IndexWriterConfig} 21 | import org.apache.lucene.search.{IndexSearcher, Query, ScoreDoc, Sort} 22 | import org.apache.lucene.store.RAMDirectory 23 | 24 | /** Class wrapping a Lucene RAM directory and its readers, writers and searchers for NRT. 25 | * 26 | * @param analyzer the index writer analyzer 27 | * @author Andres de la Pena `adelapena@stratio.com` 28 | */ 29 | class RAMIndex(analyzer: Analyzer) { 30 | 31 | private val directory = new RAMDirectory 32 | private val indexWriter = new IndexWriter(directory, new IndexWriterConfig(analyzer)) 33 | 34 | /** Adds the specified document. 35 | * 36 | * @param document the document to be added 37 | */ 38 | def add(document: Document) { 39 | indexWriter.addDocument(document) 40 | } 41 | 42 | /** Commits all pending changes to the index, waits for pending merges to complete, and closes all 43 | * associated resources. 44 | */ 45 | def close() { 46 | indexWriter.close() 47 | directory.close() 48 | } 49 | 50 | /** Finds the top count hits for a query and a sort. 51 | * 52 | * @param query the query to search for 53 | * @param sort the sort to be applied 54 | * @param count the max number of results to be collected 55 | * @param fields the names of the fields to be loaded 56 | * @return the found documents 57 | */ 58 | def search( 59 | query: Query, 60 | sort: Sort, 61 | count: Integer, 62 | fields: java.util.Set[String]): Seq[(Document, ScoreDoc)] = { 63 | indexWriter.commit() 64 | val reader = DirectoryReader.open(directory) 65 | val searcher = new IndexSearcher(reader) 66 | try { 67 | val newSort = sort.rewrite(searcher) 68 | val topDocs = searcher.search(query, count, newSort, true, true) 69 | topDocs.scoreDocs.map(score => (searcher.doc(score.doc, fields), score)) 70 | } finally searcher.getIndexReader.close() 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /plugin/src/test/scala/com/stratio/cassandra/lucene/util/BytBufferUtilsTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.util 17 | 18 | import java.nio.ByteBuffer 19 | 20 | import com.stratio.cassandra.lucene.BaseScalaTest 21 | import com.stratio.cassandra.lucene.BaseScalaTest._ 22 | import org.apache.cassandra.db.marshal.CompositeType 23 | import org.apache.cassandra.utils.ByteBufferUtil 24 | 25 | /** Class for testing [[ByteBufferUtils]]. 26 | * 27 | * @author Andres de la Pena `adelapena@stratio.com` 28 | */ 29 | class BytBufferUtilsTest extends BaseScalaTest { 30 | 31 | test("test BytesRef") { 32 | val t = CompositeType.getInstance(utf8, int32) 33 | val in = t.decompose(utf8.decompose("monkey"), int32.decompose(1)) 34 | val bytesRef = ByteBufferUtils.bytesRef(in) 35 | val out1 = ByteBufferUtils.byteBuffer(bytesRef) 36 | ByteBufferUtil.compareUnsigned(in, out1) shouldBe 0 37 | val out2 = ByteBufferUtils.byteBuffer(bytesRef) 38 | ByteBufferUtil.compareUnsigned(in, out2) shouldBe 0 39 | } 40 | 41 | test("isEmpty true") { 42 | val bb = ByteBuffer.allocate(0) 43 | ByteBufferUtils.isEmpty(bb) shouldBe true 44 | } 45 | 46 | test("isEmpty false") { 47 | val bb = ByteBuffer.allocate(10) 48 | ByteBufferUtils.isEmpty(bb) shouldBe false 49 | } 50 | 51 | test("split simple") { 52 | val bb = utf8.decompose("test") 53 | ByteBufferUtils.split(bb, utf8).length shouldBe 1 54 | } 55 | 56 | test("split composite") { 57 | val t = CompositeType.getInstance(utf8, int32) 58 | val bb = t.decompose(utf8.decompose("1"), int32.decompose(1)) 59 | ByteBufferUtils.split(bb, t).length shouldBe 2 60 | } 61 | 62 | test("compose-decompose") { 63 | val bbs = ByteBufferUtils.decompose( 64 | ByteBufferUtils.compose( 65 | utf8.decompose("test"), 66 | int32.decompose(999), 67 | boolean.decompose(true))) 68 | bbs.length shouldBe 3 69 | utf8.compose(bbs(0)) shouldBe "test" 70 | int32.compose(bbs(1)) shouldBe 999 71 | boolean.compose(bbs(2)) shouldBe true 72 | } 73 | 74 | test("compose-decompose empty") { 75 | ByteBufferUtils.decompose(ByteBufferUtils.compose()).length shouldBe 0 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /testsAT/src/test/java/com/stratio/cassandra/lucene/util/CassandraUtilsDelete.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.util; 17 | 18 | import com.datastax.driver.core.querybuilder.Delete; 19 | import com.datastax.driver.core.querybuilder.QueryBuilder; 20 | import com.stratio.cassandra.lucene.builder.search.condition.Condition; 21 | import com.stratio.cassandra.lucene.builder.search.sort.SortField; 22 | 23 | /** 24 | * @author Andres de la Pena {@literal } 25 | */ 26 | public class CassandraUtilsDelete { 27 | 28 | private final CassandraUtils parent; 29 | private final Delete.Where where; 30 | private CassandraUtilsDelete carry; 31 | 32 | public CassandraUtilsDelete(CassandraUtils parent, String... columns) { 33 | this(parent, null, columns); 34 | } 35 | 36 | public CassandraUtilsDelete(CassandraUtils parent, CassandraUtilsDelete carry, String... columns) { 37 | this.parent = parent; 38 | this.carry = carry; 39 | where = QueryBuilder.delete(columns).from(parent.getKeyspace(), parent.getTable()).where(); 40 | } 41 | 42 | public CassandraUtilsDelete where(String name, Object value) { 43 | where.and(QueryBuilder.eq(name, value)); 44 | return this; 45 | } 46 | 47 | public CassandraUtilsDelete and(String name, Object value) { 48 | return where(name, value); 49 | } 50 | 51 | public CassandraUtilsDelete delete(String... columns) { 52 | return new CassandraUtilsDelete(parent, this, columns); 53 | } 54 | 55 | private CassandraUtils execute() { 56 | if (carry != null) { 57 | carry.execute(); 58 | } 59 | parent.execute(where); 60 | return parent; 61 | } 62 | 63 | public CassandraUtilsSelect query(Condition query) { 64 | return execute().query(query); 65 | } 66 | 67 | public CassandraUtilsSelect filter(Condition filter) { 68 | return execute().filter(filter); 69 | } 70 | 71 | public CassandraUtilsSelect sort(SortField... sort) { 72 | return execute().sort(sort); 73 | } 74 | 75 | public CassandraUtils refresh() { 76 | return execute().commit().refresh(); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/search/condition/builder/PhraseConditionBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.condition.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonCreator; 19 | import com.fasterxml.jackson.annotation.JsonProperty; 20 | import com.stratio.cassandra.lucene.search.condition.PhraseCondition; 21 | 22 | /** 23 | * {@link ConditionBuilder} for building a new {@link PhraseCondition}. 24 | * 25 | * @author Andres de la Pena {@literal } 26 | */ 27 | public class PhraseConditionBuilder extends ConditionBuilder { 28 | 29 | /** The name of the field to be matched. */ 30 | @JsonProperty("field") 31 | private final String field; 32 | 33 | /** The phrase terms to be matched. */ 34 | @JsonProperty("value") 35 | private final String value; 36 | 37 | /** The number of other words permitted between words in phrase. */ 38 | @JsonProperty("slop") 39 | private Integer slop; 40 | 41 | /** 42 | * Returns a new {@link PhraseConditionBuilder} with the specified field name and values to be matched. 43 | * 44 | * @param field the name of the field to be matched 45 | * @param value the phrase terms to be matched 46 | */ 47 | @JsonCreator 48 | public PhraseConditionBuilder(@JsonProperty("field") String field, @JsonProperty("value") String value) { 49 | this.field = field; 50 | this.value = value; 51 | } 52 | 53 | /** 54 | * Returns this builder with the specified slop. Slop is the number of other words permitted between words in 55 | * phrase. 56 | * 57 | * @param slop the number of other words permitted between words in phrase to set 58 | * @return this with the specified slop 59 | */ 60 | public PhraseConditionBuilder slop(Integer slop) { 61 | this.slop = slop; 62 | return this; 63 | } 64 | 65 | /** 66 | * Returns the {@link PhraseCondition} represented by this builder. 67 | * 68 | * @return a new phrase condition 69 | */ 70 | @Override 71 | public PhraseCondition build() { 72 | return new PhraseCondition(boost, field, value, slop); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /testsAT/src/test/java/com/stratio/cassandra/lucene/util/monitoring/CassandraJolokiaClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.util.monitoring; 17 | 18 | import javax.management.MalformedObjectNameException; 19 | 20 | import org.jolokia.client.J4pClient; 21 | import org.jolokia.client.exception.J4pException; 22 | import org.jolokia.client.request.J4pExecRequest; 23 | import org.jolokia.client.request.J4pReadRequest; 24 | import org.jolokia.client.request.J4pReadResponse; 25 | 26 | /** 27 | * @author Eduardo Alonso {@literal } 28 | */ 29 | public class CassandraJolokiaClient implements CassandraMonitoringClient { 30 | 31 | private J4pClient j4pClient; 32 | private String service; 33 | 34 | public CassandraJolokiaClient(String service) { 35 | this.service = service; 36 | } 37 | 38 | /** {@inheritDoc} */ 39 | @Override 40 | public CassandraJolokiaClient connect() { 41 | j4pClient = new J4pClient("http://" + service + "/jolokia"); 42 | return this; 43 | } 44 | 45 | /** {@inheritDoc} */ 46 | @Override 47 | public void disconnect() { 48 | this.j4pClient = null; 49 | } 50 | 51 | /** {@inheritDoc} */ 52 | @Override 53 | public Object read(String bean, String attribute) { 54 | J4pReadRequest req; 55 | try { 56 | req = new J4pReadRequest(bean, attribute); 57 | } catch (MalformedObjectNameException e) { 58 | throw new RuntimeException("MalformedObjectNameException: " + e.getMessage()); 59 | } 60 | try { 61 | J4pReadResponse response = j4pClient.execute(req); 62 | return response.getValue(); 63 | } catch (J4pException e) { 64 | throw new RuntimeException("J4pException: " + e.getMessage(), e); 65 | } 66 | } 67 | 68 | /** {@inheritDoc} */ 69 | @Override 70 | public void invoke(String bean, String operation, Object[] params, String[] signature) { 71 | try { 72 | J4pExecRequest exec = new J4pExecRequest(bean, operation, params); 73 | j4pClient.execute(exec); 74 | } catch (Exception e) { 75 | throw new RuntimeException(e.getLocalizedMessage(), e); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/common/GeospatialUtilsJTS.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.common; 17 | 18 | import com.spatial4j.core.context.jts.JtsSpatialContext; 19 | import com.spatial4j.core.shape.jts.JtsGeometry; 20 | import com.stratio.cassandra.lucene.IndexException; 21 | import com.vividsolutions.jts.geom.Geometry; 22 | import com.vividsolutions.jts.geom.GeometryFactory; 23 | import com.vividsolutions.jts.io.ParseException; 24 | import com.vividsolutions.jts.io.WKTReader; 25 | import org.apache.commons.lang3.StringUtils; 26 | 27 | /** 28 | * Utilities for Java Topology Suite (JTS) related stuff. 29 | * 30 | * This class depends on Java Topology Suite (JTS). This library can't 31 | * be distributed together with this project due to license compatibility problems, but you can add it by putting jts-core-1.14.0.jar 33 | * into Cassandra lib directory. 34 | * 35 | * @author Andres de la Pena {@literal } 36 | */ 37 | public class GeospatialUtilsJTS extends GeospatialUtils { 38 | 39 | /** The spatial context to be used. */ 40 | public static final JtsSpatialContext CONTEXT = JtsSpatialContext.GEO; 41 | 42 | /** 43 | * Returns the {@link JtsGeometry} represented by the specified WKT text. 44 | * 45 | * @param string the WKT text 46 | * @return the parsed geometry 47 | */ 48 | public static JtsGeometry geometry(String string) { 49 | if (StringUtils.isBlank(string)) { 50 | throw new IndexException("Shape shouldn't be blank"); 51 | } 52 | try { 53 | GeometryFactory geometryFactory = CONTEXT.getGeometryFactory(); 54 | WKTReader reader = new WKTReader(geometryFactory); 55 | Geometry geometry = reader.read(string); 56 | if (!geometry.isValid()) { 57 | geometry = geometry.buffer(0); 58 | } 59 | return CONTEXT.makeShape(geometry); 60 | } catch (ParseException | IllegalArgumentException e) { 61 | throw new IndexException(e, "Shape '{}' is not parseable", string); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/search/condition/builder/MatchConditionBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.condition.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonCreator; 19 | import com.fasterxml.jackson.annotation.JsonProperty; 20 | import com.stratio.cassandra.lucene.search.condition.MatchCondition; 21 | 22 | /** 23 | * {@link ConditionBuilder} for building a new {@link MatchCondition}. 24 | * 25 | * @author Andres de la Pena {@literal } 26 | */ 27 | public class MatchConditionBuilder extends ConditionBuilder { 28 | 29 | /** The name of the field to be matched. */ 30 | @JsonProperty("field") 31 | private final String field; 32 | 33 | /** The value of the field to be matched. */ 34 | @JsonProperty("value") 35 | private final Object value; 36 | 37 | /** If the generated query should use doc values. */ 38 | @JsonProperty("doc_values") 39 | private Boolean docValues; 40 | 41 | /** 42 | * Creates a new {@link MatchConditionBuilder} for the specified field and value. 43 | * 44 | * @param field the name of the field to be matched 45 | * @param value the value of the field to be matched 46 | */ 47 | @JsonCreator 48 | public MatchConditionBuilder(@JsonProperty("field") String field, @JsonProperty("value") Object value) { 49 | this.field = field; 50 | this.value = value; 51 | } 52 | 53 | /** 54 | * Sets if the generated query should use doc values. Doc values queries are typically slower, but they can be 55 | * faster in the dense case where most rows match the search. 56 | * 57 | * @param docValues if the generated query should use doc values 58 | * @return this builder with the specified use doc values option. 59 | */ 60 | public MatchConditionBuilder docValues(Boolean docValues) { 61 | this.docValues = docValues; 62 | return this; 63 | } 64 | 65 | /** 66 | * Returns the {@link MatchCondition} represented by this builder. 67 | * 68 | * @return a new match condition 69 | */ 70 | @Override 71 | public MatchCondition build() { 72 | return new MatchCondition(boost, field, value, docValues); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/builder/GeoPointMapperBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonCreator; 19 | import com.fasterxml.jackson.annotation.JsonProperty; 20 | import com.stratio.cassandra.lucene.schema.mapping.GeoPointMapper; 21 | 22 | /** 23 | * {@link MapperBuilder} to build a new {@link GeoPointMapper}. 24 | * 25 | * @author Andres de la Pena {@literal } 26 | */ 27 | public class GeoPointMapperBuilder extends MapperBuilder { 28 | 29 | /** The name of the column containing the latitude. */ 30 | @JsonProperty("latitude") 31 | private final String latitude; 32 | 33 | /** The name of the column containing the longitude. */ 34 | @JsonProperty("longitude") 35 | private final String longitude; 36 | 37 | /** The maximum number of levels in the tree. */ 38 | @JsonProperty("max_levels") 39 | private Integer maxLevels; 40 | 41 | /** 42 | * Builds a new {@link GeoPointMapper}. 43 | * 44 | * @param latitude the name of the column containing the latitude 45 | * @param longitude the name of the column containing the longitude 46 | */ 47 | @JsonCreator 48 | public GeoPointMapperBuilder(@JsonProperty("latitude") String latitude, 49 | @JsonProperty("longitude") String longitude) { 50 | this.latitude = latitude; 51 | this.longitude = longitude; 52 | } 53 | 54 | /** 55 | * Sets the maximum number of levels in the tree. 56 | * 57 | * @param maxLevels the maximum number of levels in the tree 58 | * @return this 59 | */ 60 | public GeoPointMapperBuilder maxLevels(Integer maxLevels) { 61 | this.maxLevels = maxLevels; 62 | return this; 63 | } 64 | 65 | /** 66 | * Returns the {@link GeoPointMapper} represented by this {@link MapperBuilder}. 67 | * 68 | * @param field the name of the field to be built 69 | * @return the {@link GeoPointMapper} represented by this 70 | */ 71 | @Override 72 | public GeoPointMapper build(String field) { 73 | return new GeoPointMapper(field, validated, latitude, longitude, maxLevels); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /plugin/src/main/scala/com/stratio/cassandra/lucene/IndexReader.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene 17 | 18 | import com.stratio.cassandra.lucene.index.DocumentIterator 19 | import org.apache.cassandra.db._ 20 | import org.apache.cassandra.db.filter.ClusteringIndexFilter 21 | import org.apache.cassandra.db.partitions.UnfilteredPartitionIterator 22 | import org.apache.cassandra.db.rows.UnfilteredRowIterator 23 | import org.apache.cassandra.schema.TableMetadata 24 | 25 | /** [[UnfilteredPartitionIterator]] for retrieving rows from a [[DocumentIterator]]. 26 | * 27 | * @param command the read command 28 | * @param table the base table 29 | * @param controller the read execution controller 30 | * @param documents the documents iterator 31 | * @author Andres de la Pena `adelapena@stratio.com` 32 | */ 33 | abstract class IndexReader( 34 | command: ReadCommand, 35 | table: ColumnFamilyStore, 36 | controller: ReadExecutionController, 37 | documents: DocumentIterator) 38 | extends UnfilteredPartitionIterator { 39 | 40 | private lazy val metadataVal: TableMetadata = table.metadata.get() 41 | 42 | protected var nextData: Option[UnfilteredRowIterator] = None 43 | 44 | /** @inheritdoc */ 45 | override def metadata: TableMetadata = { 46 | metadataVal 47 | } 48 | 49 | /** @inheritdoc */ 50 | override def hasNext: Boolean = { 51 | prepareNext() 52 | } 53 | 54 | /** @inheritdoc */ 55 | override def next(): UnfilteredRowIterator = { 56 | if (nextData.isEmpty) prepareNext() 57 | val result = nextData.orNull 58 | nextData = None 59 | result 60 | } 61 | 62 | /** @inheritdoc */ 63 | override def remove() = { 64 | throw new UnsupportedOperationException 65 | } 66 | 67 | /** @inheritdoc */ 68 | override def close() = { 69 | try nextData.foreach(_.close()) finally documents.close() 70 | } 71 | 72 | protected def prepareNext(): Boolean 73 | 74 | protected def read(key: DecoratedKey, filter: ClusteringIndexFilter): UnfilteredRowIterator = { 75 | SinglePartitionReadCommand.create( 76 | metadataVal, 77 | command.nowInSec, 78 | command.columnFilter, 79 | command.rowFilter, 80 | command.limits, 81 | key, 82 | filter).queryMemtableAndDisk(table, controller) 83 | } 84 | 85 | 86 | } 87 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/search/condition/builder/ContainsConditionBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.condition.builder; 17 | 18 | import com.fasterxml.jackson.annotation.JsonCreator; 19 | import com.fasterxml.jackson.annotation.JsonProperty; 20 | import com.stratio.cassandra.lucene.search.condition.ContainsCondition; 21 | 22 | /** 23 | * {@link ConditionBuilder} for building a new {@link ContainsCondition}. 24 | * 25 | * @author Andres de la Pena {@literal } 26 | */ 27 | public class ContainsConditionBuilder extends ConditionBuilder { 28 | 29 | /** The name of the field to be matched. */ 30 | @JsonProperty("field") 31 | private final String field; 32 | 33 | /** The value of the field to be matched. */ 34 | @JsonProperty("values") 35 | private final Object[] values; 36 | 37 | /** If the generated query should use doc values. */ 38 | @JsonProperty("doc_values") 39 | private Boolean docValues; 40 | 41 | /** 42 | * Creates a new {@link ContainsConditionBuilder} for the specified field and value. 43 | * 44 | * @param field the name of the field to be matched 45 | * @param values the values of the field to be matched 46 | */ 47 | @JsonCreator 48 | public ContainsConditionBuilder(@JsonProperty("field") String field, @JsonProperty("values") Object... values) { 49 | this.field = field; 50 | this.values = values; 51 | } 52 | 53 | /** 54 | * Sets if the generated query should use doc values. Doc values queries are typically slower, but they can be 55 | * faster in the dense case where most rows match the search. 56 | * 57 | * @param docValues if the generated query should use doc values 58 | * @return this builder with the specified use doc values option. 59 | */ 60 | public ContainsConditionBuilder docValues(Boolean docValues) { 61 | this.docValues = docValues; 62 | return this; 63 | } 64 | 65 | /** 66 | * Returns the {@link ContainsCondition} represented by this builder. 67 | * 68 | * @return a new contains condition 69 | */ 70 | @Override 71 | public ContainsCondition build() { 72 | return new ContainsCondition(boost, field, docValues, values); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /testsAT/src/test/java/com/stratio/cassandra/lucene/varia/InOperatorWithSkinnyRowsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.varia; 17 | 18 | import static com.stratio.cassandra.lucene.builder.Builder.all; 19 | import static com.stratio.cassandra.lucene.builder.Builder.field; 20 | import static com.stratio.cassandra.lucene.builder.Builder.integerMapper; 21 | 22 | import com.stratio.cassandra.lucene.BaseTest; 23 | import com.stratio.cassandra.lucene.util.CassandraUtils; 24 | import org.junit.jupiter.api.AfterAll; 25 | import org.junit.jupiter.api.BeforeAll; 26 | import org.junit.jupiter.api.Test; 27 | 28 | /** 29 | * @author Andres de la Pena 30 | */ 31 | 32 | public class InOperatorWithSkinnyRowsTest extends BaseTest { 33 | 34 | private static final int NUM_PARTITIONS = 10; 35 | private static CassandraUtils utils; 36 | 37 | @BeforeAll 38 | public static void before() { 39 | utils = CassandraUtils.builder("in_operator_with_skinny_rows") 40 | .withUseNewQuerySyntax(true) 41 | .withPartitionKey("pk") 42 | .withColumn("pk", "int", integerMapper()) 43 | .withColumn("rc", "int", integerMapper()) 44 | .build() 45 | .createKeyspace() 46 | .createTable() 47 | .createIndex(); 48 | for (int i = 0; i < NUM_PARTITIONS; i++) { 49 | utils.insert(new String[]{"pk", "rc"}, new Object[]{i, i}); 50 | } 51 | utils.refresh(); 52 | } 53 | 54 | @AfterAll 55 | public static void after() { 56 | CassandraUtils.dropKeyspaceIfNotNull(utils); 57 | } 58 | 59 | @Test 60 | public void testPartitionKeyIn() { 61 | utils.searchAll().fetchSize(4).and("AND pk IN (0, 5, 9)").checkOrderedColumns("rc", 0, 5, 9); 62 | } 63 | 64 | @Test 65 | public void testReversedPartitionKeyIn() { 66 | utils.searchAll().fetchSize(4).and("AND pk IN (9, 5, 0)").checkOrderedColumns("rc", 0, 5, 9); 67 | } 68 | 69 | @Test 70 | public void testQueryWithIn() { 71 | utils.query(all()).fetchSize(4).and("AND pk IN (9, 5, 0)").checkOrderedColumns("rc", 5, 0, 9); 72 | } 73 | 74 | @Test 75 | public void testSortWithIn() { 76 | utils.sort(field("pk")).fetchSize(4).and("AND pk IN (9, 5, 0)").checkOrderedColumns("rc", 0, 5, 9); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/KeywordMapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping; 17 | 18 | import org.apache.lucene.document.Field; 19 | import org.apache.lucene.document.FieldType; 20 | import org.apache.lucene.document.SortedSetDocValuesField; 21 | import org.apache.lucene.index.IndexOptions; 22 | import org.apache.lucene.search.SortField; 23 | import org.apache.lucene.search.SortedSetSortField; 24 | import org.apache.lucene.util.BytesRef; 25 | 26 | import java.util.List; 27 | import java.util.Optional; 28 | 29 | /** 30 | * A {@link Mapper} to map a string, not tokenized field. 31 | * 32 | * @author Andres de la Pena {@literal } 33 | */ 34 | public abstract class KeywordMapper extends SingleColumnMapper.SingleFieldMapper { 35 | 36 | static final FieldType FIELD_TYPE = new FieldType(); 37 | 38 | static { 39 | FIELD_TYPE.setOmitNorms(true); 40 | FIELD_TYPE.setIndexOptions(IndexOptions.DOCS); 41 | FIELD_TYPE.setTokenized(true); 42 | FIELD_TYPE.freeze(); 43 | } 44 | 45 | /** 46 | * Builds a new {@link KeywordMapper}. 47 | * 48 | * @param field the name of the field 49 | * @param column the name of the column to be mapped 50 | * @param validated if the field must be validated 51 | * @param supportedTypes the supported Cassandra types 52 | */ 53 | KeywordMapper(String field, String column, Boolean validated, List> supportedTypes) { 54 | super(field, column, true, validated, KEYWORD_ANALYZER, String.class, supportedTypes); 55 | } 56 | 57 | /** {@inheritDoc} */ 58 | @Override 59 | public Optional indexedField(String name, String value) { 60 | validateTerm(name, new BytesRef(value)); 61 | return Optional.of(new Field(name, value, FIELD_TYPE)); 62 | } 63 | 64 | /** {@inheritDoc} */ 65 | @Override 66 | public Optional sortedField(String name, String value) { 67 | BytesRef bytes = new BytesRef(value); 68 | validateTerm(name, bytes); 69 | return Optional.of(new SortedSetDocValuesField(name, bytes)); 70 | } 71 | 72 | /** {@inheritDoc} */ 73 | @Override 74 | public final SortField sortField(String name, boolean reverse) { 75 | return new SortedSetSortField(name, reverse); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /testsAT/src/test/java/com/stratio/cassandra/lucene/varia/SearchMatchingManyRowsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.varia; 17 | 18 | import static com.stratio.cassandra.lucene.builder.Builder.all; 19 | import static com.stratio.cassandra.lucene.builder.Builder.field; 20 | 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | 24 | import com.stratio.cassandra.lucene.BaseTest; 25 | import com.stratio.cassandra.lucene.util.CassandraUtils; 26 | import org.junit.jupiter.api.AfterAll; 27 | import org.junit.jupiter.api.BeforeAll; 28 | import org.junit.jupiter.api.Test; 29 | 30 | /** 31 | * Test the retrieval of large volumes of rows, specially above 65535 rows. 32 | * 33 | * @author Andres de la Pena 34 | */ 35 | 36 | public class SearchMatchingManyRowsTest extends BaseTest { 37 | 38 | private static CassandraUtils utils; 39 | 40 | @BeforeAll 41 | public static void before() { 42 | utils = CassandraUtils.builder("search_matching_many_rows") 43 | .withPartitionKey("pk") 44 | .withClusteringKey("ck") 45 | .withColumn("pk", "int") 46 | .withColumn("ck", "int") 47 | .withColumn("rc", "int") 48 | .build() 49 | .createKeyspace() 50 | .createTable() 51 | .createIndex(); 52 | 53 | int numPartitions = 666; 54 | int partitionSize = 100; 55 | String[] names = new String[]{"pk", "ck", "rc"}; 56 | for (Integer pk = 0; pk < numPartitions; pk++) { 57 | List values = new ArrayList<>(); 58 | for (Integer ck = 0; ck <= partitionSize; ck++) { 59 | values.add(new Object[]{pk, ck, pk * partitionSize + ck}); 60 | } 61 | utils.insert(names, values); 62 | } 63 | utils.refresh(); 64 | } 65 | 66 | @AfterAll 67 | public static void after() { 68 | CassandraUtils.dropKeyspaceIfNotNull(utils); 69 | } 70 | 71 | @Test 72 | public void testQuery() { 73 | utils.query(all()).fetchSize(500).limit(65536).check(65536); 74 | } 75 | 76 | @Test 77 | public void testFilter() { 78 | utils.filter(all()).fetchSize(500).limit(65536).check(65536); 79 | } 80 | 81 | @Test 82 | public void testSort() { 83 | utils.sort(field("rc")).fetchSize(500).limit(65536).check(65536); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /plugin/src/test/java/com/stratio/cassandra/lucene/search/condition/AllConditionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.condition; 17 | 18 | import com.stratio.cassandra.lucene.schema.Schema; 19 | import com.stratio.cassandra.lucene.search.condition.builder.AllConditionBuilder; 20 | import org.apache.lucene.search.MatchAllDocsQuery; 21 | import org.apache.lucene.search.Query; 22 | import org.junit.Test; 23 | 24 | import static com.stratio.cassandra.lucene.schema.SchemaBuilders.schema; 25 | import static org.junit.Assert.*; 26 | 27 | /** 28 | * @author Andres de la Pena {@literal } 29 | */ 30 | public class AllConditionTest extends AbstractConditionTest { 31 | 32 | @Test 33 | public void testBuild() { 34 | AllConditionBuilder builder = new AllConditionBuilder().boost(0.7f); 35 | AllCondition condition = builder.build(); 36 | assertNotNull("Condition is not built", condition); 37 | assertEquals("Boost is not set", 0.7f, condition.boost, 0); 38 | } 39 | 40 | @Test 41 | public void testBuildDefaults() { 42 | AllConditionBuilder builder = new AllConditionBuilder(); 43 | AllCondition condition = builder.build(); 44 | assertNotNull("Condition is not built", condition); 45 | assertNull("Boost is not set to default", condition.boost); 46 | } 47 | 48 | @Test 49 | public void testJsonSerialization() { 50 | AllConditionBuilder builder = new AllConditionBuilder().boost(0.7); 51 | testJsonSerialization(builder, "{type:\"all\",boost:0.7}"); 52 | } 53 | 54 | @Test 55 | public void testJsonSerializationDefaults() { 56 | AllConditionBuilder builder = new AllConditionBuilder(); 57 | testJsonSerialization(builder, "{type:\"all\"}"); 58 | } 59 | 60 | @Test 61 | public void testQuery() { 62 | AllCondition condition = new AllCondition(0.7f); 63 | Schema schema = schema().build(); 64 | Query query = condition.doQuery(schema); 65 | assertNotNull("Query is not built", query); 66 | assertEquals("Query type is wrong", MatchAllDocsQuery.class, query.getClass()); 67 | } 68 | 69 | @Test 70 | public void testToString() { 71 | AllCondition condition = new AllCondition(0.7f); 72 | assertEquals("Method #toString is wrong", "AllCondition{boost=0.7}", condition.toString()); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /testsAT/src/test/java/com/stratio/cassandra/lucene/issues/Issue18Test.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.issues; 17 | 18 | import static com.stratio.cassandra.lucene.builder.Builder.match; 19 | import static com.stratio.cassandra.lucene.builder.Builder.stringMapper; 20 | 21 | import java.util.Map; 22 | 23 | import com.google.common.collect.Maps; 24 | import com.stratio.cassandra.lucene.BaseTest; 25 | import com.stratio.cassandra.lucene.util.CassandraUtils; 26 | import org.junit.jupiter.api.Test; 27 | 28 | /** 29 | * @author Andres de la Pena {@literal } 30 | */ 31 | 32 | public class Issue18Test extends BaseTest { 33 | 34 | @Test 35 | public void test() { 36 | Map data1 = Maps.newHashMap(); 37 | data1.put("idcol", "1"); 38 | data1.put("testtextcol", "'row1'"); 39 | data1.put("testmapcol", "{'attb1': 'row1attb1Val', 'attb2': 'row1attb2Val', 'attb3': 'row1attb3Val'}"); 40 | 41 | Map data2 = Maps.newHashMap(); 42 | data2.put("idcol", "2"); 43 | data2.put("testtextcol", "'someLongRow2Value'"); 44 | data2.put("testmapcol", "{'attb1': 'someLongattb1Val', 'attb2': 'attb2Val', 'attb3': 'attb3Val'}"); 45 | 46 | Map data3 = Maps.newHashMap(); 47 | data3.put("idcol", "3"); 48 | data3.put("testtextcol", "'row3'"); 49 | data3.put("testmapcol", "{'attb1': 'row2attb1Val', 'attb2': 'row2attb2Val', 'attb3': 'row2attb3Val'}"); 50 | 51 | Map data4 = Maps.newHashMap(); 52 | data4.put("idcol", "4"); 53 | data4.put("testtextcol", "'row4'"); 54 | data4.put("testmapcol", "{'attb2': 'row3attb2Val', 'attb3': 'row3attb3Val'}"); 55 | 56 | CassandraUtils.builder("issue_18") 57 | .withPartitionKey("idcol") 58 | .withColumn("idcol", "int") 59 | .withColumn("testtextcol", "text", stringMapper()) 60 | .withColumn("testmapcol", "map", stringMapper()) 61 | .build() 62 | .createKeyspace() 63 | .createTable() 64 | .createIndex() 65 | .insert(data1, data2, data3, data4) 66 | .refresh() 67 | .filter(match("testmapcol$attb1", "row1attb1Val")) 68 | .checkUnorderedColumns("idcol", 1) 69 | .dropTable() 70 | .dropKeyspace(); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /plugin/src/test/java/com/stratio/cassandra/lucene/search/condition/NoneConditionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.condition; 17 | 18 | import com.stratio.cassandra.lucene.schema.Schema; 19 | import com.stratio.cassandra.lucene.search.condition.builder.NoneConditionBuilder; 20 | import org.apache.lucene.search.BooleanQuery; 21 | import org.apache.lucene.search.Query; 22 | import org.junit.Test; 23 | 24 | import static com.stratio.cassandra.lucene.schema.SchemaBuilders.schema; 25 | import static org.junit.Assert.*; 26 | 27 | /** 28 | * @author Andres de la Pena {@literal } 29 | */ 30 | public class NoneConditionTest extends AbstractConditionTest { 31 | 32 | @Test 33 | public void testBuild() { 34 | NoneConditionBuilder builder = new NoneConditionBuilder().boost(0.7f); 35 | NoneCondition condition = builder.build(); 36 | assertNotNull("Condition is not built", condition); 37 | assertEquals("Boost is not set", 0.7f, condition.boost, 0); 38 | } 39 | 40 | @Test 41 | public void testBuildDefaults() { 42 | NoneConditionBuilder builder = new NoneConditionBuilder(); 43 | NoneCondition condition = builder.build(); 44 | assertNotNull("Condition is not built", condition); 45 | assertNull("Boost is not set to default", condition.boost); 46 | } 47 | 48 | @Test 49 | public void testJsonSerialization() { 50 | NoneConditionBuilder builder = new NoneConditionBuilder().boost(0.7); 51 | testJsonSerialization(builder, "{type:\"none\",boost:0.7}"); 52 | } 53 | 54 | @Test 55 | public void testJsonSerializationDefaults() { 56 | NoneConditionBuilder builder = new NoneConditionBuilder(); 57 | testJsonSerialization(builder, "{type:\"none\"}"); 58 | } 59 | 60 | @Test 61 | public void testQuery() { 62 | Schema schema = schema().build(); 63 | NoneCondition condition = new NoneCondition(0.7f); 64 | Query query = condition.doQuery(schema); 65 | assertNotNull("Query is not built", query); 66 | assertEquals("Query type is wrong", BooleanQuery.class, query.getClass()); 67 | } 68 | 69 | @Test 70 | public void testToString() { 71 | NoneCondition condition = new NoneCondition(0.7f); 72 | assertEquals("Method #toString is wrong", "NoneCondition{boost=0.7}", condition.toString()); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /testsAT/src/test/java/com/stratio/cassandra/lucene/issues/Issue64Test.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.issues; 17 | 18 | import static com.stratio.cassandra.lucene.builder.Builder.dateMapper; 19 | import static com.stratio.cassandra.lucene.builder.Builder.dateRange; 20 | import static com.stratio.cassandra.lucene.builder.Builder.dateRangeMapper; 21 | import static com.stratio.cassandra.lucene.builder.Builder.stringMapper; 22 | 23 | import com.stratio.cassandra.lucene.BaseTest; 24 | import com.stratio.cassandra.lucene.util.CassandraUtils; 25 | import org.junit.jupiter.api.Test; 26 | 27 | /** 28 | * @author Andres de la Pena {@literal } 29 | */ 30 | 31 | public class Issue64Test extends BaseTest { 32 | 33 | @Test 34 | public void test() { 35 | CassandraUtils.builder("issue_64").withTable("flights") 36 | .withIndexName("flights_index") 37 | .withPartitionKey("id") 38 | .withColumn("id", "uuid", null) 39 | .withColumn("arrival_aerodrome", "text", stringMapper()) 40 | .withColumn("assigned_squawk", "text", stringMapper()) 41 | .withColumn("departure_aerodrome", "text", stringMapper()) 42 | .withColumn("departure_time", 43 | "timestamp", 44 | dateMapper().pattern("yyyy-MM-dd HH:mm:ss")) 45 | .withColumn("arrival_time", 46 | "timestamp", 47 | dateMapper().pattern("yyyy-MM-dd HH:mm:ss")) 48 | .withColumn("flight_status", "text", stringMapper()) 49 | .withColumn("registration", "text", stringMapper()) 50 | .withColumn("target_address", "text", stringMapper()) 51 | .withColumn("target_identification", "text", stringMapper()) 52 | .withMapper("operation_duration", 53 | dateRangeMapper("departure_time", "arrival_time") 54 | .pattern("yyyy-MM-dd HH:mm:ss")) 55 | .build() 56 | .createKeyspace() 57 | .createTable() 58 | .createIndex() 59 | .refresh() 60 | .filter(dateRange("operation_duration").from("2014-01-01 00:00:00") 61 | .to("2014-12-31 23:59:59") 62 | .operation("intersects")) 63 | .check(0) 64 | .dropTable() 65 | .dropKeyspace(); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /plugin/src/main/scala/com/stratio/cassandra/lucene/util/SingleRowIterator.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.util 17 | 18 | import java.util.Collections 19 | 20 | import org.apache.cassandra.db.rows.{Row, RowIterator} 21 | import org.apache.cassandra.db.{DecoratedKey, RegularAndStaticColumns} 22 | import org.apache.cassandra.schema.TableMetadata 23 | 24 | /** [[RowIterator]] representing a single CQL [[Row]], gotten from the head position of the 25 | * specified [[RowIterator]]. Any other rows in the specified iterator won't be read. 26 | * 27 | * @param iterator the [[Row]] iterator 28 | * @param headRow a row to override the first row in the iterator 29 | * @param decorator a function to decorate the row 30 | * @author Andres de la Pena `adelapena@stratio.com` 31 | */ 32 | class SingleRowIterator( 33 | iterator: RowIterator, 34 | headRow: Option[Row] = None, 35 | decorator: Option[Row => Row] = None) 36 | extends RowIterator { 37 | 38 | val row = headRow.getOrElse(iterator.next) 39 | 40 | private[this] val _metadata = iterator.metadata 41 | private[this] val _partitionKey = iterator.partitionKey 42 | private[this] val _columns = iterator.columns 43 | private[this] val _staticRow = iterator.staticRow 44 | private[this] val singleIterator = Collections.singletonList(row).iterator 45 | 46 | /** Return a copy of this iterator with the specified row decorator. 47 | * 48 | * @param decorator a function to decorate the returned row 49 | * @return a new iterator with the decorator 50 | */ 51 | def decorated(decorator: Row => Row): SingleRowIterator = { 52 | new SingleRowIterator(iterator, Some(row), Option(decorator)) 53 | } 54 | 55 | /** @inheritdoc */ 56 | override def metadata: TableMetadata = _metadata 57 | 58 | /** @inheritdoc */ 59 | override def isReverseOrder: Boolean = false 60 | 61 | /** @inheritdoc */ 62 | override def columns: RegularAndStaticColumns = _columns 63 | 64 | /** @inheritdoc */ 65 | override def partitionKey: DecoratedKey = _partitionKey 66 | 67 | /** @inheritdoc */ 68 | override def staticRow: Row = _staticRow 69 | 70 | /** @inheritdoc */ 71 | override def close() {} 72 | 73 | /** @inheritdoc */ 74 | override def hasNext: Boolean = singleIterator.hasNext 75 | 76 | /** @inheritdoc */ 77 | override def next: Row = { 78 | val row = singleIterator.next 79 | decorator.map(_ apply row).getOrElse(row) 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/schema/mapping/DateMapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.schema.mapping; 17 | 18 | import com.stratio.cassandra.lucene.common.DateParser; 19 | import org.apache.lucene.document.Field; 20 | import org.apache.lucene.document.LongField; 21 | import org.apache.lucene.document.SortedNumericDocValuesField; 22 | import org.apache.lucene.search.SortField; 23 | import org.apache.lucene.search.SortField.Type; 24 | import org.apache.lucene.search.SortedNumericSortField; 25 | 26 | import java.util.Date; 27 | import java.util.Optional; 28 | 29 | /** 30 | * A {@link Mapper} to map a date field. 31 | * 32 | * @author Andres de la Pena {@literal } 33 | */ 34 | public class DateMapper extends SingleColumnMapper.SingleFieldMapper { 35 | 36 | /** The date format for parsing columns. */ 37 | public final DateParser parser; 38 | 39 | /** 40 | * Builds a new {@link DateMapper} using the specified pattern. 41 | * 42 | * @param field the name of the field 43 | * @param column the name of the column to be mapped 44 | * @param validated if the field must be validated 45 | * @param pattern the date pattern 46 | */ 47 | public DateMapper(String field, String column, Boolean validated, String pattern) { 48 | super(field, column, true, validated, null, Long.class, DATE_TYPES); 49 | this.parser = new DateParser(pattern); 50 | } 51 | 52 | /** {@inheritDoc} */ 53 | @Override 54 | protected Long doBase(String name, Object value) { 55 | Date date = parser.parse(value); 56 | return date == null ? null : date.getTime(); 57 | } 58 | 59 | /** {@inheritDoc} */ 60 | @Override 61 | public Optional indexedField(String name, Long value) { 62 | return Optional.of(new LongField(name, value, STORE)); 63 | } 64 | 65 | /** {@inheritDoc} */ 66 | @Override 67 | public Optional sortedField(String name, Long value) { 68 | return Optional.of(new SortedNumericDocValuesField(name, value)); 69 | } 70 | 71 | /** {@inheritDoc} */ 72 | @Override 73 | public SortField sortField(String name, boolean reverse) { 74 | return new SortedNumericSortField(name, Type.LONG, reverse); 75 | } 76 | 77 | /** {@inheritDoc} */ 78 | @Override 79 | public String toString() { 80 | return toStringHelper(this).add("pattern", parser).toString(); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /testsAT/src/test/java/com/stratio/cassandra/lucene/util/monitoring/CassandraJMXClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.util.monitoring; 17 | 18 | import javax.management.ObjectName; 19 | import javax.management.remote.JMXConnector; 20 | import javax.management.remote.JMXConnectorFactory; 21 | import javax.management.remote.JMXServiceURL; 22 | import java.io.IOException; 23 | import java.net.MalformedURLException; 24 | 25 | /** 26 | * @author Eduardo Alonso {@literal } 27 | */ 28 | public class CassandraJMXClient implements CassandraMonitoringClient { 29 | 30 | private JMXConnector jmxc; 31 | private JMXServiceURL url; 32 | 33 | public CassandraJMXClient(String service) { 34 | try { 35 | url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + service + "/jmxrmi"); 36 | } catch (MalformedURLException e) { 37 | throw new RuntimeException("Error while creating JMX client", e); 38 | } 39 | } 40 | 41 | /** {@inheritDoc} */ 42 | @Override 43 | public CassandraJMXClient connect() { 44 | try { 45 | jmxc = JMXConnectorFactory.connect(url, null); 46 | } catch (IOException e) { 47 | throw new RuntimeException("Error while connecting JMX client", e); 48 | } 49 | return this; 50 | } 51 | 52 | /** {@inheritDoc} */ 53 | @Override 54 | public void disconnect() { 55 | try { 56 | jmxc.close(); 57 | jmxc = null; 58 | } catch (IOException e) { 59 | throw new RuntimeException("Error while disconnecting JMX client", e); 60 | } 61 | } 62 | 63 | /** {@inheritDoc} */ 64 | @Override 65 | public void invoke(String bean, String operation, Object[] params, String[] signature) { 66 | try { 67 | jmxc.getMBeanServerConnection().invoke(new ObjectName(bean), operation, params, signature); 68 | } catch (Exception e) { 69 | throw new RuntimeException(e.getLocalizedMessage(), e); 70 | } 71 | } 72 | 73 | /** {@inheritDoc} */ 74 | @Override 75 | public Object read(String bean, String attribute) { 76 | try { 77 | ObjectName name = new ObjectName(bean); 78 | return jmxc.getMBeanServerConnection().getAttribute(name, attribute); 79 | } catch (Exception e) { 80 | throw new RuntimeException(e.getLocalizedMessage(), e); 81 | } 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /plugin/src/main/java/com/stratio/cassandra/lucene/search/condition/RegexpCondition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Stratio (http://stratio.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.stratio.cassandra.lucene.search.condition; 17 | 18 | import com.google.common.base.MoreObjects; 19 | import com.stratio.cassandra.lucene.IndexException; 20 | import com.stratio.cassandra.lucene.schema.mapping.SingleColumnMapper; 21 | import org.apache.lucene.analysis.Analyzer; 22 | import org.apache.lucene.index.Term; 23 | import org.apache.lucene.search.Query; 24 | import org.apache.lucene.search.RegexpQuery; 25 | 26 | /** 27 | * Implements the wildcard search query. Supported wildcards are {@code *}, which matches any character sequence 28 | * (including the empty one), and {@code ?}, which matches any single character. '\' is the escape character. 29 | * 30 | * Note this query can be slow, as it needs to iterate over many terms. In order to prevent extremely slow 31 | * WildcardQueries, a Wildcard term should not start with the wildcard {@code *}. 32 | * 33 | * @author Andres de la Pena {@literal } 34 | */ 35 | public class RegexpCondition extends SingleColumnCondition { 36 | 37 | /** The wildcard expression to be matched. */ 38 | public final String value; 39 | 40 | /** 41 | * Constructor using the field name and the value to be matched. 42 | * 43 | * @param boost The boost for this query clause. Documents matching this clause will (in addition to the normal 44 | * weightings) have their score multiplied by {@code boost}. 45 | * @param field the name of the field to be matched 46 | * @param value the wildcard expression to be matched 47 | */ 48 | public RegexpCondition(Float boost, String field, String value) { 49 | super(boost, field); 50 | if (value == null) { 51 | throw new IndexException("Field value required"); 52 | } 53 | this.value = value; 54 | } 55 | 56 | /** {@inheritDoc} */ 57 | @Override 58 | public Query doQuery(SingleColumnMapper mapper, Analyzer analyzer) { 59 | if (mapper.base == String.class) { 60 | Term term = new Term(field, value); 61 | return new RegexpQuery(term); 62 | } else { 63 | throw new IndexException("Regexp queries are not supported by mapper '{}'", mapper); 64 | } 65 | } 66 | 67 | /** {@inheritDoc} */ 68 | @Override 69 | public MoreObjects.ToStringHelper toStringHelper() { 70 | return toStringHelper(this).add("value", value); 71 | } 72 | } 73 | --------------------------------------------------------------------------------