Skip to content

Commit

Permalink
Improve method to calculate the object node size in bytes. Fixes erro…
Browse files Browse the repository at this point in the history
…r of index 1
  • Loading branch information
marciovicente committed May 21, 2014
1 parent 6e13beb commit 890e8b1
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,12 @@ def __init__(self):
self.file = None
self.filename = 'lisch.dat'
self.SIZE_OF_FILE = 11
self.STRUCT_SIZE = 0
super(Application, self).__init__()

def init_file(self):
n = Node()
n.value = n.label = n.age = n.index = ''
dumpped = pickle.dumps(n)
obj_size = sys.getsizeof(dumpped)
self.open_file()
self.file.seek(self.SIZE_OF_FILE * obj_size)
self.file.seek(self.SIZE_OF_FILE * self.STRUCT_SIZE)
# INIT THE 'R' FLAG
self.file.seek(0)
self.file.write(pickle.dumps(self.SIZE_OF_FILE))
Expand All @@ -47,13 +44,22 @@ def init_file(self):
def create_file(self):
self.file = file(self.filename, 'w+b')

def set_struct_size(self):
n = Node()
n.value = n.age = n.index = -1
n.label = ''
dumpped = pickle.dumps(n)
self.STRUCT_SIZE = sys.getsizeof(dumpped)

def main(self):
self.method = raw_input()
self.filename = 'lisch.dat' if self.method == 'l' else 'eisch.dat'
if not os.path.exists(self.filename):
self.create_file()
self.init_file()

self.set_struct_size()

operation = raw_input()
while operation != 'e':
if operation == 'i':
Expand All @@ -76,7 +82,6 @@ def solve_colision(self, old_obj, new_obj):
r_flag_index = self.get_flag_index()
old_obj.index = r_flag_index
# pointer to old object value
# import pdb; pdb.set_trace()
self.point_to_value(old_obj.value)
# overwrite the old object with the new index
self.file.write(pickle.dumps(old_obj))
Expand All @@ -90,15 +95,15 @@ def solve_colision(self, old_obj, new_obj):

def point_to_value(self, value):
""" This method point to index in a file """

self.file.seek(0)
value = int(value)
index = self.mod(value)
length = 211 if index > 1 else 1 # TODO - Change 211 to global dynamic variable
self.file.seek(index * length + len(pickle.dumps(self.SIZE_OF_FILE)))
self.file.seek(index * self.STRUCT_SIZE + len(pickle.dumps(self.SIZE_OF_FILE)))

def insert_record(self):
# if os.path.getsize(self.filename) >= self.SIZE_OF_FILE:
# print 'Arquivo cheio'
# os.path.getsize(self.filename) -> 'full archive'

value = raw_input()
label = raw_input()
age = raw_input()
Expand Down Expand Up @@ -132,15 +137,13 @@ def query(self, value=None, query_value=None):
value = raw_input()
self.point_to_value(value)

# TODO, Implements when the position doesn't exist
try:
obj = pickle.loads(self.file.read())
except Exception:
print u'chave não encontrada: %s' % value
return False

# only if it's not called recursive
# import pdb; pdb.set_trace()
if obj and (int(obj.value) is int(value) and not query_value or int(query_value or -1) is int(obj.value)):
print 'chave: %s' % obj.value
print obj.label
Expand Down

0 comments on commit 890e8b1

Please sign in to comment.