kmcginnes commented on code in PR #3238:
URL: https://github.com/apache/tinkerpop/pull/3238#discussion_r2437591328


##########
gremlin-mcp/src/main/javascript/src/gremlin/query-utils.ts:
##########
@@ -0,0 +1,213 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.
+ */
+
+/**
+ * @fileoverview Shared utilities for Gremlin query execution and batched 
processing.
+ *
+ * Provides common patterns for Effect-TS based Gremlin query execution with
+ * standardized error handling, batching, and timeout management.
+ */
+
+import { Effect } from 'effect';
+import gremlin from 'gremlin';
+import { Errors, type GremlinQueryError } from '../errors.js';
+import type { process } from 'gremlin';
+
+type GraphTraversalSource = process.GraphTraversalSource;
+
+const { label } = gremlin.process.statics;
+
+/**
+ * Processes items in controlled batches with concurrency limiting.
+ *
+ * @param items - Array of items to process
+ * @param batchSize - Number of items per batch
+ * @param processor - Effect to run on each item
+ * @returns Effect with all processed results
+ *
+ * Critical for large graphs to avoid overwhelming the database with 
concurrent queries.
+ * Processes batches sequentially but allows controlled concurrency within 
each batch.
+ */
+export const processBatched = <T, R, E>(
+  items: T[],
+  batchSize: number,
+  processor: (item: T) => Effect.Effect<R, E>
+): Effect.Effect<R[], E> =>
+  Effect.gen(function* () {
+    const batches: T[][] = [];
+    for (let i = 0; i < items.length; i += batchSize) {
+      batches.push(items.slice(i, i + batchSize));
+    }
+
+    const results: R[] = [];
+    for (const batch of batches) {
+      const batchResults = yield* Effect.all(batch.map(processor), {
+        concurrency: Math.min(batchSize, 10),
+      });
+      results.push(...batchResults);
+    }
+
+    return results;

Review Comment:
   Sounds good to me



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to