Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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<Any>): StorageResponse<Any>
suspend fun get(request: StorageRequest<Any>): StorageResponse<Any>
suspend fun search(request: StorageRequest<Any>): StorageResponse<Any>
suspend fun update(request: StorageRequest<Any>): StorageResponse<Any>
suspend fun delete(request: StorageRequest<Any>): StorageResponse<Any>
}
Original file line number Diff line number Diff line change
@@ -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<Any>): StorageResponse<Any> {
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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
@@ -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 <T> The type of the payload.
*/
data class StorageRequest<T> (
val payload: T? = null,
val options: Any? = null,
val operation: StorageOperation,
val engine: StorageEngine
)
Original file line number Diff line number Diff line change
@@ -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 <T> The type of the payload.
*/
data class StorageResponse<T> (
val payload: T? = null,
val operation: StorageOperation,
val engine: StorageEngine,
val error: StorageError? = null
)