Comments (1)
This can be obtained using db.max(), but it is slow
def keep_newest(key:str='jobid', maxlen:int=1000) -> Optional[List]:
""" keep the newest maxlen entries in database """
global db
if not db:
db = TinyDB(db_path)
currlen = len(db.all())
if currlen<=maxlen:
#log.warning(f"database size is:{currlen} which is less than {maxlen} - no deletion")
return False
ids = []
for d in db.all()[:currlen - maxlen + 1]:
id = db.remove(where(key)==d[key])
if id:
ids.append(id)
#log.info(f"removed {d} with index {id}")
return ids
number of entries in database: 10000
number of entries in database: 999
deleting 9001 took 450.83sec
The json direct version is way faster: 1875 times faster?
def keep_newest_json(fname:str, maxlen:int=1000, table:str='_default') -> Optional[List]:
""" keep the newest maxlen entries in database """
dat = None
with open(fname, 'r', encoding='utf8') as FR:
dat = json.load(FR)
if table not in dat:
log.fatal(f"table:{table} not found in dat:{list(dat.keys())}")
return None
currlen = len(dat[table].keys())
if currlen<=maxlen:
log.info(f"table:{table} has {currlen} entries, less than maxlen:{maxlen}")
return None
ids = []
for id in list(dat[table].keys())[:currlen - maxlen + 1]:
del dat[table][id]
ids.append(id)
#log.info(f"removed index {id} from {table}")
with open(fname, 'w', encoding='utf8') as FW:
FW.write(json.dumps(dat, indent=2, sort_keys=True))
return ids
number of entries in database: 10000
number of entries in database: 999
deleting 9001 took 0.24sec
from tinydb.
Related Issues (20)
- how to flush Tables with CachingMiddleware(JSONStorage)) HOT 2
- de-serialization Query() statement for search is there a way not use eval HOT 3
- Data Corruption vulnerability on TinyDB version 4.7.1 HOT 5
- Does tinyDB support multi-thread/process access ? HOT 2
- TinyDB 4.8.0 issue HOT 2
- -1073741571 (0xC00000FD) something interesting occured. HOT 2
- Are there any option to persist query caches? HOT 1
- Json decoder error when reading DB from multiple processes HOT 2
- Broken link in documentation.
- Document ID and existing unique value in the document HOT 1
- Search documents that do not have a specific key
- A simple implementation for update_multiple_by_id
- bug: MemoryStorage incorrectly keeps references to nested dicts
- json context manager
- How can we ensure uniqeness when inserting HOT 1
- Questions about Copilot + Open Source Software Hierarchy
- What specific scenarios can this database be used in? HOT 1
- LRUCache.set not update cache value when the key exists
- is it possible to encrypt DB file and add authentication? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tinydb.