tracker/server/store/string_store.go

65 lines
2 KiB
Go
Raw Normal View History

2016-03-03 09:39:19 +01:00
// Copyright 2016 The Chihaya Authors. All rights reserved.
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package store
import (
"fmt"
"github.com/chihaya/chihaya/pkg/stopper"
)
2016-03-03 09:39:19 +01:00
var stringStoreDrivers = make(map[string]StringStoreDriver)
// StringStore represents an interface for manipulating strings.
type StringStore interface {
// PutString adds the given string to the StringStore.
2016-03-03 09:39:19 +01:00
PutString(s string) error
// HasString returns whether or not the StringStore contains the given
// string.
2016-03-03 09:39:19 +01:00
HasString(s string) (bool, error)
// RemoveString removes the string from the string store.
// Returns ErrResourceDoesNotExist if the given string is not contained
// in the store.
2016-03-03 09:39:19 +01:00
RemoveString(s string) error
// Stopper provides the Stop method that stops the StringStore.
// Stop should shut down the StringStore in a separate goroutine and send
// an error to the channel if the shutdown failed. If the shutdown
// was successful, the channel is to be closed.
stopper.Stopper
2016-03-03 09:39:19 +01:00
}
// StringStoreDriver represents an interface for creating a handle to the
2016-04-07 16:24:37 +02:00
// storage of strings.
2016-03-03 09:39:19 +01:00
type StringStoreDriver interface {
2016-03-11 21:09:49 +01:00
New(*DriverConfig) (StringStore, error)
2016-03-03 09:39:19 +01:00
}
// RegisterStringStoreDriver makes a driver available by the provided name.
//
// If this function is called twice with the same name or if the driver is nil,
// it panics.
func RegisterStringStoreDriver(name string, driver StringStoreDriver) {
if driver == nil {
panic("store: could not register nil StringStoreDriver")
}
if _, dup := stringStoreDrivers[name]; dup {
panic("store: could not register duplicate StringStoreDriver: " + name)
}
stringStoreDrivers[name] = driver
}
// OpenStringStore returns a StringStore specified by a configuration.
2016-03-11 21:09:49 +01:00
func OpenStringStore(cfg *DriverConfig) (StringStore, error) {
driver, ok := stringStoreDrivers[cfg.Name]
2016-03-03 09:39:19 +01:00
if !ok {
return nil, fmt.Errorf("store: unknown StringStoreDriver %q (forgotten import?)", cfg)
2016-03-03 09:39:19 +01:00
}
return driver.New(cfg)
}