Skip to content

Commit 5f2da81

Browse files
committed
COH-29842 Add a vector store service to the gRPC proxy
(merge main -> ce/main 108267) [git-p4: depot-paths = "//dev/coherence-ce/main/": change = 108272]
1 parent 23510c8 commit 5f2da81

File tree

99 files changed

+6325
-1574
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+6325
-1574
lines changed

prj/coherence-ai/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ metadata type.
2525

2626
```java
2727
/**
28-
* @param <V> the type of the store (this will always be a primitive array type)
29-
* @param <K> the type of the key
30-
* @param <M> the type of the metadata
28+
* @param <VectorType> the type of the store (this will always be a primitive array type)
29+
* @param <KeyType> the type of the key
30+
* @param <MetadataType> the type of the metadata
3131
*/
32-
public interface VectorStore<V, K, M>
32+
public interface VectorStore<VectorType, KeyType, MetadataType>
3333
{
3434
}
3535
```

prj/coherence-ai/pom.xml

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,112 @@
3737
<artifactId>coherence</artifactId>
3838
<version>${project.version}</version>
3939
</dependency>
40+
<dependency>
41+
<groupId>${coherence.group.id}</groupId>
42+
<artifactId>coherence-json</artifactId>
43+
<version>${project.version}</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>${coherence.group.id}</groupId>
47+
<artifactId>coherence-grpc</artifactId>
48+
<version>${project.version}</version>
49+
</dependency>
50+
51+
<!--
52+
Coherence gRPC is optional.
53+
The Coherence AI services will be available to the gRPC Proxy
54+
if coherence-ai is on the class path
55+
-->
56+
<dependency>
57+
<groupId>${coherence.group.id}</groupId>
58+
<artifactId>coherence-grpc-proxy-common</artifactId>
59+
<version>${project.version}</version>
60+
<scope>provided</scope>
61+
<optional>true</optional>
62+
</dependency>
63+
64+
<dependency>
65+
<groupId>jakarta.json.bind</groupId>
66+
<artifactId>jakarta.json.bind-api</artifactId>
67+
<optional>true</optional>
68+
</dependency>
69+
<dependency>
70+
<groupId>jakarta.json</groupId>
71+
<artifactId>jakarta.json-api</artifactId>
72+
<optional>true</optional>
73+
</dependency>
4074

75+
<dependency>
76+
<groupId>org.junit.jupiter</groupId>
77+
<artifactId>junit-jupiter-api</artifactId>
78+
<scope>test</scope>
79+
</dependency>
80+
<dependency>
81+
<groupId>org.hamcrest</groupId>
82+
<artifactId>hamcrest-core</artifactId>
83+
<scope>test</scope>
84+
</dependency>
4185
</dependencies>
4286

4387
<build>
88+
<extensions>
89+
<extension>
90+
<groupId>kr.motd.maven</groupId>
91+
<artifactId>os-maven-plugin</artifactId>
92+
<version>1.6.0</version>
93+
</extension>
94+
</extensions>
4495
<plugins>
96+
<!-- compile .proto files -->
97+
<plugin>
98+
<groupId>org.xolstice.maven.plugins</groupId>
99+
<artifactId>protobuf-maven-plugin</artifactId>
100+
<version>${maven.protobuf.plugin.version}</version>
101+
<executions>
102+
<execution>
103+
<id>compile</id>
104+
<goals>
105+
<goal>compile</goal>
106+
<goal>compile-custom</goal>
107+
</goals>
108+
</execution>
109+
</executions>
110+
<configuration>
111+
<protocArtifact>
112+
com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}
113+
</protocArtifact>
114+
<pluginId>grpc-java</pluginId>
115+
<pluginArtifact>
116+
io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
117+
</pluginArtifact>
118+
</configuration>
119+
</plugin>
120+
121+
<plugin>
122+
<groupId>com.google.code.maven-replacer-plugin</groupId>
123+
<artifactId>replacer</artifactId>
124+
<version>1.5.3</version>
125+
<executions>
126+
<execution>
127+
<id>filter-javax</id>
128+
<phase>process-sources</phase>
129+
<goals>
130+
<goal>replace</goal>
131+
</goals>
132+
<configuration>
133+
<includes>
134+
<include>${project.build.directory}/generated-sources/protobuf/grpc-java/com/oracle/coherence/ai/grpc/**/*.java</include>
135+
</includes>
136+
<replacements>
137+
<replacement>
138+
<token>javax.annotation</token>
139+
<value>jakarta.annotation</value>
140+
</replacement>
141+
</replacements>
142+
</configuration>
143+
</execution>
144+
</executions>
145+
</plugin>
45146
<!-- create module descriptor (module-info) -->
46147
<plugin>
47148
<groupId>com.oracle.coherence.moditect</groupId>
@@ -77,6 +178,7 @@
77178

78179
<!-- maven-jar-plugin with configuration to package generated MANIFEST.MF to project jar -->
79180
<plugin>
181+
<groupId>org.apache.maven.plugins</groupId>
80182
<artifactId>maven-jar-plugin</artifactId>
81183
<configuration>
82184
<archive>
@@ -94,6 +196,12 @@
94196
<skip>false</skip>
95197
</configuration>
96198
</plugin>
199+
200+
<!-- flatten POM to remove build-specific content -->
201+
<plugin>
202+
<groupId>org.codehaus.mojo</groupId>
203+
<artifactId>flatten-maven-plugin</artifactId>
204+
</plugin>
97205
</plugins>
98206
</build>
99207
</project>

prj/coherence-ai/src/main/java/com/oracle/coherence/ai/Converters.java

Lines changed: 116 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
package com.oracle.coherence.ai;
99

1010
import com.tangosol.io.ReadBuffer;
11+
import com.tangosol.io.nio.ByteBufferReadBuffer;
12+
import com.tangosol.util.Binary;
13+
import com.tangosol.util.ExternalizableHelper;
1114

1215
import java.nio.ByteBuffer;
1316
import java.nio.DoubleBuffer;
@@ -16,6 +19,9 @@
1619
import java.nio.LongBuffer;
1720
import java.nio.ShortBuffer;
1821

22+
import static com.tangosol.util.ExternalizableHelper.DECO_VECTOR;
23+
24+
1925
/**
2026
* Utility methods to convert from various binary representations of
2127
* vectors into concrete primitive arrays.
@@ -247,6 +253,20 @@ static short[] shortsFromShortBuffer(ShortBuffer buffer)
247253
return array;
248254
}
249255

256+
/**
257+
* Create a {@link ReadBuffer} that contains the specified
258+
* vector of {@code double} values.
259+
*
260+
* @param vector the vector of {@code double} values
261+
*
262+
* @return a {@link ReadBuffer} that contains the specified
263+
* vector of {@code double} values
264+
*/
265+
static ReadBuffer readBufferFromDoubles(double... vector)
266+
{
267+
return new ByteBufferReadBuffer(bufferFromDoubles(vector));
268+
}
269+
250270
/**
251271
* Create a {@link ByteBuffer} that contains the specified
252272
* vector of {@code double} values.
@@ -256,13 +276,27 @@ static short[] shortsFromShortBuffer(ShortBuffer buffer)
256276
* @return a {@link ByteBuffer} that contains the specified
257277
* vector of {@code double} values
258278
*/
259-
static ByteBuffer bufferFromDoubles(double[] vector)
279+
static ByteBuffer bufferFromDoubles(double... vector)
260280
{
261281
ByteBuffer buffer = ByteBuffer.allocate(vector.length * Double.BYTES);
262282
buffer.asDoubleBuffer().put(vector);
263283
return buffer;
264284
}
265285

286+
/**
287+
* Create a {@link ReadBuffer} that contains the specified
288+
* vector of {@code float} values.
289+
*
290+
* @param vector the vector of {@code float} values
291+
*
292+
* @return a {@link ReadBuffer} that contains the specified
293+
* vector of {@code float} values
294+
*/
295+
static ReadBuffer readBufferFromFloats(float... vector)
296+
{
297+
return new ByteBufferReadBuffer(bufferFromFloats(vector));
298+
}
299+
266300
/**
267301
* Create a {@link ByteBuffer} that contains the specified
268302
* vector of {@code float} values.
@@ -272,13 +306,27 @@ static ByteBuffer bufferFromDoubles(double[] vector)
272306
* @return a {@link ByteBuffer} that contains the specified
273307
* vector of {@code float} values
274308
*/
275-
static ByteBuffer bufferFromFloats(float[] vector)
309+
static ByteBuffer bufferFromFloats(float... vector)
276310
{
277311
ByteBuffer buffer = ByteBuffer.allocate(vector.length * Float.BYTES);
278312
buffer.asFloatBuffer().put(vector);
279313
return buffer;
280314
}
281315

316+
/**
317+
* Create a {@link ReadBuffer} that contains the specified
318+
* vector of {@code int} values.
319+
*
320+
* @param vector the vector of {@code int} values
321+
*
322+
* @return a {@link ReadBuffer} that contains the specified
323+
* vector of {@code int} values
324+
*/
325+
static ReadBuffer readBufferFromInts(int... vector)
326+
{
327+
return new ByteBufferReadBuffer(bufferFromInts(vector));
328+
}
329+
282330
/**
283331
* Create a {@link ByteBuffer} that contains the specified
284332
* vector of {@code int} values.
@@ -288,13 +336,27 @@ static ByteBuffer bufferFromFloats(float[] vector)
288336
* @return a {@link ByteBuffer} that contains the specified
289337
* vector of {@code int} values
290338
*/
291-
static ByteBuffer bufferFromInts(int[] vector)
339+
static ByteBuffer bufferFromInts(int... vector)
292340
{
293341
ByteBuffer buffer = ByteBuffer.allocate(vector.length * Integer.BYTES);
294342
buffer.asIntBuffer().put(vector);
295343
return buffer;
296344
}
297345

346+
/**
347+
* Create a {@link ReadBuffer} that contains the specified
348+
* vector of {@code long} values.
349+
*
350+
* @param vector the vector of {@code long} values
351+
*
352+
* @return a {@link ReadBuffer} that contains the specified
353+
* vector of {@code long} values
354+
*/
355+
static ReadBuffer readBufferFromLongs(long... vector)
356+
{
357+
return new ByteBufferReadBuffer(bufferFromLongs(vector));
358+
}
359+
298360
/**
299361
* Create a {@link ByteBuffer} that contains the specified
300362
* vector of {@code long} values.
@@ -304,13 +366,27 @@ static ByteBuffer bufferFromInts(int[] vector)
304366
* @return a {@link ByteBuffer} that contains the specified
305367
* vector of {@code long} values
306368
*/
307-
static ByteBuffer bufferFromLongs(long[] vector)
369+
static ByteBuffer bufferFromLongs(long... vector)
308370
{
309371
ByteBuffer buffer = ByteBuffer.allocate(vector.length * Long.BYTES);
310372
buffer.asLongBuffer().put(vector);
311373
return buffer;
312374
}
313375

376+
/**
377+
* Create a {@link ReadBuffer} that contains the specified
378+
* vector of {@code short} values.
379+
*
380+
* @param vector the vector of {@code short} values
381+
*
382+
* @return a {@link ReadBuffer} that contains the specified
383+
* vector of {@code short} values
384+
*/
385+
static ReadBuffer readBufferFromShorts(short... vector)
386+
{
387+
return new ByteBufferReadBuffer(bufferFromShorts(vector));
388+
}
389+
314390
/**
315391
* Create a {@link ByteBuffer} that contains the specified
316392
* vector of {@code short} values.
@@ -320,10 +396,45 @@ static ByteBuffer bufferFromLongs(long[] vector)
320396
* @return a {@link ByteBuffer} that contains the specified
321397
* vector of {@code short} values
322398
*/
323-
static ByteBuffer bufferFromShorts(short[] vector)
399+
static ByteBuffer bufferFromShorts(short... vector)
324400
{
325401
ByteBuffer buffer = ByteBuffer.allocate(vector.length * Short.BYTES);
326402
buffer.asShortBuffer().put(vector);
327403
return buffer;
328404
}
405+
406+
/**
407+
* Decorate a binary value with its binary metadata.
408+
*
409+
* @param binVector a {@link ReadBuffer} containing the vector data
410+
* @param binMetadata a {@link ReadBuffer} containing the metadata
411+
*
412+
* @return a decorated binary containing both the vector and metadata
413+
*/
414+
static ReadBuffer combineMetadata(ReadBuffer binVector, ReadBuffer binMetadata)
415+
{
416+
if (binMetadata == null)
417+
{
418+
binMetadata = Binary.NO_BINARY;
419+
}
420+
return ExternalizableHelper.decorate(binMetadata, DECO_VECTOR, binVector);
421+
}
422+
423+
/**
424+
* Extract the metadata from a decorated binary vector.
425+
*
426+
* @param binaryValue the decorated binary vector data
427+
*
428+
* @return the binary metadata or {@code null} if there was no metadata
429+
*/
430+
static Binary extractVector(Binary binaryValue)
431+
{
432+
ReadBuffer buffer = ExternalizableHelper.getDecoration((ReadBuffer) binaryValue, DECO_VECTOR);
433+
return buffer == null ? null : buffer.toBinary();
434+
}
435+
436+
static ReadBuffer extractMetadata(Binary binary)
437+
{
438+
return ExternalizableHelper.getUndecorated((ReadBuffer) binary);
439+
}
329440
}

0 commit comments

Comments
 (0)