From 31f02992d7195614cbb7fa26a78950b1b71aa325 Mon Sep 17 00:00:00 2001 From: jinhai Date: Thu, 21 Mar 2019 21:09:09 +0800 Subject: [PATCH] Add GroupHandler and RawFileHandler --- pyengine/engine/controller/GroupHandler.py | 24 +++++++++++++++++ pyengine/engine/controller/RawFileHandler.py | 14 ++++++++++ pyengine/engine/controller/VectorEngine.py | 28 +++++++++++++------- 3 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 pyengine/engine/controller/GroupHandler.py create mode 100644 pyengine/engine/controller/RawFileHandler.py diff --git a/pyengine/engine/controller/GroupHandler.py b/pyengine/engine/controller/GroupHandler.py new file mode 100644 index 0000000000..231cb75dbc --- /dev/null +++ b/pyengine/engine/controller/GroupHandler.py @@ -0,0 +1,24 @@ +import os, shutil + +class GroupHandler(object): + + @staticmethod + def CreateGroupDirectory(group_id): + path = GetGroupDirectory(group_id) + path = path.strip() + path=path.rstrip("\\") + if not os.path.exists(): + os.makedirs(path) + + + @staticmethod + def DeleteGroupDirectory(group_id): + path = GetGroupDirectory(group_id) + path = path.strip() + path=path.rstrip("\\") + if os.path.exists(): + shutil.rmtree(path) + + @staticmethod + def GetGroupDirectory(group_id): + return DATABASE_DIRECTORY + '/' + group_id \ No newline at end of file diff --git a/pyengine/engine/controller/RawFileHandler.py b/pyengine/engine/controller/RawFileHandler.py new file mode 100644 index 0000000000..01034feeac --- /dev/null +++ b/pyengine/engine/controller/RawFileHandler.py @@ -0,0 +1,14 @@ + +class RawFileHandler(object): + @staticmethod + def Create(filename, type): + # type means: csv, parquet + pass + + @staticmethod + def Read(filename, type): + pass + + @staticmethod + def Append(filename, type, record): + pass \ No newline at end of file diff --git a/pyengine/engine/controller/VectorEngine.py b/pyengine/engine/controller/VectorEngine.py index c9966b31dc..d69e9e1403 100644 --- a/pyengine/engine/controller/VectorEngine.py +++ b/pyengine/engine/controller/VectorEngine.py @@ -1,8 +1,10 @@ from engine.model.GroupTable import GroupTable from engine.model.FileTable import FileTable +from engine.controller.RawFileHandler import RawFileHandler +from engine.controller.GroupHandler import GroupHandler from flask import jsonify from engine import db -import sys +import sys, os class VectorEngine(object): @@ -15,6 +17,7 @@ class VectorEngine(object): new_group = GroupTable(group_id) db.session.add(new_group) db.session.commit() + GroupHandler.CreateGroupDirectory(group_id) return jsonify({'code': 0, 'group_name': group_id, 'file_number': 0}) @staticmethod @@ -33,6 +36,7 @@ class VectorEngine(object): # old_group = GroupTable(group_id) db.session.delete(group) db.session.commit() + GroupHandler.DeleteGroupDirectory(group_id) return jsonify({'code': 0, 'group_name': group_id, 'file_number': group.file_number}) else: return jsonify({'code': 0, 'group_name': group_id, 'file_number': 0}) @@ -61,8 +65,8 @@ class VectorEngine(object): CreateIndex(group_id, index_filename) # create another raw file - raw_filename = file.group_id + '_' + file.seq_no - InsertVectorIntoRawFile(raw_filename, vector) + raw_filename = file.seq_no + InsertVectorIntoRawFile(group_id, raw_filename, vector) # insert a record into database db.session.add(FileTable(group_id, raw_filename, 'raw', 1)) db.session.commit() @@ -90,17 +94,21 @@ class VectorEngine(object): # construct response and send back return jsonify({'code': 0}) - @staticmethod - def CreateIndex(group_id): - print(group_id) - return jsonify({'code': 0}) - @staticmethod def CreateIndex(group_id, filename): - print(group_id, filename) + path = GroupHandler.GetGroupDirectory(group_id) + '/' + filename + print(group_id, path) return jsonify({'code': 0}) @staticmethod - def InsertVectorIntoRawFile(filename, vector): + def InsertVectorIntoRawFile(group_id, filename, vector): print(sys._getframe().f_code.co_name) + path = GroupHandler.GetGroupDirectory(group_id) + '/' + filename + + # if filename exist + # append + # if filename not exist + # create file + # append return filename +