diff --git a/lib/go/mysqlindexer/enumerate.go b/lib/go/mysqlindexer/enumerate.go index ea4d2ebe3..88a77a72f 100644 --- a/lib/go/mysqlindexer/enumerate.go +++ b/lib/go/mysqlindexer/enumerate.go @@ -28,6 +28,6 @@ func (mi *Indexer) EnumerateBlobs(dest chan *blobref.SizedBlobRef, limit uint, waitSeconds int) os.Error { // TODO: finish - return os.NewError("Fetch isn't supported by the MySQL indexer") + return os.NewError("EnumerateBlobs isn't supported by the MySQL indexer") } diff --git a/lib/go/mysqlindexer/mysqlindexer.go b/lib/go/mysqlindexer/mysqlindexer.go index 7c10d4a42..770469b40 100644 --- a/lib/go/mysqlindexer/mysqlindexer.go +++ b/lib/go/mysqlindexer/mysqlindexer.go @@ -37,6 +37,49 @@ type Indexer struct { cachedClients []*mysql.Client } +func (mi *Indexer) IsAlive() (ok bool, err os.Error) { + var client *mysql.Client + client, err = mi.getConnection() + defer mi.releaseConnection(client) + + if err != nil { + return + } + err = client.Query("SELECT 1 + 1") + if err != nil { + return + } + _, err = client.UseResult() + if err != nil { + return + } + client.FreeResult() + return true, nil +} + +// Get a free cached connection or allocate a new one. +func (mi *Indexer) getConnection() (client *mysql.Client, err os.Error) { + mi.clientLock.Lock() + if len(mi.cachedClients) > 0 { + defer mi.clientLock.Unlock() + client = mi.cachedClients[len(mi.cachedClients)-1] + mi.cachedClients = mi.cachedClients[:len(mi.cachedClients)-1] + // TODO: Outside the mutex, double check that the client is still good. + return + } + mi.clientLock.Unlock() + + client, err = mysql.DialTCP(mi.Host, mi.User, mi.Password, mi.Database) + return +} + +// Release a client to the cached client pool. +func (mi *Indexer) releaseConnection(client *mysql.Client) { + mi.clientLock.Lock() + defer mi.clientLock.Unlock() + mi.cachedClients = append(mi.cachedClients, client) +} + func (mi *Indexer) GetBlobHub(partition blobserver.Partition) blobserver.BlobHub { mi.hubLock.Lock() defer mi.hubLock.Unlock() diff --git a/server/go/blobserver/camlistored.go b/server/go/blobserver/camlistored.go index 7a1e512be..22743ca45 100644 --- a/server/go/blobserver/camlistored.go +++ b/server/go/blobserver/camlistored.go @@ -20,8 +20,11 @@ import ( "os" ) -var flagStorageRoot *string = flag.String("root", "/tmp/camliroot", "Root directory to store files") -var flagRequestLog *bool = flag.Bool("reqlog", false, "Log incoming requests") +var flagStorageRoot = flag.String("root", "/tmp/camliroot", "Root directory to store files") +var flagRequestLog = flag.Bool("reqlog", false, "Log incoming requests") + +// TODO: Temporary +var flagDevMySql = flag.Bool("devmysqlindexer", false, "Temporary option to enable MySQL indexer on /indexer") var storage blobserver.Storage var indexerStorage blobserver.Storage @@ -178,13 +181,20 @@ func main() { ws.HandleFunc("/camli/", handleCamli) // TODO: temporary - if true { - indexerStorage = &mysqlindexer.Indexer{} + if *flagDevMySql { + myIndexer := &mysqlindexer.Indexer{ + Host: "localhost", + User: "root", + Password: "root", + Database: "camlistore", + } + if ok, err := myIndexer.IsAlive(); !ok { + log.Fatalf("Could not connect indexer to MySQL server: %s", err) + } + indexerStorage = myIndexer ws.HandleFunc("/indexer/", handleIndexRequest) } - - ws.Handle("/js/", http.FileServer("../../clients/js", "/js/")) ws.Serve() }