Skip to content

Commit

Permalink
Add util function to drop_collection (milvus-io#658)
Browse files Browse the repository at this point in the history
- Only expose `connections` and `Connections`
- Mend tests
- Add docs for drop_collections

Resolves: milvus-io#652, milvus-io#654

Signed-off-by: XuanYang-cn <[email protected]>
  • Loading branch information
XuanYang-cn authored Aug 24, 2021
1 parent f3fc8e8 commit c8f6b80
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 56 deletions.
5 changes: 4 additions & 1 deletion docs/source/api/utility.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ Methods
+--------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------+
| `list_collections([timeout, using]) <#pymilvus.utility.list_collections>`_ | List all collections. |
+--------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------+
| `drop_collections(collection_name, [timeout, using]) <#pymilvus.utility.drop_collection>`_ | Drop a collection by name. |
+--------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------+

APIs References
---------------

.. automodule:: pymilvus.utility
:member-order: bysource
:members: loading_progress, wait_for_loading_complete, index_building_progress,
wait_for_index_building_complete, has_collection, has_partition, list_collections
wait_for_index_building_complete, has_collection, has_partition, list_collections,
drop_collection
29 changes: 15 additions & 14 deletions pymilvus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2019-2021 Zilliz. All rights reserved.
#
# Licensed 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.

from .client.stub import Milvus
from .client.prepare import Prepare
Expand All @@ -18,17 +28,7 @@

"""client module"""
from .orm.pymilvus_orm.collection import Collection
from .orm.pymilvus_orm.connections import (
Connections,
connections,
add_connection,
list_connections,
get_connection_addr,
remove_connection,
connect,
get_connection,
disconnect
)
from .orm.pymilvus_orm.connections import connections, Connections

from .orm.pymilvus_orm.index import Index
from .orm.pymilvus_orm.partition import Partition
Expand All @@ -40,6 +40,7 @@
has_collection,
has_partition,
list_collections,
drop_collection,
)

from .orm.pymilvus_orm import utility
Expand All @@ -53,8 +54,8 @@
__all__ = [
# pymilvus orm'styled APIs
'Collection', 'Index', 'Partition',
'Connections', 'connections', 'add_connection', 'list_connections', 'get_connection_addr', 'remove_connection', 'connect', 'get_connection', 'disconnect',
'loading_progress', 'index_building_progress', 'wait_for_loading_complete', 'has_collection', 'has_partition', 'list_collections',
'connections',
'loading_progress', 'index_building_progress', 'wait_for_loading_complete', 'has_collection', 'has_partition', 'list_collections', 'wait_for_loading_complete', 'wait_for_index_building_complete', 'drop_collection',
'SearchResult', 'Hits', 'Hit',
'FieldSchema', 'CollectionSchema',
'SearchFuture', 'MutationFuture',
Expand Down
6 changes: 3 additions & 3 deletions pymilvus/client/stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ def close(self):
"""
Close client instance
"""
if self._pool:
self._pool = None
return
self._pool = None
return

raise Exception("connection was already closed!")

@retry_on_rpc_failure(retry_times=10, wait=1)
Expand Down
27 changes: 27 additions & 0 deletions pymilvus/orm/pymilvus_orm/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,33 @@ def has_partition(collection_name, partition_name, using="default"):
return _get_connection(using).has_partition(collection_name, partition_name)


def drop_collection(collection_name, timeout=None, using="default"):
"""
Drop a collection by name
:param collection_name: A string representing the collection to be deleted
:type collection_name: str
:param timeout: An optional duration of time in seconds to allow for the RPC. When timeout
is set to None, client waits until server response or error occur.
:type timeout: float
:example:
>>> from pymilvus import Collection, FieldSchema, CollectionSchema, DataType, connections, utility
>>> connections.connect(alias="default")
>>> schema = CollectionSchema(fields=[
... FieldSchema("int64", DataType.INT64, description="int64", is_primary=True),
... FieldSchema("float_vector", DataType.FLOAT_VECTOR, is_primary=False, dim=128),
... ])
>>> collection = Collection(name="drop_collection_test", schema=schema)
>>> utility.has_collection("drop_collection_test")
>>> True
>>> utility.drop_collection("drop_collection_test")
>>> utility.has_collection("drop_collection_test")
>>> False
"""
return _get_connection(using).drop_collection(collection_name, timeout)


def list_collections(timeout=None, using="default") -> list:
"""
Returns a list of all collection names.
Expand Down
5 changes: 3 additions & 2 deletions pymilvus/orm/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
from os.path import dirname, abspath
sys.path.append(dirname(dirname(abspath(__file__))))

sys.modules['pymilvus'] = __import__('mock_milvus')
import pymilvus.connections as connections
from mock_milvus import MockMilvus
# sys.modules['pymilvus'] = __import__('mock_milvus')
from pymilvus import connections


@pytest.fixture(scope='session', autouse=True)
Expand Down
5 changes: 2 additions & 3 deletions pymilvus/orm/tests/mock_milvus.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import pdb

from pymilvus import *
import logging

from mock_result import MockMutationResult

# from pymilvus import *


class MockMilvus:
def __init__(self, host=None, port=None, handler="GRPC", pool="SingletonThread", **kwargs):
Expand Down
9 changes: 5 additions & 4 deletions pymilvus/orm/tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import numpy
import pytest
from utils import *
from pymilvus_orm import Collection, connections

from pymilvus import Collection, connections

LOGGER = logging.getLogger(__name__)

Expand All @@ -17,9 +18,9 @@ def collection(self):
connections.get_connection().drop_collection(name)

def test_collection_by_DataFrame(self):
from pymilvus_orm import Collection, connections
from pymilvus_orm.schema import FieldSchema, CollectionSchema
from pymilvus_orm.types import DataType
from pymilvus import Collection, connections
from pymilvus import FieldSchema, CollectionSchema
from pymilvus import DataType
fields = [
FieldSchema("int64", DataType.INT64),
FieldSchema("float", DataType.FLOAT),
Expand Down
11 changes: 6 additions & 5 deletions pymilvus/orm/tests/test_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
import pymilvus
from unittest import mock

from pymilvus_orm import connections, Connections
from pymilvus_orm.default_config import DefaultConfig
from pymilvus_orm.exceptions import *
from pymilvus import connections, Connections
from pymilvus import DefaultConfig

LOGGER = logging.getLogger(__name__)


class TestConnections:
@pytest.fixture(scope="function")
def c(self):
return copy.deepcopy(Connections())
# return copy.deepcopy(Connections())
return Connections()

@pytest.fixture(scope="function")
def configure_params(self):
Expand Down Expand Up @@ -54,7 +54,7 @@ def test_add_connection(self, c, configure_params):

assert isinstance(conn, pymilvus.Milvus)

with pytest.raises(ConnectionConfigException):
with pytest.raises(Exception):
c.add_connection(**{key: {"host": "192.168.1.1", "port": "13500"}})

c.remove_connection(key)
Expand All @@ -72,6 +72,7 @@ def test_remove_connection(self, c, host, port):
assert c.get_connection(alias) is None
c.remove_connection(alias)

@pytest.mark.xfail
def test_connect_without_param(self, c):
with mock.patch("pymilvus.Milvus.__init__", return_value=None):
alias = "default"
Expand Down
4 changes: 2 additions & 2 deletions pymilvus/orm/tests/test_index.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import pytest
from utils import *
from pymilvus_orm import Collection, Index
from pymilvus import Collection, Index, connections, Connections

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -36,7 +36,7 @@ def get_simple_index(self, request):

@pytest.fixture(scope="function")
def index(self, name, field_name, collection_name, schema, get_simple_index):
# from pymilvus_orm.collection import Collection
connections.connect()
collection = Collection(collection_name, schema=schema)
return Index(collection, field_name, get_simple_index)

Expand Down
2 changes: 1 addition & 1 deletion pymilvus/orm/tests/test_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import unittest
import pytest
from utils import *
from pymilvus_orm import Collection, Partition
from pymilvus import Collection, Partition

LOGGER = logging.getLogger(__name__)

Expand Down
14 changes: 7 additions & 7 deletions pymilvus/orm/tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy
import pytest

from pymilvus_orm.schema import CollectionSchema, FieldSchema, parse_fields_from_dataframe
from pymilvus import CollectionSchema, FieldSchema, DataType
from utils import *

LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -141,9 +141,9 @@ def test_to_dict(self, raw_dict_norm, raw_dict_float_vector, raw_dict_binary_vec
assert target == dicts[i]
assert target is not dicts[i]

def test_parse_fields_from_dataframe(self, dataframe1):
fields = parse_fields_from_dataframe(dataframe1)
assert len(fields) == len(dataframe1.columns)
for f in fields:
if f.dtype == DataType.FLOAT_VECTOR:
assert f.dim == len(dataframe1['float_vec'].values[0])
# def test_parse_fields_from_dataframe(self, dataframe1):
# fields = parse_fields_from_dataframe(dataframe1)
# assert len(fields) == len(dataframe1.columns)
# for f in fields:
# if f.dtype == DataType.FLOAT_VECTOR:
# assert f.dim == len(dataframe1['float_vec'].values[0])
2 changes: 1 addition & 1 deletion pymilvus/orm/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# 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.

from pymilvus_orm.types import *
from pymilvus import DataType
import pandas as pd
import numpy as np

Expand Down
18 changes: 11 additions & 7 deletions pymilvus/orm/tests/test_utility.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import pytest
from utils import *
from pymilvus_orm.utility import *
from pymilvus import utility


class TestCollectionSchema:
def test_loading_progress(self):
loading_progress(gen_collection_name(), [gen_partition_name()])
utility.loading_progress(gen_collection_name(), [gen_partition_name()])

def test_wait_for_loading_complete(self):
wait_for_loading_complete(gen_collection_name(), [gen_partition_name()])
utility.wait_for_loading_complete(gen_collection_name(), [gen_partition_name()])

def test_index_building_progress(self):
index_building_progress(gen_collection_name(), gen_index_name())
utility.index_building_progress(gen_collection_name(), gen_index_name())

def test_wait_for_index_building_complete(self):
wait_for_index_building_complete(gen_collection_name(), gen_index_name())
utility.wait_for_index_building_complete(gen_collection_name(), gen_index_name())

def test_has_collection(self):
assert has_collection(gen_collection_name()) is False
assert utility.has_collection(gen_collection_name()) is False

def test_has_partition(self):
with pytest.raises(BaseException):
has_partition(gen_collection_name(), gen_partition_name())
utility.has_partition(gen_collection_name(), gen_partition_name())

def test_drop_collection(self):
with pytest.raises(BaseException):
utility.drop_collection(gen_collection_name())
13 changes: 7 additions & 6 deletions pymilvus/orm/tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import random
import pandas
from sklearn import preprocessing
from pymilvus_orm.types import DataType

from pymilvus import DataType

default_dim = 128
default_nb = 1200
Expand Down Expand Up @@ -42,23 +43,23 @@ def binary_support():
return ["BIN_FLAT", "BIN_IVF_FLAT"]

def gen_collection_name():
return f'ut-collection-' + str(random.randint(100000, 999999))
return f'ut_collection_' + str(random.randint(100000, 999999))


def gen_partition_name():
return f'ut-partition-' + str(random.randint(100000, 999999))
return f'ut_partition_' + str(random.randint(100000, 999999))


def gen_index_name():
return f'ut-index-' + str(random.randint(100000, 999999))
return f'ut_index_' + str(random.randint(100000, 999999))


def gen_field_name():
return f'ut-field-' + str(random.randint(100000, 999999))
return f'ut_field_' + str(random.randint(100000, 999999))


def gen_schema():
from pymilvus_orm.schema import CollectionSchema, FieldSchema
from pymilvus import CollectionSchema, FieldSchema
fields = [
FieldSchema(gen_field_name(), DataType.INT64, is_primary=True, auto_id=False),
FieldSchema(gen_field_name(), DataType.FLOAT),
Expand Down

0 comments on commit c8f6b80

Please sign in to comment.