github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/index/memindex.go (about)

     1  /*
     2  Copyright 2011 Google Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package index
    18  
    19  import (
    20  	"camlistore.org/pkg/blobserver"
    21  	"camlistore.org/pkg/jsonconfig"
    22  	"camlistore.org/pkg/sorted"
    23  )
    24  
    25  func init() {
    26  	blobserver.RegisterStorageConstructor("memory-only-dev-indexer",
    27  		blobserver.StorageConstructor(newMemoryIndexFromConfig))
    28  }
    29  
    30  // NewMemoryIndex returns an Index backed only by memory, for use in tests.
    31  func NewMemoryIndex() *Index {
    32  	ix, err := New(sorted.NewMemoryKeyValue())
    33  	if err != nil {
    34  		// Nothing to fail in memory, so worth panicing about
    35  		// if we ever see something.
    36  		panic(err)
    37  	}
    38  	return ix
    39  }
    40  
    41  func newMemoryIndexFromConfig(ld blobserver.Loader, config jsonconfig.Obj) (blobserver.Storage, error) {
    42  	blobPrefix := config.RequiredString("blobSource")
    43  	if err := config.Validate(); err != nil {
    44  		return nil, err
    45  	}
    46  	sto, err := ld.GetStorage(blobPrefix)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  
    51  	ix := NewMemoryIndex()
    52  	ix.BlobSource = sto
    53  
    54  	// Good enough, for now:
    55  	ix.KeyFetcher = ix.BlobSource
    56  
    57  	return ix, err
    58  }