|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + */ |
| 18 | + |
| 19 | +package com.google.dotprompt.store; |
| 20 | + |
| 21 | +import com.google.dotprompt.models.DeletePromptOrPartialOptions; |
| 22 | +import com.google.dotprompt.models.ListPartialsOptions; |
| 23 | +import com.google.dotprompt.models.ListPromptsOptions; |
| 24 | +import com.google.dotprompt.models.LoadPartialOptions; |
| 25 | +import com.google.dotprompt.models.LoadPromptOptions; |
| 26 | +import com.google.dotprompt.models.PaginatedPartials; |
| 27 | +import com.google.dotprompt.models.PaginatedPrompts; |
| 28 | +import com.google.dotprompt.models.PromptData; |
| 29 | +import java.util.concurrent.CompletableFuture; |
| 30 | +import java.util.concurrent.Executor; |
| 31 | +import java.util.concurrent.ForkJoinPool; |
| 32 | + |
| 33 | +/** |
| 34 | + * Asynchronous filesystem-based prompt store implementation. |
| 35 | + * |
| 36 | + * <p>Reads and writes prompts and partials from/to the local file system within a specified |
| 37 | + * directory using asynchronous operations backed by {@link CompletableFuture}. |
| 38 | + * |
| 39 | + * <h2>File Naming Conventions</h2> |
| 40 | + * |
| 41 | + * <ul> |
| 42 | + * <li>Prompts: {@code [name][.variant].prompt} |
| 43 | + * <li>Partials: {@code _[name][.variant].prompt} |
| 44 | + * </ul> |
| 45 | + * |
| 46 | + * <h2>Usage Example</h2> |
| 47 | + * |
| 48 | + * <pre>{@code |
| 49 | + * DirStore store = new DirStore(DirStoreOptions.of("/path/to/prompts")); |
| 50 | + * |
| 51 | + * // List prompts asynchronously |
| 52 | + * store.list(null).thenAccept(prompts -> { |
| 53 | + * prompts.prompts().forEach(p -> System.out.println(p.name())); |
| 54 | + * }); |
| 55 | + * |
| 56 | + * // Load a specific prompt |
| 57 | + * PromptData data = store.load("my_prompt", null).join(); |
| 58 | + * }</pre> |
| 59 | + * |
| 60 | + * <p>This class wraps {@link DirStoreSync} to provide async operations. All operations are executed |
| 61 | + * on a configurable {@link Executor}. |
| 62 | + */ |
| 63 | +public class DirStore implements PromptStoreWritable { |
| 64 | + |
| 65 | + private final DirStoreSync syncStore; |
| 66 | + private final Executor executor; |
| 67 | + |
| 68 | + /** |
| 69 | + * Creates a new DirStore instance using the common ForkJoinPool. |
| 70 | + * |
| 71 | + * @param options Configuration options including the base directory. |
| 72 | + */ |
| 73 | + public DirStore(DirStoreOptions options) { |
| 74 | + this(options, ForkJoinPool.commonPool()); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Creates a new DirStore instance with a custom executor. |
| 79 | + * |
| 80 | + * @param options Configuration options including the base directory. |
| 81 | + * @param executor The executor to use for async operations. |
| 82 | + */ |
| 83 | + public DirStore(DirStoreOptions options, Executor executor) { |
| 84 | + this.syncStore = new DirStoreSync(options); |
| 85 | + this.executor = executor; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public CompletableFuture<PaginatedPrompts> list(ListPromptsOptions options) { |
| 90 | + return CompletableFuture.supplyAsync(() -> syncStore.list(options), executor); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public CompletableFuture<PaginatedPartials> listPartials(ListPartialsOptions options) { |
| 95 | + return CompletableFuture.supplyAsync(() -> syncStore.listPartials(options), executor); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public CompletableFuture<PromptData> load(String name, LoadPromptOptions options) { |
| 100 | + return CompletableFuture.supplyAsync(() -> syncStore.load(name, options), executor); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public CompletableFuture<PromptData> loadPartial(String name, LoadPartialOptions options) { |
| 105 | + return CompletableFuture.supplyAsync(() -> syncStore.loadPartial(name, options), executor); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public CompletableFuture<Void> save(PromptData prompt) { |
| 110 | + return CompletableFuture.runAsync(() -> syncStore.save(prompt), executor); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public CompletableFuture<Void> delete(String name, DeletePromptOrPartialOptions options) { |
| 115 | + return CompletableFuture.runAsync(() -> syncStore.delete(name, options), executor); |
| 116 | + } |
| 117 | +} |
0 commit comments