1010from protos import objects
1111from protos .objects import Listings , Followers , Following
1212from os .path import join
13- from db .migrations import migration1 , migration2
13+ from db .migrations import migration1 , migration2 , migration3
1414
1515
1616class Database (object ):
@@ -116,7 +116,7 @@ def _create_database(database_path):
116116 conn = lite .connect (database_path )
117117 cursor = conn .cursor ()
118118
119- cursor .execute ('''PRAGMA user_version = 2 ''' )
119+ cursor .execute ('''PRAGMA user_version = 3 ''' )
120120 cursor .execute ('''CREATE TABLE hashmap(hash TEXT PRIMARY KEY, filepath TEXT)''' )
121121
122122 cursor .execute ('''CREATE TABLE profile(id INTEGER PRIMARY KEY, serializedUserInfo BLOB, tempHandle TEXT)''' )
@@ -125,7 +125,8 @@ def _create_database(database_path):
125125
126126 cursor .execute ('''CREATE TABLE keys(type TEXT PRIMARY KEY, privkey BLOB, pubkey BLOB)''' )
127127
128- cursor .execute ('''CREATE TABLE followers(id INTEGER PRIMARY KEY, serializedFollowers BLOB)''' )
128+ cursor .execute ('''CREATE TABLE followers(guid TEXT UNIQUE, serializedFollower TEXT)''' )
129+ cursor .execute ('''CREATE INDEX index_followers ON followers(serializedFollower);''' )
129130
130131 cursor .execute ('''CREATE TABLE following(id INTEGER PRIMARY KEY, serializedFollowing BLOB)''' )
131132
@@ -182,8 +183,12 @@ def _run_migrations(self):
182183 if version == 0 :
183184 migration1 .migrate (self .PATH )
184185 migration2 .migrate (self .PATH )
186+ migration3 .migrate (self .PATH )
185187 elif version == 1 :
186188 migration2 .migrate (self .PATH )
189+ migration3 .migrate (self .PATH )
190+ elif version == 2 :
191+ migration3 .migrate (self .PATH )
187192
188193
189194class HashMap (object ):
@@ -466,47 +471,41 @@ def is_following(self, guid):
466471
467472 def set_follower (self , proto ):
468473 conn = Database .connect_database (self .PATH )
474+ p = Followers .Follower ()
475+ p .ParseFromString (proto )
469476 with conn :
470477 cursor = conn .cursor ()
471- f = Followers ()
472- ser = self .get_followers ()
473- if ser is not None :
474- f .ParseFromString (ser )
475- for follower in f .followers :
476- if follower .guid == proto .guid :
477- f .followers .remove (follower )
478- f .followers .extend ([proto ])
479- cursor .execute ('''INSERT OR REPLACE INTO followers(id, serializedFollowers) VALUES (?,?)''' ,
480- (1 , f .SerializeToString ()))
478+ cursor .execute ('''INSERT OR REPLACE INTO followers(guid, serializedFollower) VALUES (?,?)''' ,
479+ (p .guid .encode ("hex" ), proto .encode ("hex" )))
481480 conn .commit ()
482481 conn .close ()
483482
484483 def delete_follower (self , guid ):
485484 conn = Database .connect_database (self .PATH )
486485 with conn :
487486 cursor = conn .cursor ()
488- f = Followers ()
489- ser = self .get_followers ()
490- if ser is not None :
491- f .ParseFromString (ser )
492- for follower in f .followers :
493- if follower .guid == guid :
494- f .followers .remove (follower )
495- cursor .execute ('''INSERT OR REPLACE INTO followers(id, serializedFollowers) VALUES (?,?)''' ,
496- (1 , f .SerializeToString ()))
487+ cursor .execute ('''DELETE FROM followers WHERE guid=?''' , (guid .encode ("hex" ), ))
497488 conn .commit ()
498489 conn .close ()
499490
500- def get_followers (self ):
491+ def get_followers (self , start = 0 ):
501492 conn = Database .connect_database (self .PATH )
502493 cursor = conn .cursor ()
503- cursor .execute ('''SELECT serializedFollowers FROM followers WHERE id=1''' )
504- proto = cursor .fetchone ()
505- conn .close ()
506- if not proto :
507- return None
508- else :
509- return proto [0 ]
494+
495+ cursor .execute ('''SELECT Count(*) FROM followers''' )
496+ count = cursor .fetchone ()[0 ]
497+
498+ f = Followers ()
499+ if count > 0 :
500+ smt = '''select serializedFollower from followers order by rowid desc limit 30 offset ''' + str (start )
501+ cursor .execute (smt )
502+ serialized_followers = cursor .fetchall ()
503+ conn .close ()
504+ for proto in serialized_followers :
505+ p = Followers .Follower ()
506+ p .ParseFromString (proto [0 ].decode ("hex" ))
507+ f .followers .extend ([p ])
508+ return (f .SerializeToString (), count )
510509
511510
512511class MessageStore (object ):
@@ -591,7 +590,7 @@ def get_conversations(self):
591590 handle = ""
592591 if val [0 ] is not None :
593592 try :
594- with open (join (DATA_FOLDER , 'cache' , g [0 ]), "r" ) as filename :
593+ with open (join (DATA_FOLDER , 'cache' , g [0 ] + ".profile" ), "r" ) as filename :
595594 profile = filename .read ()
596595 p = objects .Profile ()
597596 p .ParseFromString (profile )
0 commit comments