def _makeSegment(self): with self.new_txn() as txn: try: segmentId = self.get_meta(txn, u'last-segment-id', RMW=True) except KeyError: segmentId = 0 segmentId = segmentId + 1 self.set_meta(txn, u'last-segment-id', segmentId) segmentName = u"%s.%i" % (self.name, segmentId) segmentUUID = unicode(uuid5(self._indexUUID, str(segmentName))) self.set_segment(txn, segmentName, segmentUUID, NOOVERWRITE=True) segment = Segment(self._env, txn, segmentName) segment.set_meta(txn, u'created-on', int(time.time())) segment.set_meta(txn, u'uuid', segmentUUID) last_update = { u'segment-size': 0, u'last-id': [EVID_MIN.ts, EVID_MIN.offset], u'last-modified': 0 } segment.set_meta(txn, u'last-update', last_update) with self._segmentLock: self._segments.append(segment) self._current = segment return segment
def __init__(self, output): self.name = output._indexName self._env = output._plugin._env backend.Index.__init__(self, self._env, self.name) self._segmentLock = Lock() self._segments = [] self._current = None self._fieldLock = Lock() self._fieldstore = output._fieldstore self._fields = {} self._indexUUID = None try: # load index metadata with self.new_txn() as txn: try: self._indexUUID = UUID( self.get_meta(txn, u'uuid', RMW=True)) except KeyError: self._indexUUID = uuid4() self.set_meta(txn, u'uuid', unicode(self._indexUUID)) # load schema with self.new_txn() as txn: for fieldname, fieldspec in self.iter_fields(txn): self._fields[fieldname] = pickle.loads(str(fieldspec)) # verify that the field type is consistent for fieldtype, stored in self._fields[fieldname].items(): field = self._fieldstore.getField(fieldtype) if not stored.field.__class__ == field.__class__: raise SchemaError( "schema field %s:%s does not match registered type %s" % (fieldname, fieldtype, field.__class__.__name__)) # load data segments indexSize = 0 with self.new_txn() as txn: for segmentName, segmentUUID in self.iter_segments(txn): segment = Segment(self._env, txn, segmentName) try: foundUUID = segment.get_meta(txn, u'uuid') except KeyError: foundUUID = None if foundUUID == None or segmentUUID != foundUUID: logger.debug("index segment %s has invalid UUID %s" % (segmentName, foundUUID)) segment.close() else: self._segments.append(segment) logger.debug("opened index segment '%s'" % segmentName) # if the index has no segments, create one if self._segments == []: self._makeSegment() logger.info("created first segment for new index '%s'" % self.name) else: logger.info("loaded %i segments for index '%s'" % (len(self._segments), self.name)) # get a reference to the current segment self._current = self._segments[-1] logger.debug("opened event index '%s' (%s)" % (self.name, str(self._indexUUID))) except: self.close() raise