Пример #1
0
def unparse(parsed, template=mastercard, encoding=None):
    parsed = parsed.copy()
    output = b""
    version = getVersion(parsed["MTI"])
    if version == IsoVersion._1993:
        parsed.update(breakPdsElement(parsed["PDS"]))
    elementIds = sorted(key for key in set(parsed) if "DE" in key)
    index = bisectLeft(elementIds, "DE065")
    finalIndex = bisectLeft(elementIds, "DE129")
    if finalIndex > index and "DE001" not in elementIds:
        insort(elementIds, "DE001")
        index += 1
        finalIndex += 1
    BMP = elementIds[:index]
    BMS = elementIds[index:finalIndex]
    output += unparseElement(parsed,
                             elementId="MTI",
                             template=template[version],
                             encoding=encoding)
    output += unparseBitmap(BMP, elementId="DE000", template=template[version])
    for id in elementIds:
        if isBitmap(id):
            output += unparseBitmap(BMS,
                                    elementId=id,
                                    template=template[version])
            continue
        output += unparseElement(parsed,
                                 elementId=id,
                                 template=template[version],
                                 encoding=encoding)
    return output
Пример #2
0
    def removeMachine(self, machineIndex):

        if machineIndex not in self.machines:
            return 0

        machine = self.machines[machineIndex]
        machineKeyHash = machine['hash']
        del self.machines[machineIndex]

        machinesHash = [machine['hash'] for machine in self.machineTuples]
        newMachineIndex = bisectLeft(machinesHash, machineKeyHash)
        del self.machineTuples[newMachineIndex]

        for replica in self.replicas[machineIndex]:

            machineKeyHash = replica['hash']
            machinesHash = [machine['hash'] for machine in self.machineTuples]
            newMachineIndex = bisectLeft(machinesHash, machineKeyHash)
            del self.machineTuples[newMachineIndex]

        del self.replicas[machineIndex]

        return 1
Пример #3
0
    def getMachine(self, key):

        if len(self.machineTuples) == 0:
            return -1

        keyHash = self._getHash(key)

        machineIndex = 0
        if keyHash < self.machineTuples[-1]['hash']:
            machinesHash = [machine['hash'] for machine in self.machineTuples]
            machineIndex = bisectLeft(machinesHash, keyHash)

        self.addObjectToMachine(key, self.machineTuples[machineIndex]['index'])
        return self.machineTuples[machineIndex]['index']
	def getMachine(self,key):

		if len(self.machineTuples) == 0:
			return -1

		keyHash = self._getHash(key)

		machineIndex = 0
		if keyHash < self.machineTuples[-1]['hash']:
			machinesHash = [machine['hash'] for machine in self.machineTuples]
			machineIndex = bisectLeft(machinesHash,keyHash)
		
		self.addObjectToMachine(key,self.machineTuples[machineIndex]['index'])
		return self.machineTuples[machineIndex]['index']
	def removeMachine(self,machineIndex):

		if machineIndex not in self.machines:
			return 0

		machine = self.machines[machineIndex]
		machineKeyHash = machine['hash']
		del self.machines[machineIndex]

		machinesHash = [machine['hash'] for machine in self.machineTuples]
		newMachineIndex = bisectLeft(machinesHash,machineKeyHash)
		del self.machineTuples[newMachineIndex]

		for replica in self.replicas[machineIndex]:
			
			machineKeyHash = replica['hash']
			machinesHash = [machine['hash'] for machine in self.machineTuples]
			newMachineIndex = bisectLeft(machinesHash,machineKeyHash)
			del self.machineTuples[newMachineIndex]

		del self.replicas[machineIndex]

		return 1
def remapObjectsOnMachineInsert(hashRing,machineIndex):
	
	hashRing.printMachines()

	machineKeyHash = hashRing.machines[machineIndex]['hash']
	machinesHash = [machine['hash'] for machine in hashRing.machineTuples]

	prevMachineIndex = bisectLeft(machinesHash,machineKeyHash) + 1
	if prevMachineIndex >= len(hashRing.machineTuples):
		prevMachineIndex = 0
	prevMachineIndex = hashRing.machineTuples[prevMachineIndex]['index']

	if prevMachineIndex in hashRing.objects:
		for key in list(hashRing.objects[prevMachineIndex]):
			if objects[key].hash < machineKeyHash:
				hashRing.removeObjectFromMachine(key,prevMachineIndex)
				hashRing.addObjectToMachine(key,machineIndex)
				print "Key: %s moved from %s to machine %s"%(key,prevMachineIndex,machineIndex)
				objects[key].updateMachine(machineIndex)
Пример #7
0
def remapObjectsOnMachineInsert(hashRing, machineIndex):

    hashRing.printMachines()

    machineKeyHash = hashRing.machines[machineIndex]['hash']
    machinesHash = [machine['hash'] for machine in hashRing.machineTuples]

    prevMachineIndex = bisectLeft(machinesHash, machineKeyHash) + 1
    if prevMachineIndex >= len(hashRing.machineTuples):
        prevMachineIndex = 0
    prevMachineIndex = hashRing.machineTuples[prevMachineIndex]['index']

    if prevMachineIndex in hashRing.objects:
        for key in list(hashRing.objects[prevMachineIndex]):
            if objects[key].hash < machineKeyHash:
                hashRing.removeObjectFromMachine(key, prevMachineIndex)
                hashRing.addObjectToMachine(key, machineIndex)
                print "Key: %s moved from %s to machine %s" % (
                    key, prevMachineIndex, machineIndex)
                objects[key].updateMachine(machineIndex)