Skip to content

Commit a58a635

Browse files
author
jensj
committed
Allow checking for existance of key - ignoring value
1 parent c83891d commit a58a635

File tree

5 files changed

+20
-15
lines changed

5 files changed

+20
-15
lines changed

ase/db/core.py

-3
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,6 @@ def new_generator(*args, **kwargs):
176176

177177
class Database:
178178
"""Base class for all databases."""
179-
180-
version = 2
181-
182179
def __init__(self, filename=None, create_indices=True,
183180
use_lock_file=False):
184181
self.filename = filename

ase/db/jsondb.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ def _select(self, keywords, cmps, explain=False, verbosity=0, limit=None):
177177
return
178178
dct = bigdct[id]
179179
for keyword in keywords:
180-
if 'keywords' not in dct or keyword not in dct['keywords']:
180+
if not (('keywords' in dct and keyword in dct['keywords']) or
181+
('key_value_pairs' in dct and
182+
keyword in dct['key_value_pairs'])):
181183
break
182184
else:
183185
for key, op, val in cmps:

ase/db/sqlite.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,21 @@ def _initialize(self, con):
8686

8787
cur = con.execute(
8888
'select count(*) from sqlite_master where name="systems"')
89+
self.version = 2
8990
if cur.fetchone()[0] == 0:
9091
for statement in init_statements:
9192
con.execute(statement)
9293
if self.create_indices:
9394
for statement in index_statements:
9495
con.execute(statement)
9596
con.commit()
96-
self.initialized = True
9797
else:
9898
cur = con.execute(
9999
'select count(*) from sqlite_master where name="user_index"')
100100
if cur.fetchone()[0] == 1:
101101
# Old version with "user" instead of "username" column
102102
self.version = 1
103+
self.initialized = True
103104

104105
def _write(self, atoms, keywords, key_value_pairs, data):
105106
Database._write(self, atoms, keywords, key_value_pairs, data)
@@ -198,15 +199,17 @@ def _write(self, atoms, keywords, key_value_pairs, data):
198199
assert isinstance(value, (str, unicode))
199200
text_key_values.append([key, value, id])
200201

201-
if text_key_values:
202-
cur.executemany('insert into text_key_values values (?, ?, ?)',
203-
text_key_values)
204-
if number_key_values:
205-
cur.executemany('insert into number_key_values values (?, ?, ?)',
206-
number_key_values)
207-
if keywords:
208-
cur.executemany('insert into keywords values (?, ?)',
209-
[(keyword, id) for keyword in keywords])
202+
cur.executemany('insert into text_key_values values (?, ?, ?)',
203+
text_key_values)
204+
cur.executemany('insert into number_key_values values (?, ?, ?)',
205+
number_key_values)
206+
cur.executemany('insert into keywords values (?, ?)',
207+
[(keyword, id) for keyword in keywords])
208+
209+
# Insert keys in keywords table also so that it is easy to query
210+
# for the existance of keys:
211+
cur.executemany('insert into keywords values (?, ?)',
212+
[(key, id) for key in key_value_pairs])
210213

211214
con.commit()
212215
con.close()

ase/test/db.py

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
assert con.get_atoms(H=1)[0].magmom == 1
2222
assert len(list(con.select('bla'))) == 0
2323
assert len(list(con.select(abc=42))) == 3
24+
assert len(list(con.select('abc'))) == 3
25+
assert len(list(con.select('abc,bla'))) == 0
26+
assert len(list(con.select('abc,hydro'))) == 3
2427
assert len(list(con.select(foo='bar'))) == 0
2528
assert len(list(con.select(formula='H2'))) == 1
2629
assert len(list(con.select(formula='H2O'))) == 1

doc/ase/db/db.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Here are some example query strings:
101101
:widths: 25 75
102102

103103
* - v3
104-
- has 'v3' keyword
104+
- has 'v3' key or keyword
105105
* - abc=H
106106
- has key 'abc' with value 'H'
107107
* - v3,abc=H

0 commit comments

Comments
 (0)