-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBTreeBuffer.py
executable file
·65 lines (55 loc) · 2.75 KB
/
BTreeBuffer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#-------------------------------------------------------------------------------
# Name: BTreeBuffer
# Purpose: Buffering BTree from PST file
#
# Author: Krishna Durai
#
# Created: 24/06/2013
# Copyright: (c) kd 2013
# Licence: <your licence>
#-------------------------------------------------------------------------------
class BTreeBufferException(RuntimeError):
'''Class to raise BTreeBuffer Errors.'''
"problem in bufferfile"
class BTreeBuffer(object):
'''BTreeBuffer implements a buffer for BTree module.
It implements getBuffer, resetBuffer, returnBuffer, readIntoBuffer and writeFromBuffer functions.'''
BufferList = [] # List containing bytearrays which act as buffers to BTree Nodes.
freeBufferQ = [] # Queue of buffers which are free to be alloted to BTree Nodes.
sections = 0 # Maximum Number of bytearray buffers which can be alloted.
buffersize = 0 # Size of the bytearray buffers.
pstfile = None # The complete file path of the pst file to be buffered.
def __init__(self, pstfile, sections = 10, buffersize = 3850):
self.pstfile = pstfile
self.sections = sections
self.buffersize = buffersize
self.BufferList = [None] * sections
self.freeBufferQ = range(sections)
for count in range(sections):
self.BufferList[count] = bytearray(buffersize)
def getBuffer(self):
'''Returns an unallocated buffer.'''
if self.freeBufferQ:
return self.freeBufferQ.pop(0)
raise BTreeBufferException, 'Too many buffers used'
def resetBuffer(self):
'''Frees all buffers, i.e. sets all buffers as unallocated.'''
self.freeBufferQ = range(self.sections)
def returnBuffer(self, buffer_number):
'''Returns given buffer to unallocated buffer queue.'''
self.freeBufferQ.append(buffer_number)
def readIntoBuffer(self, buffer_number, seek_pos, read_size):
'''Reads bytes into given buffer from the PST file from 'seek_pos' till the given 'read_size'.'''
if(read_size <= self.buffersize):
self.pstfile.seek(seek_pos)
byte_string = self.pstfile.read(read_size)
count = 0
while count < read_size:
self.BufferList[buffer_number][count] = byte_string[count]
count = count + 1
else:
raise BTreeBufferException, 'Too big to read into Buffer'
def writeFromBuffer(self, buffer_number, seek_pos, write_till):
'''Writes bytes from given buffer into the PST file from 'seek_pos' to 'write_till'.'''
self.pstfile.seek(seek_pos)
self.pstfile.write(self.BufferList[buffer_number][:write_till])