comphead commented on code in PR #1:
URL: https://github.com/apache/datafusion-java/pull/1#discussion_r3230641520


##########
src/main/java/org/apache/datafusion/SessionContext.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.datafusion;
+
+/**
+ * A DataFusion session context.
+ *
+ * <p>Instances are <strong>not thread-safe</strong>. Concurrent calls to any 
of {@link #sql},
+ * {@link #registerParquet}, or {@link #close} from different threads can 
produce a use-after-free
+ * on the native side. Callers must externally synchronize, or confine each 
context to a single
+ * thread.
+ */
+public final class SessionContext implements AutoCloseable {
+  static {
+    NativeLibraryLoader.loadLibrary();
+  }
+
+  private long nativeHandle;
+
+  public SessionContext() {
+    this.nativeHandle = createSessionContext();
+    if (this.nativeHandle == 0) {
+      throw new RuntimeException("Failed to create native SessionContext");
+    }
+  }
+
+  /**
+   * Parse and plan {@code query}, returning a lazy {@link DataFrame}. The 
query is not executed
+   * until {@link DataFrame#collect} is called.
+   */
+  public DataFrame sql(String query) {
+    if (nativeHandle == 0) {
+      throw new IllegalStateException("SessionContext is closed");
+    }
+    long dfHandle = createDataFrame(nativeHandle, query);
+    return new DataFrame(dfHandle);
+  }
+
+  public void registerParquet(String name, String path) {
+    if (nativeHandle == 0) {
+      throw new IllegalStateException("SessionContext is closed");
+    }
+    registerParquet(nativeHandle, name, path);
+  }
+
+  @Override
+  public void close() {
+    if (nativeHandle != 0) {
+      closeSessionContext(nativeHandle);
+      nativeHandle = 0;
+    }
+  }
+
+  private static native long createSessionContext();
+
+  private static native long createDataFrame(long handle, String sql);

Review Comment:
   maybe we can specify what kind of handle expected in methods? like 
sessionHandle?



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to