예제 #1
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
}
예제 #2
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
}
예제 #3
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
}