Skip to content
Merged
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
10 changes: 4 additions & 6 deletions java/com/google/dotprompt/helpers/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ public static Object json(Object context, Options options) throws IOException {

Integer indent = options.hash("indent", null);
if (indent != null) {
DefaultPrettyPrinter printer =
new DefaultPrettyPrinter();
DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
printer.indentObjectsWith(new DefaultIndenter(" ", "\n"));

localMapper =
Expand All @@ -90,16 +89,15 @@ public static Object json(Object context, Options options) throws IOException {
public DefaultPrettyPrinter createInstance() {
return new DefaultPrettyPrinter(this) {
@Override
public void writeObjectFieldValueSeparator(
JsonGenerator g) throws IOException {
public void writeObjectFieldValueSeparator(JsonGenerator g)
throws IOException {
g.writeRaw(": ");
}
};
}

@Override
public void writeObjectFieldValueSeparator(
JsonGenerator g) throws IOException {
public void writeObjectFieldValueSeparator(JsonGenerator g) throws IOException {
g.writeRaw(": ");
}
})
Expand Down
4 changes: 2 additions & 2 deletions java/com/google/dotprompt/resolvers/SchemaResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

/**
* Resolves a provided schema name to an underlying JSON schema.
Expand Down Expand Up @@ -86,8 +87,7 @@ public interface SchemaResolver {
* corresponding JSON Schema, or {@code null}.
* @return An async {@link SchemaResolver} wrapping the synchronous function.
*/
static SchemaResolver fromSync(
java.util.function.Function<String, Map<String, Object>> syncResolver) {
static SchemaResolver fromSync(Function<String, Map<String, Object>> syncResolver) {
return schemaName -> CompletableFuture.completedFuture(syncResolver.apply(schemaName));
}
}
38 changes: 38 additions & 0 deletions java/com/google/dotprompt/store/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,49 @@ load("@rules_java//java:defs.bzl", "java_library")
java_library(
name = "store",
srcs = [
"DirStore.java",
"DirStoreOptions.java",
"DirStoreSync.java",
"PromptStore.java",
"PromptStoreSync.java",
"PromptStoreWritable.java",
"PromptStoreWritableSync.java",
"StoreUtils.java",
],
visibility = ["//visibility:public"],
deps = [
"//java/com/google/dotprompt/models",
],
)

java_test(
name = "DirStoreSyncTest",
srcs = ["DirStoreSyncTest.java"],
deps = [
":store",
"//java/com/google/dotprompt/models",
"@maven//:com_google_truth_truth",
"@maven//:junit_junit",
],
)

java_test(
name = "DirStoreTest",
srcs = ["DirStoreTest.java"],
deps = [
":store",
"//java/com/google/dotprompt/models",
"@maven//:com_google_truth_truth",
"@maven//:junit_junit",
],
)

java_test(
name = "StoreUtilsTest",
srcs = ["StoreUtilsTest.java"],
deps = [
":store",
"@maven//:com_google_truth_truth",
"@maven//:junit_junit",
],
)
117 changes: 117 additions & 0 deletions java/com/google/dotprompt/store/DirStore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright 2025 Google LLC
*
* Licensed 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.
*
* SPDX-License-Identifier: Apache-2.0
*/

package com.google.dotprompt.store;

import com.google.dotprompt.models.DeletePromptOrPartialOptions;
import com.google.dotprompt.models.ListPartialsOptions;
import com.google.dotprompt.models.ListPromptsOptions;
import com.google.dotprompt.models.LoadPartialOptions;
import com.google.dotprompt.models.LoadPromptOptions;
import com.google.dotprompt.models.PaginatedPartials;
import com.google.dotprompt.models.PaginatedPrompts;
import com.google.dotprompt.models.PromptData;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.ForkJoinPool;

/**
* Asynchronous filesystem-based prompt store implementation.
*
* <p>Reads and writes prompts and partials from/to the local file system within a specified
* directory using asynchronous operations backed by {@link CompletableFuture}.
*
* <h2>File Naming Conventions</h2>
*
* <ul>
* <li>Prompts: {@code [name][.variant].prompt}
* <li>Partials: {@code _[name][.variant].prompt}
* </ul>
*
* <h2>Usage Example</h2>
*
* <pre>{@code
* DirStore store = new DirStore(DirStoreOptions.of("/path/to/prompts"));
*
* // List prompts asynchronously
* store.list(null).thenAccept(prompts -> {
* prompts.prompts().forEach(p -> System.out.println(p.name()));
* });
*
* // Load a specific prompt
* PromptData data = store.load("my_prompt", null).join();
* }</pre>
*
* <p>This class wraps {@link DirStoreSync} to provide async operations. All operations are executed
* on a configurable {@link Executor}.
*/
public class DirStore implements PromptStoreWritable {

private final DirStoreSync syncStore;
private final Executor executor;

/**
* Creates a new DirStore instance using the common ForkJoinPool.
*
* @param options Configuration options including the base directory.
*/
public DirStore(DirStoreOptions options) {
this(options, ForkJoinPool.commonPool());
}

/**
* Creates a new DirStore instance with a custom executor.
*
* @param options Configuration options including the base directory.
* @param executor The executor to use for async operations.
*/
public DirStore(DirStoreOptions options, Executor executor) {
this.syncStore = new DirStoreSync(options);
this.executor = executor;
}

@Override
public CompletableFuture<PaginatedPrompts> list(ListPromptsOptions options) {
return CompletableFuture.supplyAsync(() -> syncStore.list(options), executor);
}

@Override
public CompletableFuture<PaginatedPartials> listPartials(ListPartialsOptions options) {
return CompletableFuture.supplyAsync(() -> syncStore.listPartials(options), executor);
}

@Override
public CompletableFuture<PromptData> load(String name, LoadPromptOptions options) {
return CompletableFuture.supplyAsync(() -> syncStore.load(name, options), executor);
}

@Override
public CompletableFuture<PromptData> loadPartial(String name, LoadPartialOptions options) {
return CompletableFuture.supplyAsync(() -> syncStore.loadPartial(name, options), executor);
}

@Override
public CompletableFuture<Void> save(PromptData prompt) {
return CompletableFuture.runAsync(() -> syncStore.save(prompt), executor);
}

@Override
public CompletableFuture<Void> delete(String name, DeletePromptOrPartialOptions options) {
return CompletableFuture.runAsync(() -> syncStore.delete(name, options), executor);
}
}
39 changes: 39 additions & 0 deletions java/com/google/dotprompt/store/DirStoreOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2025 Google LLC
*
* Licensed 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.
*
* SPDX-License-Identifier: Apache-2.0
*/

package com.google.dotprompt.store;

import java.nio.file.Path;

/**
* Configuration options for directory-based prompt stores.
*
* @param directory The base directory where prompt files are stored.
*/
public record DirStoreOptions(Path directory) {

/**
* Creates options from a string path.
*
* @param directory The directory path as a string.
* @return A new DirStoreOptions instance.
*/
public static DirStoreOptions of(String directory) {
return new DirStoreOptions(Path.of(directory));
}
}
Loading
Loading