def setupHistory(self, source): requestProcessor = self.requestProcessor #data used to initialize a new store defaultStmts = base.parseRDFFromString(self.storage_template, requestProcessor.model_uri, options=self.storage_template_options) if self.save_history == 'split': version_model_factory = self.version_model_factory versionModelOptions = {} if not version_model_factory: if self.version_storage_path and self.model_factory: #if a both a version path and the model_factory was set, use the model factory version_model_factory = self.model_factory versionModelOptions = self.model_options elif self.storage_path or self.version_storage_path: #use the default version_model_factory = IncrementalNTriplesFileStoreBase else: version_model_factory = MemStore if not self.version_storage_path and issubclass(version_model_factory, FileStore): #generate path based on primary path assert self.storage_path import os.path versionStoreSource = os.path.splitext(self.storage_path)[0] + '-history.nt' else: versionStoreSource = self.version_storage_path if versionStoreSource: normalizeSource = getattr(self.version_model_factory, 'normalizeSource', DataStore._normalizeSource) versionStoreSource = normalizeSource(self, requestProcessor, versionStoreSource) revisionModel = version_model_factory(source=versionStoreSource, defaultStatements=[], **versionModelOptions) else: #either no history or no separate model revisionModel = None model = None lastScope = None return model, defaultStmts, revisionModel, lastScope
def load(self): requestProcessor = self.requestProcessor self.log = logging.getLogger("datastore." + requestProcessor.app_name) #normalizeSource = getattr(self.model_factory, 'normalizeSource', # DataStore._normalizeSource) #source is the data source for the store, usually a file path #source = normalizeSource(self, requestProcessor, self.storage_path) source = self.storage_path if not self.storage_template and self.storage_template_path: self.storage_template = open(self.storage_template_path).read() model, defaultStmts, historyModel, lastScope = self.setupHistory(source) if not model: #setupHistory didn't initialize the store, so do it now #model_factory will load the store specified by `source` or create #new one at that location and initializing it with `defaultStmts` model_factory = self.model_factory if not model_factory: if source: model_factory = FileStore else: model_factory = MemStore self.log.info("Using %s at '%s'" % (model_factory.__name__, source)) model = model_factory(source=source, defaultStatements=defaultStmts, **self.model_options) #if there's application data (data tied to the current revision #of your app's implementation) include that in the model if self.application_model: stmtGen = base.parseRDFFromString(self.application_model, requestProcessor.model_uri, scope=graphmod.APPCTX) appmodel = MemStore(stmtGen) #XXX MultiModel is not very scalable -- better would be to store #the application data in the model and update it if its difference #from what's stored (this requires a context-aware store) model = base.MultiModel(model, appmodel) if self.save_history: model, historyModel = self._addModelTxnParticipants(model, historyModel) self.model = self.graphManager = graphmod.MergeableGraphManager(model, historyModel, requestProcessor.model_uri, lastScope, self.trunk_id, self.branch_id) if self._txnparticipants: self._txnparticipants.insert(0, #must go first TwoPhaseTxnGraphManagerAdapter(self.graphManager)) else: self.model = self._addModelTxnParticipants(model)[0] self.graphManager = None #turn on update logging if a log file is specified, which can be used to #re-create the change history of the store if self.transaction_log: if not isinstance(self.transaction_log, (str,unicode)): if (self.storage_path and isinstance(self.storage_path, (str,unicode))): self.transaction_log = self.storage_path + '.log.nt' elif self.storage_template_path: self.transaction_log = self.storage_template_path + '.log.nt' else: self.log.warning("unable to determine path for transaction_log") if isinstance(self.transaction_log, (str,unicode)): #XXX doesn't log history model if store is split #XXX doesn't log models that are TransactionParticipants themselves #XXX doesn't log models that don't support updateAdvisory if isinstance(self.model, ModelWrapper): self.model.adapter.logPath = self.transaction_log else: self.log.warning( "transaction_log is configured but not compatible with model") if self.replication_hosts and self.replication_channel: import vesper.data.replication assert self.save_history, "replication requires save_history to be on" rep = vesper.data.replication.get_replicator(self, self.branch_id, self.replication_channel, hosts=self.replication_hosts, sendAck=self.send_stomp_ack) self.model.notifyChangeset = rep.send rep.start() #set the schema (default is no-op) self.schema = self.schemaFactory(self.model) #XXX need for graphManager?: #self.schema.setEntailmentTriggers(self._entailmentAdd, self._entailmentRemove) if isinstance(self.schema, base.Model): self.model = self.schema #hack!: if self.model_options.get('parseOptions', self.storage_template_options ).get('generateBnode') == 'counter': model.bnodePrefix = '_:' self.model.bnodePrefix = '_:'