예제 #1
0
def insert_one(database, table, data):
    with Profiler() as prof:
        index = IndexId(database, table)
        _id = 0
        if index.data:
            max_id = max(list(index.data.keys()))
            _id = max_id + 1
        data["_id"] = _id
        index.data[_id] = file_worker.insert_one(database, table, data)
        # это необходимо для срабатывание setter, и записывания индекса в файл
        index.data = index.data
    return to_json({"time": prof.total_time, "result": "ok"})
예제 #2
0
 def run(self):
     print("{} run".format(self.getName()))
     worker = self.context.socket(zmq.REP)
     worker.connect('inproc://backend')
     while True:
         instruction = worker.recv_string()
         # print(instruction)
         instruction = from_json(instruction)
         if not check_the_validity_of_the_instruction(instruction):
             worker.send_string("Invalid instruction syntax")
         if check_super_system_function(instruction["function"]):
             if not self.event.is_set():
                 self.event.wait()
             self.event.clear()
             if self.works:
                 while True in self.works.values():
                     self.event.wait(1)
             result = execute_instruction(instruction)
             if not result.error:
                 if instruction["function"] == "createDataBase":
                     name = get_database_name(instruction)
                     port = instruction["data"]["port"]
                     temp = DatabaseServer(name, port)
                     temp.start()
                     self.databases[instruction["data"]["database"]] = Database(name, port, temp)
                 elif instruction["function"] == "dropDataBase":
                     name = get_database_name(instruction)
                     if name in self.databases:
                         database_info = self.databases.pop(name)
                         database_server = database_info.process
                         database_server.stop()
             worker.send_string(result.info)
             self.event.set()
         else:
             if not self.event.is_set():
                 self.event.wait()
             self.works[self.getName()] = True
             name = get_database_name(instruction)
             if name in self.databases:
                 context = zmq.Context()
                 receiver = context.socket(zmq.REQ)
                 receiver.connect('tcp://127.0.0.1:{}'.format(self.databases[name].port))
                 receiver.send_string(to_json(instruction))
                 result = from_json(receiver.recv_string())
                 receiver.close()
                 # print("result")
                 # print(result["_info"])
                 worker.send_string(result["info"])
             else:
                 worker.send_string("Invalid instruction syntax")
             self.works[self.getName()] = False
예제 #3
0
 def run(self):
     print("{} run".format(self.database))
     context = zmq.Context()
     socket = context.socket(zmq.REP)
     socket.bind("tcp://127.0.0.1:{}".format(self.port))
     while self.work:
         instruction = from_json(socket.recv_string())
         # instruction = from_json(instruction)
         # TODO: delete
         # print(instruction)
         result = execute_instruction(instruction)
         # result = Answer(instruction["function"])
         socket.send_string(to_json(result))
     socket.close()
예제 #4
0
def insert(database, table, data):
    if not isinstance(data, (list, tuple)) or len(data == 1):
        return insert_one(database, table, data)
    with Profiler() as prof:
        index = IndexId(database, table)
        _id = 0
        if index.data:
            max_id = max(list(index.data.keys()))
            _id = max_id + 1
        for i in range(len(data)):
            data[i]["_id"] = _id + i
        tells = file_worker.insert(database, table, data)
        for k, v in enumerate(tells):
            index.data[_id + k] = v
        # это необходимо для срабатывание setter, и записывания индекса в файл
        index.data = index.data
    return to_json({"time": prof.total_time, "result": "ok"})
예제 #5
0
def start_client():
    context = zmq.Context()
    socket = context.socket(zmq.REQ)
    socket.connect('tcp://127.0.0.1:43000')

    data = DataStructure()
    # data.function = 'insert'
    # data.database = 'london'
    # data.collection = 'people'
    # data.data = [Ment("lol", 18).__dict__, Ment("lolita", 27).__dict__]
    # data.function = 'renameCollection'
    data.function = 'dropCollection'
    data.data = {
        "database": "poland",
        "collection": "lodz",
        "login": "******",
        "password": "******"
    }
    # data.data = {"database": 'poland', 'table': {'old': 'belostock', 'new': 'belostock'}}

    socket.send_string(to_json(data))
    # if socket.recv_string():
    print(socket.recv_string())
예제 #6
0
 socket.connect('tcp://localhost:50000')
 # poll = zmq.Poller()
 # poll.register(socket, zmq.POLLIN)
 data = DataStructure()
 data.function = 'createDataBase'
 data.database = 'warsaw'
 data.data = {
     "database": "madrid",
     "collection": "lodz",
     "login": "******",
     "password": "******",
     "port": 48710
 }
 # datas = to_json(data)
 # print(datas)
 print(to_json(data))
 with Profiler() as prof:
     socket.send_string(to_json(data))
     msg = socket.recv_string()
     # for i in range(1000):
     #     socket.send_string(to_json(data))
     #     msg = socket.recv_string()
 print(prof.total_time)
 # sockets = dict(poll.poll(1000))
 # if socket in sockets:
 #     msg = socket.recv_string()
 #     print(msg)
 socket.send_string(to_json(data))
 print("sent")
 msg = socket.recv_string()
 print(msg)
예제 #7
0
from database.utils.JSON import to_json


class Ment:
    def __init__(self, name="alex", age=15):
        self.name = name
        self.age = age
        self.language = ["greece", "spanish", "english"]


if __name__ == '__main__':
    filename = "ex1.json"
    print(Ment("lol", 18).__dict__, Ment("alala", 547).__dict__, Ment("helga").__dict__)
    a = to_json(Ment("lol", 18).__dict__)
    b = to_json(Ment("alala", 547).__dict__)
    c = to_json(Ment("helga").__dict__)

    text = [a, b, c]

    print(text)

    with open(filename, 'w+') as file:  # a - дописать w+ писать с начала
        for i in range(int(1e5)):
            for index in text:
                # print("begin: " + str(file.tell()))
                file.write(index + '\n')
                # print("end: " + str(file.tell()))
예제 #8
0
def get_hash(data):
    data = to_json(data)
    hash_code = abs(mmh3.hash(to_json(data)))
    return encode(hash_code)
예제 #9
0
from database.utils.JSON import from_json, to_json
from database.utils.profiler import Profiler

if __name__ == '__main__':
    file_path = "ex1.json"
    # tempname = filename + '.temp'  # os.tempnam() gives warning

    with Profiler():
        fh, abs_path = mkstemp()
        with open(abs_path, 'w') as new_file:
            with open(file_path) as old_file:
                for line in old_file:
                    obj = from_json(line)
                    if 'age' in obj:
                        obj['age'] = obj['age']%100
                    new_file.write(to_json(obj) + '\n')
        close(fh)
        # Remove original file
        remove(file_path)
        # Move new file
        move(abs_path, file_path)

        # with open(filename) as fin:  # python 2.6
        #     with open(tempname, 'w') as fout:  # compatible
        #         for line in fin:
        #             obj = from_json(line)
        #             if 'age' in obj:
        #                 obj['age'] = obj['age']%100
        #             fout.write(to_json(obj)+ '\n')
        # os.rename(tempname, filename)
예제 #10
0
                                  instruction["field"])
        return result
    else:
        result = function_info[0](instruction["database"],
                                  instruction["collection"],
                                  instruction["data"])
        return result


if __name__ == '__main__':

    class Ment:
        def __init__(self, name="alex", age=15):
            self.name = name
            self.age = age
            self.language = ["greece", "spanish", "english"]

    class DataStructure:
        def __init__(self):
            self.function = None
            self.data = None
            self.database = None
            self.collection = None

    data = DataStructure()
    data.function = 'insert'
    data.database = 'london'
    data.collection = 'people'
    data.data = [Ment("lol", 18).__dict__, Ment("lolita", 27).__dict__]
    execute_instruction(to_json(data.__dict__))