2013-11-23 07:24:54 +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 sorted
|
|
|
|
|
|
|
|
import (
|
2013-12-07 16:43:18 +00:00
|
|
|
"bytes"
|
2013-11-23 07:24:54 +00:00
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"camlistore.org/pkg/jsonconfig"
|
|
|
|
"camlistore.org/third_party/code.google.com/p/leveldb-go/leveldb/db"
|
|
|
|
"camlistore.org/third_party/code.google.com/p/leveldb-go/leveldb/memdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewMemoryKeyValue returns a KeyValue implementation that's backed only
|
|
|
|
// by memory. It's mostly useful for tests and development.
|
|
|
|
func NewMemoryKeyValue() KeyValue {
|
|
|
|
db := memdb.New(nil)
|
|
|
|
return &memKeys{db: db}
|
|
|
|
}
|
|
|
|
|
|
|
|
// memKeys is a naive in-memory implementation of index.Storage for test & development
|
|
|
|
// purposes only.
|
|
|
|
type memKeys struct {
|
|
|
|
mu sync.Mutex // guards db
|
|
|
|
db db.DB
|
|
|
|
}
|
|
|
|
|
2013-12-07 16:43:18 +00:00
|
|
|
// memIter converts from leveldb's db.Iterator interface, which
|
2013-11-23 07:24:54 +00:00
|
|
|
// operates on []byte, to Camlistore's index.Iterator, which operates
|
|
|
|
// on string.
|
2013-12-07 16:43:18 +00:00
|
|
|
type memIter struct {
|
2013-12-04 13:18:52 +00:00
|
|
|
lit db.Iterator // underlying leveldb iterator
|
|
|
|
k, v *string // if nil, not stringified yet
|
2013-12-07 16:43:18 +00:00
|
|
|
end []byte // if len(end) > 0, the upper bound
|
2013-11-23 07:24:54 +00:00
|
|
|
}
|
|
|
|
|
2013-12-07 16:43:18 +00:00
|
|
|
func (t *memIter) Next() bool {
|
|
|
|
t.k, t.v = nil, nil
|
|
|
|
if !t.lit.Next() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(t.end) > 0 && bytes.Compare(t.KeyBytes(), t.end) >= 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2013-11-23 07:24:54 +00:00
|
|
|
}
|
|
|
|
|
2013-12-07 16:43:18 +00:00
|
|
|
func (s *memIter) Close() error {
|
2013-12-04 13:18:52 +00:00
|
|
|
err := s.lit.Close()
|
2013-12-07 16:43:18 +00:00
|
|
|
*s = memIter{} // to cause crashes on future access
|
2013-12-04 13:18:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-07 16:43:18 +00:00
|
|
|
func (s *memIter) KeyBytes() []byte {
|
2013-12-04 13:18:52 +00:00
|
|
|
return s.lit.Key()
|
|
|
|
}
|
|
|
|
|
2013-12-07 16:43:18 +00:00
|
|
|
func (s *memIter) ValueBytes() []byte {
|
2013-12-04 13:18:52 +00:00
|
|
|
return s.lit.Value()
|
|
|
|
}
|
|
|
|
|
2013-12-07 16:43:18 +00:00
|
|
|
func (s *memIter) Key() string {
|
2013-12-04 13:18:52 +00:00
|
|
|
if s.k != nil {
|
|
|
|
return *s.k
|
|
|
|
}
|
|
|
|
str := string(s.KeyBytes())
|
|
|
|
s.k = &str
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2013-12-07 16:43:18 +00:00
|
|
|
func (s *memIter) Value() string {
|
2013-12-04 13:18:52 +00:00
|
|
|
if s.v != nil {
|
|
|
|
return *s.v
|
|
|
|
}
|
|
|
|
str := string(s.ValueBytes())
|
|
|
|
s.v = &str
|
|
|
|
return str
|
2013-11-23 07:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mk *memKeys) Get(key string) (string, error) {
|
|
|
|
mk.mu.Lock()
|
|
|
|
defer mk.mu.Unlock()
|
|
|
|
k, err := mk.db.Get([]byte(key), nil)
|
|
|
|
if err == db.ErrNotFound {
|
|
|
|
return "", ErrNotFound
|
|
|
|
}
|
|
|
|
return string(k), err
|
|
|
|
}
|
|
|
|
|
2013-12-07 16:43:18 +00:00
|
|
|
func (mk *memKeys) Find(start, end string) Iterator {
|
2013-11-23 07:24:54 +00:00
|
|
|
mk.mu.Lock()
|
|
|
|
defer mk.mu.Unlock()
|
2013-12-07 16:43:18 +00:00
|
|
|
lit := mk.db.Find([]byte(start), nil)
|
|
|
|
it := &memIter{lit: lit}
|
|
|
|
if end != "" {
|
|
|
|
it.end = []byte(end)
|
|
|
|
}
|
|
|
|
return it
|
2013-11-23 07:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mk *memKeys) Set(key, value string) error {
|
|
|
|
mk.mu.Lock()
|
|
|
|
defer mk.mu.Unlock()
|
|
|
|
return mk.db.Set([]byte(key), []byte(value), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mk *memKeys) Delete(key string) error {
|
|
|
|
mk.mu.Lock()
|
|
|
|
defer mk.mu.Unlock()
|
2013-12-07 16:43:18 +00:00
|
|
|
err := mk.db.Delete([]byte(key), nil)
|
|
|
|
if err == db.ErrNotFound {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2013-11-23 07:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mk *memKeys) BeginBatch() BatchMutation {
|
|
|
|
return &batch{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mk *memKeys) CommitBatch(bm BatchMutation) error {
|
|
|
|
b, ok := bm.(*batch)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("invalid batch type; not an instance returned by BeginBatch")
|
|
|
|
}
|
|
|
|
mk.mu.Lock()
|
|
|
|
defer mk.mu.Unlock()
|
|
|
|
for _, m := range b.Mutations() {
|
|
|
|
if m.IsDelete() {
|
|
|
|
if err := mk.db.Delete([]byte(m.Key()), nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := mk.db.Set([]byte(m.Key()), []byte(m.Value()), nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-24 05:07:03 +00:00
|
|
|
func (mk *memKeys) Close() error { return nil }
|
|
|
|
|
2013-11-23 07:24:54 +00:00
|
|
|
func init() {
|
|
|
|
RegisterKeyValue("memory", func(cfg jsonconfig.Obj) (KeyValue, error) {
|
|
|
|
if err := cfg.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewMemoryKeyValue(), nil
|
|
|
|
})
|
|
|
|
}
|