def remove(self, table, key, value=None):
     """
     Function to remove the value for a key in a lmdb table 
     Parameters:
        - table is the name of lmdb table in which 
          the key-value pair need to be removed.
        - key is the primary key of the table.
        - value is data to be removed, If the database does not support 
          sorted duplicate data items (MDB_DUPSORT) the data parameter 
          is ignored. If the database supports sorted duplicates and 
          the data parameter is NULL, all of the duplicate data items
          for the key will be deleted. Otherwise, if the data parameter is 
          non-NULL only the matching data item will be deleted.
     """
     # Remove, table, key
     request = "R\n" + escape(table) + "\n" + escape(key)
     if value is not None:
         request = "\n" + request + value.replace("\n", "\\n")
     response = self.__uri_client._postmsg(request).decode("utf-8")
     args = response.split("\n")
     # Remove successful (returned True)
     if args[0] == "t" and len(args) == 1:
         return True
     # Remove unsuccessful (returned False)
     elif args[0] == "f" and len(args) == 1:
         return False
     # Error
     elif args[0] == "e":
         if len(args) != 2:
             logger.error("Unknown error format")
         else:
             logger.error("Request error: %s", args[1])
     else:
         logger.error("Unknown response format")
 def get(self, table, key):
     """
     Function to get the value for a key in a lmdb table 
     Parameters:
        - table is the name of lmdb table from which
          the key-value pair needs to be retrieved.
        - key is the primary key of the table.
     """
     # Get, table, key
     request = "G\n" + escape(table) + "\n" + escape(key)
     response = self.__uri_client._postmsg(request).decode("utf-8")
     args = response.split("\n")
     # Value found
     if args[0] == "v" and len(args) == 2:
         return unescape(args[1])
     # Value not found
     elif args[0] == "n" and len(args) == 1:
         return None
     # Error
     elif args[0] == "e":
         if len(args) != 2:
             logger.error("Unknown error format")
         else:
             logger.error("Request error: %s", args[1])
     else:
         logger.error("Unknown response format")
 def set(self, table, key, value):
     """
     Function to set a key-value pair in a lmdb table 
     Parameters:
        - table is the name of lmdb table in which 
          the key-value pair needs to be inserted.
        - key is the primary key of the table.
        - value is the value that needs to be inserted in the table.
     """
     # Set, table, key, value
     request = "S\n" + escape(table) + "\n" + escape(key) + \
         "\n" + escape(value)
     response = self.__uri_client._postmsg(request).decode("utf-8")
     args = response.split("\n")
     # Set successful (returned True)
     if args[0] == "t" and len(args) == 1:
         return True
     # Set unsuccessful (returned False)
     elif args[0] == "f" and len(args) == 1:
         return False
     # Error
     elif args[0] == "e":
         if len(args) != 2:
             logger.error("Unknown error format")
         else:
             logger.error("Request error: %s", args[1])
     else:
         logger.error("Unknown response format")
 def lookup(self, table):
     """
     Function to get all the keys in a lmdb table 
     Parameters:
        - table is the name of the lmdb table.
     """
     result = []
     # Lookup, table
     request = "L\n" + escape(table)
     response = self.__uri_client._postmsg(request).decode("utf-8")
     args = response.split("\n")
     # Lookup result found
     if args[0] == "l":
         if len(args) == 2:
             # Result is a list of keys separated by commas
             result = unescape(args[1]).split(",")
         else:
             logger.error("Unknown response format")
     # Lookup result not found
     elif args[0] == "n" and len(args) == 1:
         return result
     # Error
     elif args[0] == "e":
         if len(args) != 2:
             logger.error("Unknown error format")
         else:
             logger.error("Request error: %s", args[1])
     else:
         logger.error("Unknown response format")
     return result
示例#5
0
    def _process_request(self, request):
        response = ""
        logger.info(request.encode('utf-8'))
        args = request.split('\n')
        for i in range(len(args)):
            args[i] = unescape(args[i])
        logger.info(args)
        cmd = args[0]

        # Lookup
        if (cmd == "L"):
            if len(args) == 2:
                result_list = self.kv_helper.lookup(args[1])
                result = ""
                for key in result_list:
                    if result == "":
                        result = key
                    else:
                        result = result + "," + key
                # Lookup result found
                if result != "":
                    response = "l\n" + escape(result)
                # No result found
                else:
                    response = "n"
            # Error
            else:
                logger.error("Invalid args for cmd Lookup")
                response = "e\nInvalid args for cmd Lookup"

        # Get
        elif (cmd == "G"):
            if len(args) == 3:
                result = self.kv_helper.get(args[1], args[2])
                # Value found
                if result is not None:
                    response = "v\n" + escape(result)
                # Value not found
                else:
                    response = "n"
            # Error
            else:
                logger.error("Invalid args for cmd Get")
                response = "e\nInvalid args for cmd Get"

        # Set
        elif (cmd == "S"):
            if len(args) == 4:
                result = self.kv_helper.set(args[1], args[2], args[3])
                # Set successful (returned True)
                if result:
                    response = "t"
                # Set unsuccessful (returned False)
                else:
                    response = "f"
            # Error
            else:
                logger.error("Invalid args for cmd Set")
                response = "e\nInvalid args for cmd Set"

        # Remove
        elif (cmd == "R"):
            if len(args) == 3 or len(args) == 4:
                if len(args) == 3:
                    result = self.kv_helper.remove(args[1], args[2])
                else:
                    result = self.kv_helper.remove(args[1],
                                                   args[2],
                                                   value=args[3])
                # Remove successful (returned True)
                if result:
                    response = "t"
                # Remove unsuccessful (returned False)
                else:
                    response = "f"
            # Error
            else:
                logger.error("Invalid args for cmd Remove")
                response = "e\nInvalid args for cmd Remove"
        # Error
        else:
            logger.error("Unknown cmd")
            response = "e\nUnknown cmd"
        return response