2011-11-07 16:40:31 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package index
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2011-11-09 16:21:19 +00:00
|
|
|
"sync"
|
2011-11-07 16:40:31 +00:00
|
|
|
|
|
|
|
"camli/blobserver"
|
|
|
|
"camli/jsonconfig"
|
|
|
|
|
|
|
|
"leveldb-go.googlecode.com/hg/leveldb/db"
|
|
|
|
"leveldb-go.googlecode.com/hg/leveldb/memdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
blobserver.RegisterStorageConstructor("memory-only-dev-indexer", blobserver.StorageConstructor(newMemoryIndexFromConfig))
|
|
|
|
}
|
|
|
|
|
2011-11-09 16:21:19 +00:00
|
|
|
// memKeys is a naive in-memory implementation of IndexStorage for test & development
|
|
|
|
// purposes only.
|
2011-11-07 16:40:31 +00:00
|
|
|
type memKeys struct {
|
2011-11-09 16:21:19 +00:00
|
|
|
mu sync.Mutex // guards db
|
2011-11-07 16:40:31 +00:00
|
|
|
db db.DB
|
|
|
|
}
|
|
|
|
|
2011-11-10 18:28:44 +00:00
|
|
|
// stringIterator converts from leveldb's db.Iterator interface, which
|
|
|
|
// operates on []byte, to Camlistore's index.Iterator, which operates
|
|
|
|
// on string.
|
|
|
|
type stringIterator struct {
|
|
|
|
db.Iterator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stringIterator) Key() string {
|
|
|
|
return string(s.Iterator.Key())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stringIterator) Value() string {
|
|
|
|
return string(s.Iterator.Value())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mk *memKeys) Find(key string) Iterator {
|
|
|
|
mk.mu.Lock()
|
|
|
|
defer mk.mu.Unlock()
|
|
|
|
dit := mk.db.Find([]byte(key))
|
|
|
|
return stringIterator{dit}
|
|
|
|
}
|
|
|
|
|
2011-11-09 16:21:19 +00:00
|
|
|
func (mk *memKeys) Set(key, value string) os.Error {
|
|
|
|
mk.mu.Lock()
|
|
|
|
defer mk.mu.Unlock()
|
|
|
|
return mk.db.Set([]byte(key), []byte(value))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mk *memKeys) Delete(key string) os.Error {
|
|
|
|
mk.mu.Lock()
|
|
|
|
defer mk.mu.Unlock()
|
|
|
|
return mk.db.Delete([]byte(key))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mk *memKeys) BeginBatch() BatchMutation {
|
|
|
|
return &batch{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mk *memKeys) CommitBatch(bm BatchMutation) os.Error {
|
|
|
|
b, ok := bm.(*batch)
|
|
|
|
if !ok {
|
|
|
|
return os.NewError("invalid batch type; not an instance returned by BeginBatch")
|
|
|
|
}
|
|
|
|
mk.mu.Lock()
|
|
|
|
defer mk.mu.Unlock()
|
|
|
|
for _ ,m := range b.m {
|
|
|
|
if m.delete {
|
|
|
|
if err := mk.db.Delete([]byte(m.key)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := mk.db.Set([]byte(m.key), []byte(m.value)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-11-07 16:40:31 +00:00
|
|
|
func newMemoryIndexFromConfig(ld blobserver.Loader, config jsonconfig.Obj) (blobserver.Storage, os.Error) {
|
|
|
|
blobPrefix := config.RequiredString("blobSource")
|
|
|
|
|
|
|
|
db := memdb.New(nil)
|
2011-11-09 16:21:19 +00:00
|
|
|
memStorage := &memKeys{db: db}
|
2011-11-07 16:40:31 +00:00
|
|
|
|
|
|
|
ix := New(memStorage)
|
|
|
|
if err := config.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sto, err := ld.GetStorage(blobPrefix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ix.BlobSource = sto
|
|
|
|
|
|
|
|
// Good enough, for now:
|
|
|
|
ix.KeyFetcher = ix.BlobSource
|
|
|
|
|
|
|
|
return ix, err
|
|
|
|
}
|