12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- use crate :: { traits:: IntoJava , Result , RT } ;
15
+ use crate :: { traits:: IntoJava , Error , Result , RT } ;
16
16
use arrow:: array:: RecordBatchReader ;
17
17
use arrow:: ffi:: FFI_ArrowSchema ;
18
18
use arrow_schema:: Schema ;
19
19
use jni:: sys:: jlong;
20
20
use jni:: { objects:: JObject , JNIEnv } ;
21
+ use lance:: dataset:: fragment:: FileFragment ;
22
+ use lance:: dataset:: transaction:: Operation ;
21
23
use lance:: dataset:: { Dataset , WriteParams } ;
22
-
24
+ use snafu :: { location , Location } ;
23
25
pub const NATIVE_DATASET : & str = "nativeDatasetHandle" ;
24
26
25
27
#[ derive( Clone ) ]
@@ -42,6 +44,11 @@ impl BlockingDataset {
42
44
Ok ( Self { inner } )
43
45
}
44
46
47
+ pub fn commit ( uri : & str , operation : Operation , read_version : Option < u64 > ) -> Result < Self > {
48
+ let inner = RT . block_on ( Dataset :: commit ( uri, operation, read_version, None , None ) ) ?;
49
+ Ok ( Self { inner } )
50
+ }
51
+
45
52
pub fn count_rows ( & self , filter : Option < String > ) -> Result < usize > {
46
53
Ok ( RT . block_on ( self . inner . count_rows ( filter) ) ?)
47
54
}
@@ -100,7 +107,7 @@ pub extern "system" fn Java_com_lancedb_lance_Dataset_releaseNativeDataset(
100
107
}
101
108
102
109
#[ no_mangle]
103
- pub extern "system" fn Java_com_lancedb_lance_Dataset_getFragmentsIds < ' a > (
110
+ pub extern "system" fn Java_com_lancedb_lance_Dataset_getJsonFragments < ' a > (
104
111
mut env : JNIEnv < ' a > ,
105
112
jdataset : JObject ,
106
113
) -> JObject < ' a > {
@@ -111,13 +118,7 @@ pub extern "system" fn Java_com_lancedb_lance_Dataset_getFragmentsIds<'a>(
111
118
dataset. inner . get_fragments ( )
112
119
} ;
113
120
114
- let array_list = env
115
- . new_int_array ( fragments. len ( ) as i32 )
116
- . expect ( "Failed to create int array" ) ;
117
- let fragment_ids = fragments. iter ( ) . map ( |f| f. id ( ) as i32 ) . collect :: < Vec < _ > > ( ) ;
118
- env. set_int_array_region ( & array_list, 0 , & fragment_ids)
119
- . expect ( "Failed to set int array region" ) ;
120
- array_list. into ( )
121
+ ok_or_throw ! ( env, create_json_fragment_list( & mut env, fragments) )
121
122
}
122
123
123
124
#[ no_mangle]
@@ -133,20 +134,34 @@ pub extern "system" fn Java_com_lancedb_lance_Dataset_importFfiSchema(
133
134
Schema :: from ( dataset. inner . schema ( ) )
134
135
} ;
135
136
let out_c_schema = arrow_schema_addr as * mut FFI_ArrowSchema ;
136
- let c_schema = match FFI_ArrowSchema :: try_from ( & schema) {
137
- Ok ( schema) => schema,
138
- Err ( err) => {
139
- env. throw_new (
140
- "java/lang/RuntimeException" ,
141
- format ! ( "Failed to convert Arrow schema: {}" , err) ,
142
- )
143
- . expect ( "Error throwing exception" ) ;
144
- return ;
145
- }
146
- } ;
137
+ let c_schema = ok_or_throw_without_return ! ( env, FFI_ArrowSchema :: try_from( & schema) ) ;
147
138
148
139
unsafe {
149
140
std:: ptr:: copy ( std:: ptr:: addr_of!( c_schema) , out_c_schema, 1 ) ;
150
141
std:: mem:: forget ( c_schema) ;
151
142
} ;
152
143
}
144
+
145
+ fn create_json_fragment_list < ' a > (
146
+ env : & mut JNIEnv < ' a > ,
147
+ fragments : Vec < FileFragment > ,
148
+ ) -> Result < JObject < ' a > > {
149
+ let array_list_class = env. find_class ( "java/util/ArrayList" ) ?;
150
+
151
+ let array_list = env. new_object ( array_list_class, "()V" , & [ ] ) ?;
152
+
153
+ for fragment in fragments {
154
+ let json_string = serde_json:: to_string ( fragment. metadata ( ) ) . map_err ( |e| Error :: JSON {
155
+ message : e. to_string ( ) ,
156
+ location : location ! ( ) ,
157
+ } ) ?;
158
+ let jstring = env. new_string ( json_string) ?;
159
+ env. call_method (
160
+ & array_list,
161
+ "add" ,
162
+ "(Ljava/lang/Object;)Z" ,
163
+ & [ ( & jstring) . into ( ) ] ,
164
+ ) ?;
165
+ }
166
+ Ok ( array_list)
167
+ }
0 commit comments