Skip to content

Commit 87cbb37

Browse files
adutraolim7t
authored andcommitted
JAVA-1347: Add support for duration type (apache#776)
1 parent 2f8073f commit 87cbb37

File tree

11 files changed

+1215
-14
lines changed

11 files changed

+1215
-14
lines changed

changelog/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## Changelog
22

3+
### 3.2.0 (in progress)
4+
5+
- [new feature] JAVA-1347: Add support for duration type.
6+
7+
38
### 3.1.3
49

510
Merged from 3.0.x branch:

driver-core/src/main/java/com/datastax/driver/core/CodecRegistry.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ public final class CodecRegistry {
161161
TypeCodec.time(),
162162
TypeCodec.uuid(), // must be declared before TimeUUIDCodec so it gets chosen when CQL type not available
163163
TypeCodec.timeUUID(),
164-
TypeCodec.inet()
164+
TypeCodec.inet(),
165+
TypeCodec.duration()
165166
);
166167

167168
/**

driver-core/src/main/java/com/datastax/driver/core/DataType.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ public static CollectionType frozenMap(DataType keyType, DataType valueType) {
488488
* Returns a Custom type.
489489
* <p/>
490490
* A custom type is defined by the name of the class used on the Cassandra
491-
* side to implement it. Note that the support for custom type by the
491+
* side to implement it. Note that the support for custom types by the
492492
* driver is limited.
493493
* <p/>
494494
* The use of custom types is rarely useful and is thus not encouraged.
@@ -499,7 +499,22 @@ public static CollectionType frozenMap(DataType keyType, DataType valueType) {
499499
public static DataType.CustomType custom(String typeClassName) {
500500
if (typeClassName == null)
501501
throw new NullPointerException();
502-
return new DataType.CustomType(Name.CUSTOM, typeClassName);
502+
return DataTypeClassNameParser.isDuration(typeClassName)
503+
? DataType.duration()
504+
: new DataType.CustomType(Name.CUSTOM, typeClassName);
505+
}
506+
507+
/**
508+
* Returns the Duration type, introduced in Cassandra 3.10.
509+
* <p/>
510+
* Note that a Duration type does not have a native representation in CQL, and
511+
* technically, is merely a special {@link DataType#custom(String) custom type}
512+
* from the driver's point of view.
513+
*
514+
* @return the Duration type. The returned instance is a singleton.
515+
*/
516+
public static DurationType duration() {
517+
return DurationType.instance;
503518
}
504519

505520
/**
@@ -759,4 +774,25 @@ public String toString() {
759774
return String.format("'%s'", customClassName);
760775
}
761776
}
777+
778+
/**
779+
* The Duration type, introduced in Cassandra 3.10.
780+
* <p/>
781+
* This class is a singleton; to obtain its unique instance,
782+
* call {@link #duration()}.
783+
*/
784+
public static class DurationType extends CustomType {
785+
786+
private static final DurationType instance = new DurationType();
787+
788+
private DurationType() {
789+
super(Name.CUSTOM, "org.apache.cassandra.db.marshal.DurationType");
790+
}
791+
792+
@Override
793+
public String toString() {
794+
return "duration";
795+
}
796+
}
797+
762798
}

driver-core/src/main/java/com/datastax/driver/core/DataTypeClassNameParser.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class DataTypeClassNameParser {
4848
private static final String MAP_TYPE = "org.apache.cassandra.db.marshal.MapType";
4949
private static final String UDT_TYPE = "org.apache.cassandra.db.marshal.UserType";
5050
private static final String TUPLE_TYPE = "org.apache.cassandra.db.marshal.TupleType";
51+
private static final String DURATION_TYPE = "org.apache.cassandra.db.marshal.DurationType";
5152

5253
private static ImmutableMap<String, DataType> cassTypeToDataType =
5354
new ImmutableMap.Builder<String, DataType>()
@@ -70,6 +71,7 @@ class DataTypeClassNameParser {
7071
.put("org.apache.cassandra.db.marshal.TimeUUIDType", DataType.timeuuid())
7172
.put("org.apache.cassandra.db.marshal.ByteType", DataType.tinyint())
7273
.put("org.apache.cassandra.db.marshal.ShortType", DataType.smallint())
74+
.put(DURATION_TYPE, DataType.duration())
7375
.build();
7476

7577
static DataType parseOne(String className, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) {
@@ -161,6 +163,10 @@ private static boolean isCollection(String className) {
161163
return className.startsWith(COLLECTION_TYPE);
162164
}
163165

166+
public static boolean isDuration(String className) {
167+
return className.equals(DURATION_TYPE);
168+
}
169+
164170
static ParseResult parseWithComposite(String className, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) {
165171
Parser parser = new Parser(className, 0);
166172

@@ -285,9 +291,7 @@ public List<String> getTypeParameters() {
285291
try {
286292
list.add(readOne());
287293
} catch (DriverInternalError e) {
288-
DriverInternalError ex = new DriverInternalError(String.format("Exception while parsing '%s' around char %d", str, idx));
289-
ex.initCause(e);
290-
throw ex;
294+
throw new DriverInternalError(String.format("Exception while parsing '%s' around char %d", str, idx), e);
291295
}
292296
}
293297
throw new DriverInternalError(String.format("Syntax error parsing '%s' at char %d: unexpected end of string", str, idx));
@@ -333,9 +337,7 @@ public Map<String, String> getNameAndTypeParameters() {
333337
try {
334338
map.put(name, readOne());
335339
} catch (DriverInternalError e) {
336-
DriverInternalError ex = new DriverInternalError(String.format("Exception while parsing '%s' around char %d", str, idx));
337-
ex.initCause(e);
338-
throw ex;
340+
throw new DriverInternalError(String.format("Exception while parsing '%s' around char %d", str, idx), e);
339341
}
340342
}
341343
throw new DriverInternalError(String.format("Syntax error parsing '%s' at char %d: unexpected end of string", str, idx));
@@ -391,11 +393,6 @@ public String readNextIdentifier() {
391393
return str.substring(i, idx);
392394
}
393395

394-
public char readNextChar() {
395-
skipBlank();
396-
return str.charAt(idx++);
397-
}
398-
399396
@Override
400397
public String toString() {
401398
return str.substring(0, idx) + "[" + (idx == str.length() ? "" : str.charAt(idx)) + "]" + str.substring(idx + 1);

driver-core/src/main/java/com/datastax/driver/core/DataTypeCqlNameParser.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class DataTypeCqlNameParser {
6565
.put("timeuuid", timeuuid())
6666
.put("tinyint", tinyint())
6767
.put("smallint", smallint())
68+
// duration is not really a native CQL type, but appears as so in system tables
69+
.put("duration", duration())
6870
.build();
6971

7072
/**

0 commit comments

Comments
 (0)