MCPcopy Create free account
hub / github.com/ActiveState/code / DAL2

Class DAL2

recipes/Python/492210_dal2py/recipe-492210.py:6–95  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4################################################################################
5
6class DAL2:
7
8 # CONSTANTS
9 MAX_BLOCKS = (2 ** 16) - 1
10 MAX_SIZE = (2 ** 16)
11
12 # Disk Abstraction Layer
13 def __init__(self, blocks, size):
14 assert type(blocks) is int and 1 <= blocks <= self.MAX_BLOCKS
15 assert type(size) is int and 1 <= size <= self.MAX_SIZE
16 blocks = int(ceil(float(blocks * (size + 1)) / size))
17 assert blocks <= self.MAX_SIZE
18 DAL1.BLOCKS = blocks
19 DAL1.SIZE = size
20 self.__disk = DAL1()
21 self.__blocks = (blocks * size) / (size + 1)
22 self.__index = (blocks - self.__blocks) * size
23 self.__BIT = [0 for index in range(self.__blocks)]
24
25 # Open A Block
26 def open(self, status):
27 assert type(status) is int and 1 <= status <= 255
28 index = self.__BIT.index(0)
29 self.__BIT[index] = status
30 return index
31
32 # Read A Block
33 def read(self, block):
34 assert type(block) is int and 0 <= block < self.__blocks
35 assert self.__BIT[block] != 0
36 return self.__disk.read(self.__index + block * self.__disk.SIZE, \
37 self.__disk.SIZE)
38
39 # Write A Block
40 def write(self, block, data):
41 assert type(block) is int and 0 <= block < self.__blocks
42 assert type(data) is str and len(data) == self.__disk.SIZE
43 assert self.__BIT[block] != 0
44 self.__disk.write(self.__index + block * self.__disk.SIZE, data)
45
46 # Close A Block
47 def close(self, block):
48 assert type(block) is int and 0 <= block < self.__blocks
49 assert self.__BIT[block] != 0
50 self.__disk.erase(self.__index + block * self.__disk.SIZE, \
51 self.__disk.SIZE)
52 self.__BIT[block] = 0
53
54 # Get Status Information
55 def status(self, block):
56 assert type(block) is int and 0 <= block < self.__blocks
57 return self.__BIT[block]
58
59 # Probability Of Failure
60 def fail(self, probability):
61 self.__disk.fail(probability)
62
63 # Dump To File

Callers 1

testFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected