def __init__(self, io, host='localhost', port=7474, cleanoutdb=False, debug=False , retries=300, readonly=False, encryption_required=False, use_network=True): 'Initialize and construct a global database instance' #print >> sys.stderr, 'CALLING NEW initglobal' CMAdb.log = logging.getLogger('cma') CMAdb.debug = debug CMAdb.io = io from hbring import HbRing syslog = logging.handlers.SysLogHandler(address='/dev/log' , facility=logging.handlers.SysLogHandler.LOG_DAEMON) syslog.setFormatter(logging.Formatter('%(name)s %(levelname)s: %(message)s')) CMAdb.log.addHandler(syslog) CMAdb.log.setLevel(logging.DEBUG) url = ('http://%s:%d/db/data/' % (host, port)) #print >> sys.stderr, 'CREATING GraphDatabaseService("%s")' % url neodb = neo4j.GraphDatabaseService(url) self.db = neodb if cleanoutdb: CMAdb.log.info('Re-initializing the NEO4j database') self.delete_all() self.db = neodb CMAdb.use_network = use_network trycount=0 while True: try: CMAdb.cdb = CMAdb(db=neodb) # Neo4j started. All is well with the world. break except (RuntimeError, IOError, py2neo.exceptions.ClientError) as exc: print >> sys.stderr, 'TRYING AGAIN [[%s]...[%s]' % (url, str(exc)) trycount += 1 if trycount > retries: print >> sys.stderr, ('Neo4j still not started - giving up.') CMAdb.log.critical('Neo4j still not started - giving up.') raise RuntimeError('Neo4j not running - giving up [%s]' % str(exc)) if (trycount % 60) == 1: print >> sys.stderr, ('Waiting for Neo4j [%s] to start [%s].' % (url, str(exc))) CMAdb.log.warning('Waiting for Neo4j [%s] to start [%s].' % (url, str(exc))) # Let's try again in a second... time.sleep(1) Store.debug = debug Store.log = CMAdb.log CMAdb.store = Store(neodb, CMAconsts.uniqueindexes, CMAconsts.classkeymap , readonly=readonly) if not readonly: for classname in GraphNode.classmap: GraphNode.initclasstypeobj(CMAdb.store, classname) from transaction import Transaction CMAdb.transaction = Transaction(encryption_required=encryption_required) #print >> sys.stderr, 'CMAdb:', CMAdb #print >> sys.stderr, 'CMAdb.store(cmadb.py):', CMAdb.store CMAdb.TheOneRing = CMAdb.store.load_or_create(HbRing, name='The_One_Ring' , ringtype=HbRing.THEONERING) CMAdb.transaction.commit_trans(io) #print >> sys.stderr, 'COMMITTING Store' #print >> sys.stderr, 'Transaction Commit results:', CMAdb.store.commit() CMAdb.store.commit() #print >> sys.stderr, 'Store COMMITTED' else: CMAdb.transaction = None
dronelist = CMAdb.store.load_in_related(drone, self.ourreltype, Drone) for elem in dronelist: ringcount += 1 #print >> sys.stderr \ #, ('%s status: %s mbrcount: %d, nextcount:%d, incount:%d, ringcount:%d' #% (drone, drone.status, mbrcount, nextcount, incount, ringcount)) assert drone.status == 'up' assert mbrcount < 2 or 1 == nextcount assert mbrcount < 2 or 1 == incount assert 1 == ringcount for drone in listmembers.keys(): assert(drone in ringmembers) for drone in ringmembers.keys(): assert(drone in listmembers) def __str__(self): ret = 'Ring("%s"' % self.name #comma = ', [' #for drone in self.members_ring_order(): # ret += '%s%s' % (comma, drone) # comma = ', ' #ret += ']' ret += ')' return ret if __name__ == '__main__': db = CMAdb()