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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
from __future__ import unicode_literals from six import iteritems
import os import time import sqlite3 from tqdm import tqdm from threading import Thread
import sys PY2 = int(sys.version[0]) == 2
if PY2: text_type = unicode binary_type = str string_types = (str, unicode) unicode = unicode basestring = basestring else: text_type = str binary_type = bytes string_types = (str,) unicode = str basestring = (str, bytes)
import json import sqlite3 import numpy as np
def random_ints(n): """return n random ints that are distinct""" assert n < 10**9, 'Too many distinct numbers asked.' row_randoms = np.random.randint(0, np.iinfo(np.int64).max, 2*n) uniques = np.unique(row_randoms) while len(uniques) < n: r = np.random.randint(0, np.iinfo(np.int64).max, 2*n) uniques = np.unique(np.stack([uniques, r])) return uniques[:n]
class TrainDBBase(object): """ An immutable dataset once write. """
def add_sindex(self, labels): indexes = random_ints(len(labels)) for i, l in enumerate(labels): l['info']['sindex'] = indexes[i] self.sindex_to_sid_dict = {s['info']['sindex']: s['info']['sid'] for s in labels} return labels
def write(self, samples): """save samples""" raise NotImplementedError()
def get_by_sid(self, sid): """get sample by sid""" raise NotImplementedError()
def sindex_to_sid(self, sindex): """ return sid given sindex""" raise NotImplementedError()
def __getitem__(self, item): """ get sample by index in dataset""" raise NotImplementedError()
def __len__(self): """return the number of samples in this dataset""" raise NotImplementedError()
def __iter__(self): self.n = 0 return self
def next(self): if self.n == self.__len__(): raise StopIteration n = self.n self.n += 1 return self[n]
def __next__(self): return self.next()
@property def all_samples(self): """return all samples in this dataset""" return [self[i] for i in range(len(self))]
class SQLiteDB(TrainDBBase):
def __init__(self, db_path, n_samples=None, read_only=True, load_now=False): self.samples = None self.n_samples = n_samples self.sids = None self.sid_to_sample = None self.db_path = db_path self.sindexes = None self.sindex_to_sid_dict =None self.sid_to_sindex_dict =None self.conn = None self.cursor = None self.saved_length = None self.pure_text_samples = True if load_now: self.get_cursor() self.load_sid_sindex() self.cursor.close() self.conn = None self.cursor = None
def get_cursor(self): if self.cursor is not None: return
conn = sqlite3.connect( self.db_path, isolation_level=None, check_same_thread=False, timeout=3)
conn.row_factory = sqlite3.Row self.conn = conn self.cursor = conn.cursor() self.cursor.execute('PRAGMA journal_mode=wal') self.cursor.execute('PRAGMA synchronous=OFF')
def remove_file(self): import os os.remove(self.db_path)
def write(self, samples): self.get_cursor()
self.cursor.execute( 'CREATE TABLE samples (sid TEXT PRIMARY KEY NOT NULL, data TEXT, sindex INT)') self.conn.commit()
if self.pure_text_samples: for i, s in tqdm(enumerate(samples)): sid = unicode(f'{i}') s = unicode(s.strip().replace("'", "''")) try: self.cursor.execute( "insert into samples(sid, data, sindex) values ('{}', '{}', {})".format(sid, s, i)) except Exception as e: print(e) print(sid) print(s) print(i) else: for s in tqdm(samples): s['info']['sid'] = unicode(s['info']['sid']) sample_dict = {s['info']['sid']: json.dumps(s) for s in samples}
i = 0 for sid, s in tqdm(iteritems(sample_dict)): self.cursor.execute( "insert into samples(sid, data, sindex) values ('{}', '{}', {})".format(sid, s, i)) i += 1
self.conn.commit()
def get_by_sid(self, sid): self.load_sid_sindex() sql = "select data from samples where sid = '{}' ".format(sid) try: ret = self.cursor.execute(sql).fetchone()[0] except Exception as e: print(f"{e}\nError at:", sql) raise ValueError() if self.pure_text_samples: sample = ret else: sample = json.loads(ret) sample['info']['sindex'] = self.sid_to_sindex_dict[sid] return sample
def load_sid_sindex(self): if self.sids is not None: return self.get_cursor() sid_sindex = self.cursor.execute( "select sid, sindex from samples").fetchall() if self.n_samples: sid_sindex = sid_sindex[: self.n_samples] self.sids, self.sindexes = zip(*sid_sindex) assert len(set(self.sids)) == len(self.sids) assert len(set(self.sindexes)) == len(self.sindexes)
self.sid_to_sindex_dict = {sid: sindex for sid, sindex in sid_sindex} self.sindex_to_sid_dict = {sindex: sid for sid, sindex in sid_sindex} self.saved_length = len(self.sids) def sindex_to_sid(self, sindex): self.get_cursor() self.load_sid_sindex() return self.sindex_to_sid_dict[sindex]
def __getitem__(self, item): self.get_cursor() self.load_sid_sindex()
sid = self.sids[item] return self.get_by_sid(sid)
def __len__(self): return self.saved_length
def write_existed_samples(txt_path, db_path): db = SQLiteDB(db_path, load_now=False) db.remove_file() samples = open(txt_path, 'r') db.write(samples)
def single_thread_load_samples(_id, dataset): print(f"init {_id}-th subprocess.") total_length = 0 for i in range(1000): res = dataset[i] total_length += res.__len__()
def test_multiprocessing(dataset): import multiprocessing print('Run the main process (%s).' % (os.getpid()))
i = 0 n_cores = 32 for i in range(n_cores): p = multiprocessing.Process( target=single_thread_load_samples, args=(i, dataset)) p.start() print('Waiting for all subprocesses done ...')
if __name__ == "__main__": import time start_time = time.time()
test_path = '/data/chendian/cleaned_findoc_samples/autodoc_test.220424.txt' test_db_path = '/data/chendian/cleaned_findoc_samples/autodoc_test.220424.db'
dataset = SQLiteDB( test_db_path, load_now=True) print("Init SQLite Ends.", time.time() - start_time) print("The first sample is:", dataset[0])
|