Esempio n. 1
0
func (*testSuite) TestT(c *C) {
	driver := localstore.Driver{Driver: goleveldb.MemoryDriver{}}
	store, err := driver.Open("memory")
	c.Assert(err, IsNil)
	defer store.Close()

	// For GenID
	txn, err := store.Begin()
	c.Assert(err, IsNil)
	key := []byte(meta.AutoIDKey(1))
	id, err := meta.GenID(txn, key, 1)
	c.Assert(id, Equals, int64(1))
	id, err = meta.GenID(txn, key, 2)
	c.Assert(id, Equals, int64(3))
	id, err = meta.GenID(txn, []byte{}, 1)
	c.Assert(err, NotNil)

	// For DBMetaKey
	mkey := meta.DBMetaKey(1)
	c.Assert(mkey, Equals, "mDB::1")

	//For AutoIDKey
	mkey = meta.AutoIDKey(1)
	c.Assert(mkey, Equals, "mTable::1_autoID")
	mkey = meta.AutoIDKey(0)
	c.Assert(mkey, Equals, "mTable::0_autoID")

	// For GenGlobalID
	id, err = meta.GenGlobalID(store)
	c.Assert(err, IsNil)
	c.Assert(id, Equals, int64(1))
	id, err = meta.GenGlobalID(store)
	c.Assert(err, IsNil)
	c.Assert(id, Equals, int64(2))
}
Esempio n. 2
0
File: ddl.go Progetto: H0bby/tidb
func (d *ddl) DropSchema(ctx context.Context, schema model.CIStr) (err error) {
	is := d.GetInformationSchema()
	old, ok := is.SchemaByName(schema)
	if !ok {
		return errors.Trace(ErrNotExists)
	}

	// Update InfoSchema
	oldInfo := is.Clone()
	var newInfo []*model.DBInfo
	for _, v := range oldInfo {
		if v.Name.L != schema.L {
			newInfo = append(newInfo, v)
		}
	}

	// Remove data.
	txn, err := ctx.GetTxn(true)
	if err != nil {
		return errors.Trace(err)
	}
	tables := is.SchemaTables(schema)
	for _, t := range tables {
		err = t.Truncate(ctx)
		if err != nil {
			return errors.Trace(err)
		}
		// Remove indices.
		for _, v := range t.Indices() {
			if v != nil && v.X != nil {
				if err = v.X.Drop(txn); err != nil {
					return errors.Trace(err)
				}
			}
		}
	}

	// Delete meta key.
	err = kv.RunInNewTxn(d.store, false, func(txn kv.Transaction) error {
		err := d.verifySchemaMetaVersion(txn, is.SchemaMetaVersion())
		if err != nil {
			return errors.Trace(err)
		}
		key := []byte(meta.DBMetaKey(old.ID))
		if err := txn.LockKeys(key); err != nil {
			return errors.Trace(err)
		}
		return txn.Delete(key)
	})
	if d.onDDLChange != nil {
		err = d.onDDLChange(err)
	}
	return errors.Trace(err)
}
Esempio n. 3
0
File: ddl.go Progetto: H0bby/tidb
func (d *ddl) writeSchemaInfo(info *model.DBInfo, txn kv.Transaction) error {
	var b []byte
	b, err := json.Marshal(info)
	if err != nil {
		return errors.Trace(err)
	}
	key := []byte(meta.DBMetaKey(info.ID))
	if err := txn.LockKeys(key); err != nil {
		return errors.Trace(err)
	}
	txn.Set(key, b)
	log.Warn("save schema", string(b))

	return errors.Trace(err)
}
Esempio n. 4
0
func (d *ddl) writeSchemaInfo(info *model.DBInfo) error {
	var b []byte
	b, err := json.Marshal(info)
	if err != nil {
		return errors.Trace(err)
	}
	err = kv.RunInNewTxn(d.store, false, func(txn kv.Transaction) error {
		key := []byte(meta.DBMetaKey(info.ID))
		if err := txn.LockKeys(key); err != nil {
			return errors.Trace(err)
		}
		return txn.Set(key, b)
	})
	log.Warn("save schema", string(b))
	return errors.Trace(err)
}