PHOENIX-1722 Speedup CONVERT_TZ function add JodaTimezoneCache (Vaclav Loffelmann)
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/fcedbe6a Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/fcedbe6a Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/fcedbe6a Branch: refs/heads/4.x-HBase-1.x Commit: fcedbe6a492faa7cc39fe0ed7c4c24c7d41db1a5 Parents: 4248be3 Author: Thomas <[email protected]> Authored: Fri Mar 27 15:32:38 2015 -0700 Committer: Thomas <[email protected]> Committed: Fri Mar 27 15:32:38 2015 -0700 ---------------------------------------------------------------------- .../apache/phoenix/cache/JodaTimezoneCache.java | 84 ++++++++++++++++++++ .../phoenix/cache/JodaTimezoneCacheTest.java | 51 ++++++++++++ 2 files changed, 135 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/fcedbe6a/phoenix-core/src/main/java/org/apache/phoenix/cache/JodaTimezoneCache.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/cache/JodaTimezoneCache.java b/phoenix-core/src/main/java/org/apache/phoenix/cache/JodaTimezoneCache.java new file mode 100644 index 0000000..54904d7 --- /dev/null +++ b/phoenix-core/src/main/java/org/apache/phoenix/cache/JodaTimezoneCache.java @@ -0,0 +1,84 @@ +/* + * Copyright 2015 Apache Software Foundation. + * + * 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 org.apache.phoenix.cache; + +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import com.google.common.util.concurrent.UncheckedExecutionException; +import java.nio.ByteBuffer; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.phoenix.schema.IllegalDataException; +import org.joda.time.DateTimeZone; + +public class JodaTimezoneCache { + + public static final int CACHE_EXPRIRE_TIME_MINUTES = 10; + private static final LoadingCache<ByteBuffer, DateTimeZone> cachedJodaTimeZones = createTimezoneCache(); + + /** + * Returns joda's DateTimeZone instance from cache or create new instance and cache it. + * + * @param timezoneId Timezone Id as accepted by {@code DateTimeZone.forID()}. E.g. Europe/Isle_of_Man + * @return joda's DateTimeZone instance + * @throws IllegalDataException if unknown timezone id is passed + */ + public static DateTimeZone getInstance(ByteBuffer timezoneId) { + try { + return cachedJodaTimeZones.get(timezoneId); + } catch (ExecutionException ex) { + throw new IllegalDataException(ex); + } catch (UncheckedExecutionException e) { + throw new IllegalDataException("Unknown timezone " + Bytes.toString(timezoneId.array())); + } + } + + /** + * Returns joda's DateTimeZone instance from cache or create new instance and cache it. + * + * @param timezoneId Timezone Id as accepted by {@code DateTimeZone.forID()}. E.g. Europe/Isle_of_Man + * @return joda's DateTimeZone instance + * @throws IllegalDataException if unknown timezone id is passed + */ + public static DateTimeZone getInstance(ImmutableBytesWritable timezoneId) { + return getInstance(ByteBuffer.wrap(timezoneId.copyBytes())); + } + + /** + * Returns joda's DateTimeZone instance from cache or create new instance and cache it. + * + * @param timezoneId Timezone Id as accepted by {@code DateTimeZone.forID()}. E.g. Europe/Isle_of_Man + * @return joda's DateTimeZone instance + * @throws IllegalDataException if unknown timezone id is passed + */ + public static DateTimeZone getInstance(String timezoneId) { + return getInstance(ByteBuffer.wrap(Bytes.toBytes(timezoneId))); + } + + private static LoadingCache<ByteBuffer, DateTimeZone> createTimezoneCache() { + return CacheBuilder.newBuilder().expireAfterAccess(CACHE_EXPRIRE_TIME_MINUTES, TimeUnit.MINUTES).build(new CacheLoader<ByteBuffer, DateTimeZone>() { + + @Override + public DateTimeZone load(ByteBuffer timezone) throws Exception { + return DateTimeZone.forID(Bytes.toString(timezone.array())); + } + }); + } + +} http://git-wip-us.apache.org/repos/asf/phoenix/blob/fcedbe6a/phoenix-core/src/test/java/org/apache/phoenix/cache/JodaTimezoneCacheTest.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/test/java/org/apache/phoenix/cache/JodaTimezoneCacheTest.java b/phoenix-core/src/test/java/org/apache/phoenix/cache/JodaTimezoneCacheTest.java new file mode 100644 index 0000000..f388703 --- /dev/null +++ b/phoenix-core/src/test/java/org/apache/phoenix/cache/JodaTimezoneCacheTest.java @@ -0,0 +1,51 @@ +/* + * Copyright 2015 Apache Software Foundation. + * + * 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 org.apache.phoenix.cache; + +import java.nio.ByteBuffer; +import org.apache.hadoop.hbase.io.ImmutableBytesWritable; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.phoenix.schema.IllegalDataException; +import org.joda.time.DateTimeZone; +import static org.junit.Assert.assertTrue; +import org.junit.Test; + +public class JodaTimezoneCacheTest { + + @Test + public void testGetInstanceByteBufferUTC() { + DateTimeZone instance = JodaTimezoneCache.getInstance(ByteBuffer.wrap(Bytes.toBytes("UTC"))); + assertTrue(instance instanceof DateTimeZone); + } + + @Test + public void testGetInstanceString() { + DateTimeZone instance = JodaTimezoneCache.getInstance("America/St_Vincent"); + assertTrue(instance instanceof DateTimeZone); + } + + @Test(expected = IllegalDataException.class) + public void testGetInstanceStringUnknown() { + JodaTimezoneCache.getInstance("SOME_UNKNOWN_TIMEZONE"); + } + + @Test + public void testGetInstanceImmutableBytesWritable() { + ImmutableBytesWritable ptr = new ImmutableBytesWritable(Bytes.toBytes("Europe/Isle_of_Man")); + DateTimeZone instance = JodaTimezoneCache.getInstance(ptr); + assertTrue(instance instanceof DateTimeZone); + } +}
