Exemple #1
0
    def test_12_table_reopen(self):

        db = Database(self._db_name)
        table = db.table(self._tb_name)
        table.index('by_age_name', '{age:03}{name}')
        table.index('by_name', '{name}')
        table.index('by_age', '{age:03}', duplicates=True)
        self.generate_data(db, self._tb_name)
        db.close()
        db = Database(self._db_name)
        table = db.table(self._tb_name)
        self.assertEqual(['by_age', 'by_age_name', 'by_name'], table.indexes())
Exemple #2
0
    def test_13_index_exists(self):

        db = Database(self._db_name)
        table = db.table(self._tb_name)
        table.index('by_age_name', '{age:03}{name}')
        table.index('by_name', '{name}')
        table.index('by_age', '{age:03}', duplicates=True)
        self.assertTrue(table.exists('by_name'))
        db.close()
        db = Database(self._db_name)
        table = db.table(self._tb_name)
        self.assertTrue(table.exists('by_name'))
Exemple #3
0
    def test_08_delete_binlog(self):
        db = Database(self._db_name)
        db.set_binlog(True)
        self.generate_data(db, self._tb_name)
        self.assertEqual(db.tables_all,
                         ['__binidx__', '__binlog__', '__metadata__', 'demo1'])
        db.close()

        db = Database(self._db_name)
        db.drop(self._tb_name)
        db.drop('__binlog__')
        self.assertEqual(db.tables_all, ['__binidx__', '__metadata__'])
        with self.assertRaises(xTableMissing):
            db.drop('fred')
Exemple #4
0
 def do_register(self, opts):
     """Register a new database with this tool\n"""
     database = opts.database[0]
     alias = opts.alias[0]
     path = Path(database).expanduser()
     if not path.exists():
         self.ppfeedback('register', 'error', 'failed to find path "{}"'.format(database))
         return
     try:
         db = Database(str(path))
         db.close()
     except Exception as e:
         print(e)
     if Path(self._base / alias).exists():
         self.ppfeedback('register', 'failed', 'the alias already exists "{}"'.format(alias))
         return
     Path(self._base / alias).symlink_to(str(path), target_is_directory=True)
Exemple #5
0
db = Database("databases/raw")  # Open (/create) a database
table = db.table('people')   # Open (/create) a table

print('>> Index table by name and age')
table.index('by_name', '{name}')
table.index('by_age', '{age:03}', duplicates=True)

print('>> Adding data')
for item in data:
    table.append(item)
print("Count=", table.records)

print('>> Scanning table sequentially')
for record in table.find():
    print('{name} is {age} years old'.format(**record))

print('>> Scanning tables in name order [string index]')
for record in table.find('by_name'):
    print('{name} sorted alphabetically'.format(**record))

print('>> Scanning table in age order [numerical index]')
for record in table.find('by_age'):
    print('{age} - {name} in ascending order of age'.format(**record))

print('>> Scanning on name index with filter')
for record in table.find('by_name', expression=lambda doc: doc['age'] > 40):
    print('{name} is {age} years old (filtered age>40)'.format(**record))

db.drop('people')
db.close()