faster sort/find update

This commit is contained in:
Jan Gerber 2015-04-21 19:58:32 +02:00
parent d980a8ae04
commit 505d7efe9b

View file

@ -123,6 +123,7 @@ class Item(db.Model):
return f.fullpath() if f else None return f.fullpath() if f else None
def update_sort(self): def update_sort(self):
update = False
s = Sort.get_or_create(self.id) s = Sort.get_or_create(self.id)
for key in config['itemKeys']: for key in config['itemKeys']:
if key.get('sort'): if key.get('sort'):
@ -158,19 +159,24 @@ class Item(db.Model):
value = ox.sort_string(value).lower() value = ox.sort_string(value).lower()
elif isinstance(value, list): #empty list elif isinstance(value, list): #empty list
value = '' value = ''
setattr(s, key['id'], value) if getattr(s, key['id']) != value:
state.db.session.add(s) setattr(s, key['id'], value)
update = True
if update:
state.db.session.add(s)
def update_find(self): def update_find(self):
def add(k, v): def add(k, v):
f = Find(item_id=self.id, key=k) f = Find.query.filter_by(item_id=self.id, key=k, value=v).first()
if isinstance(v, bytes): if not f:
v = v.decode('utf-8') f = Find(item_id=self.id, key=k)
f.findvalue = unicodedata.normalize('NFKD', v).lower() if f.value != v:
f.value = v f.findvalue = unicodedata.normalize('NFKD', v).lower()
state.db.session.add(f) f.value = v
state.db.session.add(f)
keys = []
for key in config['itemKeys']: for key in config['itemKeys']:
if key.get('find') or key.get('filter') or key.get('type') in [['string'], 'string']: if key.get('find') or key.get('filter') or key.get('type') in [['string'], 'string']:
value = self.json().get(key['id'], None) value = self.json().get(key['id'], None)
@ -178,17 +184,22 @@ class Item(db.Model):
value = re.compile(key.get('filterMap')).findall(value) value = re.compile(key.get('filterMap')).findall(value)
if value: value = value[0] if value: value = value[0]
if value: if value:
Find.query.filter_by(item_id=self.id, key=key['id']).delete() keys.append(key['id'])
if isinstance(value, dict): if isinstance(value, dict):
value = ' '.join(list(value.values())) value = ' '.join(list(value.values()))
if not isinstance(value, list): if not isinstance(value, list):
value = [value] value = [value]
value = [
v.decode('utf-8') if isinstance(v, bytes) else v
for v in value
]
for v in value: for v in value:
add(key['id'], v) add(key['id'], v)
else: for f in Find.query.filter_by(item_id=self.id,
f = Find.get(self.id, key['id']) key=key['id']).filter(Find.value.notin_(value)):
if f:
state.db.session.delete(f) state.db.session.delete(f)
for f in Find.query.filter_by(item_id=self.id).filter(Find.key.notin_(keys)):
state.db.session.delete(f)
def update(self): def update(self):
for key in ('mediastate', 'coverRatio', 'previewRatio'): for key in ('mediastate', 'coverRatio', 'previewRatio'):
@ -252,7 +263,8 @@ class Item(db.Model):
key = self.meta['primaryid'][0] key = self.meta['primaryid'][0]
record = {} record = {}
if id: if id:
self.meta[key] = id if not key in self.meta or not key in self.meta[key]:
self.meta[key] = list(set([id] + self.meta.get(key, [])))
self.meta['primaryid'] = [key, id] self.meta['primaryid'] = [key, id]
record[key] = id record[key] = id
else: else:
@ -450,7 +462,7 @@ class Find(db.Model):
findvalue = sa.Column(sa.Text(), index=True) findvalue = sa.Column(sa.Text(), index=True)
def __repr__(self): def __repr__(self):
return ('%s=%s' % (self.key, self.findvalue)).encode('utf-8') return '%s=%s' % (self.key, self.findvalue)
@classmethod @classmethod
def get(cls, item, key): def get(cls, item, key):