You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by db...@apache.org on 2016/05/31 19:44:34 UTC
[18/21] ambari git commit: AMBARI-16963: JDBC implementation of hive
view. (dipayanb)
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/client/PersistentCursor.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/client/PersistentCursor.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/client/PersistentCursor.java
new file mode 100644
index 0000000..aadf31c
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/client/PersistentCursor.java
@@ -0,0 +1,87 @@
+/*
+ * 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.ambari.view.hive2.client;
+
+
+import com.google.common.collect.Lists;
+
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Wrapper over other iterables. Does not block and can be reset to start again from beginning.
+ */
+public class PersistentCursor<T, R> implements Cursor<T, R> {
+ private List<T> rows = Lists.newArrayList();
+ private List<R> columns = Lists.newArrayList();
+ private int offset = 0;
+
+ public PersistentCursor(List<T> rows, List<R> columns) {
+ this.rows = rows;
+ this.columns = columns;
+ }
+
+
+ @Override
+ public Iterator<T> iterator() {
+ return this;
+ }
+
+ @Override
+ public boolean hasNext() {
+ return rows.size() > 0 && offset < rows.size();
+ }
+
+ @Override
+ public T next() {
+ T row = rows.get(offset);
+ offset++;
+ return row;
+ }
+
+ @Override
+ public void remove() {
+ throw new RuntimeException("Read only cursor. Method not supported");
+ }
+
+ @Override
+ public boolean isResettable() {
+ return true;
+ }
+
+ @Override
+ public void reset() {
+ this.offset = 0;
+ }
+
+ @Override
+ public int getOffset() {
+ return offset;
+ }
+
+ @Override
+ public List<R> getDescriptions() {
+ return columns;
+ }
+
+ @Override
+ public void keepAlive() {
+ // Do Nothing as we are pre-fetching everything.
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/client/Row.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/client/Row.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/client/Row.java
new file mode 100644
index 0000000..0faa6c7
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/client/Row.java
@@ -0,0 +1,74 @@
+/**
+ * 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.ambari.view.hive2.client;
+
+import java.util.Arrays;
+import java.util.HashSet;
+
+public class Row {
+ private Object[] row;
+
+ public Row(Object[] row) {
+ this(row, null);
+ }
+
+ public Row(Object[] row, HashSet<Integer> selectedColumns) {
+ if (selectedColumns == null || selectedColumns.size() == 0)
+ this.row = row.clone();
+ else {
+ this.row = new Object[selectedColumns.size()];
+ int rowIndex = 0;
+ for (Integer selectedIndex : selectedColumns) {
+ this.row[rowIndex] = row[selectedIndex];
+ rowIndex ++;
+ }
+ }
+ }
+
+ public Object[] getRow() {
+ return row;
+ }
+
+ public void setRow(Object[] row) {
+ this.row = row;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ Row row1 = (Row) o;
+
+ boolean retValue = Arrays.equals(row, row1.row);
+ return retValue;
+ }
+
+ @Override
+ public int hashCode() {
+ return Arrays.hashCode(row);
+ }
+
+ @Override
+ public String toString() {
+ return "Row{" +
+ "row=" + Arrays.toString(row) +
+ '}';
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/exceptions/NotConnectedException.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/exceptions/NotConnectedException.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/exceptions/NotConnectedException.java
new file mode 100644
index 0000000..71e64d2
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/exceptions/NotConnectedException.java
@@ -0,0 +1,28 @@
+/*
+ * 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.ambari.view.hive2.exceptions;
+
+/**
+ * Exception thrown when the connection is not made and we try to execute some job
+ */
+public class NotConnectedException extends RuntimeException {
+ public NotConnectedException(String message) {
+ super(message);
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/AsyncExecutionFailure.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/AsyncExecutionFailure.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/AsyncExecutionFailure.java
new file mode 100644
index 0000000..f587ce0
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/AsyncExecutionFailure.java
@@ -0,0 +1,23 @@
+/*
+ * 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.ambari.view.hive2.internal;
+
+public class AsyncExecutionFailure {
+}
+
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/AsyncExecutionSuccess.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/AsyncExecutionSuccess.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/AsyncExecutionSuccess.java
new file mode 100644
index 0000000..c45b303
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/AsyncExecutionSuccess.java
@@ -0,0 +1,25 @@
+/*
+ * 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.ambari.view.hive2.internal;
+
+public class AsyncExecutionSuccess {
+
+
+}
+
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/Connectable.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/Connectable.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/Connectable.java
new file mode 100644
index 0000000..5bfb6dc
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/Connectable.java
@@ -0,0 +1,59 @@
+/*
+ * 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.ambari.view.hive2.internal;
+
+import com.google.common.base.Optional;
+import org.apache.hive.jdbc.HiveConnection;
+
+/**
+ * Life cycle management for java.sql.Connection
+ */
+public interface Connectable {
+
+ /**
+ * Get the underlying connection
+ * @return an optional wrapping the connection
+ */
+ Optional<HiveConnection> getConnection();
+
+ /**
+ * Check if the connection is open
+ * @return
+ */
+ boolean isOpen();
+
+ /**
+ * Open a connection
+ * @throws ConnectionException
+ */
+ void connect() throws ConnectionException;
+
+ /**
+ * Reconnect if closed
+ * @throws ConnectionException
+ */
+ void reconnect() throws ConnectionException;
+
+ /**
+ * Close the connection
+ * @throws ConnectionException
+ */
+ void disconnect() throws ConnectionException;
+
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/ConnectionException.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/ConnectionException.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/ConnectionException.java
new file mode 100644
index 0000000..14db3f1
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/ConnectionException.java
@@ -0,0 +1,25 @@
+/*
+ * 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.ambari.view.hive2.internal;
+
+public class ConnectionException extends Exception {
+ public ConnectionException(Exception e, String message) {
+ super(message,e);
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/ConnectionProperties.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/ConnectionProperties.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/ConnectionProperties.java
new file mode 100644
index 0000000..6f829c3
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/ConnectionProperties.java
@@ -0,0 +1,94 @@
+/*
+ * 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.ambari.view.hive2.internal;
+
+import com.google.common.collect.Maps;
+
+import java.util.Map;
+
+/**
+ * Holds all information needed to connect to HS2
+ */
+public class ConnectionProperties {
+
+ private String host;
+ private int port;
+ private String userName;
+ private String password;
+ private Map<String, String> authParams = Maps.newHashMap();
+
+ public Map<String, String> getAuthParams() {
+ return authParams;
+ }
+
+ public void addAuthParam(String key,String value){
+ authParams.put(key, value);
+ }
+
+ public String getHost() {
+ return host;
+ }
+
+ public void setHost(String host) {
+ this.host = host;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+ public int getPort() {
+ return port;
+ }
+
+ public void setPort(int port) {
+ this.port = port;
+ }
+
+ public String getUserName() {
+ return userName;
+ }
+
+ public void setUserName(String userName) {
+ this.userName = userName;
+ }
+
+ @Override
+ public String toString() {
+ return "HiveConnectionProps{" +
+ "authParams=" + authParams +
+ ", host='" + host + '\'' +
+ ", port=" + port +
+ ", userName='" + userName + '\'' +
+ ", password='" + password + '\'' +
+ '}';
+ }
+
+ public String asUrl() {
+ return null;
+ }
+
+ public String asUrlWithoutCredentials() {
+ return null;
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/ConnectionSupplier.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/ConnectionSupplier.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/ConnectionSupplier.java
new file mode 100644
index 0000000..3cc268b
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/ConnectionSupplier.java
@@ -0,0 +1,37 @@
+/*
+ * 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.ambari.view.hive2.internal;
+
+import org.apache.ambari.view.ViewContext;
+import org.apache.ambari.view.hive2.ConnectionDelegate;
+import org.apache.ambari.view.hive2.HiveJdbcConnectionDelegate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ConnectionSupplier implements ContextSupplier<ConnectionDelegate> {
+
+ protected final Logger LOG =
+ LoggerFactory.getLogger(getClass());
+
+ @Override
+ public ConnectionDelegate get(ViewContext context) {
+ LOG.debug("Creating Connection delegate instance for Viewname: {}, Instance Name: {}", context.getViewName(), context.getInstanceName());
+ return new HiveJdbcConnectionDelegate();
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/ContextSupplier.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/ContextSupplier.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/ContextSupplier.java
new file mode 100644
index 0000000..e35885d
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/ContextSupplier.java
@@ -0,0 +1,37 @@
+/*
+ * 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.ambari.view.hive2.internal;
+
+import org.apache.ambari.view.ViewContext;
+
+/**
+ * A class that can supply objects of same type.
+ * @param <T>
+ */
+public interface ContextSupplier<T> {
+ /**
+ * Retrieves an instance of appropriate type. The returned object could be a new instance
+ * or an exiting instance. No guarantee on that.
+ * @param context View Context to be used to create the instance
+ * @return instance of appropriateType
+ */
+ T get(ViewContext context);
+
+
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/DataStorageSupplier.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/DataStorageSupplier.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/DataStorageSupplier.java
new file mode 100644
index 0000000..f496d06
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/DataStorageSupplier.java
@@ -0,0 +1,42 @@
+/*
+ * 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.ambari.view.hive2.internal;
+
+import org.apache.ambari.view.ViewContext;
+import org.apache.ambari.view.hive2.persistence.DataStoreStorage;
+import org.apache.ambari.view.hive2.persistence.Storage;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * A supplier for data storage
+ * Duplicated to keep the API uniform
+ */
+public class DataStorageSupplier implements ContextSupplier<Storage> {
+
+ protected final Logger LOG =
+ LoggerFactory.getLogger(getClass());
+
+ @Override
+ public Storage get(ViewContext context) {
+ LOG.debug("Creating storage instance for Viewname: {}, Instance Name: {}", context.getViewName(), context.getInstanceName());
+ return new DataStoreStorage(context);
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/DefaultSupplier.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/DefaultSupplier.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/DefaultSupplier.java
new file mode 100644
index 0000000..39c8594
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/DefaultSupplier.java
@@ -0,0 +1,62 @@
+/*
+ * 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.ambari.view.hive2.internal;
+
+import com.google.common.base.Supplier;
+
+import java.util.List;
+
+/**
+ * Create instances of classes
+ * for which no constructors have been specified
+ * @param <T>
+ */
+public class DefaultSupplier<T> implements Supplier<T>{
+
+ private Class<T> clazz;
+ T instance;
+
+ public DefaultSupplier(T instance) {
+ this.instance = instance;
+ }
+
+ public DefaultSupplier(Class<T> clazz) throws IllegalAccessException, InstantiationException {
+ this.clazz = clazz;
+ }
+
+ /**
+ * Get the instance
+ * @return
+ */
+ @Override
+ public T get() {
+ if(clazz != null){
+ try {
+ return clazz.newInstance();
+ } catch (InstantiationException e) {
+ e.printStackTrace();
+ return null;
+ } catch (IllegalAccessException e) {
+ return null;
+ }
+ } else {
+ return instance;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/Either.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/Either.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/Either.java
new file mode 100644
index 0000000..52fb01e
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/Either.java
@@ -0,0 +1,78 @@
+/*
+ * 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.ambari.view.hive2.internal;
+
+import com.google.common.base.Optional;
+
+/**
+ * Simple implementation of a container class which can
+ * hold one of two values
+ *
+ * Callers should check if the value if left or right before
+ * trying to get the value
+ *
+ * @param <L> Left Value
+ * @param <R> Right value
+ */
+public class Either<L,R> {
+
+ private final Optional<L> left;
+ private final Optional<R> right;
+
+
+ public boolean isLeft(){
+ return left.isPresent() && !right.isPresent();
+ }
+
+ public boolean isRight(){
+ return !left.isPresent() && right.isPresent();
+ }
+
+ public L getLeft(){
+ return left.orNull();
+ }
+
+ public R getRight(){
+ return right.orNull();
+ }
+
+
+ private Either(Optional<L> left, Optional<R> right) {
+ this.left = left;
+ this.right = right;
+ }
+
+
+
+ public static <L,R> Either<L,R> left(L value) {
+ return new Either<>(Optional.of(value), Optional.<R>absent());
+ }
+
+ public static <L,R> Either<L,R> right(R value) {
+ return new Either<>(Optional.<L>absent(), Optional.of(value));
+ }
+
+ public static <L,R> Either<L,R> none() {
+ return new Either<>(Optional.<L>absent(), Optional.<R>absent());
+ }
+
+}
+
+
+
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HdfsApiSupplier.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HdfsApiSupplier.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HdfsApiSupplier.java
new file mode 100644
index 0000000..25df8ce
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HdfsApiSupplier.java
@@ -0,0 +1,59 @@
+/*
+ * 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.ambari.view.hive2.internal;
+
+import com.google.common.base.Optional;
+import org.apache.ambari.view.ViewContext;
+import org.apache.ambari.view.utils.hdfs.HdfsApi;
+import org.apache.ambari.view.utils.hdfs.HdfsApiException;
+import org.apache.ambari.view.utils.hdfs.HdfsUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class HdfsApiSupplier implements ContextSupplier<Optional<HdfsApi>> {
+
+ protected final Logger LOG =
+ LoggerFactory.getLogger(getClass());
+
+ private static final Map<String, HdfsApi> hdfsApiMap = new ConcurrentHashMap<>();
+ private final Object lock = new Object();
+
+ @Override
+ public Optional<HdfsApi> get(ViewContext context) {
+ try {
+ if(!hdfsApiMap.containsKey(context.getInstanceName())) {
+ synchronized (lock) {
+ if(!hdfsApiMap.containsKey(context.getInstanceName())) {
+ LOG.debug("Creating HDFSApi instance for Viewname: {}, Instance Name: {}", context.getViewName(), context.getInstanceName());
+ HdfsApi api = HdfsUtil.connectToHDFSApi(context);
+ hdfsApiMap.put(context.getInstanceName(), api);
+ return Optional.of(api);
+ }
+ }
+ }
+ return Optional.of(hdfsApiMap.get(context.getInstanceName()));
+ } catch (HdfsApiException e) {
+ LOG.error("Cannot get the HDFS API", e);
+ return Optional.absent();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HiveConnectionWrapper.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HiveConnectionWrapper.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HiveConnectionWrapper.java
new file mode 100644
index 0000000..811df23
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HiveConnectionWrapper.java
@@ -0,0 +1,109 @@
+/*
+ * 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.ambari.view.hive2.internal;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Supplier;
+import org.apache.hive.jdbc.HiveConnection;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+/**
+ * Composition over a Hive jdbc connection
+ * This class only provides a connection over which
+ * callers should run their own JDBC statements
+ */
+public class HiveConnectionWrapper implements Connectable,Supplier<HiveConnection> {
+
+ private static String DRIVER_NAME = "org.apache.hive.jdbc.HiveDriver";
+ private final String jdbcUrl;
+ private final String username;
+ private final String password;
+
+ private HiveConnection connection = null;
+
+ public HiveConnectionWrapper(String jdbcUrl, String username, String password) {
+ this.jdbcUrl = jdbcUrl;
+ this.username = username;
+ this.password = password;
+ }
+
+
+ @Override
+ public void connect() throws ConnectionException {
+ try {
+ Class.forName(DRIVER_NAME);
+ } catch (ClassNotFoundException e) {
+ throw new ConnectionException(e, "Cannot load the hive JDBC driver");
+ }
+
+ try {
+ Connection conn = DriverManager.getConnection(jdbcUrl, username, password);
+ connection = (HiveConnection)conn;
+
+ } catch (SQLException e) {
+ throw new ConnectionException(e, "Cannot open a hive connection with connect string " + jdbcUrl);
+ }
+
+
+ }
+
+ @Override
+ public void reconnect() throws ConnectionException {
+
+ }
+
+ @Override
+ public void disconnect() throws ConnectionException {
+ if (connection != null) {
+ try {
+ connection.close();
+ } catch (SQLException e) {
+ throw new ConnectionException(e, "Cannot close the hive connection with connect string " + jdbcUrl);
+ }
+ }
+ }
+
+ public Optional<HiveConnection> getConnection() {
+ return Optional.fromNullable(connection);
+ }
+
+ @Override
+ public boolean isOpen() {
+ try {
+ return connection != null && !connection.isClosed() && connection.isValid(100);
+ } catch (SQLException e) {
+ // in case of an SQ error just return
+ return false;
+ }
+ }
+
+ /**
+ * Retrieves an instance of the appropriate type. The returned object may or
+ * may not be a new instance, depending on the implementation.
+ *
+ * @return an instance of the appropriate type
+ */
+ @Override
+ public HiveConnection get() {
+ return null;
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HiveQuery.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HiveQuery.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HiveQuery.java
new file mode 100644
index 0000000..29f0db3
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HiveQuery.java
@@ -0,0 +1,71 @@
+/*
+ * 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.ambari.view.hive2.internal;
+
+
+import com.google.common.base.Function;
+import com.google.common.collect.FluentIterable;
+
+import javax.annotation.Nullable;
+import java.util.Arrays;
+import java.util.Collection;
+
+/**
+ * Holder for query submitted by the user
+ * This may contain multiple hive queries
+ */
+public class HiveQuery {
+
+ private String query;
+
+ public HiveQuery(String query) {
+ this.query = query;
+ }
+
+ public HiveQueries fromMultiLineQuery(String multiLineQuery){
+ return new HiveQueries(multiLineQuery);
+ }
+
+
+ public static class HiveQueries{
+
+ static final String QUERY_SEP = ";";
+ Collection<HiveQuery> hiveQueries;
+
+ private HiveQueries(String userQuery) {
+ hiveQueries = FluentIterable.from(Arrays.asList(userQuery.split(QUERY_SEP)))
+ .transform(new Function<String, HiveQuery>() {
+ @Nullable
+ @Override
+ public HiveQuery apply(@Nullable String input) {
+ return new HiveQuery(input.trim());
+ }
+ }).toList();
+ }
+
+
+
+
+
+ }
+
+
+
+
+};
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HiveResult.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HiveResult.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HiveResult.java
new file mode 100644
index 0000000..25696d2
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HiveResult.java
@@ -0,0 +1,160 @@
+/*
+ * 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.ambari.view.hive2.internal;
+
+import com.google.common.collect.Lists;
+
+import java.math.RoundingMode;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.text.DecimalFormat;
+import java.text.NumberFormat;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+public class HiveResult implements Iterator<HiveResult.Row> {
+
+ public static final String NULL = "NULL";
+ private static final int DEFAULT_BATCH_SIZE = 50;
+ private static ResultSetMetaData metaData;
+ private Row colNames;
+ private NumberFormat nf = new DecimalFormat();
+ private List<Row> rows = Lists.newArrayList();
+
+ public HiveResult(ResultSet rs) throws SQLException {
+ nf.setRoundingMode(RoundingMode.FLOOR);
+ nf.setMinimumFractionDigits(0);
+ nf.setMaximumFractionDigits(2);
+ metaData = rs.getMetaData();
+ int columnCount = metaData.getColumnCount();
+ colNames = new Row(columnCount);
+ int index = 0;
+ while (rs.next() && index <DEFAULT_BATCH_SIZE){
+ index ++;
+ rows.add(new Row(columnCount,rs));
+ }
+
+
+ }
+
+ public List<Row> getRows(){
+ return rows;
+ }
+
+ public List<Row> getData() {
+ return rows;
+ }
+
+ /**
+ * use the lists iterator
+ *
+ * @return {@code true} if the iteration has more elements
+ */
+ @Override
+ public boolean hasNext() {
+ return rows.iterator().hasNext();
+ }
+
+ /**
+ * Returns the next row in the iteration.
+ *
+ * @return the next element in the iteration
+ */
+ @Override
+ public Row next() {
+ return rows.iterator().next();
+ }
+
+ /**
+ * Removes from the underlying collection the last element returned
+ * by this iterator (optional operation). This method can be called
+ * only once per call to {@link #next}. The behavior of an iterator
+ * is unspecified if the underlying collection is modified while the
+ * iteration is in progress in any way other than by calling this
+ * method.
+ *
+ * @throws UnsupportedOperationException if the {@code remove}
+ * operation is not supported by this iterator
+ * @throws IllegalStateException if the {@code next} method has not
+ * yet been called, or the {@code remove} method has already
+ * been called after the last call to the {@code next}
+ * method
+ */
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ public Row getColNames() {
+ return colNames;
+ }
+
+
+ @Override
+ public String toString() {
+ return "HiveResult{" +
+ "colNames=" + colNames +
+ ", rows=" + rows +
+ '}';
+ }
+
+ public class Row {
+ String[] values;
+
+ public Row(int size) throws SQLException {
+ values = new String[size];
+ for (int i = 0; i < size; i++) {
+ values[i] = metaData.getColumnLabel(i + 1);
+ }
+ }
+
+
+ public Row(int size, ResultSet rs) throws SQLException {
+ values = new String[size];
+ for (int i = 0; i < size; i++) {
+ if (nf != null) {
+ Object object = rs.getObject(i + 1);
+ if (object == null) {
+ values[i] = null;
+ } else if (object instanceof Number) {
+ values[i] = nf.format(object);
+ } else {
+ values[i] = object.toString();
+ }
+ } else {
+ values[i] = rs.getString(i + 1);
+ }
+ values[i] = values[i] == null ? NULL : values[i];
+
+ }
+
+ }
+
+ @Override
+ public String toString() {
+ return "Row{" +
+ "values=" + Arrays.toString(values) +
+ '}';
+ }
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HiveTask.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HiveTask.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HiveTask.java
new file mode 100644
index 0000000..3a682ec
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HiveTask.java
@@ -0,0 +1,53 @@
+/*
+ * 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.ambari.view.hive2.internal;
+
+public interface HiveTask {
+
+ /**
+ * The task id for this task
+ * @return task Id
+ */
+ Long getId();
+
+ /**
+ * The user for which this task was submitted
+ * @return
+ */
+ String getUser();
+
+ /**
+ * The view instance tied to this task
+ * @return
+ */
+ String getInstance();
+
+ /**
+ * Connection properties pulled from the view context and request
+ * @return
+ */
+
+ //Connectable getConnectionClass();
+
+ ConnectionProperties getConnectionProperties();
+
+ HiveQuery.HiveQueries getQueries();
+
+
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HiveTaskMessage.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HiveTaskMessage.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HiveTaskMessage.java
new file mode 100644
index 0000000..7002f28
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/internal/HiveTaskMessage.java
@@ -0,0 +1,118 @@
+/*
+ * 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.ambari.view.hive2.internal;
+
+public class HiveTaskMessage implements HiveTask {
+
+ private Long id;
+ private String instance;
+ private ConnectionProperties connectionProps;
+ private HiveQuery.HiveQueries queries;
+ //private Connectable connectable = new HiveConnectionWrapper(connectMessage);
+
+
+ public void setConnectionProps(ConnectionProperties connectionProps) {
+ this.connectionProps = connectionProps;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public void setInstance(String instance) {
+ this.instance = instance;
+ }
+
+ public void setQueries(HiveQuery.HiveQueries queries) {
+ this.queries = queries;
+ }
+
+
+ /*public void setConnectable(Connectable connectable) {
+ this.connectable = connectable;
+ }*/
+
+ /**
+ * The task id for this task
+ *
+ * @return task Id
+ */
+ @Override
+ public Long getId() {
+ return id;
+ }
+
+ /**
+ * The user for which this task was submitted
+ *
+ * @return
+ */
+ @Override
+ public String getUser() {
+ return connectionProps.getUserName();
+ }
+
+ /**
+ * The view instance tied to this task
+ *
+ * @return
+ */
+ @Override
+ public String getInstance() {
+ return instance;
+ }
+
+ /**
+ * Connection properties pulled from the view context and request
+ *
+ * @return
+ */
+ /*@Override
+ public Connectable getConnectionClass() {
+ return connectable;
+ }
+*/
+ /**
+ * Connection properties pulled from the view context and request
+ *
+ * @return
+ */
+ @Override
+ public ConnectionProperties getConnectionProperties() {
+ return connectionProps;
+ }
+
+ @Override
+ public HiveQuery.HiveQueries getQueries() {
+ return queries;
+ }
+
+
+ @Override
+ public String toString() {
+ return "HiveTaskMessage{" +
+ "connectionProps=" + connectionProps +
+ ", id=" + id +
+ ", instance='" + instance + '\'' +
+ ", queries=" + queries +
+ '}';
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/DataStoreStorage.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/DataStoreStorage.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/DataStoreStorage.java
new file mode 100644
index 0000000..9adf129
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/DataStoreStorage.java
@@ -0,0 +1,172 @@
+/**
+ * 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.ambari.view.hive2.persistence;
+
+import org.apache.ambari.view.PersistenceException;
+import org.apache.ambari.view.ViewContext;
+import org.apache.ambari.view.hive2.persistence.utils.FilteringStrategy;
+import org.apache.ambari.view.hive2.persistence.utils.Indexed;
+import org.apache.ambari.view.hive2.persistence.utils.ItemNotFound;
+import org.apache.ambari.view.hive2.persistence.utils.OnlyOwnersFilteringStrategy;
+import org.apache.ambari.view.hive2.utils.ServiceFormattedException;
+import org.apache.commons.beanutils.BeanUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.beans.Transient;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Engine for storing objects to context DataStore storage
+ */
+public class DataStoreStorage implements Storage {
+ private final static Logger LOG =
+ LoggerFactory.getLogger(DataStoreStorage.class);
+
+ protected ViewContext context;
+
+ /**
+ * Constructor
+ * @param context View Context instance
+ */
+ public DataStoreStorage(ViewContext context) {
+ this.context = context;
+ }
+
+ @Override
+ public synchronized void store(Class model, Indexed obj) {
+ assignId(model, obj);
+
+ Indexed newBean;
+ try {
+ newBean = (Indexed) BeanUtils.cloneBean(obj);
+ } catch (IllegalAccessException e) {
+ throw new ServiceFormattedException("S010 Data storage error", e);
+ } catch (InstantiationException e) {
+ throw new ServiceFormattedException("S010 Data storage error", e);
+ } catch (InvocationTargetException e) {
+ throw new ServiceFormattedException("S010 Data storage error", e);
+ } catch (NoSuchMethodException e) {
+ throw new ServiceFormattedException("S010 Data storage error", e);
+ }
+ preprocessEntity(newBean);
+
+ try {
+ context.getDataStore().store(newBean);
+ } catch (PersistenceException e) {
+ throw new ServiceFormattedException("S020 Data storage error", e);
+ }
+ }
+
+ public void assignId(Class model, Indexed obj) {
+ if (obj.getId() == null) {
+ String id = nextIdForEntity(context, model);
+ obj.setId(id);
+ }
+ }
+
+ private void preprocessEntity(Indexed obj) {
+ cleanTransientFields(obj);
+ }
+
+ private void cleanTransientFields(Indexed obj) {
+ for (Method m : obj.getClass().getMethods()) {
+ Transient aTransient = m.getAnnotation(Transient.class);
+ if (aTransient != null && m.getName().startsWith("set")) {
+ try {
+ m.invoke(obj, new Object[]{ null });
+ } catch (IllegalAccessException e) {
+ throw new ServiceFormattedException("S030 Data storage error", e);
+ } catch (InvocationTargetException e) {
+ throw new ServiceFormattedException("S030 Data storage error", e);
+ }
+ }
+ }
+ }
+
+ private static synchronized String nextIdForEntity(ViewContext context, Class aClass) {
+ // auto increment id implementation
+ String lastId = context.getInstanceData(aClass.getName());
+ int newId;
+ if (lastId == null) {
+ newId = 1;
+ } else {
+ newId = Integer.parseInt(lastId) + 1;
+ }
+ context.putInstanceData(aClass.getName(), String.valueOf(newId));
+ return String.valueOf(newId);
+ }
+
+ @Override
+ public synchronized <T extends Indexed> T load(Class<T> model, Object id) throws ItemNotFound {
+ LOG.debug(String.format("Loading %s #%s", model.getName(), id));
+ try {
+ T obj = context.getDataStore().find(model, id);
+ if (obj != null) {
+ return obj;
+ } else {
+ throw new ItemNotFound();
+ }
+ } catch (PersistenceException e) {
+ throw new ServiceFormattedException("S040 Data storage error", e);
+ }
+ }
+
+ @Override
+ public synchronized <T extends Indexed> List<T> loadAll(Class<? extends T> model, FilteringStrategy filter) {
+ LinkedList<T> list = new LinkedList<T>();
+ LOG.debug(String.format("Loading all %s-s", model.getName()));
+ try {
+ for(T item: context.getDataStore().findAll(model, filter.whereStatement())) {
+ list.add(item);
+ }
+ } catch (PersistenceException e) {
+ throw new ServiceFormattedException("S050 Data storage error", e);
+ }
+ return list;
+ }
+
+ @Override
+ public synchronized <T extends Indexed> List<T> loadAll(Class<T> model) {
+ return loadAll(model, new OnlyOwnersFilteringStrategy(this.context.getUsername()));
+ }
+
+ @Override
+ public synchronized void delete(Class model, Object id) throws ItemNotFound {
+ LOG.debug(String.format("Deleting %s:%s", model.getName(), id));
+ Object obj = load(model, id);
+ try {
+ context.getDataStore().remove(obj);
+ } catch (PersistenceException e) {
+ throw new ServiceFormattedException("S060 Data storage error", e);
+ }
+ }
+
+ @Override
+ public boolean exists(Class model, Object id) {
+ try {
+ return context.getDataStore().find(model, id) != null;
+ } catch (PersistenceException e) {
+ throw new ServiceFormattedException("S070 Data storage error", e);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/IStorageFactory.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/IStorageFactory.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/IStorageFactory.java
new file mode 100644
index 0000000..6c39359
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/IStorageFactory.java
@@ -0,0 +1,23 @@
+/**
+ * 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.ambari.view.hive2.persistence;
+
+public interface IStorageFactory {
+ Storage getStorage();
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/InstanceKeyValueStorage.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/InstanceKeyValueStorage.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/InstanceKeyValueStorage.java
new file mode 100644
index 0000000..86f2bfc
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/InstanceKeyValueStorage.java
@@ -0,0 +1,132 @@
+/**
+ * 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.ambari.view.hive2.persistence;
+
+import org.apache.ambari.view.ViewContext;
+import org.apache.ambari.view.hive2.persistence.utils.ContextConfigurationAdapter;
+import org.apache.ambari.view.hive2.utils.ServiceFormattedException;
+import org.apache.commons.configuration.Configuration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.WebApplicationException;
+
+
+/**
+ * Persistent storage engine for storing java beans to
+ * instance data
+ */
+@Deprecated
+public class InstanceKeyValueStorage extends KeyValueStorage {
+ private final static Logger LOG =
+ LoggerFactory.getLogger(InstanceKeyValueStorage.class);
+
+ private ContextConfigurationAdapter config = null;
+ private int VALUE_LENGTH_LIMIT = 254;
+
+ /**
+ * Constructor.
+ * @param context View Context instance
+ */
+ public InstanceKeyValueStorage(ViewContext context) {
+ super(context);
+ }
+
+ /**
+ * Returns config instance, adapter to Persistence API
+ * @return config instance
+ */
+ @Override
+ protected synchronized Configuration getConfig() {
+ if (config == null) {
+ config = new ContextConfigurationAdapter(context);
+ }
+ return config;
+ }
+
+ /**
+ * Value is limited to 256 symbols, this code splits value into chunks and saves them as <key>#<chunk_id>
+ * @param modelPropName key
+ * @param json value
+ */
+ protected void write(String modelPropName, String json) {
+ int saved = 0;
+ int page = 1;
+ while (saved < json.length()) {
+ int end = Math.min(saved + VALUE_LENGTH_LIMIT, json.length());
+ String substring = json.substring(saved, end);
+ getConfig().setProperty(modelPropName + "#" + page, substring);
+ saved += VALUE_LENGTH_LIMIT;
+ page += 1;
+ LOG.debug("Chunk saved: " + modelPropName + "#" + page + "=" + substring);
+ }
+ getConfig().setProperty(modelPropName, page - 1);
+ LOG.debug("Write finished: " + modelPropName + " pages:" + (page - 1));
+ }
+
+ /**
+ * Read chunked value (keys format <key>#<chunk_id>)
+ * @param modelPropName key
+ * @return value
+ */
+ protected String read(String modelPropName) {
+ StringBuilder result = new StringBuilder();
+ int pages = getConfig().getInt(modelPropName);
+ LOG.debug("Read started: " + modelPropName + " pages:" + pages);
+
+ for(int page = 1; page <= pages; page++) {
+ String substring = getConfig().getString(modelPropName + "#" + page);
+ LOG.debug("Chunk read: " + modelPropName + "#" + page + "=" + substring);
+ if (substring != null) {
+ result.append(substring);
+ }
+ }
+
+ return result.toString();
+ }
+
+ /**
+ * Remove chunked value (keys format <key>#<chunk_id>)
+ * @param modelPropName key
+ */
+ protected void clear(String modelPropName) {
+ int pages = getConfig().getInt(modelPropName);
+ LOG.debug("Clean started: " + modelPropName + " pages:" + pages);
+
+ for(int page = 1; page <= pages; page++) {
+ getConfig().clearProperty(modelPropName + "#" + page);
+ LOG.debug("Chunk clean: " + modelPropName + "#" + page);
+ }
+ getConfig().clearProperty(modelPropName);
+ }
+
+ public static void storageSmokeTest(ViewContext context) {
+ try {
+ final String property = "test.smoke.property";
+ context.putInstanceData(property, "42");
+ boolean status = context.getInstanceData(property).equals("42");
+ context.removeInstanceData(property);
+ if (!status) throw new ServiceFormattedException("Ambari Views instance data DB doesn't work properly", null);
+ } catch (WebApplicationException ex) {
+ throw ex;
+ } catch (Exception ex) {
+ throw new ServiceFormattedException(ex.getMessage(), ex);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/KeyValueStorage.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/KeyValueStorage.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/KeyValueStorage.java
new file mode 100644
index 0000000..7b9c61c
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/KeyValueStorage.java
@@ -0,0 +1,163 @@
+/**
+ * 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.ambari.view.hive2.persistence;
+
+import com.google.gson.Gson;
+import org.apache.ambari.view.ViewContext;
+import org.apache.ambari.view.hive2.persistence.utils.FilteringStrategy;
+import org.apache.ambari.view.hive2.persistence.utils.Indexed;
+import org.apache.ambari.view.hive2.persistence.utils.ItemNotFound;
+import org.apache.ambari.view.hive2.persistence.utils.OnlyOwnersFilteringStrategy;
+import org.apache.commons.configuration.Configuration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Engine for storing objects to key-value storage
+ */
+public abstract class KeyValueStorage implements Storage {
+ private final static Logger LOG =
+ LoggerFactory.getLogger(KeyValueStorage.class);
+ protected final Gson gson = new Gson();
+ protected ViewContext context;
+
+ /**
+ * Constructor
+ * @param context View Context instance
+ */
+ public KeyValueStorage(ViewContext context) {
+ this.context = context;
+ }
+
+ /**
+ * Returns config instance, adapter to Persistence API
+ * @return config instance
+ */
+ protected abstract Configuration getConfig();
+
+ @Override
+ public <T extends Indexed> void store(Class<T> model, Indexed obj) {
+ String modelIndexingPropName = getIndexPropertyName(model);
+
+ if (obj.getId() == null) {
+ int lastIndex = getConfig().getInt(modelIndexingPropName, 0);
+ lastIndex ++;
+ getConfig().setProperty(modelIndexingPropName, lastIndex);
+ obj.setId(String.valueOf(lastIndex));
+ }
+
+ String modelPropName = getItemPropertyName(model, obj.getId());
+ String json = serialize(obj);
+ write(modelPropName, json);
+ }
+
+ @Override
+ public <T extends Indexed> T load(Class<T> model, Object id) throws ItemNotFound {
+ String modelPropName = getItemPropertyName(model, id);
+ LOG.debug(String.format("Loading %s", modelPropName));
+ if (getConfig().containsKey(modelPropName)) {
+ String json = read(modelPropName);
+ LOG.debug(String.format("json: %s", json));
+
+ return deserialize(model, json);
+ } else {
+ throw new ItemNotFound();
+ }
+ }
+
+ /**
+ * Write json to storage
+ * @param modelPropName key
+ * @param json value
+ */
+ protected void write(String modelPropName, String json) {
+ getConfig().setProperty(modelPropName, json);
+ }
+
+ /**
+ * Read json from storage
+ * @param modelPropName key
+ * @return value
+ */
+ protected String read(String modelPropName) {
+ return getConfig().getString(modelPropName);
+ }
+
+ /**
+ * Remove line from storage
+ * @param modelPropName key
+ */
+ protected void clear(String modelPropName) {
+ getConfig().clearProperty(modelPropName);
+ }
+
+ protected String serialize(Indexed obj) {
+ return gson.toJson(obj);
+ }
+
+ protected <T extends Indexed> T deserialize(Class<T> model, String json) {
+ return gson.fromJson(json, model);
+ }
+
+ @Override
+ public synchronized <T extends Indexed> List<T> loadAll(Class<? extends T> model, FilteringStrategy filter) {
+ ArrayList<T> list = new ArrayList<T>();
+ String modelIndexingPropName = getIndexPropertyName(model);
+ LOG.debug(String.format("Loading all %s-s", model.getName()));
+ int lastIndex = getConfig().getInt(modelIndexingPropName, 0);
+ for(int i=1; i<=lastIndex; i++) {
+ try {
+ T item = load(model, i);
+ if ((filter == null) || filter.isConform(item)) {
+ list.add(item);
+ }
+ } catch (ItemNotFound ignored) {
+ }
+ }
+ return list;
+ }
+
+ @Override
+ public synchronized <T extends Indexed> List<T> loadAll(Class<T> model) {
+ return loadAll(model, new OnlyOwnersFilteringStrategy(this.context.getUsername()));
+ }
+
+ @Override
+ public synchronized void delete(Class model, Object id) {
+ LOG.debug(String.format("Deleting %s:%s", model.getName(), id));
+ String modelPropName = getItemPropertyName(model, id);
+ clear(modelPropName);
+ }
+
+ @Override
+ public boolean exists(Class model, Object id) {
+ return getConfig().containsKey(getItemPropertyName(model, id));
+ }
+
+ private String getIndexPropertyName(Class model) {
+ return String.format("%s:index", model.getName());
+ }
+
+ private String getItemPropertyName(Class model, Object id) {
+ return String.format("%s.%s", model.getName(), id);
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/LocalKeyValueStorage.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/LocalKeyValueStorage.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/LocalKeyValueStorage.java
new file mode 100644
index 0000000..7293363
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/LocalKeyValueStorage.java
@@ -0,0 +1,69 @@
+/**
+ * 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.ambari.view.hive2.persistence;
+
+import org.apache.ambari.view.ViewContext;
+import org.apache.ambari.view.hive2.utils.MisconfigurationFormattedException;
+import org.apache.commons.configuration.ConfigurationException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Persistent storage engine for storing java beans to
+ * properties file
+ * Path to file should be in 'dataworker.storagePath' parameter
+ */
+@Deprecated
+public class LocalKeyValueStorage extends KeyValueStorage {
+ private final static Logger LOG =
+ LoggerFactory.getLogger(LocalKeyValueStorage.class);
+
+ private PersistentConfiguration config = null;
+
+ /**
+ * Constructor
+ * @param context View Context instance
+ */
+ public LocalKeyValueStorage(ViewContext context) {
+ super(context);
+ }
+
+ /**
+ * Returns config instance
+ * @return config instance
+ */
+ @Override
+ protected synchronized PersistentConfiguration getConfig() {
+ if (config == null) {
+ String fileName = context.getProperties().get("dataworker.storagePath");
+ if (fileName == null) {
+ String msg = "dataworker.storagePath is not configured!";
+ LOG.error(msg);
+ throw new MisconfigurationFormattedException("dataworker.storagePath");
+ }
+ try {
+ config = new PersistentConfiguration(fileName);
+ } catch (ConfigurationException e) {
+ e.printStackTrace();
+ }
+ }
+ return config;
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/PersistentConfiguration.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/PersistentConfiguration.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/PersistentConfiguration.java
new file mode 100644
index 0000000..222225d
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/PersistentConfiguration.java
@@ -0,0 +1,52 @@
+/**
+ * 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.ambari.view.hive2.persistence;
+
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.PropertiesConfiguration;
+import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
+
+import java.io.File;
+
+/**
+ * Configuration enables all necessary options for PropertiesConfiguration:
+ * auto-save, auto-reloading, no delimiter parsing and other
+ */
+@Deprecated
+public class PersistentConfiguration extends PropertiesConfiguration {
+ /**
+ * Constructor
+ * @param fileName path to data file
+ * @throws ConfigurationException
+ */
+ public PersistentConfiguration(String fileName) throws ConfigurationException {
+ super();
+
+ File config = new File(fileName);
+ setFile(config);
+ this.setAutoSave(true);
+ this.setReloadingStrategy(new FileChangedReloadingStrategy());
+ this.setDelimiterParsingDisabled(true);
+ this.setListDelimiter((char) 0);
+
+ if (config.exists()) {
+ this.load();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/Storage.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/Storage.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/Storage.java
new file mode 100644
index 0000000..2c962ba
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/Storage.java
@@ -0,0 +1,77 @@
+/**
+ * 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.ambari.view.hive2.persistence;
+
+import org.apache.ambari.view.hive2.persistence.utils.FilteringStrategy;
+import org.apache.ambari.view.hive2.persistence.utils.Indexed;
+import org.apache.ambari.view.hive2.persistence.utils.ItemNotFound;
+
+import java.util.List;
+
+/**
+ * Object storage interface
+ */
+public interface Storage {
+ /**
+ * Persist object to DB. It should be Indexed
+ * @param obj object to save
+ */
+ <T extends Indexed> void store(Class<T> model, Indexed obj);
+
+ /**
+ * Load object
+ * @param model bean class
+ * @param id identifier
+ * @return bean instance
+ * @throws ItemNotFound thrown if item with id was not found in DB
+ */
+ <T extends Indexed> T load(Class<T> model, Object id) throws ItemNotFound;
+
+ /**
+ * Load all objects of given bean class
+ * @param model bean class
+ * @param filter filtering strategy (return only those objects that conform condition)
+ * @param <T> bean class
+ * @return list of filtered objects
+ */
+ <T extends Indexed> List<T> loadAll(Class<? extends T> model, FilteringStrategy filter);
+
+ /**
+ * Load all objects of given bean class
+ * @param model bean class
+ * @param <T> bean class
+ * @return list of all objects
+ */
+ <T extends Indexed> List<T> loadAll(Class<T> model);
+
+ /**
+ * Delete object
+ * @param model bean class
+ * @param id identifier
+ */
+ void delete(Class model, Object id) throws ItemNotFound;
+
+ /**
+ * Check is object exists
+ * @param model bean class
+ * @param id identifier
+ * @return true if exists
+ */
+ boolean exists(Class model, Object id);
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/utils/ContextConfigurationAdapter.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/utils/ContextConfigurationAdapter.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/utils/ContextConfigurationAdapter.java
new file mode 100644
index 0000000..8c907b8
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/utils/ContextConfigurationAdapter.java
@@ -0,0 +1,260 @@
+/**
+ * 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.ambari.view.hive2.persistence.utils;
+
+import org.apache.ambari.view.ViewContext;
+import org.apache.commons.configuration.Configuration;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * Persistence API to Apache Configuration adapter
+ */
+@Deprecated
+public class ContextConfigurationAdapter implements Configuration {
+ private ViewContext context;
+
+ /**
+ * Constructor of adapter
+ * @param context View Context
+ */
+ public ContextConfigurationAdapter(ViewContext context) {
+ this.context = context;
+ }
+
+ @Override
+ public Configuration subset(String prefix) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return context.getInstanceData().isEmpty();
+ }
+
+ @Override
+ public boolean containsKey(String s) {
+ Map<String, String> data = context.getInstanceData();
+ return data.containsKey(s);
+ }
+
+ @Override
+ public void addProperty(String s, Object o) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void setProperty(String s, Object o) {
+ context.putInstanceData(s, o.toString());
+ }
+
+ @Override
+ public void clearProperty(String key) {
+ context.removeInstanceData(key);
+ }
+
+ @Override
+ public void clear() {
+ for (String key : context.getInstanceData().keySet())
+ context.removeInstanceData(key);
+ }
+
+ @Override
+ public Object getProperty(String key) {
+ return context.getInstanceData(key);
+ }
+
+ @Override
+ public Iterator getKeys(String s) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Iterator getKeys() {
+ return context.getInstanceData().keySet().iterator();
+ }
+
+ @Override
+ public Properties getProperties(String s) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean getBoolean(String s) {
+ return getBoolean(s, null);
+ }
+
+ @Override
+ public boolean getBoolean(String s, boolean b) {
+ return getBoolean(s, (Boolean)b);
+ }
+
+ @Override
+ public Boolean getBoolean(String s, Boolean aBoolean) {
+ String data = context.getInstanceData(s);
+ return (data != null)?Boolean.parseBoolean(data):aBoolean;
+ }
+
+ @Override
+ public byte getByte(String s) {
+ return getByte(s, null);
+ }
+
+ @Override
+ public byte getByte(String s, byte b) {
+ return getByte(s, (Byte)b);
+ }
+
+ @Override
+ public Byte getByte(String s, Byte aByte) {
+ String data = context.getInstanceData(s);
+ return (data != null)?Byte.parseByte(data):aByte;
+ }
+
+ @Override
+ public double getDouble(String s) {
+ return getDouble(s, null);
+ }
+
+ @Override
+ public double getDouble(String s, double v) {
+ return getDouble(s, (Double)v);
+ }
+
+ @Override
+ public Double getDouble(String s, Double aDouble) {
+ String data = context.getInstanceData(s);
+ return (data != null)?Double.parseDouble(data):aDouble;
+ }
+
+ @Override
+ public float getFloat(String s) {
+ return getFloat(s, null);
+ }
+
+ @Override
+ public float getFloat(String s, float v) {
+ return getFloat(s, (Float)v);
+ }
+
+ @Override
+ public Float getFloat(String s, Float aFloat) {
+ String data = context.getInstanceData(s);
+ return (data != null)?Float.parseFloat(data):aFloat;
+ }
+
+ @Override
+ public int getInt(String s) {
+ return getInteger(s, null);
+ }
+
+ @Override
+ public int getInt(String s, int i) {
+ return getInteger(s, i);
+ }
+
+ @Override
+ public Integer getInteger(String s, Integer integer) {
+ String data = context.getInstanceData(s);
+ return (data != null)?Integer.parseInt(data):integer;
+ }
+
+ @Override
+ public long getLong(String s) {
+ return getLong(s, null);
+ }
+
+ @Override
+ public long getLong(String s, long l) {
+ return getLong(s, (Long)l);
+ }
+
+ @Override
+ public Long getLong(String s, Long aLong) {
+ String data = context.getInstanceData(s);
+ return (data != null)?Long.parseLong(data):aLong;
+ }
+
+ @Override
+ public short getShort(String s) {
+ return getShort(s, null);
+ }
+
+ @Override
+ public short getShort(String s, short i) {
+ return getShort(s, (Short)i);
+ }
+
+ @Override
+ public Short getShort(String s, Short aShort) {
+ String data = context.getInstanceData(s);
+ return (data != null)?Short.parseShort(data):aShort;
+ }
+
+ @Override
+ public BigDecimal getBigDecimal(String s) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public BigDecimal getBigDecimal(String s, BigDecimal bigDecimal) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public BigInteger getBigInteger(String s) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public BigInteger getBigInteger(String s, BigInteger bigInteger) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public String getString(String s) {
+ return context.getInstanceData(s);
+ }
+
+ @Override
+ public String getString(String s, String s2) {
+ String data = getString(s);
+ return (data != null)?data:s2;
+ }
+
+ @Override
+ public String[] getStringArray(String s) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public List getList(String s) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public List getList(String s, List list) {
+ throw new UnsupportedOperationException();
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/utils/FilteringStrategy.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/utils/FilteringStrategy.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/utils/FilteringStrategy.java
new file mode 100644
index 0000000..cec204a
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/utils/FilteringStrategy.java
@@ -0,0 +1,32 @@
+/**
+ * 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.ambari.view.hive2.persistence.utils;
+
+/**
+ * Filtering strategy for stored objects
+ */
+public interface FilteringStrategy {
+ /**
+ * Check whether item conforms chosen filter or not
+ * @param item item to check
+ * @return true if item conforms this filter
+ */
+ boolean isConform(Indexed item);
+ String whereStatement();
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/utils/Indexed.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/utils/Indexed.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/utils/Indexed.java
new file mode 100644
index 0000000..c45e32c
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/persistence/utils/Indexed.java
@@ -0,0 +1,36 @@
+/**
+ * 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.ambari.view.hive2.persistence.utils;
+
+/**
+ * Interface to represent item with identifier
+ */
+public interface Indexed {
+ /**
+ * Get the ID
+ * @return ID
+ */
+ String getId();
+
+ /**
+ * Set ID
+ * @param id ID
+ */
+ void setId(String id);
+}