Skip to content

Commit 152f275

Browse files
committed
feat(admin): add index management operations
Add createIndex and dropIndex methods to AdminClient with corresponding validation logic. These methods enable management of database indexes on boundaries, including proper request validation, error handling, and logging. Updated documentation to include usage examples for the new index management functionality.
1 parent 7532247 commit 152f275

4 files changed

Lines changed: 125 additions & 1 deletion

File tree

CLAUDE.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ The `AdminClient` class mirrors the architecture of `OrisunClient` but focuses o
9999
- **User Management**: `createUser`, `deleteUser`, `changePassword`, `listUsers`
100100
- **Authentication**: `validateCredentials`
101101
- **Statistics**: `getUserCount`, `getEventCount`
102+
- **Index Management**: `createIndex`, `dropIndex`
102103

103104
Both clients share the same authentication flow, token caching mechanism, and connection management patterns.
104105

@@ -232,4 +233,36 @@ if (response.getSuccess()) {
232233
```java
233234
long userCount = adminClient.getUserCount();
234235
long eventCount = adminClient.getEventCount("users-boundary");
236+
```
237+
238+
**Creating an index:**
239+
```java
240+
CreateIndexRequest request = CreateIndexRequest.newBuilder()
241+
.setBoundary("events-boundary")
242+
.setName("idx_user_id")
243+
.addFields(IndexField.newBuilder()
244+
.setJsonKey("userId")
245+
.setValueType(ValueType.TEXT)
246+
.build())
247+
.addFields(IndexField.newBuilder()
248+
.setJsonKey("timestamp")
249+
.setValueType(ValueType.TIMESTAMPTZ)
250+
.build())
251+
.addConditions(IndexCondition.newBuilder()
252+
.setKey("eventType")
253+
.setOperator("=")
254+
.setValue("UserCreated")
255+
.build())
256+
.setConditionCombinator(ConditionCombinator.AND)
257+
.build();
258+
adminClient.createIndex(request);
259+
```
260+
261+
**Dropping an index:**
262+
```java
263+
DropIndexRequest request = DropIndexRequest.newBuilder()
264+
.setBoundary("events-boundary")
265+
.setName("idx_user_id")
266+
.build();
267+
adminClient.dropIndex(request);
235268
```

protos

Submodule protos updated 1 file

src/main/java/com/orisunlabs/orisun/client/AdminClient.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,48 @@ public long getEventCount(String boundary) throws OrisunException {
472472
return getEventCount(GetEventCountRequest.newBuilder().setBoundary(boundary).build());
473473
}
474474

475+
// Index Management Operations
476+
477+
/**
478+
* Create a new index on a boundary
479+
*/
480+
public void createIndex(CreateIndexRequest request) throws OrisunException {
481+
AdminRequestValidator.validateCreateIndexRequest(request);
482+
483+
logger.debug("Creating index '{}' on boundary: {}", request.getName(), request.getBoundary());
484+
485+
try {
486+
blockingStub
487+
.withDeadlineAfter(defaultTimeoutSeconds, TimeUnit.SECONDS)
488+
.createIndex(request);
489+
490+
logger.info("Successfully created index '{}' on boundary: {}", request.getName(), request.getBoundary());
491+
492+
} catch (StatusRuntimeException e) {
493+
throw handleException(e, "createIndex");
494+
}
495+
}
496+
497+
/**
498+
* Drop an index from a boundary
499+
*/
500+
public void dropIndex(DropIndexRequest request) throws OrisunException {
501+
AdminRequestValidator.validateDropIndexRequest(request);
502+
503+
logger.debug("Dropping index '{}' from boundary: {}", request.getName(), request.getBoundary());
504+
505+
try {
506+
blockingStub
507+
.withDeadlineAfter(defaultTimeoutSeconds, TimeUnit.SECONDS)
508+
.dropIndex(request);
509+
510+
logger.info("Successfully dropped index '{}' from boundary: {}", request.getName(), request.getBoundary());
511+
512+
} catch (StatusRuntimeException e) {
513+
throw handleException(e, "dropIndex");
514+
}
515+
}
516+
475517
private OrisunException handleException(StatusRuntimeException e, String operation) {
476518
Map<String, Object> context = new HashMap<>();
477519
context.put("operation", operation);

src/main/java/com/orisunlabs/orisun/client/AdminRequestValidator.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,53 @@ public static void validateGetEventCountRequest(GetEventCountRequest request) {
172172
.addContext("operation", "getEventCount");
173173
}
174174
}
175+
176+
/**
177+
* Validate a CreateIndexRequest
178+
*/
179+
public static void validateCreateIndexRequest(CreateIndexRequest request) {
180+
if (request == null) {
181+
throw new OrisunException("CreateIndexRequest cannot be null")
182+
.addContext("operation", "createIndex");
183+
}
184+
185+
if (request.getBoundary() == null || request.getBoundary().trim().isEmpty()) {
186+
throw new OrisunException("Boundary is required")
187+
.addContext("operation", "createIndex");
188+
}
189+
190+
if (request.getName() == null || request.getName().trim().isEmpty()) {
191+
throw new OrisunException("Index name is required")
192+
.addContext("operation", "createIndex")
193+
.addContext("boundary", request.getBoundary());
194+
}
195+
196+
if (request.getFieldsCount() == 0) {
197+
throw new OrisunException("At least one field is required")
198+
.addContext("operation", "createIndex")
199+
.addContext("boundary", request.getBoundary())
200+
.addContext("indexName", request.getName());
201+
}
202+
}
203+
204+
/**
205+
* Validate a DropIndexRequest
206+
*/
207+
public static void validateDropIndexRequest(DropIndexRequest request) {
208+
if (request == null) {
209+
throw new OrisunException("DropIndexRequest cannot be null")
210+
.addContext("operation", "dropIndex");
211+
}
212+
213+
if (request.getBoundary() == null || request.getBoundary().trim().isEmpty()) {
214+
throw new OrisunException("Boundary is required")
215+
.addContext("operation", "dropIndex");
216+
}
217+
218+
if (request.getName() == null || request.getName().trim().isEmpty()) {
219+
throw new OrisunException("Index name is required")
220+
.addContext("operation", "dropIndex")
221+
.addContext("boundary", request.getBoundary());
222+
}
223+
}
175224
}

0 commit comments

Comments
 (0)