From 673e33a126091ccd11794fefc69927a67508f2d7 Mon Sep 17 00:00:00 2001 From: Mohammed Aghil Puthiyottil <57040494+MohammedAghil@users.noreply.github.com> Date: Wed, 26 Mar 2025 20:45:53 +0000 Subject: [PATCH] Added Basic API's for Storage Repository Signed-off-by: Mohammed Aghil Puthiyottil <57040494+MohammedAghil@users.noreply.github.com> --- .../storage/api/StorageAdapterFactory.kt | 20 +++++++++++ .../commons/storage/api/StorageEngine.kt | 11 ++++++ .../commons/storage/api/StorageOperation.kt | 14 ++++++++ .../commons/storage/api/StorageRepository.kt | 21 +++++++++++ .../commons/storage/core/StorageService.kt | 35 +++++++++++++++++++ .../commons/storage/error/StorageError.kt | 13 +++++++ .../commons/storage/model/StorageRequest.kt | 20 +++++++++++ .../commons/storage/model/StorageResponse.kt | 21 +++++++++++ 8 files changed, 155 insertions(+) create mode 100644 src/main/kotlin/org/opensearch/commons/storage/api/StorageAdapterFactory.kt create mode 100644 src/main/kotlin/org/opensearch/commons/storage/api/StorageEngine.kt create mode 100644 src/main/kotlin/org/opensearch/commons/storage/api/StorageOperation.kt create mode 100644 src/main/kotlin/org/opensearch/commons/storage/api/StorageRepository.kt create mode 100644 src/main/kotlin/org/opensearch/commons/storage/core/StorageService.kt create mode 100644 src/main/kotlin/org/opensearch/commons/storage/error/StorageError.kt create mode 100644 src/main/kotlin/org/opensearch/commons/storage/model/StorageRequest.kt create mode 100644 src/main/kotlin/org/opensearch/commons/storage/model/StorageResponse.kt diff --git a/src/main/kotlin/org/opensearch/commons/storage/api/StorageAdapterFactory.kt b/src/main/kotlin/org/opensearch/commons/storage/api/StorageAdapterFactory.kt new file mode 100644 index 00000000..1fd29b96 --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/storage/api/StorageAdapterFactory.kt @@ -0,0 +1,20 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.commons.storage.api + +/** + * StorageAdapterFactory is used by the StorageService to get the StorageRepository + * based on the provided StorageEngine. + */ +interface StorageAdapterFactory { + /** + * returns the StorageRepository based on the provided StorageEngine. + * + * @param engine The StorageEngine. + * @return The StorageRepository + */ + fun getAdapter(engine: StorageEngine): StorageRepository +} diff --git a/src/main/kotlin/org/opensearch/commons/storage/api/StorageEngine.kt b/src/main/kotlin/org/opensearch/commons/storage/api/StorageEngine.kt new file mode 100644 index 00000000..663a2c6d --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/storage/api/StorageEngine.kt @@ -0,0 +1,11 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.commons.storage.api + +enum class StorageEngine { + OPEN_SEARCH_CLUSTER, + DYNAMO_DB +} diff --git a/src/main/kotlin/org/opensearch/commons/storage/api/StorageOperation.kt b/src/main/kotlin/org/opensearch/commons/storage/api/StorageOperation.kt new file mode 100644 index 00000000..0d11903c --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/storage/api/StorageOperation.kt @@ -0,0 +1,14 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.commons.storage.api + +enum class StorageOperation { + SAVE, + DELETE, + GET, + UPDATE, + SEARCH +} diff --git a/src/main/kotlin/org/opensearch/commons/storage/api/StorageRepository.kt b/src/main/kotlin/org/opensearch/commons/storage/api/StorageRepository.kt new file mode 100644 index 00000000..38849372 --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/storage/api/StorageRepository.kt @@ -0,0 +1,21 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.commons.storage.api + +import org.opensearch.commons.storage.model.StorageRequest +import org.opensearch.commons.storage.model.StorageResponse + +/** + * The StorageRepository Interface defines the basic operations such as saving, + * retrieving, searching, updating, and deleting data for the storage accessor. + */ +interface StorageRepository { + suspend fun save(request: StorageRequest): StorageResponse + suspend fun get(request: StorageRequest): StorageResponse + suspend fun search(request: StorageRequest): StorageResponse + suspend fun update(request: StorageRequest): StorageResponse + suspend fun delete(request: StorageRequest): StorageResponse +} diff --git a/src/main/kotlin/org/opensearch/commons/storage/core/StorageService.kt b/src/main/kotlin/org/opensearch/commons/storage/core/StorageService.kt new file mode 100644 index 00000000..d975a654 --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/storage/core/StorageService.kt @@ -0,0 +1,35 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.commons.storage.core + +import org.apache.logging.log4j.LogManager +import org.opensearch.commons.storage.api.StorageAdapterFactory +import org.opensearch.commons.storage.api.StorageOperation +import org.opensearch.commons.storage.model.StorageRequest +import org.opensearch.commons.storage.model.StorageResponse + +/** + * StorageService is the primary layer for handling requests from the clients and + * delegates the requests to the appropriate storage adapter and returns the response. + */ +class StorageService(private val storageAdapterFactory: StorageAdapterFactory) { + private val log = LogManager.getLogger(StorageService::class.java) + + suspend fun handleRequest(request: StorageRequest): StorageResponse { + log.info( + "[StorageService] Handling request: engine=${request.engine}, operation=${request.operation}" + ) + + val storageRepository = storageAdapterFactory.getAdapter(request.engine) + return when (request.operation) { + StorageOperation.SAVE -> storageRepository.save(request) + StorageOperation.GET -> storageRepository.get(request) + StorageOperation.SEARCH -> storageRepository.search(request) + StorageOperation.UPDATE -> storageRepository.update(request) + StorageOperation.DELETE -> storageRepository.delete(request) + } + } +} diff --git a/src/main/kotlin/org/opensearch/commons/storage/error/StorageError.kt b/src/main/kotlin/org/opensearch/commons/storage/error/StorageError.kt new file mode 100644 index 00000000..e074dd49 --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/storage/error/StorageError.kt @@ -0,0 +1,13 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.commons.storage.error + +/** + * Represents a storage-related error that can be returned as part of a [StorageResponse]. + */ +sealed class StorageError { + data class Generic(val message: String, val cause: Throwable? = null) : StorageError() +} diff --git a/src/main/kotlin/org/opensearch/commons/storage/model/StorageRequest.kt b/src/main/kotlin/org/opensearch/commons/storage/model/StorageRequest.kt new file mode 100644 index 00000000..cddb87b1 --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/storage/model/StorageRequest.kt @@ -0,0 +1,20 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.commons.storage.model + +import org.opensearch.commons.storage.api.StorageEngine +import org.opensearch.commons.storage.api.StorageOperation + +/** + * StorageRequest represents a request to the storage service. + * @param The type of the payload. + */ +data class StorageRequest ( + val payload: T? = null, + val options: Any? = null, + val operation: StorageOperation, + val engine: StorageEngine +) diff --git a/src/main/kotlin/org/opensearch/commons/storage/model/StorageResponse.kt b/src/main/kotlin/org/opensearch/commons/storage/model/StorageResponse.kt new file mode 100644 index 00000000..c639af08 --- /dev/null +++ b/src/main/kotlin/org/opensearch/commons/storage/model/StorageResponse.kt @@ -0,0 +1,21 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.commons.storage.model + +import org.opensearch.commons.storage.api.StorageEngine +import org.opensearch.commons.storage.api.StorageOperation +import org.opensearch.commons.storage.error.StorageError + +/** + * StorageResponse represents the response returned after handling a StorageRequest + * @param The type of the payload. + */ +data class StorageResponse ( + val payload: T? = null, + val operation: StorageOperation, + val engine: StorageEngine, + val error: StorageError? = null +)