|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.cassandra.cql3; |
| 20 | + |
| 21 | +import java.time.Instant; |
| 22 | + |
| 23 | +import org.apache.cassandra.cql3.functions.Arguments; |
| 24 | +import org.apache.cassandra.cql3.functions.FunctionArguments; |
| 25 | +import org.apache.cassandra.db.marshal.TimeUUIDType; |
| 26 | +import org.apache.cassandra.transport.ProtocolVersion; |
| 27 | +import org.apache.cassandra.utils.Clock.Global; |
| 28 | +import org.apache.cassandra.utils.TimeUUID; |
| 29 | + |
| 30 | +public interface FunctionContext |
| 31 | +{ |
| 32 | + NoTimeOrQueryFunctionContext NONE = new NoTimeOrQueryFunctionContext() {}; |
| 33 | + |
| 34 | + interface RealTimeFunctionContext extends FunctionContext |
| 35 | + { |
| 36 | + @Override default byte[] nextTimeUUIDAsBytes() { return TimeUUID.Generator.nextTimeUUIDAsBytes(); } |
| 37 | + @Override default Instant now() { return Global.currentTime(); } |
| 38 | + @Override default long nowMicros() { return Global.currentTimeMicros(); } |
| 39 | + @Override default long nowMillis() { return Global.currentTimeMillis(); } |
| 40 | + } |
| 41 | + |
| 42 | + interface NoTimeFunctionContext extends FunctionContext |
| 43 | + { |
| 44 | + @Override default byte[] nextTimeUUIDAsBytes() { throw new UnsupportedOperationException(); } |
| 45 | + @Override default Instant now() { throw new UnsupportedOperationException(); } |
| 46 | + @Override default long nowMicros() { throw new UnsupportedOperationException(); } |
| 47 | + @Override default long nowMillis() { throw new UnsupportedOperationException(); } |
| 48 | + } |
| 49 | + |
| 50 | + interface NoTimeOrQueryFunctionContext extends NoTimeFunctionContext |
| 51 | + { |
| 52 | + @Override default QueryOptions options() { throw new UnsupportedOperationException(); } |
| 53 | + } |
| 54 | + |
| 55 | + interface PartialFunctionContext extends FunctionContext |
| 56 | + { |
| 57 | + default long nowMillis() { return nowMicros() / 1000; } |
| 58 | + default Instant now() |
| 59 | + { |
| 60 | + long nowMicros = nowMicros(); |
| 61 | + return Instant.ofEpochSecond(nowMicros / 1000_000, (nowMicros % 1000_000) * 1000); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + abstract class MicrosFunctionContext implements PartialFunctionContext |
| 66 | + { |
| 67 | + final long atMicros; |
| 68 | + private long timeUuidNanos; |
| 69 | + |
| 70 | + public MicrosFunctionContext(long atMicros) |
| 71 | + { |
| 72 | + this.atMicros = atMicros; |
| 73 | + } |
| 74 | + |
| 75 | + @Override public long nowMicros() { return atMicros; } |
| 76 | + |
| 77 | + @Override |
| 78 | + public byte[] nextTimeUUIDAsBytes() |
| 79 | + { |
| 80 | + return TimeUUID.toBytes(TimeUUID.unixMicrosToMsb(atMicros), TimeUUIDType.signedBytesToNativeLong(timeUuidNanos++)); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + QueryOptions options(); |
| 85 | + Instant now(); |
| 86 | + long nowMillis(); |
| 87 | + long nowMicros(); |
| 88 | + byte[] nextTimeUUIDAsBytes(); |
| 89 | + |
| 90 | + default ProtocolVersion getProtocolVersion() { return options().getProtocolVersion(); } |
| 91 | + |
| 92 | + default Arguments noArguments() |
| 93 | + { |
| 94 | + return new FunctionArguments(this); |
| 95 | + } |
| 96 | +} |
0 commit comments