You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2022/06/01 16:05:04 UTC

[GitHub] [ignite] timoninmaxim commented on a diff in pull request #10046: IGNITE-16977 Move date/time indexes to the core module

timoninmaxim commented on code in PR #10046:
URL: https://github.com/apache/ignite/pull/10046#discussion_r886923152


##########
modules/core/src/main/java/org/apache/ignite/internal/cache/query/index/sorted/inline/types/DateValueUtils.java:
##########
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.ignite.internal.cache.query.index.sorted.inline.types;
+
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.TimeZone;
+
+/**
+ * DateValue is a representation of a date in bit form:
+ *
+ * dv = (year << SHIFT_YEAR) | (month << SHIFT_MONTH) | day.
+ */
+public class DateValueUtils {
+    /** Calendar with UTC time zone instance. */
+    private static final ThreadLocal<Calendar> UTC_CALENDAR = ThreadLocal.withInitial(
+        () -> Calendar.getInstance(TimeZone.getTimeZone("UTC")));
+
+    /** Cached default time zone. */
+    private static final TimeZone DEFAULT_TZ = TimeZone.getDefault();
+
+    /** Forbid instantiation of this class. Just hold constants there. */
+    private DateValueUtils() {}
+
+    /** */
+    private static final int SHIFT_YEAR = 9;
+
+    /** */
+    private static final int SHIFT_MONTH = 5;
+
+    /** */
+    private static final long MONTH_MASK = ~(-1L << (SHIFT_YEAR - SHIFT_MONTH));
+
+    /** */
+    private static final long DAY_MASK = ~(-1L << SHIFT_MONTH);
+
+    /** Min date value. */
+    public static final long MIN_DATE_VALUE = (-999_999_999L << SHIFT_YEAR) + (1 << SHIFT_MONTH) + 1;
+
+    /** Max date value. */
+    public static final long MAX_DATE_VALUE = (999_999_999L << SHIFT_YEAR) + (12 << SHIFT_MONTH) + 31;
+
+    /** The number of milliseconds per day. */
+    public static final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000L;
+
+    /** The number of nanoseconds per day. */
+    public static final long NANOS_PER_DAY = MILLIS_PER_DAY * 1_000_000;
+
+    /**
+     * Extract the year from a date value.
+     */
+    private static int yearFromDateValue(long dateVal) {
+        return (int)(dateVal >>> SHIFT_YEAR);
+    }
+
+    /**
+     * Extract the month from a date value.
+     */
+    private static int monthFromDateValue(long dateVal) {
+        return (int)((dateVal >>> SHIFT_MONTH) & MONTH_MASK);
+    }
+
+    /**
+     * Extract the day of month from a date value.
+     */
+    private static int dayFromDateValue(long dateVal) {
+        return (int)(dateVal & DAY_MASK);
+    }
+
+    /**
+     * Construct date value from components.
+     */
+    public static long dateValue(int year, int month, int day) {
+        return ((long)year << SHIFT_YEAR) | month << SHIFT_MONTH | day;
+    }
+
+    /**
+     * Convert date value to epoch milliseconds.
+     */
+    public static long millisFromDateValue(long dateVal) {
+        Calendar cal = UTC_CALENDAR.get();
+
+        cal.setTimeInMillis(0); // Reset time.

Review Comment:
   JFYI, there is a method `Calendar#clean()` for reset.



##########
modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/CreateIndexOnInvalidDataTypeTest.java:
##########
@@ -156,6 +156,8 @@ public void testAddInvalidDataToIndex() throws Exception {
         bob.setField("VAL_INT", 10);
         bob.setField("VAL_DATE", new java.util.Date());
 
+        //grid().cache("test").put(0, bob.build());

Review Comment:
   Unused line.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org