OmniSciDB  a987f07e93
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CalciteParserFactory.java
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.mapd.parser.server;
18 
22 
23 import org.apache.calcite.sql.fun.SqlStdOperatorTable;
24 import org.apache.calcite.util.ConversionUtil;
25 import org.apache.commons.pool.PoolableObjectFactory;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 
29 import java.util.Map;
30 import java.util.function.Supplier;
31 
32 class CalciteParserFactory implements PoolableObjectFactory {
33  final static Logger HEAVYDBLOGGER = LoggerFactory.getLogger(CalciteParserFactory.class);
34 
35  private final String dataDir;
36  private final Map<String, ExtensionFunction> extSigs;
37  private final int dbPort;
40  private final Supplier<HeavyDBSqlOperatorTable> tableOperatorSupplier =
41  new Supplier<HeavyDBSqlOperatorTable>() {
42  @Override
43  public HeavyDBSqlOperatorTable get() {
44  return tableOperator;
45  }
46  };
47 
48  static {
49  System.setProperty(
50  "saffron.default.charset", ConversionUtil.NATIVE_UTF16_CHARSET_NAME);
51  System.setProperty(
52  "saffron.default.nationalcharset", ConversionUtil.NATIVE_UTF16_CHARSET_NAME);
53  System.setProperty("saffron.default.collation.name",
54  ConversionUtil.NATIVE_UTF16_CHARSET_NAME + "$en_US");
55  }
56 
58  final Map<String, ExtensionFunction> extSigs,
59  int dbPort,
61  this.dataDir = dataDir;
62  this.extSigs = extSigs;
63  this.dbPort = dbPort;
64  this.socket_transport_properties = skT;
65 
67  }
68 
69  public void updateOperatorTable() {
71  new HeavyDBSqlOperatorTable(SqlStdOperatorTable.instance());
72  tableOperator.addUDF(extSigs);
73  this.tableOperator = tableOperator;
74  }
75 
76  @Override
77  public Object makeObject() throws Exception {
78  HeavyDBParser obj = new HeavyDBParser(
80  return obj;
81  }
82 
83  @Override
84  public void destroyObject(Object obj) throws Exception {
85  // no need to do anything
86  }
87 
88  @Override
89  public boolean validateObject(Object obj) {
90  HeavyDBParser mdp = (HeavyDBParser) obj;
91  if (mdp.getCallCount() < 1000) {
92  return true;
93  } else {
94  HEAVYDBLOGGER.debug(" invalidating object due to max use count");
95  return false;
96  }
97  }
98 
99  @Override
100  public void activateObject(Object obj) throws Exception {
101  // don't need to do anything
102  }
103 
104  @Override
105  public void passivateObject(Object obj) throws Exception {
106  // nothing to currently do here
107  }
108 }
CalciteParserFactory(String dataDir, final Map< String, ExtensionFunction > extSigs, int dbPort, SockTransportProperties skT)
final Supplier< HeavyDBSqlOperatorTable > tableOperatorSupplier
volatile HeavyDBSqlOperatorTable tableOperator
final Map< String, ExtensionFunction > extSigs
final SockTransportProperties socket_transport_properties