Beispiel #1
0
    def add_record(self, filetag, startpos, length, table):
        key_list = table[self.primary_namespace]
        if len(key_list) != 1:
            raise TypeError(
                "Field %s has %d entries but must have only one "
                "(must be unique)" % (repr(self.primary_namespace),
                                      len(key_list)))
        key = key_list[0]
        if self.primary_table.has_key(key):
            raise TypeError("Field %r = %r already exists" %
                            (self.primary_namespace, key))
        self.primary_table[key] = "%s\t%s\t%s" % (filetag,
                                                  BaseDB._int_str(startpos),
                                                  BaseDB._int_str(length))

        for namespace in self.secondary_namespaces:
            lookup = self._load_namespace(namespace)
            # Get the list of secondary identifiers for this identifier
            for val in table.get(namespace, ()):
                # Go from secondary identifier to list of primary identifiers
                if lookup.has_key(val):
                    lookup[val] = lookup[val] + "\t" + key
                else:
                    lookup[val] = key
        self.__need_flush = 1
Beispiel #2
0
    def flush(self):
        if not self._need_flush:
            return

        config_filename = os.path.join(self.dbname, "config.dat")
        BaseDB.write_config(config_filename = config_filename,
                            index_type = INDEX_TYPE,
                            primary_namespace = self.primary_namespace,
                            secondary_namespaces =
                                    self.secondary_tables.keys(),
                            fileid_info = self.fileid_info,
                            formatname = self.formatname,
                      )

        primary_filename = os.path.join(self.dbname,
                           "key_%s.key" % (self.primary_namespace,) )
        _write_primary_table(filename = primary_filename,
                             primary_table = self.primary_table)


        # Write the secondary identifier information
        for namespace, table in self.secondary_tables.items():
            filename = os.path.join(self.dbname, "id_%s.index" % namespace)
            _write_secondary_table(filename = filename,
                                   table = table)

        self._need_flush = 0
Beispiel #3
0
def create(dbname, primary_namespace, secondary_namespaces,
           formatname = "unknown"):
    os.mkdir(dbname)
    config_filename = os.path.join(dbname, "config.dat")
    BaseDB.write_config(config_filename = config_filename,
                        index_type = INDEX_TYPE,
                        primary_namespace = primary_namespace,
                        secondary_namespaces = secondary_namespaces,
                        fileid_info = {},
                        formatname = formatname
                        )

    dbenv = db.DBEnv(0)
    envflags = db.DB_THREAD | db.DB_INIT_MPOOL
    dbenv.open(dbname, envflags | db.DB_CREATE)

    primary_table = db.DB(dbenv)
    primary_table.open("key_%s" % (primary_namespace,), None,
                       db.DB_BTREE, db.DB_CREATE, 0660)

    secondary_tables = {}
    for namespace in secondary_namespaces:
        x = db.DB(dbenv)
        x.open("id_%s" % (namespace,), None, db.DB_BTREE, db.DB_CREATE, 0)
        secondary_tables[namespace] = x

    for x in secondary_tables.values():
        x.close()
    primary_table.close()
    dbenv.close()

    return open(dbname, "rw")
Beispiel #4
0
def create(dbname,
           primary_namespace,
           secondary_namespaces,
           formatname="unknown"):
    os.mkdir(dbname)
    config_filename = os.path.join(dbname, "config.dat")
    BaseDB.write_config(config_filename=config_filename,
                        index_type=INDEX_TYPE,
                        primary_namespace=primary_namespace,
                        secondary_namespaces=secondary_namespaces,
                        fileid_info={},
                        formatname=formatname)

    dbenv = db.DBEnv(0)
    envflags = db.DB_THREAD | db.DB_INIT_MPOOL
    dbenv.open(dbname, envflags | db.DB_CREATE)

    primary_table = db.DB(dbenv)
    primary_table.open("key_%s" % (primary_namespace, ), None, db.DB_BTREE,
                       db.DB_CREATE, 0660)

    secondary_tables = {}
    for namespace in secondary_namespaces:
        x = db.DB(dbenv)
        x.open("id_%s" % (namespace, ), None, db.DB_BTREE, db.DB_CREATE, 0)
        secondary_tables[namespace] = x

    for x in secondary_tables.values():
        x.close()
    primary_table.close()
    dbenv.close()

    return open(dbname, "rw")
Beispiel #5
0
 def flush(self):
     if not self.__need_flush:
         return
     config_filename = os.path.join(self.dbname, "config.dat")
     BaseDB.write_config(config_filename = config_filename,
                         index_type = INDEX_TYPE,
                         primary_namespace = self.primary_namespace,
                         secondary_namespaces =
                              self.secondary_tables.keys(),
                         fileid_info = self.fileid_info,
                         formatname = self.formatname,
                   )
     self.__need_flush = 0
Beispiel #6
0
 def flush(self):
     if not self.__need_flush:
         return
     config_filename = os.path.join(self.dbname, "config.dat")
     BaseDB.write_config(
         config_filename=config_filename,
         index_type=INDEX_TYPE,
         primary_namespace=self.primary_namespace,
         secondary_namespaces=self.secondary_tables.keys(),
         fileid_info=self.fileid_info,
         formatname=self.formatname,
     )
     self.__need_flush = 0
Beispiel #7
0
    def add_record(self, filetag, startpos, length, table):
        key_list = table[self.primary_namespace]
        if len(key_list) != 1:
            raise TypeError("Field %s has %d entries but must have only one "
                            "(must be unique)" %
                            (repr(self.primary_namespace), len(key_list)))
        key = key_list[0]
        if self.primary_table.has_key(key):
            raise TypeError("Field %r = %r already exists" %
                            (self.primary_namespace, key))
        self.primary_table[key] = "%s\t%s\t%s" % (
            filetag, BaseDB._int_str(startpos), BaseDB._int_str(length))

        for namespace in self.secondary_namespaces:
            lookup = self._load_namespace(namespace)
            # Get the list of secondary identifiers for this identifier
            for val in table.get(namespace, ()):
                # Go from secondary identifier to list of primary identifiers
                if lookup.has_key(val):
                    lookup[val] = lookup[val] + "\t" + key
                else:
                    lookup[val] = key
        self.__need_flush = 1
Beispiel #8
0
def create(dbname, primary_namespace, secondary_namespaces,
           formatname = "unknown"):
    os.mkdir(dbname)
    config_filename = os.path.join(dbname, "config.dat")
    BaseDB.write_config(config_filename = config_filename,
                        index_type = INDEX_TYPE,
                        primary_namespace = primary_namespace,
                        secondary_namespaces = secondary_namespaces,
                        fileid_info = {},
                        formatname = formatname,
                        )

    primary_filename = os.path.join(dbname,
                       "key_%s.key" % (primary_namespace,) )
    _write_primary_table(filename = primary_filename,
                         primary_table = {})


    # Write the secondary identifier information
    for namespace in secondary_namespaces:
        filename = os.path.join(dbname, "id_%s.index" % namespace)
        _write_secondary_table(filename = filename,
                               table = {})
    return open(dbname, "rw")