Skip to content

Commit

Permalink
support for accessing the underlying index/store impls
Browse files Browse the repository at this point in the history
now you can access the underlying index/store implementations
using the Advanced() method.  this is intedned for advanced
usage only, and can lead to problems if misused.

also, there is a new method NewUsing(...) which allows callers
of the top-level API to choose which underlying k/v store they
want to use.
  • Loading branch information
mschoch committed Dec 27, 2014
1 parent 38bdcbe commit 68712cd
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
15 changes: 15 additions & 0 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ package bleve

import (
"github.com/blevesearch/bleve/document"
"github.com/blevesearch/bleve/index"
"github.com/blevesearch/bleve/index/store"
)

// A Batch groups together multiple Index and Delete
Expand Down Expand Up @@ -90,6 +92,8 @@ type Index interface {
GetInternal(key []byte) ([]byte, error)
SetInternal(key, val []byte) error
DeleteInternal(key []byte) error

Advanced() (index.Index, store.KVStore, error)
}

// A Classifier is an interface describing any object
Expand All @@ -105,6 +109,17 @@ func New(path string, mapping *IndexMapping) (Index, error) {
return newIndex(path, mapping)
}

// NewUsing creates index at the specified path,
// which must not already exist.
// The provided mapping will be used for all
// Index/Search operations.
// The specified kvstore implemenation will be used
// and the provided kvconfig will be passed to its
// constructor.
func NewUsing(path string, mapping *IndexMapping, kvstore string, kvconfig map[string]interface{}) (Index, error) {
return newIndexUsing(path, mapping, kvstore, kvconfig)
}

// Open index at the specified path, must exist.
// The mapping used when it was created will be used for all Index/Search operations.
func Open(path string) (Index, error) {
Expand Down
18 changes: 18 additions & 0 deletions index_alias_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"sync"

"github.com/blevesearch/bleve/document"
"github.com/blevesearch/bleve/index"
"github.com/blevesearch/bleve/index/store"
"github.com/blevesearch/bleve/search"
)

Expand Down Expand Up @@ -298,6 +300,22 @@ func (i *indexAliasImpl) DeleteInternal(key []byte) error {
return i.indexes[0].DeleteInternal(key)
}

func (i *indexAliasImpl) Advanced() (index.Index, store.KVStore, error) {
i.mutex.RLock()
defer i.mutex.RUnlock()

if !i.open {
return nil, nil, ErrorIndexClosed
}

err := i.isAliasToSingleIndex()
if err != nil {
return nil, nil, err
}

return i.indexes[0].Advanced()
}

func (i *indexAliasImpl) Add(indexes ...Index) {
i.mutex.Lock()
defer i.mutex.Unlock()
Expand Down
6 changes: 6 additions & 0 deletions index_alias_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"time"

"github.com/blevesearch/bleve/document"
"github.com/blevesearch/bleve/index"
"github.com/blevesearch/bleve/index/store"
"github.com/blevesearch/bleve/search"
)

Expand Down Expand Up @@ -725,3 +727,7 @@ func (i *stubIndex) SetInternal(key, val []byte) error {
func (i *stubIndex) DeleteInternal(key []byte) error {
return i.err
}

func (i *stubIndex) Advanced() (index.Index, store.KVStore, error) {
return nil, nil, nil
}
27 changes: 20 additions & 7 deletions index_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func newMemIndex(mapping *IndexMapping) (*indexImpl, error) {
return &rv, nil
}

func newIndex(path string, mapping *IndexMapping) (*indexImpl, error) {
func newIndexUsing(path string, mapping *IndexMapping, kvstore string, kvconfig map[string]interface{}) (*indexImpl, error) {
// first validate the mapping
err := mapping.validate()
if err != nil {
Expand All @@ -104,7 +104,7 @@ func newIndex(path string, mapping *IndexMapping) (*indexImpl, error) {
rv := indexImpl{
path: path,
m: mapping,
meta: newIndexMeta(Config.DefaultKVStore),
meta: newIndexMeta(kvstore),
stats: &IndexStat{},
}
storeConstructor := registry.KVStoreConstructorByName(rv.meta.Storage)
Expand All @@ -116,14 +116,13 @@ func newIndex(path string, mapping *IndexMapping) (*indexImpl, error) {
if err != nil {
return nil, err
}
storeConfig := map[string]interface{}{
"path": indexStorePath(path),
"create_if_missing": true,
"error_if_exists": true,
if kvconfig == nil {
kvconfig = map[string]interface{}{}
}
kvconfig["path"] = indexStorePath(path)

// now open the store
rv.s, err = storeConstructor(storeConfig)
rv.s, err = storeConstructor(kvconfig)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -153,6 +152,14 @@ func newIndex(path string, mapping *IndexMapping) (*indexImpl, error) {
return &rv, nil
}

func newIndex(path string, mapping *IndexMapping) (*indexImpl, error) {
defaultKVConfig := map[string]interface{}{
"create_if_missing": true,
"error_if_exists": true,
}
return newIndexUsing(path, mapping, Config.DefaultKVStore, defaultKVConfig)
}

func openIndex(path string) (*indexImpl, error) {

rv := indexImpl{
Expand Down Expand Up @@ -224,6 +231,12 @@ func openIndex(path string) (*indexImpl, error) {
return &rv, nil
}

// Advanced returns implementation internals
// necessary ONLY for advanced usage.
func (i *indexImpl) Advanced() (index.Index, store.KVStore, error) {
return i.i, i.s, nil
}

// Mapping returns the IndexMapping in use by this
// Index.
func (i *indexImpl) Mapping() *IndexMapping {
Expand Down

0 comments on commit 68712cd

Please sign in to comment.