Home Download Docs Code Community
     1	/*
     2	Copyright 2014 The Perkeep Authors
     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 test
    18	
    19	import (
    20		"errors"
    21		"log"
    22		"strings"
    23		"sync"
    24	
    25		"perkeep.org/pkg/blobserver"
    26	)
    27	
    28	// NewLoader
    29	func NewLoader() *Loader {
    30		return &Loader{}
    31	}
    32	
    33	type Loader struct {
    34		mu  sync.Mutex
    35		sto map[string]blobserver.Storage
    36	}
    37	
    38	var _ blobserver.Loader = (*Loader)(nil)
    39	
    40	func (ld *Loader) FindHandlerByType(handlerType string) (prefix string, handler interface{}, err error) {
    41		panic("NOIMPL")
    42	}
    43	
    44	func (ld *Loader) AllHandlers() (map[string]string, map[string]interface{}) {
    45		panic("NOIMPL")
    46	}
    47	
    48	func (ld *Loader) MyPrefix() string {
    49		return "/lies/"
    50	}
    51	
    52	func (ld *Loader) BaseURL() string {
    53		return "http://localhost:1234"
    54	}
    55	
    56	func (ld *Loader) GetHandlerType(prefix string) string {
    57		log.Printf("test.Loader: GetHandlerType called but not implemented.")
    58		return ""
    59	}
    60	
    61	func (ld *Loader) GetHandler(prefix string) (interface{}, error) {
    62		log.Printf("test.Loader: GetHandler called but not implemented.")
    63		return nil, errors.New("doesn't exist")
    64	}
    65	
    66	func (ld *Loader) SetStorage(prefix string, s blobserver.Storage) {
    67		ld.mu.Lock()
    68		defer ld.mu.Unlock()
    69		if ld.sto == nil {
    70			ld.sto = make(map[string]blobserver.Storage)
    71		}
    72		ld.sto[prefix] = s
    73	}
    74	
    75	func (ld *Loader) GetStorage(prefix string) (blobserver.Storage, error) {
    76		ld.mu.Lock()
    77		defer ld.mu.Unlock()
    78		if bs, ok := ld.sto[prefix]; ok {
    79			return bs, nil
    80		}
    81		if ld.sto == nil {
    82			ld.sto = make(map[string]blobserver.Storage)
    83		}
    84		sto, err := ld.genStorage(prefix)
    85		if err != nil {
    86			return nil, err
    87		}
    88		ld.sto[prefix] = sto
    89		return sto, nil
    90	}
    91	
    92	func (ld *Loader) genStorage(prefix string) (blobserver.Storage, error) {
    93		if strings.HasPrefix(prefix, "/good") {
    94			return &Fetcher{}, nil
    95		}
    96		if strings.HasPrefix(prefix, "/fail") {
    97			return &Fetcher{ReceiveErr: errors.New("test.Loader intentional failure for /fail storage handler")}, nil
    98		}
    99		panic("test.Loader.GetStorage: unrecognized prefix type")
   100	}
Website layout inspired by memcached.
Content by the authors.