Beispiel #1
0
func do(db *mysql.Client, sql string) {
	err := db.Query(sql)
	if err == nil {
		return
	}
	exitf("Error %v running SQL: %s", err, sql)
}
Beispiel #2
0
func (mi *Indexer) ReceiveBlob(blobRef *blobref.BlobRef, source io.Reader) (retsb blobref.SizedBlobRef, err os.Error) {
	sniffer := new(blobSniffer)
	hash := blobRef.Hash()
	var written int64
	written, err = io.Copy(io.MultiWriter(hash, sniffer), source)
	log.Printf("mysqlindexer: hashed+sniffed %d bytes; err %v", written, err)
	if err != nil {
		return
	}

	if !blobRef.HashMatches(hash) {
		err = blobserver.ErrCorruptBlob
		return
	}

	sniffer.Parse()
	mimeType := sniffer.MimeType()
	log.Printf("mysqlindexer: type=%v; truncated=%v", mimeType, sniffer.IsTruncated())

	var client *mysql.Client
	if client, err = mi.getConnection(); err != nil {
		return
	}
	defer mi.releaseConnection(client)

	var stmt *mysql.Statement
	if stmt, err = client.Prepare("INSERT IGNORE INTO blobs (blobref, size, type) VALUES (?, ?, ?)"); err != nil {
		log.Printf("mysqlindexer: prepare error: %v", err)
		return
	}
	if err = stmt.BindParams(blobRef.String(), written, mimeType); err != nil {
		log.Printf("mysqlindexer: bind error: %v", err)
		return
	}
	if err = stmt.Execute(); err != nil {
		log.Printf("mysqlindexer: execute error: %v", err)
		return
	}

	if camli := sniffer.camli; camli != nil {
		switch camli.Type {
		case "claim":
			if err = mi.populateClaim(client, blobRef, camli, sniffer); err != nil {
				return
			}
		case "permanode":
			if err = mi.populatePermanode(client, blobRef, camli); err != nil {
				return
			}
		case "file":
			if err = mi.populateFile(client, blobRef, camli); err != nil {
				return
			}
		}
	}

	retsb = blobref.SizedBlobRef{BlobRef: blobRef, Size: written}
	return
}
Beispiel #3
0
func execSQL(client *mysql.Client, sql string, args ...interface{}) (err os.Error) {
	var stmt *mysql.Statement
	if stmt, err = client.Prepare(sql); err != nil {
		log.Printf("mysqlindexer execSQL prepare: %v", err)
		return
	}
	if err = stmt.BindParams(args...); err != nil {
		log.Printf("mysqlindexer execSQL bind: %v", err)
		return
	}
	if err = stmt.Execute(); err != nil {
		log.Printf("mysqlindexer execSQL exe: %v", err)
		return
	}
	return
}
Beispiel #4
0
func dbExists(db *mysql.Client, dbname string) bool {
	check(db.Query("SHOW DATABASES"))
	result, err := db.UseResult()
	check(err)
	defer result.Free()
	for {
		row := result.FetchRow()
		if row == nil {
			break
		}
		if row[0].(string) == dbname {
			return true
		}
	}
	return false
}
Beispiel #5
0
func (mi *Indexer) EnumerateBlobs(dest chan<- blobref.SizedBlobRef, after string, limit uint, waitSeconds int) (err os.Error) {
	// MySQL connection stuff.
	var client *mysql.Client
	client, err = mi.getConnection()
	if err != nil {
		return
	}
	defer mi.releaseConnection(client)

	var stmt *mysql.Statement
	stmt, err = client.Prepare("SELECT blobref, size FROM blobs WHERE blobref > ? ORDER BY blobref LIMIT ?")
	if err != nil {
		return
	}
	err = stmt.BindParams(after, limit)
	if err != nil {
		return
	}
	err = stmt.Execute()
	if err != nil {
		return
	}

	var row blobRow
	stmt.BindResult(&row.blobref, &row.size)
	for {
		var done bool
		done, err = stmt.Fetch()
		if err != nil {
			return
		}
		if done {
			break
		}
		br := blobref.Parse(row.blobref)
		if br == nil {
			continue
		}
		dest <- blobref.SizedBlobRef{
			BlobRef: br,
			Size:    row.size,
		}
	}
	close(dest)
	return
}
Beispiel #6
0
func (mi *Indexer) SchemaVersion() (version int, err os.Error) {
	var client *mysql.Client
	client, err = mi.getConnection()
	if err != nil {
		return
	}
	defer mi.releaseConnection(client)

	err = client.Query("SELECT value FROM meta WHERE metakey='version'")
	if err != nil {
		return
	}
	res, err := client.UseResult()
	if err != nil {
		return
	}

	row := res.FetchRow()
	if row == nil {
		return 0, nil
	}

	version, err = strconv.Atoi(row[0].(string))

	client.FreeResult()
	return
}
Beispiel #7
0
func (mi *Indexer) IsAlive() (ok bool, err os.Error) {
	var client *mysql.Client
	client, err = mi.getConnection()
	if err != nil {
		return
	}
	defer mi.releaseConnection(client)

	err = client.Query("SELECT 1 + 1")
	if err != nil {
		return
	}
	_, err = client.UseResult()
	if err != nil {
		return
	}
	client.FreeResult()
	return true, nil
}