OmniSciDB  c0231cc57d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HeavyDBTestClient.java
Go to the documentation of this file.
1 /*
2  * Copyright 2015 The Apache Software Foundation.
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 package com.mapd.tests;
17 
18 import org.apache.thrift.TException;
19 import org.apache.thrift.protocol.TBinaryProtocol;
20 import org.apache.thrift.protocol.TProtocol;
21 import org.apache.thrift.transport.TSocket;
22 
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashSet;
26 import java.util.List;
27 
28 import ai.heavy.thrift.server.*;
29 import ai.heavy.thrift.server.Heavy;
30 
31 public class HeavyDBTestClient {
32  Heavy.Client client;
33  String sessionId;
34 
35  public TServerStatus get_server_status() throws TDBException, TException {
36  return client.get_server_status(sessionId);
37  }
38 
39  public List<TServerStatus> get_status() throws TDBException, TException {
40  return client.get_status(sessionId);
41  }
42 
43  public TClusterHardwareInfo get_hardware_info() throws TDBException, TException {
44  return client.get_hardware_info(sessionId);
45  }
46 
47  public List<TNodeMemoryInfo> get_memory(String memory_level)
48  throws TDBException, TException {
49  return client.get_memory(sessionId, memory_level);
50  }
51 
52  public TTableDetails get_table_details(String table_name) throws Exception {
53  return client.get_table_details(sessionId, table_name);
54  }
55 
56  public TTableDetails get_table_details_for_database(
57  String tableName, String databaseName) throws Exception {
58  return client.get_table_details_for_database(sessionId, tableName, databaseName);
59  }
60 
61  public List<TTableMeta> get_tables_meta() throws TDBException, Exception {
62  return client.get_tables_meta(sessionId);
63  }
64 
65  public TQueryResult runSql(String sql) throws Exception {
66  return client.sql_execute(sessionId, sql, true, null, -1, -1);
67  }
68 
69  public List<TColumnType> sqlValidate(String sql) throws Exception {
70  return client.sql_validate(sessionId, sql);
71  }
72 
73  public int create_dashboard(String name) throws Exception {
74  return client.create_dashboard(
75  sessionId, name, "STATE", name + "_hash", name + "_meta");
76  }
77 
78  public void replace_dashboard(
79  int dashboard_id, java.lang.String name, java.lang.String new_owner)
80  throws Exception {
81  client.replace_dashboard(sessionId,
82  dashboard_id,
83  name,
84  new_owner,
85  "STATE",
86  name + "_hash",
87  name + "_meta");
88  }
89 
90  public TDashboard get_dashboard(int id) throws Exception {
91  TDashboard dashboard = client.get_dashboard(sessionId, id);
92  return dashboard;
93  }
94 
95  public void delete_dashboard(int id) throws Exception {
96  client.delete_dashboard(sessionId, id);
97  }
98 
99  public List<TDashboard> get_dashboards() throws Exception {
100  return client.get_dashboards(sessionId);
101  }
102 
103  public void import_table(String table_name, String file_name, TCopyParams copy_params)
104  throws Exception {
105  client.import_table(sessionId, table_name, file_name, copy_params);
106  }
107 
108  public void import_geo_table(String table_name,
109  String file_name,
110  TCopyParams copy_params,
111  java.util.List<TColumnType> row_desc,
112  TCreateParams create_params) throws Exception {
113  client.import_geo_table(
114  sessionId, table_name, file_name, copy_params, row_desc, create_params);
115  }
116 
117  public List<String> get_users() throws Exception {
118  return client.get_users(sessionId);
119  }
120 
121  public List<String> get_roles() throws Exception {
122  return client.get_roles(sessionId);
123  }
124 
125  public List<TDBObject> get_db_object_privs(String objectName, TDBObjectType type)
126  throws Exception {
127  return client.get_db_object_privs(sessionId, objectName, type);
128  }
129 
130  public void disconnect() throws Exception {
131  client.disconnect(sessionId);
132  }
133 
134  public Collection<String> get_all_roles_for_user(String username) throws Exception {
135  List<String> roles = client.get_all_roles_for_user(sessionId, username);
136  return new HashSet<String>(roles);
137  }
138 
139  public List<TQueryInfo> get_queries_info() throws Exception {
140  return client.get_queries_info(sessionId);
141  }
142 
143  public void load_table(
144  String tableName, List<List<String>> rows, List<String> columnNames)
145  throws Exception {
146  List<TStringRow> load_rows = new ArrayList<>();
147  for (List<String> row : rows) {
148  TStringRow tStringRow = new TStringRow(new ArrayList<>());
149  for (String value : row) {
150  tStringRow.cols.add(new TStringValue(value, false));
151  }
152  load_rows.add(tStringRow);
153  }
154  client.load_table(sessionId, tableName, load_rows, columnNames);
155  }
156 
157  public void load_table_binary(
158  String tableName, List<List<Object>> rows, List<String> columnNames)
159  throws Exception {
160  List<TRow> load_rows = new ArrayList<>();
161  for (List<Object> row : rows) {
162  TRow tRow = new TRow(new ArrayList<>());
163  for (Object value : row) {
164  tRow.cols.add(convertToTDatum(value));
165  }
166  load_rows.add(tRow);
167  }
168  client.load_table_binary(sessionId, tableName, load_rows, columnNames);
169  }
170 
171  private TDatum convertToTDatum(Object value) {
172  TDatumVal tDatumVal = new TDatumVal();
173  if (value instanceof Long) {
174  tDatumVal.int_val = ((Long) value);
175  } else if (value instanceof Double) {
176  tDatumVal.real_val = ((Double) value);
177  } else if (value instanceof String) {
178  tDatumVal.str_val = ((String) value);
179  } else if (value instanceof List) {
180  tDatumVal.arr_val = new ArrayList<>();
181  for (Object arrayValue : ((List<Object>) value)) {
182  tDatumVal.arr_val.add(convertToTDatum(arrayValue));
183  }
184  } else {
185  throw new RuntimeException("Unexpected value type. Value: " + value);
186  }
187  return new TDatum(tDatumVal, false);
188  }
189 
191  String tableName, List<List<Object>> columns, List<String> columnNames)
192  throws Exception {
193  List<TColumn> load_columns = convertToTColumns(columns);
194  client.load_table_binary_columnar(sessionId, tableName, load_columns, columnNames);
195  }
196 
198  String tableName, List<List<Object>> columns, List<String> columnNames)
199  throws Exception {
200  List<TColumn> load_columns = convertToTColumns(columns);
201  client.load_table_binary_columnar_polys(
202  sessionId, tableName, load_columns, columnNames, true);
203  }
204 
205  private List<TColumn> convertToTColumns(List<List<Object>> columns) {
206  List<TColumn> load_columns = new ArrayList<>();
207  for (List<Object> column : columns) {
208  load_columns.add(convertToTColumn(column));
209  }
210  return load_columns;
211  }
212 
213  private TColumn convertToTColumn(List<Object> column) {
214  TColumn tColumn = new TColumn();
215  tColumn.data = new TColumnData(
216  new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
217  tColumn.nulls = new ArrayList<>();
218  for (Object value : column) {
219  if (value instanceof Long) {
220  tColumn.data.int_col.add((Long) value);
221  } else if (value instanceof Double) {
222  tColumn.data.real_col.add((Double) value);
223  } else if (value instanceof String) {
224  tColumn.data.str_col.add((String) value);
225  } else if (value instanceof List) {
226  tColumn.data.arr_col.add(convertToTColumn((List<Object>) value));
227  } else {
228  throw new RuntimeException("Unexpected value type. Value: " + value);
229  }
230  tColumn.nulls.add(false);
231  }
232  return tColumn;
233  }
234 
236  String host, int port, String db, String user, String password)
237  throws Exception {
238  TSocket transport = new TSocket(host, port);
239  transport.open();
240  TProtocol protocol = new TBinaryProtocol(transport);
241  Heavy.Client client = new Heavy.Client(protocol);
242  HeavyDBTestClient session = new HeavyDBTestClient();
243  session.client = client;
244  session.sessionId = client.connect(user, password, db);
245  return session;
246  }
247 }
void load_table_binary_columnar_polys(String tableName, List< List< Object >> columns, List< String > columnNames)
TColumn convertToTColumn(List< Object > column)
TClusterHardwareInfo get_hardware_info()
void load_table(String tableName, List< List< String >> rows, List< String > columnNames)
static HeavyDBTestClient getClient(String host, int port, String db, String user, String password)
void import_table(String table_name, String file_name, TCopyParams copy_params)
Collection< String > get_all_roles_for_user(String username)
TTableDetails get_table_details(String table_name)
void replace_dashboard(int dashboard_id, java.lang.String name, java.lang.String new_owner)
void import_geo_table(String table_name, String file_name, TCopyParams copy_params, java.util.List< TColumnType > row_desc, TCreateParams create_params)
List< TColumnType > sqlValidate(String sql)
List< TDBObject > get_db_object_privs(String objectName, TDBObjectType type)
void load_table_binary_columnar(String tableName, List< List< Object >> columns, List< String > columnNames)
List< TNodeMemoryInfo > get_memory(String memory_level)
void load_table_binary(String tableName, List< List< Object >> rows, List< String > columnNames)
List< TServerStatus > get_status()
TQueryResult runSql(String sql)
List< TColumn > convertToTColumns(List< List< Object >> columns)
string name
Definition: setup.in.py:72
TTableDetails get_table_details_for_database(String tableName, String databaseName)