Browse Source

first commit :p

zhiru 5 years ago
parent
commit
27ceaae32f

+ 2 - 0
milvus-sdk-java.iml

@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4" />

+ 129 - 0
pom.xml

@@ -0,0 +1,129 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>io.milvus</groupId>
+    <artifactId>milvus-sdk-java</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <grpc.version>1.23.0</grpc.version><!-- CURRENT_GRPC_VERSION -->
+        <protobuf.version>3.9.0</protobuf.version>
+        <protoc.version>3.9.0</protoc.version>
+        <maven.compiler.source>1.8</maven.compiler.source>
+        <maven.compiler.target>1.8</maven.compiler.target>
+    </properties>
+
+    <dependencyManagement>
+        <dependencies>
+            <dependency>
+                <groupId>io.grpc</groupId>
+                <artifactId>grpc-bom</artifactId>
+                <version>${grpc.version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
+    <dependencies>
+        <dependency>
+            <groupId>io.grpc</groupId>
+            <artifactId>grpc-netty-shaded</artifactId>
+            <scope>runtime</scope>
+        </dependency>
+        <dependency>
+            <groupId>io.grpc</groupId>
+            <artifactId>grpc-protobuf</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>io.grpc</groupId>
+            <artifactId>grpc-stub</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>javax.annotation</groupId>
+            <artifactId>javax.annotation-api</artifactId>
+            <version>1.2</version>
+            <scope>provided</scope> <!-- not needed at runtime -->
+        </dependency>
+        <dependency>
+            <groupId>io.grpc</groupId>
+            <artifactId>grpc-testing</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.google.protobuf</groupId>
+            <artifactId>protobuf-java-util</artifactId>
+            <version>${protobuf.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.12</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <version>2.25.1</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.javatuples</groupId>
+            <artifactId>javatuples</artifactId>
+            <version>1.2</version>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <extensions>
+            <extension>
+                <groupId>kr.motd.maven</groupId>
+                <artifactId>os-maven-plugin</artifactId>
+                <version>1.6.2</version>
+            </extension>
+        </extensions>
+        <plugins>
+            <plugin>
+                <groupId>org.xolstice.maven.plugins</groupId>
+                <artifactId>protobuf-maven-plugin</artifactId>
+                <version>0.6.1</version>
+                <configuration>
+                    <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
+                    <pluginId>grpc-java</pluginId>
+                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
+                </configuration>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>compile</goal>
+                            <goal>compile-custom</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-enforcer-plugin</artifactId>
+                <version>1.4.1</version>
+                <executions>
+                    <execution>
+                        <id>enforce</id>
+                        <goals>
+                            <goal>enforce</goal>
+                        </goals>
+                        <configuration>
+                            <rules>
+                                <requireUpperBoundDeps/>
+                            </rules>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

+ 258 - 0
src/main/java/io/milvus/client/MilvusGrpcClient.java

@@ -0,0 +1,258 @@
+package io.milvus.client;
+
+import io.grpc.ManagedChannel;
+import io.grpc.ManagedChannelBuilder;
+import io.grpc.StatusRuntimeException;
+
+import io.milvus.client.grpc.RowRecord;
+import io.milvus.client.grpc.VectorIds;
+import io.milvus.client.params.*;
+import org.javatuples.Pair;
+
+import javax.annotation.Nonnull;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.Random;
+import java.util.concurrent.TimeUnit;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public class MilvusGrpcClient {
+
+    private static final Logger logger = Logger.getLogger(MilvusGrpcClient.class.getName());
+
+    private final ManagedChannel channel;
+    private final io.milvus.client.grpc.MilvusServiceGrpc.MilvusServiceBlockingStub blockingStub;
+    private final io.milvus.client.grpc.MilvusServiceGrpc.MilvusServiceFutureStub futureStub;
+    private final io.milvus.client.grpc.MilvusServiceGrpc.MilvusServiceStub asyncStub;
+
+    public MilvusGrpcClient(String host, int port) {
+        this(ManagedChannelBuilder.forAddress(host, port).usePlaintext());
+    }
+
+    public MilvusGrpcClient(ManagedChannelBuilder<?> channelBuilder) {
+        channel = channelBuilder.build();
+        blockingStub = io.milvus.client.grpc.MilvusServiceGrpc.newBlockingStub(channel);
+        futureStub = io.milvus.client.grpc.MilvusServiceGrpc.newFutureStub(channel);
+        asyncStub = io.milvus.client.grpc.MilvusServiceGrpc.newStub(channel);
+    }
+
+    public void shutdown() throws InterruptedException {
+        channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
+        logInfo("Shut down complete");
+    }
+
+    ///////////////////////Client Calls///////////////////////
+
+    public Response createTable(@Nonnull TableSchema tableSchema) {
+
+        io.milvus.client.grpc.TableSchema request = io.milvus.client.grpc.TableSchema
+                                                     .newBuilder()
+                                                     .setTableName(tableSchema.getTableName())
+                                                     .setDimension(tableSchema.getDimension())
+                                                     .setIndexFileSize(tableSchema.getIndexFileSize())
+                                                     .setMetricType(tableSchema.getMetricType().getVal())
+                                                     .build();
+
+        io.milvus.client.grpc.Status response;
+
+        try {
+            response = blockingStub.createTable(request);
+
+            if (response.getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
+                logInfo("Created table successfully!\nTableSchema = {0}", tableSchema.toString());
+                return new Response(Response.Status.SUCCESS);
+            } else {
+                logSevere("Create table failed\nTableSchema = {0}", tableSchema.toString());
+                return new Response(Response.Status.valueOf(response.getErrorCodeValue()), response.getReason());
+            }
+        } catch (StatusRuntimeException e) {
+            logSevere("Create table RPC failed: {0}", e.getStatus().toString());
+            return new Response(Response.Status.RPC_ERROR, e.toString());
+        }
+    }
+
+    public boolean hasTable(@Nonnull String tableName) {
+        io.milvus.client.grpc.TableName request = io.milvus.client.grpc.TableName
+                                                 .newBuilder()
+                                                 .setTableName(tableName)
+                                                 .build();
+        io.milvus.client.grpc.BoolReply response;
+
+        try {
+            response = blockingStub.hasTable(request);
+
+            if (response.getStatus().getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
+                logInfo("hasTable \'{0}\' = {1}", tableName, response.getBoolReply());
+                return response.getBoolReply();
+            } else {
+                logSevere("hasTable failed: ", response.toString());
+                return false;
+            }
+        } catch (StatusRuntimeException e) {
+            logSevere("hasTable RPC failed: {0}", e.getStatus().toString());
+            return false;
+        }
+    }
+
+    public Response dropTable(@Nonnull String tableName) {
+        io.milvus.client.grpc.TableName request = io.milvus.client.grpc.TableName
+                                                  .newBuilder()
+                                                  .setTableName(tableName)
+                                                  .build();
+        io.milvus.client.grpc.Status response;
+
+        try {
+            response = blockingStub.dropTable(request);
+
+            if (response.getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
+                logInfo("Dropped table \'{0}\' successfully!", tableName);
+                return new Response(Response.Status.SUCCESS);
+            } else {
+                logSevere("Drop table \'{0}\' failed", tableName);
+                return new Response(Response.Status.valueOf(response.getErrorCodeValue()), response.getReason());
+            }
+        } catch (StatusRuntimeException e) {
+            logSevere("Drop table RPC failed: {0}", e.getStatus().toString());
+            return new Response(Response.Status.RPC_ERROR, e.toString());
+        }
+    }
+
+    public Response createIndex(@Nonnull IndexParam indexParam) {
+        io.milvus.client.grpc.Index index = io.milvus.client.grpc.Index
+                                            .newBuilder()
+                                            .setIndexType(indexParam.getIndex().getIndexType().getVal())
+                                            .setNlist(indexParam.getIndex().getnNList())
+                                            .build();
+        io.milvus.client.grpc.IndexParam request = io.milvus.client.grpc.IndexParam
+                                           .newBuilder()
+                                           .setTableName(indexParam.getTableName())
+                                           .setIndex(index)
+                                           .build();
+
+        io.milvus.client.grpc.Status response;
+
+        try {
+            response = blockingStub.createIndex(request);
+
+            if (response.getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
+                logInfo("Created index successfully!\nIndexParam = {0}", indexParam.toString());
+                return new Response(Response.Status.SUCCESS);
+            } else {
+                logSevere("Create index failed\nIndexParam = {0}", indexParam.toString());
+                return new Response(Response.Status.valueOf(response.getErrorCodeValue()), response.getReason());
+            }
+        } catch (StatusRuntimeException e) {
+            logSevere("Create index RPC failed: {0}", e.getStatus().toString());
+            return new Response(Response.Status.RPC_ERROR, e.toString());
+        }
+    }
+
+    public Pair<Response, Optional<List<Long>>> insert(@Nonnull InsertParam insertParam) {
+
+        List<io.milvus.client.grpc.RowRecord> rowRecordList = new ArrayList<>();
+        for (List<Float> vectors : insertParam.getVectors()) {
+            io.milvus.client.grpc.RowRecord rowRecord = io.milvus.client.grpc.RowRecord
+                                                        .newBuilder()
+                                                        .addAllVectorData(vectors)
+                                                        .build();
+            rowRecordList.add(rowRecord);
+        }
+
+        io.milvus.client.grpc.InsertParam request = io.milvus.client.grpc.InsertParam
+                                                    .newBuilder()
+                                                    .setTableName(insertParam.getTableName())
+                                                    .addAllRowRecordArray(rowRecordList)
+                                                    .addAllRowIdArray(insertParam.getVectorIds())
+                                                    .build();
+        io.milvus.client.grpc.VectorIds response;
+
+        try {
+            response = blockingStub.insert(request);
+            Optional<List<Long>> resultVectorIds = Optional.ofNullable(response.getVectorIdArrayList());
+
+            if (response.getStatus().getErrorCode() == io.milvus.client.grpc.ErrorCode.SUCCESS) {
+                logInfo("Inserted vectors successfully!");
+                return Pair.with(new Response(Response.Status.SUCCESS), resultVectorIds);
+            } else {
+                logSevere("Insert vectors failed");
+                return Pair.with(new Response(Response.Status.valueOf(response.getStatus().getErrorCodeValue()),
+                                              response.getStatus().getReason()),
+                                 Optional.empty());
+            }
+        } catch (StatusRuntimeException e) {
+            logSevere("Insert RPC failed: {0}", e.getStatus().toString());
+            return Pair.with(new Response(Response.Status.RPC_ERROR, e.toString()), Optional.empty());
+        }
+    }
+
+    /////////////////////Util Functions/////////////////////
+
+    public static final String ANSI_RESET = "\u001B[0m";
+    public static final String ANSI_BLACK = "\u001B[30m";
+    public static final String ANSI_RED = "\u001B[31m";
+    public static final String ANSI_GREEN = "\u001B[32m";
+    public static final String ANSI_YELLOW = "\u001B[33m";
+    public static final String ANSI_BLUE = "\u001B[34m";
+    public static final String ANSI_PURPLE = "\u001B[35m";
+    public static final String ANSI_CYAN = "\u001B[36m";
+    public static final String ANSI_WHITE = "\u001B[37m";
+
+    private void logInfo(String msg, Object... params) {
+        logger.log(Level.INFO, ANSI_GREEN + msg + ANSI_RESET, params);
+    }
+
+    private void logWarning(String msg, Object... params) {
+        logger.log(Level.WARNING, ANSI_YELLOW + msg + ANSI_RESET, params);
+    }
+
+    private void logSevere(String msg, Object... params) {
+        logger.log(Level.SEVERE, ANSI_PURPLE + msg + ANSI_RESET, params);
+    }
+
+    //////////////////////////Main///////////////////////////
+    public static void main(String[] args) throws InterruptedException {
+        MilvusGrpcClient client = new MilvusGrpcClient("192.168.1.188", 19531);
+
+        try {
+            String tableName = "test_zhiru";
+            long dimension = 128;
+            TableSchema tableSchema = new TableSchema.Builder(tableName, dimension)
+                                                               .setIndexFileSize(1024)
+                                                               .setMetricType(MetricType.L2)
+                                                               .build();
+            Response createTableResponse = client.createTable(tableSchema);
+            System.out.println(createTableResponse);
+
+            boolean hasTableResponse = client.hasTable(tableName);
+
+            Random random = new Random();
+            List<List<Float>> vectors = new ArrayList<>();
+            int size = 100;
+            for (int i = 0; i < size; ++i) {
+                List<Float> vector = new ArrayList<>();
+                for (int j = 0; j < dimension; ++j) {
+                    vector.add(random.nextFloat());
+                }
+                vectors.add(vector);
+            }
+            InsertParam insertParam = new InsertParam.Builder(tableName, vectors).build();
+            Pair<Response, Optional<List<Long>>> insertResult = client.insert(insertParam);
+            System.out.println(insertResult.getValue0());
+
+            Index index = new Index.Builder()
+                                   .setIndexType(IndexType.IVF_SQ8)
+                                   .setNList(16384)
+                                   .build();
+            IndexParam indexParam = new IndexParam.Builder(tableName)
+                                                  .setIndex(index)
+                                                  .build();
+            Response createIndexResponse = client.createIndex(indexParam);
+            System.out.println(createIndexResponse);
+
+        } finally {
+            client.shutdown();
+        }
+    }
+}

+ 86 - 0
src/main/java/io/milvus/client/Response.java

@@ -0,0 +1,86 @@
+package io.milvus.client;
+
+import java.util.Arrays;
+import java.util.Optional;
+
+public class Response {
+
+    public enum Status {
+        SUCCESS(0),
+        UNEXPECTED_ERROR(1),
+        CONNECT_FAILED(2),
+        PERMISSION_DENIED(3),
+        TABLE_NOT_EXISTS(4),
+        ILLEGAL_ARGUMENT(5),
+        ILLEGAL_RANGE(6),
+        ILLEGAL_DIMENSION(7),
+        ILLEGAL_INDEX_TYPE(8),
+        ILLEGAL_TABLE_NAME(9),
+        ILLEGAL_TOPK(10),
+        ILLEGAL_ROWRECORD(11),
+        ILLEGAL_VECTOR_ID(12),
+        ILLEGAL_SEARCH_RESULT(13),
+        FILE_NOT_FOUND(14),
+        META_FAILED(15),
+        CACHE_FAILED(16),
+        CANNOT_CREATE_FOLDER(17),
+        CANNOT_CREATE_FILE(18),
+        CANNOT_DELETE_FOLDER(19),
+        CANNOT_DELETE_FILE(20),
+        BUILD_INDEX_ERROR(21),
+        ILLEGAL_NLIST(22),
+        ILLEGAL_METRIC_TYPE(23),
+        OUT_OF_MEMORY(24),
+
+        RPC_ERROR(-1),
+        UNKNOWN(-2);
+
+        private final int code;
+
+        Status(int code) {
+            this.code = code;
+        }
+
+        public int getCode() {
+            return code;
+        }
+
+        //TODO: Potential performance issue?
+        public static Status valueOf(int val) {
+            Optional<Status> search = Arrays.stream(values())
+                                            .filter(status -> status.code == val)
+                                            .findFirst();
+            return search.orElse(UNKNOWN);
+        }
+    };
+
+    private final Status status;
+    private final String message;
+
+    Response(Status status, String message) {
+        this.status = status;
+        this.message = message;
+    }
+
+    Response(Status status) {
+        this.status = status;
+        this.message = "Success!";
+    }
+
+    public Status getStatus() {
+        return status;
+    }
+
+    public String getMessage() {
+        return message;
+    }
+
+    public boolean ok() {
+        return status == Status.SUCCESS;
+    }
+
+    @Override
+    public String toString() {
+        return String.format("{code = %s, message = %s}", status.name(), this.message);
+    }
+}

+ 39 - 0
src/main/java/io/milvus/client/params/Index.java

@@ -0,0 +1,39 @@
+package io.milvus.client.params;
+
+public class Index {
+    private final IndexType indexType;
+    private final int nList;
+
+    public static class Builder {
+        // Optional parameters - initialized to default values
+        private IndexType indexType = IndexType.FLAT;
+        private int nList = 16384;
+
+        public Builder setIndexType(IndexType val) {
+            indexType = val;
+            return this;
+        }
+
+        public Builder setNList(int val) {
+            nList = val;
+            return this;
+        }
+
+        public Index build() {
+            return new Index(this);
+        }
+    }
+
+    private Index(Builder builder) {
+        this.indexType = builder.indexType;
+        this.nList = builder.nList;
+    }
+
+    public IndexType getIndexType() {
+        return indexType;
+    }
+
+    public int getnNList() {
+        return nList;
+    }
+}

+ 47 - 0
src/main/java/io/milvus/client/params/IndexParam.java

@@ -0,0 +1,47 @@
+package io.milvus.client.params;
+
+public class IndexParam {
+
+    private final String tableName;
+    private final Index index;
+
+    public static class Builder {
+        // Required parameters
+        private final String tableName;
+
+        // Optional parameters - initialized to default values
+        private Index index;
+
+        public Builder(String tableName) {
+            this.tableName = tableName;
+        }
+
+        public Builder setIndex(Index indexToSet) {
+            index = indexToSet;
+            return this;
+        }
+
+        public IndexParam build() {
+            return new IndexParam(this);
+        }
+    }
+
+    private IndexParam(Builder builder) {
+        this.tableName = builder.tableName;
+        this.index = builder.index;
+    }
+
+    public String getTableName() {
+        return tableName;
+    }
+
+    public Index getIndex() {
+        return index;
+    }
+
+    @Override
+    public String toString() {
+        return String.format("IndexParam {tableName = %s, index = {indexType = %s, nList = %d}",
+                                tableName, index.getIndexType().name(), index.getnNList());
+    }
+}

+ 20 - 0
src/main/java/io/milvus/client/params/IndexType.java

@@ -0,0 +1,20 @@
+package io.milvus.client.params;
+
+public enum IndexType {
+
+    INVALID(0),
+    FLAT(1),
+    IVFLAT(2),
+    IVF_SQ8(3),
+    MIX_NSG(4);
+
+    private final int indexType;
+
+    IndexType(int indexType) {
+        this.indexType = indexType;
+    }
+
+    public int getVal() {
+        return indexType;
+    }
+}

+ 51 - 0
src/main/java/io/milvus/client/params/InsertParam.java

@@ -0,0 +1,51 @@
+package io.milvus.client.params;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class InsertParam {
+    private final String tableName;
+    private final List<List<Float>> vectors;
+    private final List<Long> vectorIds;
+
+    public static class Builder {
+        // Required parameters
+        private final String tableName;
+        private final List<List<Float>> vectors;
+
+        // Optional parameters - initialized to default values
+        private List<Long> vectorIds = new ArrayList<>();
+
+        public Builder(String tableName, List<List<Float>> vectors) {
+            this.tableName = tableName;
+            this.vectors = vectors;
+        }
+
+        public Builder setVectorIds(List<Long> val) {
+            vectorIds = val;
+            return this;
+        }
+
+        public InsertParam build() {
+            return new InsertParam(this);
+        }
+    }
+
+    private InsertParam(Builder builder) {
+        this.tableName = builder.tableName;
+        this.vectors = builder.vectors;
+        this.vectorIds = builder.vectorIds;
+    }
+
+    public String getTableName() {
+        return tableName;
+    }
+
+    public List<List<Float>> getVectors() {
+        return vectors;
+    }
+
+    public List<Long> getVectorIds() {
+        return vectorIds;
+    }
+}

+ 16 - 0
src/main/java/io/milvus/client/params/MetricType.java

@@ -0,0 +1,16 @@
+package io.milvus.client.params;
+
+public enum MetricType {
+    L2(1),
+    IP(2);
+
+    private final int metricType;
+
+    MetricType(int metricType) {
+        this.metricType = metricType;
+    }
+
+    public int getVal() {
+        return metricType;
+    }
+}

+ 68 - 0
src/main/java/io/milvus/client/params/TableSchema.java

@@ -0,0 +1,68 @@
+package io.milvus.client.params;
+
+import javax.annotation.*;
+
+// Builder Pattern
+public class TableSchema {
+    private final String tableName;
+    private final long dimension;
+    private final long indexFileSize;
+    private final MetricType metricType;
+
+    public static class Builder {
+        // Required parameters
+        private final String tableName;
+        private final long dimension;
+
+        // Optional parameters - initialized to default values
+        private long indexFileSize = 1024;
+        private MetricType metricType = MetricType.L2;
+
+        public Builder(String tableName, long dimension) {
+            this.tableName = tableName;
+            this.dimension = dimension;
+        }
+
+        public Builder setIndexFileSize(long val) {
+            indexFileSize = val;
+            return this;
+        }
+        public Builder setMetricType(MetricType val) {
+            metricType = val;
+            return this;
+        }
+
+        public TableSchema build() {
+            return new TableSchema(this);
+        }
+    }
+
+    private TableSchema(@Nonnull Builder builder) {
+        tableName = builder.tableName;
+        dimension = builder.dimension;
+        indexFileSize = builder.indexFileSize;
+        metricType = builder.metricType;
+    }
+
+    public String getTableName() {
+        return tableName;
+    }
+
+    public long getDimension() {
+        return dimension;
+    }
+
+    public long getIndexFileSize() {
+        return indexFileSize;
+    }
+
+    public MetricType getMetricType() {
+        return metricType;
+    }
+
+    @Override
+    public String toString() {
+        return String.format("TableSchema {tableName = %s, dimension = %d, indexFileSize = %d, metricType = %s}",
+                             tableName, dimension, indexFileSize, metricType.name());
+    }
+}

+ 327 - 0
src/main/proto/milvus.proto

@@ -0,0 +1,327 @@
+syntax = "proto3";
+
+import "status.proto";
+
+option java_multiple_files = true;
+option java_package = "io.milvus.client.grpc";
+option java_outer_classname = "MilvusProto";
+
+package milvus.grpc;
+
+/**
+ * @brief Table Name
+ */
+message TableName {
+    string table_name = 1;
+}
+
+/**
+ * @brief Table Name List
+ */
+message TableNameList {
+    Status status = 1;
+    repeated string table_names = 2;
+}
+
+/**
+ * @brief Table Schema
+ */
+message TableSchema {
+    Status status = 1;
+    string table_name = 2;
+    int64 dimension = 3;
+    int64 index_file_size = 4;
+    int32 metric_type = 5;
+}
+
+/**
+ * @brief Range Schema
+ */
+message Range {
+    string start_value = 1;
+    string end_value = 2;
+}
+
+/**
+ * @brief Record inserted
+ */
+message RowRecord {
+    repeated float vector_data = 1;             //binary vector data
+}
+
+/**
+ * @brief params to be inserted
+ */
+message InsertParam {
+    string table_name = 1;
+    repeated RowRecord row_record_array = 2;
+    repeated int64 row_id_array = 3;            //optional
+}
+
+/**
+ * @brief Vector ids
+ */
+message VectorIds {
+    Status status = 1;
+    repeated int64 vector_id_array = 2;
+}
+
+/**
+ * @brief params for searching vector
+ */
+message SearchParam {
+    string table_name = 1;
+    repeated RowRecord query_record_array = 2;
+    repeated Range query_range_array = 3;
+    int64 topk = 4;
+    int64 nprobe = 5;
+}
+
+/**
+ * @brief params for searching vector in files
+ */
+message SearchInFilesParam {
+    repeated string file_id_array = 1;
+    SearchParam search_param = 2;
+}
+
+/**
+ * @brief Query result params
+ */
+message QueryResult {
+    int64 id = 1;
+    double distance = 2;
+}
+
+/**
+ * @brief TopK query result
+ */
+message TopKQueryResult {
+    repeated QueryResult query_result_arrays = 1;
+}
+
+/**
+ * @brief List of topK query result
+ */
+message TopKQueryResultList {
+    Status status = 1;
+    repeated TopKQueryResult topk_query_result = 2;
+}
+
+/**
+ * @brief Server String Reply
+ */
+message StringReply {
+    Status status = 1;
+    string string_reply = 2;
+}
+
+/**
+ * @brief Server bool Reply
+ */
+message BoolReply {
+    Status status = 1;
+    bool bool_reply = 2;
+}
+
+/**
+ * @brief Return table row count
+ */
+message TableRowCount {
+    Status status = 1;
+    int64 table_row_count = 2;
+}
+
+/**
+ * @brief Give Server Command
+ */
+message Command {
+    string cmd = 1;
+}
+
+/**
+ * @brief Index
+ * @index_type: 0-invalid, 1-idmap, 2-ivflat, 3-ivfsq8, 4-nsgmix
+ * @metric_type: 1-L2, 2-IP
+ */
+message Index {
+    int32 index_type = 1;
+    int32 nlist = 2;
+}
+
+/**
+ * @brief Index params
+ */
+message IndexParam {
+    Status status = 1;
+    string table_name = 2;
+    Index index = 3;
+}
+
+/**
+ * @brief table name and range for DeleteByRange
+ */
+message DeleteByRangeParam {
+    Range range = 1;
+    string table_name = 2;
+}
+
+service MilvusService {
+    /**
+     * @brief Create table method
+     *
+     * This method is used to create table
+     *
+     * @param param, use to provide table information to be created.
+     *
+     */
+    rpc CreateTable(TableSchema) returns (Status){}
+
+    /**
+     * @brief Test table existence method
+     *
+     * This method is used to test table existence.
+     *
+     * @param table_name, table name is going to be tested.
+     *
+     */
+    rpc HasTable(TableName) returns (BoolReply) {}
+
+    /**
+     * @brief Delete table method
+     *
+     * This method is used to delete table.
+     *
+     * @param table_name, table name is going to be deleted.
+     *
+     */
+    rpc DropTable(TableName) returns (Status) {}
+
+    /**
+     * @brief Build index by table method
+     *
+     * This method is used to build index by table in sync mode.
+     *
+     * @param table_name, table is going to be built index.
+     *
+     */
+    rpc CreateIndex(IndexParam) returns (Status) {}
+
+    /**
+     * @brief Add vector array to table
+     *
+     * This method is used to add vector array to table.
+     *
+     * @param table_name, table_name is inserted.
+     * @param record_array, vector array is inserted.
+     *
+     * @return vector id array
+     */
+    rpc Insert(InsertParam) returns (VectorIds) {}
+
+    /**
+     * @brief Query vector
+     *
+     * This method is used to query vector in table.
+     *
+     * @param table_name, table_name is queried.
+     * @param query_record_array, all vector are going to be queried.
+     * @param query_range_array, optional ranges for conditional search. If not specified, search whole table
+     * @param topk, how many similarity vectors will be searched.
+     *
+     * @return query result array.
+     */
+    rpc Search(SearchParam) returns (TopKQueryResultList) {}
+
+    /**
+     * @brief Internal use query interface
+     *
+     * This method is used to query vector in specified files.
+     *
+     * @param file_id_array, specified files id array, queried.
+     * @param query_record_array, all vector are going to be queried.
+     * @param query_range_array, optional ranges for conditional search. If not specified, search whole table
+     * @param topk, how many similarity vectors will be searched.
+     *
+     * @return query result array.
+     */
+    rpc SearchInFiles(SearchInFilesParam) returns (TopKQueryResultList) {}
+
+    /**
+     * @brief Get table schema
+     *
+     * This method is used to get table schema.
+     *
+     * @param table_name, target table name.
+     *
+     * @return table schema
+     */
+    rpc DescribeTable(TableName) returns (TableSchema) {}
+
+    /**
+     * @brief Get table schema
+     *
+     * This method is used to get table schema.
+     *
+     * @param table_name, target table name.
+     *
+     * @return table schema
+     */
+    rpc CountTable(TableName) returns (TableRowCount) {}
+
+    /**
+     * @brief List all tables in database
+     *
+     * This method is used to list all tables.
+     *
+     *
+     * @return table names.
+     */
+    rpc ShowTables(Command) returns (TableNameList) {}
+
+    /**
+     * @brief Give the server status
+     *
+     * This method is used to give the server status.
+     *
+     * @return Server status.
+     */
+    rpc Cmd(Command) returns (StringReply) {}
+
+    /**
+    * @brief delete table by range
+    *
+    * This method is used to delete vector by range
+    *
+    * @return rpc status.
+    */
+    rpc DeleteByRange(DeleteByRangeParam) returns (Status) {}
+
+    /**
+     * @brief preload table
+     *
+     * This method is used to preload table
+     *
+     * @return Status.
+     */
+    rpc PreloadTable(TableName) returns (Status) {}
+
+    /**
+     * @brief describe index
+     *
+     * This method is used to describe index
+     *
+     * @return Status.
+     */
+    rpc DescribeIndex(TableName) returns (IndexParam) {}
+
+    /**
+     * @brief drop index
+     *
+     * This method is used to drop index
+     *
+     * @return Status.
+     */
+    rpc DropIndex(TableName) returns (Status) {}
+
+}

+ 40 - 0
src/main/proto/status.proto

@@ -0,0 +1,40 @@
+syntax = "proto3";
+
+option java_multiple_files = true;
+option java_package = "io.milvus.client.grpc";
+option java_outer_classname = "MilvusStatusProto";
+
+package milvus.grpc;
+
+enum ErrorCode {
+    SUCCESS = 0;
+    UNEXPECTED_ERROR = 1;
+    CONNECT_FAILED = 2;
+    PERMISSION_DENIED = 3;
+    TABLE_NOT_EXISTS = 4;
+    ILLEGAL_ARGUMENT = 5;
+    ILLEGAL_RANGE = 6;
+    ILLEGAL_DIMENSION = 7;
+    ILLEGAL_INDEX_TYPE = 8;
+    ILLEGAL_TABLE_NAME = 9;
+    ILLEGAL_TOPK = 10;
+    ILLEGAL_ROWRECORD = 11;
+    ILLEGAL_VECTOR_ID = 12;
+    ILLEGAL_SEARCH_RESULT = 13;
+    FILE_NOT_FOUND = 14;
+    META_FAILED = 15;
+    CACHE_FAILED = 16;
+    CANNOT_CREATE_FOLDER = 17;
+    CANNOT_CREATE_FILE = 18;
+    CANNOT_DELETE_FOLDER = 19;
+    CANNOT_DELETE_FILE = 20;
+    BUILD_INDEX_ERROR = 21;
+    ILLEGAL_NLIST = 22;
+    ILLEGAL_METRIC_TYPE = 23;
+    OUT_OF_MEMORY = 24;
+}
+
+message Status {
+    ErrorCode error_code = 1;
+    string reason = 2;
+}