Skip to content

Commit

Permalink
optimize connect
Browse files Browse the repository at this point in the history
Signed-off-by: nameczz <zizhao.chen@zilliz.com>
  • Loading branch information
nameczz committed Jul 25, 2023
1 parent b6571ce commit cf98b9a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ __pycache__/
*.py[cod]
*$py.class
.vscode
.DS_Store

# C extensions
*.so
Expand Down
19 changes: 7 additions & 12 deletions milvus_cli/Connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,25 @@
from tabulate import tabulate

class MilvusConnection(object):
host = "127.0.0.1"
port = 19530
uri = "127.0.0.1:19530"
alias = "default"

def connect(self,
alias='',
host=None,
port=None,
secure=False,
uri=None,
username=None,
password=None):
self.alias = alias
self.host = host
self.port = port
self.uri = uri
trimUsername = None if username is None else username.strip()
trimPwd = None if password is None else password.strip()

try:
res = connections.connect(alias= self.alias,
host=self.host,
port=self.port,
uri=self.uri,
user=trimUsername,
password=trimPwd,
secure=secure)
password=trimPwd
)
return res
except Exception as e:
raise ConnectException(f"Connect to Milvus error!{str(e)}")
Expand All @@ -42,7 +37,7 @@ def checkConnection(self,alias=None):
def showConnection(self, alias=None, showAll=False):
tempAlias = alias if alias else self.alias
allConnections = connections.list_connections()

if showAll:
return tabulate(allConnections,
headers=["Alias"],
Expand Down
13 changes: 6 additions & 7 deletions milvus_cli/Database.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,31 @@ class Database():
def create_database(self,dbName=None,alias=None):
tempAlias = alias if alias else self.alias
try:
res = db.create_database(db_name=dbName,using=tempAlias)
print(res)
db.create_database(db_name=dbName,using=tempAlias)
return f"Create database {dbName} successfully!"
except Exception as e:
raise f"Create database error!{str(e)}"

def list_databases(self,alias=None):
tempAlias = alias if alias else self.alias
try:
res = db.list_database(using=tempAlias)
print('----',res)
return res
except Exception as e:
raise f"List database error!{str(e)}"

def drop_database(self,dbName=None,alias=None):
tempAlias = alias if alias else self.alias
try:
res = db.drop_database(db_name=dbName,using=tempAlias)
print(res)
db.drop_database(db_name=dbName,using=tempAlias)
return f"Drop database {dbName} successfully!"
except Exception as e:
raise f"Drop database error!{str(e)}"

def using_database(self,dbName=None,alias=None):
tempAlias = alias if alias else self.alias
try:
res = db.using_database(db_name=dbName,using=tempAlias)
print(res)
db.using_database(db_name=dbName,using=tempAlias)
return f"Using database {dbName} successfully!"
except Exception as e:
raise f"Using database error!{str(e)}"
9 changes: 4 additions & 5 deletions milvus_cli/test/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@
sys.path.append(parentdir)
from Connection import MilvusConnection

defaultAddress = 'localhost'
defaultPort = 19530
uri = "http://localhost:19530"
tempAlias = "zilliz"
milvusConnection = MilvusConnection()

class TestConnection(unittest.TestCase):

def setUp(self):
milvusConnection.connect(host=defaultAddress,port=defaultPort,alias=tempAlias)
milvusConnection.connect(uri=uri ,alias=tempAlias)

def tearDown(self):
milvusConnection.disconnect(alias=tempAlias)

def test_show_connection(self):
res = milvusConnection.showConnection(alias=tempAlias)
expectRes = tabulate(
[["Address", f'{defaultAddress}:{defaultPort}'], ["User", ''], ["Alias", tempAlias]],
[["Address", f'localhost:19530'], ["User", ''], ["Alias", tempAlias]],
tablefmt="pretty",
)
self.assertEqual(res, expectRes)
Expand Down
5 changes: 2 additions & 3 deletions milvus_cli/test/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
from Connection import MilvusConnection
from Database import Database

defaultAddress = 'localhost'
defaultPort = 19530
uri = "http://localhost:19530"
tempAlias = "zilliz"
dbName="test_db"
milvusConnection = MilvusConnection()
Expand All @@ -19,7 +18,7 @@
class TestDatabase(unittest.TestCase):

def setUp(self):
milvusConnection.connect(host=defaultAddress,port=defaultPort,alias=tempAlias)
milvusConnection.connect(uri=uri,alias=tempAlias)

def tearDown(self):
database.drop_database(dbName=dbName,alias=tempAlias)
Expand Down

0 comments on commit cf98b9a

Please sign in to comment.