1 | """ |
---|
2 | <Author> |
---|
3 | Justin Cappos |
---|
4 | |
---|
5 | <Start Date> |
---|
6 | May 25th, 2011 |
---|
7 | |
---|
8 | <Description> |
---|
9 | A wrapper for a C-based datastore. This uses objects, etc. to make |
---|
10 | the C interface more Pythonic... |
---|
11 | |
---|
12 | This is really just a version of the Python datastore with the Python code |
---|
13 | replaced with the C extension. I left in all of the error checking. |
---|
14 | |
---|
15 | |
---|
16 | """ |
---|
17 | |
---|
18 | import fastsimplexordatastore_c |
---|
19 | import math |
---|
20 | |
---|
21 | |
---|
22 | def do_xor(string_a, string_b): |
---|
23 | if type(string_a) != str or type(string_b) != str: |
---|
24 | raise TypeError("do_xor called with a non-string") |
---|
25 | |
---|
26 | if len(string_a) != len(string_b): |
---|
27 | raise ValueError("do_xor requires strings of the same length") |
---|
28 | |
---|
29 | return fastsimplexordatastore_c.do_xor(string_a,string_b) |
---|
30 | |
---|
31 | |
---|
32 | class XORDatastore: |
---|
33 | """ |
---|
34 | <Purpose> |
---|
35 | Class that has information for an XORdatastore. This data structure can |
---|
36 | quickly XOR blocks of data that it stores. The real work is done in a |
---|
37 | C extension |
---|
38 | |
---|
39 | <Side Effects> |
---|
40 | None. |
---|
41 | |
---|
42 | """ |
---|
43 | |
---|
44 | # this is the private, internal storage area for data... |
---|
45 | ds = None |
---|
46 | |
---|
47 | # these are public so that a caller can read information about a created |
---|
48 | # datastore. They should not be changed. |
---|
49 | numberofblocks = None |
---|
50 | sizeofblocks = None |
---|
51 | |
---|
52 | def __init__(self, block_size, num_blocks): # allocate |
---|
53 | """ |
---|
54 | <Purpose> |
---|
55 | Allocate a place to store data for efficient XOR. |
---|
56 | |
---|
57 | <Arguments> |
---|
58 | block_size: the size of each block. This must be a positive int / long. |
---|
59 | The value must be a multiple of 64 |
---|
60 | |
---|
61 | num_blocks: the number of blocks. This must be a positive integer |
---|
62 | |
---|
63 | <Exceptions> |
---|
64 | TypeError is raised if invalid parameters are given. |
---|
65 | |
---|
66 | """ |
---|
67 | |
---|
68 | if type(block_size) != int and type(block_size) != long: |
---|
69 | raise TypeError("Block size must be an integer") |
---|
70 | |
---|
71 | if block_size <= 0: |
---|
72 | raise TypeError("Block size must be positive") |
---|
73 | |
---|
74 | if block_size %64 != 0: |
---|
75 | raise TypeError("Block size must be a multiple of 64") |
---|
76 | |
---|
77 | if type(num_blocks) != int and type(num_blocks) != long: |
---|
78 | raise TypeError("Number of blocks must be an integer") |
---|
79 | |
---|
80 | if num_blocks <= 0: |
---|
81 | raise TypeError("Number of blocks must be positive") |
---|
82 | |
---|
83 | |
---|
84 | self.numberofblocks = num_blocks |
---|
85 | self.sizeofblocks = block_size |
---|
86 | |
---|
87 | self.ds = fastsimplexordatastore_c.Allocate(block_size, num_blocks) |
---|
88 | |
---|
89 | |
---|
90 | def produce_xor_from_bitstring(self, bitstring): |
---|
91 | """ |
---|
92 | <Purpose> |
---|
93 | Returns an XORed block from an XORdatastore. It will always return |
---|
94 | a string of the size of the datastore blocks |
---|
95 | |
---|
96 | <Arguments> |
---|
97 | bitstring: a string of bits that indicates what to XOR. The length |
---|
98 | of this string must be ceil(numberofblocks / 8.0). Extra |
---|
99 | bits are ignored (e.g. if are 10 blocks, the last |
---|
100 | six bits are ignored). |
---|
101 | |
---|
102 | <Exceptions> |
---|
103 | TypeError is raised if the bitstring is invalid |
---|
104 | |
---|
105 | <Returns> |
---|
106 | The XORed block. |
---|
107 | |
---|
108 | """ |
---|
109 | if type(bitstring) != str: |
---|
110 | raise TypeError("bitstring must be a string") |
---|
111 | |
---|
112 | if len(bitstring) != math.ceil(self.numberofblocks/8.0): |
---|
113 | raise TypeError("bitstring is not of the correct length") |
---|
114 | |
---|
115 | |
---|
116 | return fastsimplexordatastore_c.Produce_Xor_From_Bitstring(self.ds, bitstring) |
---|
117 | |
---|
118 | |
---|
119 | |
---|
120 | |
---|
121 | def set_data(self, offset, data_to_add): |
---|
122 | """ |
---|
123 | <Purpose> |
---|
124 | Sets the raw data in an XORdatastore. It ignores block layout, etc. |
---|
125 | |
---|
126 | <Arguments> |
---|
127 | offset: this is a non-negative integer that must be less than the |
---|
128 | numberofblocks * blocksize. |
---|
129 | |
---|
130 | data_to_add: the string that should be added. offset + len(data_to_add) |
---|
131 | must be less than the numberofblocks * blocksize. |
---|
132 | |
---|
133 | <Exceptions> |
---|
134 | TypeError if the arguments are the wrong type or have invalid values. |
---|
135 | |
---|
136 | <Returns> |
---|
137 | None |
---|
138 | |
---|
139 | """ |
---|
140 | if type(offset) != int and type(offset) != long: |
---|
141 | raise TypeError("Offset must be an integer") |
---|
142 | |
---|
143 | if offset < 0: |
---|
144 | raise TypeError("Offset must be non-negative") |
---|
145 | |
---|
146 | if type(data_to_add) != str: |
---|
147 | raise TypeError("Data_to_add to XORdatastore must be a string.") |
---|
148 | |
---|
149 | if offset + len(data_to_add) > self.numberofblocks * self.sizeofblocks: |
---|
150 | raise TypeError("Offset + added data overflows the XORdatastore") |
---|
151 | |
---|
152 | return fastsimplexordatastore_c.SetData(self.ds, offset, data_to_add) |
---|
153 | |
---|
154 | |
---|
155 | |
---|
156 | |
---|
157 | |
---|
158 | |
---|
159 | |
---|
160 | def get_data(self, offset, quantity): |
---|
161 | """ |
---|
162 | <Purpose> |
---|
163 | Returns raw data from an XORdatastore. It ignores block layout, etc. |
---|
164 | |
---|
165 | <Arguments> |
---|
166 | offset: this is a non-negative integer that must be less than the |
---|
167 | numberofblocks * blocksize. |
---|
168 | |
---|
169 | quantity: quantity must be a positive integer. offset + quantity |
---|
170 | must be less than the numberofblocks * blocksize. |
---|
171 | |
---|
172 | <Exceptions> |
---|
173 | TypeError if the arguments are the wrong type or have invalid values. |
---|
174 | |
---|
175 | <Returns> |
---|
176 | A string containing the data. |
---|
177 | |
---|
178 | """ |
---|
179 | if type(offset) != int and type(offset) != long: |
---|
180 | raise TypeError("Offset must be an integer") |
---|
181 | |
---|
182 | if offset < 0: |
---|
183 | raise TypeError("Offset must be non-negative") |
---|
184 | |
---|
185 | if type(quantity) != int and type(quantity) != long: |
---|
186 | raise TypeError("Quantity must be an integer") |
---|
187 | |
---|
188 | if quantity <= 0: |
---|
189 | raise TypeError("Quantity must be positive") |
---|
190 | |
---|
191 | if offset + quantity > self.numberofblocks * self.sizeofblocks: |
---|
192 | raise TypeError("Quantity + offset is larger than XORdatastore") |
---|
193 | |
---|
194 | return fastsimplexordatastore_c.GetData(self.ds, offset, quantity) |
---|
195 | |
---|
196 | |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | def __del__(self): # deallocate |
---|
201 | """ |
---|
202 | <Purpose> |
---|
203 | Deallocate the XORdatastore |
---|
204 | |
---|
205 | <Arguments> |
---|
206 | None |
---|
207 | |
---|
208 | <Exceptions> |
---|
209 | None |
---|
210 | |
---|
211 | """ |
---|
212 | # if there is an error, this might be an uninitialized object... |
---|
213 | if self.ds != None: |
---|
214 | fastsimplexordatastore_c.Deallocate(self.ds) |
---|
215 | |
---|
216 | |
---|