From 095fccd4a5462e62408fb16eca0311061e496006 Mon Sep 17 00:00:00 2001 From: jinhai Date: Tue, 9 Apr 2019 12:03:17 +0800 Subject: [PATCH] Update Group handle Former-commit-id: 763ec1587990a4c47ee52e1e255ca75e1b91320a --- pyengine/engine/controller/meta_manager.py | 16 ++++++--- pyengine/engine/controller/storage_manager.py | 8 +++++ .../controller/tests/test_vector_engine.py | 12 +++---- pyengine/engine/controller/vector_engine.py | 36 ++++++++----------- pyengine/engine/controller/views.py | 4 +-- 5 files changed, 40 insertions(+), 36 deletions(-) diff --git a/pyengine/engine/controller/meta_manager.py b/pyengine/engine/controller/meta_manager.py index ab556387e1..195302a246 100644 --- a/pyengine/engine/controller/meta_manager.py +++ b/pyengine/engine/controller/meta_manager.py @@ -27,8 +27,14 @@ class MetaManager(object): else: return ErrorCode.FAULT_CODE, None - # def DeleteGroup(group_id): - # group = GroupTable.query.filter(GroupTable.group_name==group_id).first() - # if(group): - # db.session.delete(group) - # else: \ No newline at end of file + @staticmethod + def DeleteGroup(group): + db.session.delete(group) + + @staticmethod + def DeleteGroupFiles(group_name): + records = FileTable.query.filter(FileTable.group_name == group_name).all() + for record in records: + print("record.group_name: ", record.group_name) + db.session.delete(record) + \ No newline at end of file diff --git a/pyengine/engine/controller/storage_manager.py b/pyengine/engine/controller/storage_manager.py index 450d1b75f1..aa0ee19f4a 100644 --- a/pyengine/engine/controller/storage_manager.py +++ b/pyengine/engine/controller/storage_manager.py @@ -14,6 +14,14 @@ class StorageManager(object): def GetGroupDirectory(group_name): return DATABASE_DIRECTORY + '/' + group_name + @staticmethod + def DeleteGroup(group_id): + path = StorageManager.GetGroupDirectory(group_id) + path = path.strip() + path=path.rstrip("\\") + if os.path.exists(path): + shutil.rmtree(path) + def Read(): pass diff --git a/pyengine/engine/controller/tests/test_vector_engine.py b/pyengine/engine/controller/tests/test_vector_engine.py index 9eb84f6e08..d1774157c6 100644 --- a/pyengine/engine/controller/tests/test_vector_engine.py +++ b/pyengine/engine/controller/tests/test_vector_engine.py @@ -21,10 +21,9 @@ class TestVectorEngine: def test_group(self): # Make sure there is no group - code, group_id, file_number = VectorEngine.DeleteGroup('test_group') + code, group_id = VectorEngine.DeleteGroup('test_group') assert code == VectorEngine.SUCCESS_CODE assert group_id == 'test_group' - assert file_number == 0 # Add a group code, group_id = VectorEngine.AddGroup('test_group', 8) @@ -32,10 +31,9 @@ class TestVectorEngine: assert group_id == 'test_group' # Check the group existing - code, group_id, file_number = VectorEngine.GetGroup('test_group') + code, group_id = VectorEngine.GetGroup('test_group') assert code == VectorEngine.SUCCESS_CODE assert group_id == 'test_group' - assert file_number == 0 # Check the group list code, group_list = VectorEngine.GetGroupList() @@ -63,16 +61,14 @@ class TestVectorEngine: assert code == VectorEngine.SUCCESS_CODE # Remove the group - code, group_id, file_number = VectorEngine.DeleteGroup('test_group') + code, group_id = VectorEngine.DeleteGroup('test_group') assert code == VectorEngine.SUCCESS_CODE assert group_id == 'test_group' - assert file_number == 0 # Check the group is disppeared - code, group_id, file_number = VectorEngine.GetGroup('test_group') + code, group_id = VectorEngine.GetGroup('test_group') assert code == VectorEngine.FAULT_CODE assert group_id == 'test_group' - assert file_number == 0 # Check SearchVector interface code, vector_ids = VectorEngine.SearchVector('test_group', self.__vector, self.__limit) diff --git a/pyengine/engine/controller/vector_engine.py b/pyengine/engine/controller/vector_engine.py index 91719cb3d5..8b31be2a71 100644 --- a/pyengine/engine/controller/vector_engine.py +++ b/pyengine/engine/controller/vector_engine.py @@ -24,41 +24,35 @@ class VectorEngine(object): @staticmethod def AddGroup(group_name, dimension): error, group = MetaManager.GetGroup(group_name) - if(error == ErrorCode.SUCCESS_CODE): + if error == ErrorCode.SUCCESS_CODE: return ErrorCode.FAULT_CODE, group_name else: StorageManager.AddGroup(group_name) MetaManager.AddGroup(group_name, dimension) - return VectorEngine.SUCCESS_CODE, group_name + return ErrorCode.SUCCESS_CODE, group_name @staticmethod - def GetGroup(group_id): - group = GroupTable.query.filter(GroupTable.group_name==group_id).first() - if group: - return VectorEngine.SUCCESS_CODE, group_id, group.file_number - else: - return VectorEngine.FAULT_CODE, group_id, 0 - + def GetGroup(group_name): + error, _ = MetaManager.GetGroup(group_name) + return error, group_name @staticmethod - def DeleteGroup(group_id): - group = GroupTable.query.filter(GroupTable.group_name==group_id).first() + def DeleteGroup(group_name): + group = GroupTable.query.filter(GroupTable.group_name==group_name).first() if(group): - # old_group = GroupTable(group_id) - db.session.delete(group) - db.session.commit() - GroupHandler.DeleteGroupDirectory(group_id) + MetaManager.DeleteGroup(group) + StorageManager.DeleteGroup(group_name) - records = FileTable.query.filter(FileTable.group_name == group_id).all() + records = FileTable.query.filter(FileTable.group_name == group_name).all() for record in records: print("record.group_name: ", record.group_name) db.session.delete(record) db.session.commit() - return VectorEngine.SUCCESS_CODE, group_id, group.file_number + return VectorEngine.SUCCESS_CODE, group_name else: - return VectorEngine.SUCCESS_CODE, group_id, 0 + return VectorEngine.SUCCESS_CODE, group_name @staticmethod @@ -78,7 +72,7 @@ class VectorEngine(object): @staticmethod def AddVector(group_id, vectors): print(group_id, vectors) - code, _, _ = VectorEngine.GetGroup(group_id) + code, _, = VectorEngine.GetGroup(group_id) if code == VectorEngine.FAULT_CODE: return VectorEngine.GROUP_NOT_EXIST, 'invalid' @@ -140,7 +134,7 @@ class VectorEngine(object): @staticmethod def SearchVector(group_id, vector, limit): # Check the group exist - code, _, _ = VectorEngine.GetGroup(group_id) + code, _ = VectorEngine.GetGroup(group_id) if code == VectorEngine.FAULT_CODE: return VectorEngine.GROUP_NOT_EXIST, {} @@ -168,7 +162,7 @@ class VectorEngine(object): @staticmethod def CreateIndex(group_id): # Check the group exist - code, _, _ = VectorEngine.GetGroup(group_id) + code, _ = VectorEngine.GetGroup(group_id) if code == VectorEngine.FAULT_CODE: return VectorEngine.GROUP_NOT_EXIST diff --git a/pyengine/engine/controller/views.py b/pyengine/engine/controller/views.py index 51e57cfba7..6848133a35 100644 --- a/pyengine/engine/controller/views.py +++ b/pyengine/engine/controller/views.py @@ -63,11 +63,11 @@ class Group(Resource): return jsonify({'code': code, 'group': group_id, 'filenumber': 0}) def get(self, group_id): - code, group_id, file_number = VectorEngine.GetGroup(group_id) + code, group_id = VectorEngine.GetGroup(group_id) return jsonify({'code': code, 'group': group_id, 'filenumber': 0}) def delete(self, group_id): - code, group_id, file_number = VectorEngine.DeleteGroup(group_id) + code, group_id = VectorEngine.DeleteGroup(group_id) return jsonify({'code': code, 'group': group_id, 'filenumber': 0})