roee88 commented on a change in pull request #11067:
URL: https://github.com/apache/arrow/pull/11067#discussion_r704666905



##########
File path: 
java/ffi/src/main/java/org/apache/arrow/ffi/FFIDictionaryProvider.java
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.arrow.ffi;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.arrow.vector.dictionary.Dictionary;
+import org.apache.arrow.vector.dictionary.DictionaryProvider;
+
+/**
+ * A DictionaryProvider that is used in FFI for imports.
+ * <p>
+ * FFIDictionaryProvider is similar to
+ * {@link DictionaryProvider.MapDictionaryProvider} with a key difference that
+ * the dictionaries are owned by the provider so it must eventually be closed.
+ * <p>
+ * The typical usage is to create the FFIDictionaryProvider and pass it to
+ * {@link FFI#importField} or {@link FFI#importSchema} to allocate empty
+ * dictionaries based on the information in {@link ArrowSchema}. Then you can
+ * re-use the same dictionary provider in any function that imports an
+ * {@link ArrowArray} that has the same schema.
+ */
+public class FFIDictionaryProvider implements DictionaryProvider, 
AutoCloseable {
+
+  private final Map<Long, Dictionary> map;
+
+  public FFIDictionaryProvider() {
+    this.map = new HashMap<>();
+  }
+
+  void put(Dictionary dictionary) {
+    Dictionary previous = map.put(dictionary.getEncoding().getId(), 
dictionary);
+    if (previous != null) {

Review comment:
       Dictionaries in Java are implemented very differently than the cpp 
implementation. The approach we took (after struggling with alternatives) is 
having this FFIDictionatyProvider as a reusable owner of imported dictionaries. 
That is, you can create it once and import ArrowArray instances to it. On 
import of batches it is actually expected to import the same IDs multiple times 
(possibly with slightly different values due to deltas I believe but unsure). 
   
   See the mailing list thread for background 
https://lists.apache.org/x/thread.html/rd2aecfe5ad71a6f81240ed5c6f706b1a6b2f4a95b8dd5db515e5fceb@%3Cdev.arrow.apache.org%3E
 




-- 
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]


Reply via email to