2011-03-05 22:14:00 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
nYou may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package mysqlindexer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"camli/blobref"
|
|
|
|
"camli/blobserver"
|
2011-03-13 18:38:53 +00:00
|
|
|
"camli/schema"
|
2011-03-06 17:28:02 +00:00
|
|
|
|
2011-03-13 04:43:18 +00:00
|
|
|
"bytes"
|
2011-03-05 22:14:00 +00:00
|
|
|
"io"
|
2011-03-13 04:43:18 +00:00
|
|
|
"json"
|
2011-03-06 17:28:02 +00:00
|
|
|
"log"
|
2011-03-05 22:14:00 +00:00
|
|
|
"os"
|
2011-03-07 04:11:36 +00:00
|
|
|
|
|
|
|
mysql "github.com/Philio/GoMySQL"
|
2011-03-05 22:14:00 +00:00
|
|
|
)
|
|
|
|
|
2011-03-07 04:11:36 +00:00
|
|
|
const maxSniffSize = 1024 * 16
|
|
|
|
|
|
|
|
type blobSniffer struct {
|
2011-03-13 18:38:53 +00:00
|
|
|
header []byte
|
|
|
|
written int64
|
|
|
|
camli schema.Superset
|
|
|
|
mimeType *string
|
2011-03-06 17:28:02 +00:00
|
|
|
}
|
|
|
|
|
2011-03-07 04:11:36 +00:00
|
|
|
func (sn *blobSniffer) Write(d []byte) (int, os.Error) {
|
|
|
|
sn.written += int64(len(d))
|
|
|
|
if len(sn.header) < maxSniffSize {
|
|
|
|
n := maxSniffSize - len(sn.header)
|
|
|
|
if len(d) < n {
|
|
|
|
n = len(d)
|
|
|
|
}
|
|
|
|
sn.header = append(sn.header, d[:n]...)
|
|
|
|
}
|
2011-03-06 17:28:02 +00:00
|
|
|
return len(d), nil
|
|
|
|
}
|
|
|
|
|
2011-03-07 04:11:36 +00:00
|
|
|
func (sn *blobSniffer) IsTruncated() bool {
|
|
|
|
return sn.written > maxSniffSize
|
|
|
|
}
|
|
|
|
|
2011-03-13 04:43:18 +00:00
|
|
|
type prefixEntry struct {
|
|
|
|
prefix []byte
|
|
|
|
mtype string
|
|
|
|
}
|
|
|
|
|
|
|
|
var prefixTable = []prefixEntry{
|
|
|
|
{[]byte("\xff\xd8\xff\xe1"), "image/jpeg"},
|
2011-03-13 05:24:28 +00:00
|
|
|
{[]byte("\xff\xd8\xff\xe0"), "image/jpeg"},
|
|
|
|
{[]byte{137, 'P', 'N', 'G', '\r', '\n', 26, 10}, "image/png"},
|
2011-03-13 04:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns content type (string) or nil if unknown
|
|
|
|
func (sn *blobSniffer) MimeType() interface{} {
|
2011-03-13 18:38:53 +00:00
|
|
|
if sn.mimeType != nil {
|
|
|
|
return *sn.mimeType
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sn *blobSniffer) Parse() {
|
2011-03-13 04:43:18 +00:00
|
|
|
hlen := len(sn.header)
|
|
|
|
for _, pte := range prefixTable {
|
|
|
|
plen := len(pte.prefix)
|
|
|
|
if hlen > plen && bytes.Equal(sn.header[:plen], pte.prefix) {
|
2011-03-13 18:38:53 +00:00
|
|
|
sn.mimeType = &pte.mtype
|
2011-03-13 04:43:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to parse it as JSON
|
2011-03-13 18:38:53 +00:00
|
|
|
if sn.bufferIsCamliJson() {
|
|
|
|
str := "application/json; camliType=" + sn.camli.Type
|
|
|
|
sn.mimeType = &str
|
2011-03-13 04:43:18 +00:00
|
|
|
}
|
|
|
|
|
2011-03-13 18:38:53 +00:00
|
|
|
return
|
2011-03-13 04:43:18 +00:00
|
|
|
}
|
|
|
|
|
2011-03-13 18:38:53 +00:00
|
|
|
func (sn *blobSniffer) bufferIsCamliJson() bool {
|
|
|
|
buf := sn.header
|
2011-03-13 04:43:18 +00:00
|
|
|
if len(buf) < 2 || buf[0] != '{' {
|
2011-03-13 18:38:53 +00:00
|
|
|
return false
|
2011-03-13 04:43:18 +00:00
|
|
|
}
|
2011-03-13 18:38:53 +00:00
|
|
|
err := json.Unmarshal(buf, &sn.camli)
|
2011-03-13 04:43:18 +00:00
|
|
|
if err != nil {
|
2011-03-13 18:38:53 +00:00
|
|
|
return false
|
2011-03-13 04:43:18 +00:00
|
|
|
}
|
2011-03-13 18:38:53 +00:00
|
|
|
return true
|
2011-03-13 04:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mi *Indexer) ReceiveBlob(blobRef *blobref.BlobRef, source io.Reader, mirrorPartions []blobserver.Partition) (retsb *blobref.SizedBlobRef, err os.Error) {
|
2011-03-07 04:11:36 +00:00
|
|
|
sniffer := new(blobSniffer)
|
2011-03-06 17:28:02 +00:00
|
|
|
hash := blobRef.Hash()
|
|
|
|
var written int64
|
2011-03-07 04:11:36 +00:00
|
|
|
written, err = io.Copy(io.MultiWriter(hash, sniffer), source)
|
|
|
|
log.Printf("mysqlindexer: wrote %d; err %v", written, err)
|
2011-03-06 17:28:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !blobRef.HashMatches(hash) {
|
|
|
|
err = blobserver.CorruptBlobError
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-03-13 18:38:53 +00:00
|
|
|
sniffer.Parse()
|
2011-03-13 04:43:18 +00:00
|
|
|
mimeType := sniffer.MimeType()
|
|
|
|
log.Printf("Read %d bytes; type=%v; truncated=%v", written, sniffer.IsTruncated())
|
2011-03-07 04:11:36 +00:00
|
|
|
|
|
|
|
var client *mysql.Client
|
|
|
|
if client, err = mi.getConnection(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer mi.releaseConnection(client)
|
|
|
|
|
|
|
|
var stmt *mysql.Statement
|
2011-03-13 04:43:18 +00:00
|
|
|
if stmt, err = client.Prepare("INSERT INTO blobs (blobref, size, type) VALUES (?, ?, ?)"); err != nil {
|
2011-03-07 04:11:36 +00:00
|
|
|
return
|
|
|
|
}
|
2011-03-13 04:43:18 +00:00
|
|
|
if err = stmt.BindParams(blobRef.String(), written, mimeType); err != nil {
|
2011-03-07 04:11:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = stmt.Execute(); err != nil {
|
2011-03-13 04:43:18 +00:00
|
|
|
return
|
2011-03-07 04:11:36 +00:00
|
|
|
}
|
2011-03-06 17:28:02 +00:00
|
|
|
|
2011-03-07 04:11:36 +00:00
|
|
|
retsb = &blobref.SizedBlobRef{BlobRef: blobRef, Size: written}
|
|
|
|
return
|
2011-03-05 22:14:00 +00:00
|
|
|
}
|