2011-01-28 07:07:18 +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.
|
|
|
|
You 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.
|
|
|
|
*/
|
|
|
|
|
2011-02-04 22:31:23 +00:00
|
|
|
package localdisk
|
2010-07-11 04:18:16 +00:00
|
|
|
|
|
|
|
import (
|
2010-12-03 15:34:24 +00:00
|
|
|
"camli/blobref"
|
2011-02-04 01:08:04 +00:00
|
|
|
"camli/blobserver"
|
2010-07-11 04:18:16 +00:00
|
|
|
"fmt"
|
2011-02-03 06:42:31 +00:00
|
|
|
"log"
|
2010-12-14 02:20:31 +00:00
|
|
|
"os"
|
2011-02-10 01:05:55 +00:00
|
|
|
"sync"
|
2010-07-11 04:18:16 +00:00
|
|
|
)
|
|
|
|
|
2010-12-14 02:20:31 +00:00
|
|
|
type diskStorage struct {
|
2011-02-04 22:31:23 +00:00
|
|
|
root string
|
2011-02-10 01:05:55 +00:00
|
|
|
|
|
|
|
hubLock sync.Mutex
|
|
|
|
hubMap map[blobserver.Partition]blobserver.BlobHub
|
2011-02-04 22:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(root string) (storage blobserver.Storage, err os.Error) {
|
|
|
|
// Local disk.
|
|
|
|
fi, staterr := os.Stat(root)
|
|
|
|
if staterr != nil || !fi.IsDirectory() {
|
|
|
|
err = os.NewError(fmt.Sprintf("Storage root %q doesn't exist or is not a directory.", root))
|
|
|
|
return
|
|
|
|
}
|
2011-02-10 01:05:55 +00:00
|
|
|
storage = &diskStorage{
|
|
|
|
root: root,
|
|
|
|
hubMap: make(map[blobserver.Partition]blobserver.BlobHub),
|
|
|
|
}
|
2011-02-04 22:31:23 +00:00
|
|
|
return
|
2010-12-14 02:20:31 +00:00
|
|
|
}
|
|
|
|
|
2011-02-10 01:05:55 +00:00
|
|
|
func (ds *diskStorage) GetBlobHub(partition blobserver.Partition) blobserver.BlobHub {
|
|
|
|
ds.hubLock.Lock()
|
|
|
|
defer ds.hubLock.Unlock()
|
|
|
|
if hub, ok := ds.hubMap[partition]; ok {
|
|
|
|
return hub
|
|
|
|
}
|
2011-02-28 00:18:17 +00:00
|
|
|
|
|
|
|
// TODO: in the future, allow for different blob hub
|
|
|
|
// implementations rather than the
|
|
|
|
// everything-in-memory-on-a-single-machine SimpleBlobHub.
|
2011-02-10 01:05:55 +00:00
|
|
|
hub := new(blobserver.SimpleBlobHub)
|
|
|
|
ds.hubMap[partition] = hub
|
|
|
|
return hub
|
|
|
|
}
|
|
|
|
|
2010-12-17 17:59:03 +00:00
|
|
|
func (ds *diskStorage) Fetch(blob *blobref.BlobRef) (blobref.ReadSeekCloser, int64, os.Error) {
|
2011-02-04 22:31:23 +00:00
|
|
|
fileName := ds.blobFileName(blob)
|
2010-12-14 02:20:31 +00:00
|
|
|
stat, err := os.Stat(fileName)
|
2011-02-04 22:31:23 +00:00
|
|
|
if errorIsNoEnt(err) {
|
2010-12-14 02:20:31 +00:00
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
file, err := os.Open(fileName, os.O_RDONLY, 0)
|
|
|
|
if err != nil {
|
2011-03-05 08:03:53 +00:00
|
|
|
if errorIsNoEnt(err) {
|
|
|
|
err = os.ENOENT
|
|
|
|
}
|
2010-12-14 02:20:31 +00:00
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return file, stat.Size, nil
|
|
|
|
}
|
|
|
|
|
2011-02-04 01:08:04 +00:00
|
|
|
func (ds *diskStorage) Remove(partition blobserver.Partition, blobs []*blobref.BlobRef) os.Error {
|
2011-02-03 06:42:31 +00:00
|
|
|
for _, blob := range blobs {
|
2011-02-04 22:31:23 +00:00
|
|
|
fileName := ds.partitionBlobFileName(partition, blob)
|
2011-02-03 06:42:31 +00:00
|
|
|
err := os.Remove(fileName)
|
2011-02-03 16:28:47 +00:00
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
continue
|
|
|
|
case errorIsNoEnt(err):
|
2011-02-03 06:42:31 +00:00
|
|
|
log.Printf("Deleting already-deleted file; harmless.")
|
|
|
|
continue
|
2011-02-03 16:28:47 +00:00
|
|
|
default:
|
|
|
|
return err
|
2011-02-03 06:42:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|