17
17
*/
18
18
package org .apache .cassandra .cql3 .statements ;
19
19
20
+ import java .util .ArrayList ;
21
+ import java .util .List ;
20
22
import java .util .concurrent .TimeoutException ;
21
23
24
+ import com .google .common .collect .Iterables ;
25
+
22
26
import org .apache .cassandra .audit .AuditLogContext ;
23
27
import org .apache .cassandra .audit .AuditLogEntryType ;
24
28
import org .apache .cassandra .auth .Permission ;
27
31
import org .apache .cassandra .db .Keyspace ;
28
32
import org .apache .cassandra .db .guardrails .Guardrails ;
29
33
import org .apache .cassandra .db .virtual .VirtualKeyspaceRegistry ;
34
+ import org .apache .cassandra .db .virtual .VirtualTable ;
30
35
import org .apache .cassandra .exceptions .*;
31
36
import org .apache .cassandra .schema .Schema ;
32
37
import org .apache .cassandra .schema .TableId ;
39
44
import org .apache .commons .lang3 .builder .ToStringBuilder ;
40
45
import org .apache .commons .lang3 .builder .ToStringStyle ;
41
46
47
+ import static java .util .stream .Collectors .joining ;
48
+
42
49
public class TruncateStatement extends QualifiedStatement implements CQLStatement
43
50
{
44
51
public TruncateStatement (QualifiedName name )
@@ -58,16 +65,54 @@ public void authorize(ClientState state) throws InvalidRequestException, Unautho
58
65
59
66
public void validate (ClientState state ) throws InvalidRequestException
60
67
{
61
- Schema .instance .validateTable (keyspace (), name ());
68
+ if (qualifiedName instanceof SemiQualifiedName )
69
+ Schema .instance .validateKeyspace (keyspace ());
70
+ else
71
+ Schema .instance .validateTable (keyspace (), name ());
72
+
62
73
Guardrails .dropTruncateTableEnabled .ensureEnabled (state );
63
74
}
64
75
65
76
@ Override
66
77
public ResultMessage execute (QueryState state , QueryOptions options , Dispatcher .RequestTime requestTime ) throws InvalidRequestException , TruncateException
78
+ {
79
+ if (name () == null )
80
+ {
81
+ Iterable <TableMetadata > tablesIterable = Iterables .filter (Schema .instance .getTablesAndViews (keyspace ()), tmd -> !tmd .isView ());
82
+ List <TableMetadata > tablesToTruncate = new ArrayList <>();
83
+ for (TableMetadata t : tablesIterable )
84
+ tablesToTruncate .add (t );
85
+
86
+ for (TableMetadata tmd : tablesIterable )
87
+ {
88
+ try
89
+ {
90
+ truncateOne (tmd .keyspace , tmd .name );
91
+ tablesToTruncate .remove (tmd );
92
+ }
93
+ catch (TruncateException ex )
94
+ {
95
+ throw new TruncateException (ex , "Tables not truncated: " + tablesToTruncate .stream ()
96
+ .map (TableMetadata ::toString )
97
+ .collect (joining ("," )));
98
+ }
99
+ }
100
+ }
101
+ else
102
+ truncateOne (keyspace (), name ());
103
+
104
+ return null ;
105
+ }
106
+
107
+ private void truncateOne (String keyspace , String table )
67
108
{
68
109
try
69
110
{
70
- TableMetadata metaData = Schema .instance .getTableMetadata (keyspace (), name ());
111
+ TableMetadata metaData = Schema .instance .getTableMetadata (keyspace , table );
112
+
113
+ if (metaData == null )
114
+ throw new InvalidRequestException (String .format ("Cannot TRUNCATE %s.%s as it does not exist." , keyspace (), name ()));
115
+
71
116
if (metaData .isView ())
72
117
throw new InvalidRequestException ("Cannot TRUNCATE materialized view directly; must truncate base table instead" );
73
118
@@ -77,21 +122,24 @@ public ResultMessage execute(QueryState state, QueryOptions options, Dispatcher.
77
122
}
78
123
else
79
124
{
80
- StorageProxy .truncateBlocking (keyspace (), name () );
125
+ StorageProxy .truncateBlocking (keyspace , table );
81
126
}
82
127
}
83
128
catch (UnavailableException | TimeoutException e )
84
129
{
85
130
throw new TruncateException (e );
86
131
}
87
- return null ;
88
132
}
89
133
90
134
public ResultMessage executeLocally (QueryState state , QueryOptions options )
91
135
{
92
136
try
93
137
{
94
138
TableMetadata metaData = Schema .instance .getTableMetadata (keyspace (), name ());
139
+
140
+ if (metaData == null )
141
+ throw new InvalidRequestException (String .format ("Cannot TRUNCATE %s.%s as it does not exist." , keyspace (), name ()));
142
+
95
143
if (metaData .isView ())
96
144
throw new InvalidRequestException ("Cannot TRUNCATE materialized view directly; must truncate base table instead" );
97
145
@@ -114,7 +162,9 @@ public ResultMessage executeLocally(QueryState state, QueryOptions options)
114
162
115
163
private void executeForVirtualTable (TableId id )
116
164
{
117
- VirtualKeyspaceRegistry .instance .getTableNullable (id ).truncate ();
165
+ VirtualTable maybeVTable = VirtualKeyspaceRegistry .instance .getTableNullable (id );
166
+ if (maybeVTable != null )
167
+ maybeVTable .truncate ();
118
168
}
119
169
120
170
@ Override
0 commit comments