def search_by_embedding(host, id, count, timeout):
    with grpc.insecure_channel(host) as channel:
        stub = pb2_grpc.ServerStub(channel)
        emb_response = stub.GetEmbedding(pb2.GetEmbeddingRequest(id=id),
                                         timeout=timeout)
        embedding = emb_response.embedding
        # print(embedding)
        response = stub.SearchByEmbedding(pb2.SearchByEmbeddingRequest(
            embedding=embedding, count=count),
                                          timeout=timeout)
    print("response: %s, %s" % (response.ids, response.scores))
def test(host, port, dim):
    print("host: %s:%d" % (host, port))

    channel = grpc.insecure_channel("%s:%d" % (host, port))
    stub = pb2_grpc.ServerStub(channel)

    response = stub.Total(pb2.EmptyRequest())
    print("total: %d" % response.count)

    embedding = list(np.random.random(dim).astype('float32'))
    print(embedding)
    id = 1
    response = stub.Add(pb2.AddRequest(id=id, embedding=embedding))
    print("response: %s" % response.message)

    embedding = list(np.random.random(dim).astype('float32'))
    print(embedding)
    id = 1
    response = stub.Add(pb2.AddRequest(id=id, embedding=embedding))
    print("response: %s" % response.message)

    embedding = list(np.random.random(dim).astype('float32'))
    id = 2
    response = stub.Add(pb2.AddRequest(id=id, embedding=embedding))
    print("response: %s" % response.message)

    embedding = list(np.random.random(dim).astype('float32'))
    id = 3
    response = stub.Add(pb2.AddRequest(id=id, embedding=embedding))
    print("response: %s" % response.message)

    response = stub.Total(pb2.EmptyRequest())
    print("total: %d" % response.count)

    id = 2
    response = stub.Search(pb2.SearchRequest(id=id, count=5))
    print("response: %s, %s" % (response.ids, response.scores))

    response = stub.Remove(pb2.IdRequest(id=2))
    print("response: %s" % response.message)

    response = stub.Total(pb2.EmptyRequest())
    print("total: %d" % response.count)

    id = 2
    response = stub.Search(pb2.SearchRequest(id=id, count=5))
    print("response: %s, %s" % (response.ids, response.scores))

    # response = stub.Remove(pb2.IdRequest(id=1))
    # response = stub.Remove(pb2.IdRequest(id=3))

    response = stub.Total(pb2.EmptyRequest())
    print("total: %d" % response.count)
def imports(host, embs_path, ids_path, keys_path):
    print("host: %s" % host)
    channel = grpc.insecure_channel(host)
    stub = pb2_grpc.ServerStub(channel)

    response = stub.Total(pb2.EmptyRequest())
    print("total: %d" % response.count)

    response = stub.Import(
        pb2.ImportRequest(embs_path=embs_path,
                          ids_path=ids_path,
                          keys_path=keys_path))
    print("response: %s" % response.message)

    response = stub.Total(pb2.EmptyRequest())
    print("total: %d" % response.count)
def test_key(host, port, dim):
    print("host: %s:%d" % (host, port))

    channel = grpc.insecure_channel("%s:%d" % (host, port))
    stub = pb2_grpc.ServerStub(channel)

    response = stub.Total(pb2.EmptyRequest())
    print("total: %d" % response.count)

    embedding = list(np.random.random(dim).astype('float32'))
    print(embedding)
    key = "k1"
    response = stub.Add(pb2.AddRequest(key=key, embedding=embedding))
    print("response: %s" % response.message)

    embedding = list(np.random.random(dim).astype('float32'))
    key = "k1"
    response = stub.Add(pb2.AddRequest(key=key, embedding=embedding))
    print("response: %s" % response.message)

    embedding = list(np.random.random(dim).astype('float32'))
    key = "k2"
    response = stub.Add(pb2.AddRequest(key=key, embedding=embedding))
    print("response: %s" % response.message)

    embedding = list(np.random.random(dim).astype('float32'))
    key = "k3"
    response = stub.Add(pb2.AddRequest(key=key, embedding=embedding))
    print("response: %s" % response.message)

    response = stub.Total(pb2.EmptyRequest())
    print("total: %d" % response.count)

    key = "k1"
    response = stub.Search(pb2.SearchRequest(key=key, count=5))
    print("response: %s, %s" % (response.ids, response.scores))

    key = "k2"
    response = stub.Search(pb2.SearchRequest(key=key, count=5))
    print("response: %s, %s" % (response.ids, response.scores))

    response = stub.Total(pb2.EmptyRequest())
    print("total: %d" % response.count)
def get_embedding(host, id, timeout):
    with grpc.insecure_channel(host) as channel:
        stub = pb2_grpc.ServerStub(channel)
        response = stub.GetEmbedding(pb2.GetEmbeddingRequest(id=id),
                                     timeout=timeout)
        print("response: %s" % response.embedding)
def _search_by_key(host, key, count, timeout, channel):
    stub = pb2_grpc.ServerStub(channel)
    return stub.Search(pb2.SearchRequest(key=key, count=count))
def search(host, id, count, timeout):
    with grpc.insecure_channel(host) as channel:
        stub = pb2_grpc.ServerStub(channel)
        response = stub.Search(pb2.SearchRequest(id=id, count=count),
                               timeout=timeout)
        print("response: %s, %s" % (response.ids, response.scores))