-
Notifications
You must be signed in to change notification settings - Fork 865
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
f91979f
commit cfeb55f
Showing
17 changed files
with
593 additions
and
6 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
core/src/main/java/com/datastax/oss/driver/api/core/data/CqlVector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright DataStax, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.datastax.oss.driver.api.core.data; | ||
|
||
import com.datastax.oss.driver.shaded.guava.common.base.Joiner; | ||
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList; | ||
import com.datastax.oss.driver.shaded.guava.common.collect.Iterators; | ||
import java.util.Arrays; | ||
|
||
/** An n-dimensional vector defined in CQL */ | ||
public class CqlVector<T> { | ||
|
||
private final ImmutableList<T> values; | ||
|
||
private CqlVector(ImmutableList<T> values) { | ||
this.values = values; | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
public Iterable<T> getValues() { | ||
return values; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (o == this) { | ||
return true; | ||
} else if (o instanceof CqlVector) { | ||
CqlVector that = (CqlVector) o; | ||
return this.values.equals(that.values); | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Arrays.hashCode(values.toArray()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
|
||
String contents = Joiner.on(", ").join(this.values); | ||
return "CqlVector{" + contents + '}'; | ||
} | ||
|
||
public static class Builder<T> { | ||
|
||
private ImmutableList.Builder<T> listBuilder; | ||
|
||
private Builder() { | ||
listBuilder = new ImmutableList.Builder<T>(); | ||
} | ||
|
||
public Builder add(T element) { | ||
listBuilder.add(element); | ||
return this; | ||
} | ||
|
||
public Builder add(T... elements) { | ||
listBuilder.addAll(Iterators.forArray(elements)); | ||
return this; | ||
} | ||
|
||
public Builder addAll(Iterable<T> iter) { | ||
listBuilder.addAll(iter); | ||
return this; | ||
} | ||
|
||
public CqlVector<T> build() { | ||
return new CqlVector<T>(listBuilder.build()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
core/src/main/java/com/datastax/oss/driver/api/core/type/CqlVectorType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright DataStax, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.datastax.oss.driver.api.core.type; | ||
|
||
import com.datastax.oss.driver.api.core.detach.AttachmentPoint; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import java.util.Objects; | ||
|
||
public class CqlVectorType implements CustomType { | ||
|
||
public static final String CQLVECTOR_CLASS_NAME = "org.apache.cassandra.db.marshal.VectorType"; | ||
|
||
private final DataType subtype; | ||
private final int dimensions; | ||
|
||
public CqlVectorType(DataType subtype, int dimensions) { | ||
|
||
this.dimensions = dimensions; | ||
this.subtype = subtype; | ||
} | ||
|
||
public int getDimensions() { | ||
return this.dimensions; | ||
} | ||
|
||
public DataType getSubtype() { | ||
return this.subtype; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public String getClassName() { | ||
return CQLVECTOR_CLASS_NAME; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public String asCql(boolean includeFrozen, boolean pretty) { | ||
return String.format("'%s(%d)'", getClassName(), getDimensions()); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (o == this) { | ||
return true; | ||
} else if (o instanceof CqlVectorType) { | ||
CqlVectorType that = (CqlVectorType) o; | ||
return that.subtype.equals(this.subtype) && that.dimensions == this.dimensions; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(super.hashCode(), subtype, dimensions); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("CqlVector(%s, %d)", getSubtype(), getDimensions()); | ||
} | ||
|
||
@Override | ||
public boolean isDetached() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void attach(@NonNull AttachmentPoint attachmentPoint) { | ||
// nothing to do | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.