def pack_storage_main(): parser = OptionParser() parser.set_description("Packs a Durus storage.") parser.add_option( '--file', dest="file", default=None, help="If this is not given, the storage is through a Durus server.") parser.add_option('--port', dest="port", default=DEFAULT_PORT, type="int", help="Port the server is on. (default=%s)" % DEFAULT_PORT) parser.add_option('--host', dest="host", default=DEFAULT_HOST, help="Host of the server. (default=%s)" % DEFAULT_HOST) (options, args) = parser.parse_args() if options.file is None: wait_for_server(options.host, options.port) storage = ClientStorage(host=options.host, port=options.port) else: storage = FileStorage(options.file) connection = Connection(storage) connection.pack()
def check_oid_reuse(self): # Requires ShelfStorage oid reuse pack semantics s1 = ClientStorage(address=self.address) s1.oid_pool_size = 1 c1 = Connection(s1) r1 = c1.get_root() s2 = ClientStorage(address=self.address) s2.oid_pool_size = 1 c2 = Connection(s2) r2 = c2.get_root() r1['a'] = PersistentDict() r1['b'] = PersistentDict() c1.commit() c2.abort() a_oid = r1['a']._p_oid assert 'a' in r1 and 'b' in r1 and len(r1['b']) == 0 assert 'a' in r2 and 'b' in r2 and len(r2['b']) == 0 del r2['a'] # remove only reference to a c2.commit() c2.pack() # force relinquished oid back into availability sleep(0.5) # Give time for pack to complete c2.abort() assert c2.get(a_oid) is None c1.abort() assert c1.get(a_oid)._p_is_ghost() r2['b']['new'] = Persistent() r2['b']['new'].bogus = 1 c2.commit() assert c2.get(a_oid) is r2['b']['new'] c1.abort() assert c1.get(a_oid).__class__ == PersistentDict r1['b']['new'].bogus assert c1.get(a_oid).__class__ == Persistent s1.close()
class Session(object): """ Representation of the game state. """ _persistent_attributes = ('scheduler', 'started', 'lastroom', 'universe', 'characters', 'player', 'debugging') # default values scheduler = None # Scheduler instance started = False # Is game started yet? (I.e. have player turns/actions begun) lastroom = None # Used to determine auto-placement of items universe = None # Top level container object (provides storage for entire game state) characters = () # List of character agents (references into universe) player = () # List of player character agents (normally only 1 in PUB) debugging = False # Debugging mode is for use during game development def __init__(self, storagefile="default.sav"): self.storage = Connection(FileStorage(storagefile)) self.root = self.storage.get_root() self.running = False def __setattr__(self, name, value): if name in self._persistent_attributes: self.root[name] = value else: object.__setattr__(self, name, value) def __getattribute__(self, name): persistent_attributes = object.__getattribute__( self, '_persistent_attributes') if name in persistent_attributes: try: return self.root[name] except KeyError: return getattr(self.__class__, name) else: return object.__getattribute__(self, name) def new_game(self): """ Start up a new game (clear the storage instance). """ self.scheduler = None self.started = True self.lastroom = None self.universe = None self.characters = None self.player = None self.debugging = False self.commit() self.pack() def commit(self): self.storage.commit() def abort(self): self.storage.abort() def pack(self): self.storage.pack()
def check_oid_reuse_with_invalidation(self): connection = Connection(ClientStorage(address=self.address)) root = connection.get_root() root['x'] = Persistent() connection.commit() connection = Connection(ClientStorage(address=self.address)) root = connection.get_root() root['x'] = Persistent() connection.commit() connection.pack() sleep(1) # Make sure pack finishes. connection = Connection(ClientStorage(address=self.address)) root = connection.get_root() root['x'] = Persistent() connection.commit()
def b(self): f = File(prefix='shelftest') name = f.get_name() f.close() s = FileStorage(name) c = Connection(s) r = c.get_root() for x in range(10): r["a%s" % x] = Persistent() c.commit() deleted_oid = r['a9']._p_oid del r['a9'] c.commit() c.pack() c.abort() assert len([repr(oid) for oid, record in s.gen_oid_record()]) == 10 new_oid = s.new_oid() assert new_oid == deleted_oid new_oid = s.new_oid() assert new_oid == int8_to_str(11)
def main(old_file, new_file): if old_file.startswith('-'): usage() if new_file.startswith('-'): usage() assert not exists(new_file) connection = Connection(sys.argv[1]) tmpfile = TemporaryFile() print("pickling from " + old_file) dump(connection.get_root().__getstate__(), tmpfile, 2) connection = None tmpfile.seek(0) connection2 = Connection(sys.argv[2]) print("unpickling") connection2.get_root().__setstate__(load(tmpfile)) connection2.get_root()._p_note_change() print("commit to " + new_file) connection2.commit() print("pack") connection2.pack()
def main(old_file, new_file): if old_file.startswith('-'): usage() if new_file.startswith('-'): usage() assert not exists(new_file) connection = Connection(sys.argv[1]) tmpfile = TemporaryFile() print("pickling from " + old_file) dump(connection.get_root().__getstate__(), tmpfile, 2) connection = None tmpfile.seek(0) connection2 = Connection(sys.argv[2]) print("unpickling") connection2.get_root().__setstate__(load(tmpfile)) connection2.get_root()._p_note_change() print("commit to " + new_file) connection2.commit() print("pack") connection2.pack()
def b(self): f = File(prefix='shelftest') name = f.get_name() f.close() s = FileStorage(name) c = Connection(s) r = c.get_root() for x in range(10): r["a%s" % x] = Persistent() c.commit() deleted_oid = r['a9']._p_oid del r['a9'] c.commit() c.pack() c.abort() assert len([repr(oid) for oid, record in s.gen_oid_record()]) == 10 new_oid = s.new_oid() assert new_oid == deleted_oid new_oid = s.new_oid() assert new_oid == int8_to_str(11)
def pack_storage_main(): parser = ArgumentParser("Packs a Durus storage.") parser.add_argument( '--file', dest="file", default=None, help="If this is not given, the storage is through a Durus server.") parser.add_argument( '--port', dest="port", default=DEFAULT_PORT, type=int, help="Port the server is on. (default=%s)" % DEFAULT_PORT) parser.add_argument( '--host', dest="host", default=DEFAULT_HOST, help="Host of the server. (default=%s)" % DEFAULT_HOST) args = parser.parse_args() if args.file is None: wait_for_server(args.host, args.port) storage = ClientStorage(host=args.host, port=args.port) else: storage = FileStorage(args.file) connection = Connection(storage) connection.pack()
def pack_storage_main(): parser = OptionParser() parser.set_description("Packs a Durus storage.") parser.add_option( '--file', dest="file", default=None, help="If this is not given, the storage is through a Durus server.") parser.add_option( '--port', dest="port", default=DEFAULT_PORT, type="int", help="Port the server is on. (default=%s)" % DEFAULT_PORT) parser.add_option( '--host', dest="host", default=DEFAULT_HOST, help="Host of the server. (default=%s)" % DEFAULT_HOST) (options, args) = parser.parse_args() if options.file is None: wait_for_server(options.host, options.port) storage = ClientStorage(host=options.host, port=options.port) else: storage = get_storage(options.file) connection = Connection(storage) connection.pack()
def main(): from shutil import copyfile from os.path import exists def usage(): sys.stdout.write( "Usage: python %s <existing_file> <new_file>\n" % sys.argv[0]) sys.stdout.write(" Creates a new py3k-compatible file ") sys.stdout.write("from an existing FileStorage file.\n") raise SystemExit if len(sys.argv) != 3: usage() infile = sys.argv[1] outfile = sys.argv[2] if not exists(infile): usage() if exists(outfile): if input('overwrite %r? [y/N] ' % outfile).strip().lower() != 'y': raise SystemExit copyfile(infile, outfile) # monkey patch pickler class, must be done before importing durus stuff patch_pickler() from durus.__main__ import get_storage_class from durus.connection import Connection storage_class = get_storage_class(outfile) storage = storage_class(outfile) connection = Connection(storage) print ("Converting %s for use with py3k." % outfile) for j, x in enumerate(connection.get_crawler()): x._p_note_change() if j > 0 and j % 10000 == 0: print(j) connection.commit() print(j) connection.commit() connection.pack()
def main(): from shutil import copyfile from os.path import exists def usage(): sys.stdout.write("Usage: python %s <existing_file> <new_file>\n" % sys.argv[0]) sys.stdout.write(" Creates a new py3k-compatible file ") sys.stdout.write("from an existing FileStorage file.\n") raise SystemExit if len(sys.argv) != 3: usage() infile = sys.argv[1] outfile = sys.argv[2] if not exists(infile): usage() if exists(outfile): if input('overwrite %r? [y/N] ' % outfile).strip().lower() != 'y': raise SystemExit copyfile(infile, outfile) # monkey patch pickler class, must be done before importing durus stuff patch_pickler() from durus.__main__ import get_storage_class from durus.connection import Connection storage_class = get_storage_class(outfile) storage = storage_class(outfile) connection = Connection(storage) print("Converting %s for use with py3k." % outfile) for j, x in enumerate(connection.get_crawler()): x._p_note_change() if j > 0 and j % 10000 == 0: print(j) connection.commit() print(j) connection.commit() connection.pack()
def a(self): f = File(prefix='shelftest') name = f.get_name() f.close() s = FileStorage(name) c = Connection(s) r = c.get_root() for x in range(10): r["a%s" % x] = Persistent() c.commit() deleted_oids = [ r['a0']._p_oid, r['a2']._p_oid, r['a7']._p_oid, r['a8']._p_oid ] del r['a0'] del r['a2'] del r['a7'] del r['a8'] c.commit() c.pack() c.abort() assert c.get(deleted_oids[0])._p_is_ghost() assert c.get(deleted_oids[1])._p_is_ghost() raises(KeyError, getattr, c.get(deleted_oids[0]), 'a') assert len([repr(oid) for oid, record in s.gen_oid_record()]) == 7 c.commit() c.pack() new_oid = s.new_oid() assert new_oid == deleted_oids[-1], (new_oid, deleted_oids) new_oid = s.new_oid() assert new_oid == deleted_oids[-2], (new_oid, deleted_oids) new_oid = s.new_oid() assert new_oid == deleted_oids[-3], (new_oid, deleted_oids) new_oid = s.new_oid() assert new_oid == deleted_oids[-4], (new_oid, deleted_oids) new_oid = s.new_oid() assert new_oid == int8_to_str(11), repr(new_oid) new_oid = s.new_oid() assert new_oid == int8_to_str(12), repr(new_oid)
def c(self): f = File(prefix='shelftest') name = f.get_name() f.close() s = FileStorage(name) c = Connection(s) r = c.get_root() for x in range(10): r["a%s" % x] = Persistent() c.commit() deleted_oid = r['a9']._p_oid del r['a9'] c.commit() c.pack() c.abort() r.clear() c.commit() c.pack() c.abort() new_oid = s.new_oid() assert new_oid == int8_to_str(1), repr(new_oid) new_oid = s.new_oid() assert new_oid == int8_to_str(2), repr(new_oid)
def c(self): f = File(prefix='shelftest') name = f.get_name() f.close() s = FileStorage(name) c = Connection(s) r = c.get_root() for x in range(10): r["a%s" % x] = Persistent() c.commit() deleted_oid = r['a9']._p_oid del r['a9'] c.commit() c.pack() c.abort() r.clear() c.commit() c.pack() c.abort() new_oid = s.new_oid() assert new_oid == int8_to_str(1), repr(new_oid) new_oid = s.new_oid() assert new_oid == int8_to_str(2), repr(new_oid)
def a(self): f = File(prefix='shelftest') name = f.get_name() f.close() s = FileStorage(name) c = Connection(s) r = c.get_root() for x in range(10): r["a%s" % x] = Persistent() c.commit() deleted_oids = [ r['a0']._p_oid, r['a2']._p_oid, r['a7']._p_oid, r['a8']._p_oid] del r['a0'] del r['a2'] del r['a7'] del r['a8'] c.commit() c.pack() c.abort() assert c.get(deleted_oids[0])._p_is_ghost() assert c.get(deleted_oids[1])._p_is_ghost() raises(ReadConflictError, getattr, c.get(deleted_oids[0]), 'a') assert len([repr(oid) for oid, record in s.gen_oid_record()]) == 7 c.commit() c.pack() new_oid = s.new_oid() assert new_oid == deleted_oids[-1], (new_oid, deleted_oids) new_oid = s.new_oid() assert new_oid == deleted_oids[-2], (new_oid, deleted_oids) new_oid = s.new_oid() assert new_oid == deleted_oids[-3], (new_oid, deleted_oids) new_oid = s.new_oid() assert new_oid == deleted_oids[-4], (new_oid, deleted_oids) new_oid = s.new_oid() assert new_oid == int8_to_str(11), repr(new_oid) new_oid = s.new_oid() assert new_oid == int8_to_str(12), repr(new_oid)
from durus.connection import Connection from durus.file_storage import FileStorage from shutil import copyfile def usage(): sys.stdout.write("Usage: python %s <existing_file> <new_file>\n" % sys.argv[0]) sys.stdout.write(" Creates a new py3k-compatible file ") sys.stdout.write("from an existing FileStorage file.\n") raise SystemExit if len(sys.argv) != 3: usage() from os.path import exists if not exists(sys.argv[1]): usage() if exists(sys.argv[2]): usage() copyfile(sys.argv[1], sys.argv[2]) storage = FileStorage(sys.argv[2]) connection = Connection(storage) print("Converting %s for use with py3k." % sys.argv[2]) for j, x in enumerate(connection.get_crawler()): x._p_note_change() if j > 0 and j % 10000 == 0: print(j) connection.commit() print(j) connection.commit() connection.pack()
from durus.connection import Connection from durus.file_storage import FileStorage from shutil import copyfile def usage(): sys.stdout.write( "Usage: python %s <existing_file> <new_file>\n" % sys.argv[0]) sys.stdout.write(" Creates a new py3k-compatible file ") sys.stdout.write("from an existing FileStorage file.\n") raise SystemExit if len(sys.argv) != 3: usage() from os.path import exists if not exists(sys.argv[1]): usage() if exists(sys.argv[2]): usage() copyfile(sys.argv[1], sys.argv[2]) storage = FileStorage(sys.argv[2]) connection = Connection(storage) print ("Converting %s for use with py3k." % sys.argv[2]) for j, x in enumerate(connection.get_crawler()): x._p_note_change() if j > 0 and j % 10000 == 0: print(j) connection.commit() print(j) connection.commit() connection.pack()
class Session(object): """ Representation of the game state. """ _persistent_attributes = ( 'scheduler', 'started', 'lastroom', 'universe', 'characters', 'player', 'debugging') # default values scheduler = None # Scheduler instance started = False # Is game started yet? (I.e. have player turns/actions begun) lastroom = None # Used to determine auto-placement of items universe = None # Top level container object (provides storage for entire game state) characters = () # List of character agents (references into universe) player = () # List of player character agents (normally only 1 in PUB) debugging = False # Debugging mode is for use during game development def __init__(self, storagefile="default.sav"): self.storage = Connection(FileStorage(storagefile)) self.root = self.storage.get_root() self.running = False def __setattr__(self, name, value): if name in self._persistent_attributes: self.root[name] = value else: object.__setattr__(self, name, value) def __getattribute__(self, name): persistent_attributes = object.__getattribute__(self, '_persistent_attributes') if name in persistent_attributes: try: return self.root[name] except KeyError: return getattr(self.__class__, name) else: return object.__getattribute__(self, name) def new_game(self): """ Start up a new game (clear the storage instance). """ self.scheduler = None self.started = True self.lastroom = None self.universe = None self.characters = None self.player = None self.debugging = False self.commit() self.pack() def commit(self): self.storage.commit() def abort(self): self.storage.abort() def pack(self): self.storage.pack()
class XdserverBackend(object): """Schevo backend that directly uses Xdserver 3.9.""" DEFAULT_CACHE_SIZE = 100000 description = __doc__.splitlines()[0].strip() backend_args_help = """ cache_size=%(DEFAULT_CACHE_SIZE)i (int) Maximum number of objects to keep in the cache. client=None (xdserver.client.Client instance) An existing client connection to use; overrides host and port in URL. """ % locals() __test__ = False BTree = BTree PDict = PersistentDict PList = PersistentList conflict_exceptions = (ConflictError,) # TestMethods_CreatesDatabase = Xdserver_TestMethods_CreatesDatabase # TestMethods_CreatesSchema = Xdserver_TestMethods_CreatesSchema # TestMethods_EvolvesSchemata = Xdserver_TestMethods_EvolvesSchemata def __init__(self, database, cache_size=DEFAULT_CACHE_SIZE, host=xdserver.client.DEFAULT_HOST, port=xdserver.client.DEFAULT_PORT, client=None, ): self.database = database self.cache_size = cache_size self.host = host self.port = port self.client = client self.is_open = False self.open() @classmethod def usable_by_backend(cls, filename): """Return (`True`, *additional backend args*) if the named file is usable by this backend, or `False` if not.""" return False @property def has_db(self): """Return `True` if the backend contains a Schevo database.""" return self.get_root().has_key('SCHEVO') def close(self): """Close the underlying storage (and the connection if needed).""" self.storage.close() self.is_open = False def commit(self): """Commit the current transaction.""" self.conn.commit() def get_root(self): """Return the backend's `root` object.""" return self.conn.get_root() def open(self): """Open the underlying storage based on initial arguments.""" if not self.is_open: # Find or create storage. if self.client is not None: self.storage = self.client.storage(self.database) elif None not in (self.host, self.port): self.client = xdserver.client.Client(self.host, self.port) self.storage = self.client.storage(self.database) # Connect to storage. self.conn = Connection( self.storage, cache_size=self.cache_size) self.is_open = True def pack(self): """Pack the underlying storage.""" self.conn.pack() def rollback(self): """Abort the current transaction.""" self.conn.abort()