Skip to content

Commit

Permalink
feat: support write,read,delete with template (#5156)
Browse files Browse the repository at this point in the history
Signed-off-by: ZhangJian He <[email protected]>
  • Loading branch information
shoothzj authored Oct 5, 2024
1 parent 865fe58 commit 579425c
Show file tree
Hide file tree
Showing 16 changed files with 422 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

package org.apache.opendal.spring.config;

import org.apache.opendal.AsyncOperator;
import org.apache.opendal.spring.core.DefaultOpenDALSerializerFactory;
import org.apache.opendal.spring.core.OpenDALSerializerFactory;
import org.apache.opendal.spring.core.ReactiveOpenDALTemplate;
import org.apache.opendal.spring.core.OpenDALOperations;
import org.apache.opendal.spring.core.OpenDALProperties;
import org.apache.opendal.spring.core.OpenDALSerializerFactory;
import org.apache.opendal.spring.core.ReactiveOpenDALOperations;
import org.apache.opendal.spring.core.ReactiveOpenDALTemplate;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
Expand All @@ -40,8 +42,14 @@ public OpenDALReactiveAutoConfiguration(OpenDALProperties openDALProperties) {

@Bean
@ConditionalOnMissingBean(ReactiveOpenDALOperations.class)
public ReactiveOpenDALTemplate reactiveOpendalTemplate(OpenDALProperties openDALProperties) {
return new ReactiveOpenDALTemplate(openDALProperties);
public ReactiveOpenDALTemplate reactiveOpendalTemplate(AsyncOperator asyncOperator, OpenDALSerializerFactory openDALSerializerFactory) {
return new ReactiveOpenDALTemplate(asyncOperator, openDALSerializerFactory);
}

@Bean
@ConditionalOnMissingBean(OpenDALOperations.class)
public AsyncOperator asyncOperator(OpenDALProperties openDALProperties) {
return AsyncOperator.of(openDALProperties.getSchema(), openDALProperties.getConf());
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.apache.opendal.spring.TestReactiveApplication;
import org.apache.opendal.spring.core.OpenDALProperties;
import org.apache.opendal.spring.core.OpenDALSerializerFactory;
import org.apache.opendal.spring.core.ReactiveOpenDALOperations;
import org.apache.opendal.spring.core.ReactiveOpenDALTemplate;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -37,7 +37,7 @@ public class OpenDALReactiveAutoConfigurationTest {
private OpenDALProperties openDALProperties;

@Autowired
private ReactiveOpenDALOperations openDALReactive;
private ReactiveOpenDALTemplate openDALTemplate;

@Autowired
private OpenDALSerializerFactory openDALSerializerFactory;
Expand All @@ -53,8 +53,8 @@ public void propertiesBeanShouldBeDeclared() {

@Test
public void reactiveBeanShouldBeDeclared() {
Assertions.assertNotNull(openDALReactive);
Assertions.assertInstanceOf(ReactiveOpenDALOperations.class, openDALReactive);
Assertions.assertNotNull(openDALTemplate);
Assertions.assertInstanceOf(ReactiveOpenDALTemplate.class, openDALTemplate);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.opendal.spring.config;

import org.apache.opendal.AsyncOperator;
import org.apache.opendal.spring.TestReactiveApplication;
import org.apache.opendal.spring.core.ReactiveOpenDALOperations;
import org.apache.opendal.spring.core.ReactiveOpenDALTemplate;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutionException;

@SpringJUnitConfig
@SpringBootTest(classes = TestReactiveApplication.class)
public class OpenDALReactiveTemplateTest {
@Autowired
private ReactiveOpenDALTemplate openDALTemplate;

@Autowired
private AsyncOperator asyncOperator;

@Test
public void simpleReactiveTest() throws ExecutionException, InterruptedException {
String path = "my";
ReactiveOpenDALOperations<Person> ops = openDALTemplate.ops(Person.class);
ops.write(path, new Person("Alice", 1)).block();
Person person = ops.read(path).block();
Assertions.assertEquals("Alice", person.name());
Assertions.assertEquals(1, person.age());
String content = new String(asyncOperator.read(path).get(), StandardCharsets.UTF_8);
Assertions.assertEquals("""
{"name":"Alice","age":1}""", content);
ops.delete(path).block();
Assertions.assertThrows(Exception.class, () -> ops.read(path).block());
}
}
Original file line number Diff line number Diff line change
@@ -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.opendal.spring.config;

public record Person(String name, int age) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@

package org.apache.opendal.spring.config;

import org.apache.opendal.AsyncOperator;
import org.apache.opendal.spring.core.DefaultOpenDALSerializerFactory;
import org.apache.opendal.spring.core.OpenDALOperations;
import org.apache.opendal.spring.core.OpenDALProperties;
import org.apache.opendal.spring.core.OpenDALSerializerFactory;
import org.apache.opendal.spring.core.OpenDALTemplate;
import org.apache.opendal.spring.core.OpenDALProperties;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
Expand All @@ -32,20 +33,20 @@
@AutoConfiguration
@EnableConfigurationProperties(OpenDALProperties.class)
public class OpenDALAutoConfiguration {
private final OpenDALProperties openDALProperties;

public OpenDALAutoConfiguration(OpenDALProperties openDALProperties) {
this.openDALProperties = openDALProperties;
@Bean
@ConditionalOnMissingBean(OpenDALOperations.class)
public OpenDALTemplate opendalTemplate(AsyncOperator asyncOperator, OpenDALSerializerFactory openDALSerializerFactory) {
return new OpenDALTemplate(asyncOperator, openDALSerializerFactory);
}

@Bean
@ConditionalOnMissingBean(OpenDALOperations.class)
public OpenDALTemplate opendalTemplate(OpenDALProperties openDALProperties) {
return new OpenDALTemplate(openDALProperties);
public AsyncOperator asyncOperator(OpenDALProperties openDALProperties) {
return AsyncOperator.of(openDALProperties.getSchema(), openDALProperties.getConf());
}

@Bean
@ConditionalOnMissingBean(name = "openDALSerializerFactory")
@ConditionalOnMissingBean(OpenDALSerializerFactory.class)
public OpenDALSerializerFactory openDALSerializerFactory() {
return new DefaultOpenDALSerializerFactory();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

package org.apache.opendal.spring.config;

import org.apache.opendal.AsyncOperator;
import org.apache.opendal.spring.TestApplication;
import org.apache.opendal.spring.core.OpenDALOperations;
import org.apache.opendal.spring.core.OpenDALProperties;
import org.apache.opendal.spring.core.OpenDALSerializerFactory;
import org.apache.opendal.spring.core.OpenDALTemplate;
Expand All @@ -37,7 +37,10 @@ public class OpenDALAutoConfigurationTest {
private OpenDALProperties openDALProperties;

@Autowired
private OpenDALOperations openDAL;
private OpenDALTemplate openDALTemplate;

@Autowired
private AsyncOperator asyncOperator;

@Autowired
private OpenDALSerializerFactory openDALSerializerFactory;
Expand All @@ -53,8 +56,13 @@ public void propertiesBeanShouldBeDeclared() {

@Test
public void beanShouldBeDeclared() {
Assertions.assertNotNull(openDAL);
Assertions.assertInstanceOf(OpenDALTemplate.class, openDAL);
Assertions.assertNotNull(openDALTemplate);
Assertions.assertInstanceOf(OpenDALTemplate.class, openDALTemplate);
}

@Test
public void asyncOperatorShouldBeDeclared() {
Assertions.assertNotNull(asyncOperator);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.opendal.spring.config;

import org.apache.opendal.AsyncOperator;
import org.apache.opendal.spring.TestApplication;
import org.apache.opendal.spring.core.OpenDALOperations;
import org.apache.opendal.spring.core.OpenDALTemplate;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutionException;

@SpringJUnitConfig
@SpringBootTest(classes = TestApplication.class)
public class OpenDALTemplateTest {
@Autowired
private OpenDALTemplate openDALTemplate;

@Autowired
private AsyncOperator asyncOperator;

@Test
public void simpleTest() {
String path = "my";
OpenDALOperations<Person> ops = openDALTemplate.ops(Person.class);
ops.write(path, new Person("Alice", 1));
Person person = ops.read(path);
Assertions.assertEquals("Alice", person.name());
Assertions.assertEquals(1, person.age());
String content = new String(asyncOperator.blocking().read(path), StandardCharsets.UTF_8);
Assertions.assertEquals("""
{"name":"Alice","age":1}""", content);
ops.delete(path);
Assertions.assertThrows(Exception.class, () -> ops.read(path));
}

@Test
public void simpleAsyncTest() throws ExecutionException, InterruptedException {
String path = "my";
OpenDALOperations<Person> ops = openDALTemplate.ops(Person.class);
ops.writeAsync(path, new Person("Alice", 1)).get();
Person person = ops.readAsync(path).get();
Assertions.assertEquals("Alice", person.name());
Assertions.assertEquals(1, person.age());
String content = new String(asyncOperator.read(path).get(), StandardCharsets.UTF_8);
Assertions.assertEquals("""
{"name":"Alice","age":1}""", content);
ops.deleteAsync(path).get();
Assertions.assertThrows(Exception.class, () -> ops.readAsync(path).get());
}
}
Original file line number Diff line number Diff line change
@@ -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.opendal.spring.config;

public record Person(String name, int age) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,21 @@

package org.apache.opendal.spring.core;

import java.util.concurrent.CompletableFuture;

/**
* Interface that specified a basic set of Apache OpenDAL, implemented by {@link OpenDALTemplate}.
*/
public interface OpenDALOperations {
public interface OpenDALOperations<T> {
void write(String path, T entity);

T read(String path);

void delete(String path);

CompletableFuture<Void> writeAsync(String path, T entity);

CompletableFuture<T> readAsync(String path);

CompletableFuture<Void> deleteAsync(String path);
}
Loading

0 comments on commit 579425c

Please sign in to comment.