├── .gitignore ├── README.md └── landerdb ├── __init__.py ├── functions ├── __init__.py ├── delete.py ├── find.py └── insert.py ├── landerdb.py ├── test_find.py └── test_insert.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.db 2 | *.pyc 3 | *.swo 4 | *.swp 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Update 2 | ====== 3 | 4 | LanderDB 2.0 is under heavy development. The old version is now completely obsolete. Stay tuned. 5 | -------------------------------------------------------------------------------- /landerdb/__init__.py: -------------------------------------------------------------------------------- 1 | from functions import * 2 | import db 3 | -------------------------------------------------------------------------------- /landerdb/functions/__init__.py: -------------------------------------------------------------------------------- 1 | import find 2 | import insert 3 | import delete 4 | -------------------------------------------------------------------------------- /landerdb/functions/delete.py: -------------------------------------------------------------------------------- 1 | 2 | def delete(dbdata, collection, found): 3 | for f in found: 4 | index = dbdata['index'][collection][f['id']] 5 | dbdata['collections'][collection].pop(index) 6 | del dbdata['index'][collection][f['id']] 7 | -------------------------------------------------------------------------------- /landerdb/functions/find.py: -------------------------------------------------------------------------------- 1 | """ 2 | { 3 | collections: { 4 | users:[ 5 | { 6 | _id:5, 7 | firstname:"Frankie", 8 | username:"Omg" 9 | } 10 | ] 11 | } 12 | 13 | index: { 14 | users:{ 15 | "asdkk12k123sad":0 16 | } 17 | } 18 | } 19 | """ 20 | 21 | 22 | def find(dbdata, collection, data): 23 | if collection not in dbdata['collections']: 24 | return [] 25 | if "id" in data: 26 | if collection in dbdata['index']: 27 | index = dbdata['index'][collection].get(data['id']) 28 | if index: 29 | return [dbdata['collections'][collection][index]] 30 | return [] 31 | output = [] 32 | for on in dbdata['collections'][collection]: 33 | for key in data: 34 | if key in data and key in on: 35 | if data[key] != on[key]: 36 | break 37 | else: 38 | break 39 | else: 40 | output.append(on) 41 | return output 42 | 43 | -------------------------------------------------------------------------------- /landerdb/functions/insert.py: -------------------------------------------------------------------------------- 1 | import uuid 2 | import pprint 3 | 4 | def insert(dbdata, collection, data): 5 | _id = uuid.uuid4().hex 6 | data["id"] = _id 7 | if collection not in dbdata['collections']: 8 | dbdata['collections'][collection] = [data] 9 | else: 10 | dbdata['collections'][collection].append(data) 11 | 12 | if collection not in dbdata['index']: 13 | dbdata['index'][collection] = { 14 | _id: len(dbdata['collections'][collection]) - 1 15 | } 16 | else: 17 | dbdata['index'][collection][_id] = len(dbdata['collections'][collection]) - 1 18 | return _id 19 | -------------------------------------------------------------------------------- /landerdb/landerdb.py: -------------------------------------------------------------------------------- 1 | import cPickle 2 | import os 3 | import shutil 4 | from functions import * 5 | 6 | class LanderDB: 7 | def __init__(self, dbname, autosave=True): 8 | self.autosave = autosave 9 | if not dbname.endswith(".db"): 10 | dbname = dbname + ".db" 11 | if os.path.exists(dbname + ".lock"): 12 | raise Exception("The DB is locked. Please remove the {}.lock file".format(dbname)) 13 | self.dbname = dbname 14 | if os.path.exists(dbname): 15 | try: 16 | self.databaseData = cPickle.load(open(dbname)) 17 | except Exception as e: 18 | raise Exception("Could not connect to database {}. DB file might be corrupted. {}".format(dbname, e)) 19 | else: 20 | self.databaseData = { 21 | "collections":{}, 22 | "index":{} 23 | } 24 | self.save() 25 | 26 | def save(self): 27 | while os.path.exists(self.dbname + ".lock"): 28 | pass 29 | open(self.dbname + ".lock", 'w') 30 | if not os.path.exists(self.dbname): 31 | open(self.dbname, 'w').write("{}") 32 | try: 33 | cPickle.dump(self.databaseData, open(self.dbname, 'w'), protocol=cPickle.HIGHEST_PROTOCOL) 34 | except Exception as e: 35 | os.remove(self.dbname + ".lock") 36 | raise Exception("There was an error writing data to the database: {}".format(e)) 37 | os.remove(self.dbname + ".lock") 38 | 39 | def find(self, collection, data): 40 | return find.find(self.databaseData, collection, data) 41 | 42 | def insert(self, collection, data): 43 | insert.insert(self.databaseData, collection, data) 44 | if self.autosave: 45 | self.save() 46 | 47 | def delete(self, collection, data): 48 | found = self.find(collection, data) 49 | if found: 50 | delete.delete(self.databaseData, collection, found) 51 | if self.autosave: 52 | self.save() 53 | 54 | -------------------------------------------------------------------------------- /landerdb/test_find.py: -------------------------------------------------------------------------------- 1 | import db 2 | q = db.Connect("test") 3 | print q.find("users", {"b":6}) 4 | 5 | -------------------------------------------------------------------------------- /landerdb/test_insert.py: -------------------------------------------------------------------------------- 1 | import landerdb 2 | 3 | d = landerdb.LanderDB("test") 4 | d.insert("users", {"name":"frankie"}) 5 | --------------------------------------------------------------------------------