another round of golint cleanup
This commit is contained in:
parent
e1b77956d4
commit
e21935f850
@ -18,28 +18,28 @@ type op struct {
|
|||||||
v []byte
|
v []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
type BoltDBBatch struct {
|
type Batch struct {
|
||||||
store *BoltDBStore
|
store *Store
|
||||||
ops []op
|
ops []op
|
||||||
}
|
}
|
||||||
|
|
||||||
func newBoltDBBatch(store *BoltDBStore) *BoltDBBatch {
|
func newBatch(store *Store) *Batch {
|
||||||
rv := BoltDBBatch{
|
rv := Batch{
|
||||||
store: store,
|
store: store,
|
||||||
ops: make([]op, 0),
|
ops: make([]op, 0),
|
||||||
}
|
}
|
||||||
return &rv
|
return &rv
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *BoltDBBatch) Set(key, val []byte) {
|
func (i *Batch) Set(key, val []byte) {
|
||||||
i.ops = append(i.ops, op{key, val})
|
i.ops = append(i.ops, op{key, val})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *BoltDBBatch) Delete(key []byte) {
|
func (i *Batch) Delete(key []byte) {
|
||||||
i.ops = append(i.ops, op{key, nil})
|
i.ops = append(i.ops, op{key, nil})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *BoltDBBatch) Execute() error {
|
func (i *Batch) Execute() error {
|
||||||
return i.store.db.Update(func(tx *bolt.Tx) error {
|
return i.store.db.Update(func(tx *bolt.Tx) error {
|
||||||
b := tx.Bucket([]byte(i.store.bucket))
|
b := tx.Bucket([]byte(i.store.bucket))
|
||||||
|
|
||||||
@ -59,6 +59,6 @@ func (i *BoltDBBatch) Execute() error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *BoltDBBatch) Close() error {
|
func (i *Batch) Close() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -13,8 +13,8 @@ import (
|
|||||||
"github.com/boltdb/bolt"
|
"github.com/boltdb/bolt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type BoltDBIterator struct {
|
type Iterator struct {
|
||||||
store *BoltDBStore
|
store *Store
|
||||||
tx *bolt.Tx
|
tx *bolt.Tx
|
||||||
cursor *bolt.Cursor
|
cursor *bolt.Cursor
|
||||||
valid bool
|
valid bool
|
||||||
@ -22,52 +22,52 @@ type BoltDBIterator struct {
|
|||||||
val []byte
|
val []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func newBoltDBIterator(store *BoltDBStore) *BoltDBIterator {
|
func newIterator(store *Store) *Iterator {
|
||||||
tx, _ := store.db.Begin(false)
|
tx, _ := store.db.Begin(false)
|
||||||
b := tx.Bucket([]byte(store.bucket))
|
b := tx.Bucket([]byte(store.bucket))
|
||||||
cursor := b.Cursor()
|
cursor := b.Cursor()
|
||||||
|
|
||||||
return &BoltDBIterator{
|
return &Iterator{
|
||||||
store: store,
|
store: store,
|
||||||
tx: tx,
|
tx: tx,
|
||||||
cursor: cursor,
|
cursor: cursor,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *BoltDBIterator) SeekFirst() {
|
func (i *Iterator) SeekFirst() {
|
||||||
i.key, i.val = i.cursor.First()
|
i.key, i.val = i.cursor.First()
|
||||||
|
|
||||||
i.valid = (i.key != nil)
|
i.valid = (i.key != nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *BoltDBIterator) Seek(k []byte) {
|
func (i *Iterator) Seek(k []byte) {
|
||||||
i.key, i.val = i.cursor.Seek(k)
|
i.key, i.val = i.cursor.Seek(k)
|
||||||
|
|
||||||
i.valid = (i.key != nil)
|
i.valid = (i.key != nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *BoltDBIterator) Next() {
|
func (i *Iterator) Next() {
|
||||||
i.key, i.val = i.cursor.Next()
|
i.key, i.val = i.cursor.Next()
|
||||||
|
|
||||||
i.valid = (i.key != nil)
|
i.valid = (i.key != nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *BoltDBIterator) Current() ([]byte, []byte, bool) {
|
func (i *Iterator) Current() ([]byte, []byte, bool) {
|
||||||
return i.key, i.val, i.valid
|
return i.key, i.val, i.valid
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *BoltDBIterator) Key() []byte {
|
func (i *Iterator) Key() []byte {
|
||||||
return i.key
|
return i.key
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *BoltDBIterator) Value() []byte {
|
func (i *Iterator) Value() []byte {
|
||||||
return i.val
|
return i.val
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *BoltDBIterator) Valid() bool {
|
func (i *Iterator) Valid() bool {
|
||||||
return i.valid
|
return i.valid
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *BoltDBIterator) Close() {
|
func (i *Iterator) Close() {
|
||||||
i.tx.Rollback()
|
i.tx.Rollback()
|
||||||
}
|
}
|
||||||
|
@ -19,14 +19,14 @@ import (
|
|||||||
|
|
||||||
const Name = "boltdb"
|
const Name = "boltdb"
|
||||||
|
|
||||||
type BoltDBStore struct {
|
type Store struct {
|
||||||
path string
|
path string
|
||||||
bucket string
|
bucket string
|
||||||
db *bolt.DB
|
db *bolt.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func Open(path string, bucket string) (*BoltDBStore, error) {
|
func Open(path string, bucket string) (*Store, error) {
|
||||||
rv := BoltDBStore{
|
rv := Store{
|
||||||
path: path,
|
path: path,
|
||||||
bucket: bucket,
|
bucket: bucket,
|
||||||
}
|
}
|
||||||
@ -49,7 +49,7 @@ func Open(path string, bucket string) (*BoltDBStore, error) {
|
|||||||
return &rv, nil
|
return &rv, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bs *BoltDBStore) Get(key []byte) ([]byte, error) {
|
func (bs *Store) Get(key []byte) ([]byte, error) {
|
||||||
var rv []byte
|
var rv []byte
|
||||||
|
|
||||||
err := bs.db.View(func(tx *bolt.Tx) error {
|
err := bs.db.View(func(tx *bolt.Tx) error {
|
||||||
@ -61,34 +61,34 @@ func (bs *BoltDBStore) Get(key []byte) ([]byte, error) {
|
|||||||
return rv, err
|
return rv, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bs *BoltDBStore) Set(key, val []byte) error {
|
func (bs *Store) Set(key, val []byte) error {
|
||||||
return bs.db.Update(func(tx *bolt.Tx) error {
|
return bs.db.Update(func(tx *bolt.Tx) error {
|
||||||
return tx.Bucket([]byte(bs.bucket)).Put(key, val)
|
return tx.Bucket([]byte(bs.bucket)).Put(key, val)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bs *BoltDBStore) Delete(key []byte) error {
|
func (bs *Store) Delete(key []byte) error {
|
||||||
return bs.db.Update(func(tx *bolt.Tx) error {
|
return bs.db.Update(func(tx *bolt.Tx) error {
|
||||||
return tx.Bucket([]byte(bs.bucket)).Delete(key)
|
return tx.Bucket([]byte(bs.bucket)).Delete(key)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bs *BoltDBStore) Commit() error {
|
func (bs *Store) Commit() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bs *BoltDBStore) Close() error {
|
func (bs *Store) Close() error {
|
||||||
return bs.db.Close()
|
return bs.db.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bs *BoltDBStore) Iterator(key []byte) store.KVIterator {
|
func (bs *Store) Iterator(key []byte) store.KVIterator {
|
||||||
rv := newBoltDBIterator(bs)
|
rv := newIterator(bs)
|
||||||
rv.Seek(key)
|
rv.Seek(key)
|
||||||
return rv
|
return rv
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bs *BoltDBStore) NewBatch() store.KVBatch {
|
func (bs *Store) NewBatch() store.KVBatch {
|
||||||
return newBoltDBBatch(bs)
|
return newBatch(bs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func StoreConstructor(config map[string]interface{}) (store.KVStore, error) {
|
func StoreConstructor(config map[string]interface{}) (store.KVStore, error) {
|
||||||
|
@ -16,7 +16,7 @@ import (
|
|||||||
"github.com/blevesearch/bleve/index/store/test"
|
"github.com/blevesearch/bleve/index/store/test"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestBoltDBStore(t *testing.T) {
|
func TestStore(t *testing.T) {
|
||||||
s, err := Open("test", "bleve")
|
s, err := Open("test", "bleve")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -9,14 +9,14 @@
|
|||||||
|
|
||||||
package inmem
|
package inmem
|
||||||
|
|
||||||
type InMemBatch struct {
|
type Batch struct {
|
||||||
store *InMemStore
|
store *Store
|
||||||
keys [][]byte
|
keys [][]byte
|
||||||
vals [][]byte
|
vals [][]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func newInMemBatch(store *InMemStore) *InMemBatch {
|
func newBatch(store *Store) *Batch {
|
||||||
rv := InMemBatch{
|
rv := Batch{
|
||||||
store: store,
|
store: store,
|
||||||
keys: make([][]byte, 0),
|
keys: make([][]byte, 0),
|
||||||
vals: make([][]byte, 0),
|
vals: make([][]byte, 0),
|
||||||
@ -24,17 +24,17 @@ func newInMemBatch(store *InMemStore) *InMemBatch {
|
|||||||
return &rv
|
return &rv
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InMemBatch) Set(key, val []byte) {
|
func (i *Batch) Set(key, val []byte) {
|
||||||
i.keys = append(i.keys, key)
|
i.keys = append(i.keys, key)
|
||||||
i.vals = append(i.vals, val)
|
i.vals = append(i.vals, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InMemBatch) Delete(key []byte) {
|
func (i *Batch) Delete(key []byte) {
|
||||||
i.keys = append(i.keys, key)
|
i.keys = append(i.keys, key)
|
||||||
i.vals = append(i.vals, nil)
|
i.vals = append(i.vals, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InMemBatch) Execute() error {
|
func (i *Batch) Execute() error {
|
||||||
for index, key := range i.keys {
|
for index, key := range i.keys {
|
||||||
val := i.vals[index]
|
val := i.vals[index]
|
||||||
if val == nil {
|
if val == nil {
|
||||||
@ -46,6 +46,6 @@ func (i *InMemBatch) Execute() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InMemBatch) Close() error {
|
func (i *Batch) Close() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -13,57 +13,57 @@ import (
|
|||||||
"github.com/ryszard/goskiplist/skiplist"
|
"github.com/ryszard/goskiplist/skiplist"
|
||||||
)
|
)
|
||||||
|
|
||||||
type InMemIterator struct {
|
type Iterator struct {
|
||||||
store *InMemStore
|
store *Store
|
||||||
iterator skiplist.Iterator
|
iterator skiplist.Iterator
|
||||||
valid bool
|
valid bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newInMemIterator(store *InMemStore) *InMemIterator {
|
func newIterator(store *Store) *Iterator {
|
||||||
rv := InMemIterator{
|
rv := Iterator{
|
||||||
store: store,
|
store: store,
|
||||||
iterator: store.list.Iterator(),
|
iterator: store.list.Iterator(),
|
||||||
}
|
}
|
||||||
return &rv
|
return &rv
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InMemIterator) SeekFirst() {
|
func (i *Iterator) SeekFirst() {
|
||||||
i.Seek([]byte{0})
|
i.Seek([]byte{0})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InMemIterator) Seek(k []byte) {
|
func (i *Iterator) Seek(k []byte) {
|
||||||
i.valid = i.iterator.Seek(string(k))
|
i.valid = i.iterator.Seek(string(k))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InMemIterator) Next() {
|
func (i *Iterator) Next() {
|
||||||
i.valid = i.iterator.Next()
|
i.valid = i.iterator.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InMemIterator) Current() ([]byte, []byte, bool) {
|
func (i *Iterator) Current() ([]byte, []byte, bool) {
|
||||||
if i.valid {
|
if i.valid {
|
||||||
return []byte(i.Key()), []byte(i.Value()), true
|
return []byte(i.Key()), []byte(i.Value()), true
|
||||||
}
|
}
|
||||||
return nil, nil, false
|
return nil, nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InMemIterator) Key() []byte {
|
func (i *Iterator) Key() []byte {
|
||||||
if i.valid {
|
if i.valid {
|
||||||
return []byte(i.iterator.Key().(string))
|
return []byte(i.iterator.Key().(string))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InMemIterator) Value() []byte {
|
func (i *Iterator) Value() []byte {
|
||||||
if i.valid {
|
if i.valid {
|
||||||
return []byte(i.iterator.Value().(string))
|
return []byte(i.iterator.Value().(string))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InMemIterator) Valid() bool {
|
func (i *Iterator) Valid() bool {
|
||||||
return i.valid
|
return i.valid
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InMemIterator) Close() {
|
func (i *Iterator) Close() {
|
||||||
i.iterator.Close()
|
i.iterator.Close()
|
||||||
}
|
}
|
||||||
|
@ -17,27 +17,27 @@ import (
|
|||||||
|
|
||||||
const Name = "mem"
|
const Name = "mem"
|
||||||
|
|
||||||
type InMemStore struct {
|
type Store struct {
|
||||||
list *skiplist.SkipList
|
list *skiplist.SkipList
|
||||||
}
|
}
|
||||||
|
|
||||||
func Open() (*InMemStore, error) {
|
func Open() (*Store, error) {
|
||||||
rv := InMemStore{
|
rv := Store{
|
||||||
list: skiplist.NewStringMap(),
|
list: skiplist.NewStringMap(),
|
||||||
}
|
}
|
||||||
|
|
||||||
return &rv, nil
|
return &rv, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func MustOpen() *InMemStore {
|
func MustOpen() *Store {
|
||||||
rv := InMemStore{
|
rv := Store{
|
||||||
list: skiplist.NewStringMap(),
|
list: skiplist.NewStringMap(),
|
||||||
}
|
}
|
||||||
|
|
||||||
return &rv
|
return &rv
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InMemStore) Get(key []byte) ([]byte, error) {
|
func (i *Store) Get(key []byte) ([]byte, error) {
|
||||||
val, ok := i.list.Get(string(key))
|
val, ok := i.list.Get(string(key))
|
||||||
if ok {
|
if ok {
|
||||||
return []byte(val.(string)), nil
|
return []byte(val.(string)), nil
|
||||||
@ -45,32 +45,32 @@ func (i *InMemStore) Get(key []byte) ([]byte, error) {
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InMemStore) Set(key, val []byte) error {
|
func (i *Store) Set(key, val []byte) error {
|
||||||
i.list.Set(string(key), string(val))
|
i.list.Set(string(key), string(val))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InMemStore) Delete(key []byte) error {
|
func (i *Store) Delete(key []byte) error {
|
||||||
i.list.Delete(string(key))
|
i.list.Delete(string(key))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InMemStore) Commit() error {
|
func (i *Store) Commit() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InMemStore) Close() error {
|
func (i *Store) Close() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InMemStore) Iterator(key []byte) store.KVIterator {
|
func (i *Store) Iterator(key []byte) store.KVIterator {
|
||||||
rv := newInMemIterator(i)
|
rv := newIterator(i)
|
||||||
rv.Seek(key)
|
rv.Seek(key)
|
||||||
return rv
|
return rv
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InMemStore) NewBatch() store.KVBatch {
|
func (i *Store) NewBatch() store.KVBatch {
|
||||||
return newInMemBatch(i)
|
return newBatch(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
func StoreConstructor(config map[string]interface{}) (store.KVStore, error) {
|
func StoreConstructor(config map[string]interface{}) (store.KVStore, error) {
|
||||||
|
@ -15,7 +15,7 @@ import (
|
|||||||
"github.com/blevesearch/bleve/index/store/test"
|
"github.com/blevesearch/bleve/index/store/test"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestInMemStore(t *testing.T) {
|
func TestStore(t *testing.T) {
|
||||||
s, err := Open()
|
s, err := Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -19,17 +19,17 @@ const Name = "ansi"
|
|||||||
|
|
||||||
const DefaultAnsiHighlight = bgYellow
|
const DefaultAnsiHighlight = bgYellow
|
||||||
|
|
||||||
type ANSIFragmentFormatter struct {
|
type FragmentFormatter struct {
|
||||||
color string
|
color string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewANSIFragmentFormatter() *ANSIFragmentFormatter {
|
func NewFragmentFormatter() *FragmentFormatter {
|
||||||
return &ANSIFragmentFormatter{
|
return &FragmentFormatter{
|
||||||
color: DefaultAnsiHighlight,
|
color: DefaultAnsiHighlight,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ANSIFragmentFormatter) Format(f *highlight.Fragment, tlm search.TermLocationMap) string {
|
func (a *FragmentFormatter) Format(f *highlight.Fragment, tlm search.TermLocationMap) string {
|
||||||
orderedTermLocations := highlight.OrderTermLocations(tlm)
|
orderedTermLocations := highlight.OrderTermLocations(tlm)
|
||||||
rv := ""
|
rv := ""
|
||||||
curr := f.Start
|
curr := f.Start
|
||||||
@ -86,7 +86,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Constructor(config map[string]interface{}, cache *registry.Cache) (highlight.FragmentFormatter, error) {
|
func Constructor(config map[string]interface{}, cache *registry.Cache) (highlight.FragmentFormatter, error) {
|
||||||
return NewANSIFragmentFormatter(), nil
|
return NewFragmentFormatter(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -20,19 +20,19 @@ const Name = "html"
|
|||||||
const defaultHTMLHighlightBefore = "<b>"
|
const defaultHTMLHighlightBefore = "<b>"
|
||||||
const defaultHTMLHighlightAfter = "</b>"
|
const defaultHTMLHighlightAfter = "</b>"
|
||||||
|
|
||||||
type HTMLFragmentFormatter struct {
|
type FragmentFormatter struct {
|
||||||
before string
|
before string
|
||||||
after string
|
after string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTMLFragmentFormatter(before, after string) *HTMLFragmentFormatter {
|
func NewFragmentFormatter(before, after string) *FragmentFormatter {
|
||||||
return &HTMLFragmentFormatter{
|
return &FragmentFormatter{
|
||||||
before: before,
|
before: before,
|
||||||
after: after,
|
after: after,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *HTMLFragmentFormatter) Format(f *highlight.Fragment, tlm search.TermLocationMap) string {
|
func (a *FragmentFormatter) Format(f *highlight.Fragment, tlm search.TermLocationMap) string {
|
||||||
orderedTermLocations := highlight.OrderTermLocations(tlm)
|
orderedTermLocations := highlight.OrderTermLocations(tlm)
|
||||||
rv := ""
|
rv := ""
|
||||||
curr := f.Start
|
curr := f.Start
|
||||||
@ -71,7 +71,7 @@ func Constructor(config map[string]interface{}, cache *registry.Cache) (highligh
|
|||||||
if ok {
|
if ok {
|
||||||
after = afterVal
|
after = afterVal
|
||||||
}
|
}
|
||||||
return NewHTMLFragmentFormatter(before, after), nil
|
return NewFragmentFormatter(before, after), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -41,7 +41,7 @@ func TestHTMLFragmentFormatter1(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
emHTMLFormatter := NewHTMLFragmentFormatter("<b>", "</b>")
|
emHTMLFormatter := NewFragmentFormatter("<b>", "</b>")
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
result := emHTMLFormatter.Format(test.fragment, test.tlm)
|
result := emHTMLFormatter.Format(test.fragment, test.tlm)
|
||||||
if result != test.output {
|
if result != test.output {
|
||||||
@ -75,7 +75,7 @@ func TestHTMLFragmentFormatter2(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
emHTMLFormatter := NewHTMLFragmentFormatter("<em>", "</em>")
|
emHTMLFormatter := NewFragmentFormatter("<em>", "</em>")
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
result := emHTMLFormatter.Format(test.fragment, test.tlm)
|
result := emHTMLFormatter.Format(test.fragment, test.tlm)
|
||||||
if result != test.output {
|
if result != test.output {
|
||||||
|
@ -18,17 +18,17 @@ const Name = "simple"
|
|||||||
|
|
||||||
const defaultFragmentSize = 200
|
const defaultFragmentSize = 200
|
||||||
|
|
||||||
type SimpleFragmenter struct {
|
type Fragmenter struct {
|
||||||
fragmentSize int
|
fragmentSize int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSimpleFragmenter(fragmentSize int) *SimpleFragmenter {
|
func NewFragmenter(fragmentSize int) *Fragmenter {
|
||||||
return &SimpleFragmenter{
|
return &Fragmenter{
|
||||||
fragmentSize: fragmentSize,
|
fragmentSize: fragmentSize,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SimpleFragmenter) Fragment(orig []byte, ot highlight.TermLocations) []*highlight.Fragment {
|
func (s *Fragmenter) Fragment(orig []byte, ot highlight.TermLocations) []*highlight.Fragment {
|
||||||
rv := make([]*highlight.Fragment, 0)
|
rv := make([]*highlight.Fragment, 0)
|
||||||
|
|
||||||
maxbegin := 0
|
maxbegin := 0
|
||||||
@ -91,7 +91,7 @@ func Constructor(config map[string]interface{}, cache *registry.Cache) (highligh
|
|||||||
if ok {
|
if ok {
|
||||||
size = int(sizeVal)
|
size = int(sizeVal)
|
||||||
}
|
}
|
||||||
return NewSimpleFragmenter(size), nil
|
return NewFragmenter(size), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -178,7 +178,7 @@ func TestSimpleFragmenter(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
fragmenter := NewSimpleFragmenter(100)
|
fragmenter := NewFragmenter(100)
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
fragments := fragmenter.Fragment(test.orig, test.ot)
|
fragments := fragmenter.Fragment(test.orig, test.ot)
|
||||||
if !reflect.DeepEqual(fragments, test.fragments) {
|
if !reflect.DeepEqual(fragments, test.fragments) {
|
||||||
@ -228,7 +228,7 @@ func TestSimpleFragmenterWithSize(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
fragmenter := NewSimpleFragmenter(5)
|
fragmenter := NewFragmenter(5)
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
fragments := fragmenter.Fragment(test.orig, test.ot)
|
fragments := fragmenter.Fragment(test.orig, test.ot)
|
||||||
if !reflect.DeepEqual(fragments, test.fragments) {
|
if !reflect.DeepEqual(fragments, test.fragments) {
|
||||||
|
@ -14,20 +14,20 @@ import (
|
|||||||
"github.com/blevesearch/bleve/search/highlight"
|
"github.com/blevesearch/bleve/search/highlight"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SimpleFragmentScorer will score fragments by how many
|
// FragmentScorer will score fragments by how many
|
||||||
// unique terms occur in the fragment with no regard for
|
// unique terms occur in the fragment with no regard for
|
||||||
// any boost values used in the original query
|
// any boost values used in the original query
|
||||||
type SimpleFragmentScorer struct {
|
type FragmentScorer struct {
|
||||||
tlm search.TermLocationMap
|
tlm search.TermLocationMap
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSimpleFragmentScorer(tlm search.TermLocationMap) *SimpleFragmentScorer {
|
func NewFragmentScorer(tlm search.TermLocationMap) *FragmentScorer {
|
||||||
return &SimpleFragmentScorer{
|
return &FragmentScorer{
|
||||||
tlm: tlm,
|
tlm: tlm,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SimpleFragmentScorer) Score(f *highlight.Fragment) {
|
func (s *FragmentScorer) Score(f *highlight.Fragment) {
|
||||||
score := 0.0
|
score := 0.0
|
||||||
OUTER:
|
OUTER:
|
||||||
for _, locations := range s.tlm {
|
for _, locations := range s.tlm {
|
||||||
|
@ -67,7 +67,7 @@ func TestSimpleFragmentScorer(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
scorer := NewSimpleFragmentScorer(test.tlm)
|
scorer := NewFragmentScorer(test.tlm)
|
||||||
scorer.Score(test.fragment)
|
scorer.Score(test.fragment)
|
||||||
if test.fragment.Score != test.score {
|
if test.fragment.Score != test.score {
|
||||||
t.Errorf("expected score %f, got %f", test.score, test.fragment.Score)
|
t.Errorf("expected score %f, got %f", test.score, test.fragment.Score)
|
||||||
|
@ -22,45 +22,45 @@ import (
|
|||||||
const Name = "simple"
|
const Name = "simple"
|
||||||
const defaultSeparator = "…"
|
const defaultSeparator = "…"
|
||||||
|
|
||||||
type SimpleHighlighter struct {
|
type Highlighter struct {
|
||||||
fragmenter highlight.Fragmenter
|
fragmenter highlight.Fragmenter
|
||||||
formatter highlight.FragmentFormatter
|
formatter highlight.FragmentFormatter
|
||||||
sep string
|
sep string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSimpleHighlighter(fragmenter highlight.Fragmenter, formatter highlight.FragmentFormatter, separator string) *SimpleHighlighter {
|
func NewHighlighter(fragmenter highlight.Fragmenter, formatter highlight.FragmentFormatter, separator string) *Highlighter {
|
||||||
return &SimpleHighlighter{
|
return &Highlighter{
|
||||||
fragmenter: fragmenter,
|
fragmenter: fragmenter,
|
||||||
formatter: formatter,
|
formatter: formatter,
|
||||||
sep: separator,
|
sep: separator,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SimpleHighlighter) Fragmenter() highlight.Fragmenter {
|
func (s *Highlighter) Fragmenter() highlight.Fragmenter {
|
||||||
return s.fragmenter
|
return s.fragmenter
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SimpleHighlighter) SetFragmenter(f highlight.Fragmenter) {
|
func (s *Highlighter) SetFragmenter(f highlight.Fragmenter) {
|
||||||
s.fragmenter = f
|
s.fragmenter = f
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SimpleHighlighter) FragmentFormatter() highlight.FragmentFormatter {
|
func (s *Highlighter) FragmentFormatter() highlight.FragmentFormatter {
|
||||||
return s.formatter
|
return s.formatter
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SimpleHighlighter) SetFragmentFormatter(f highlight.FragmentFormatter) {
|
func (s *Highlighter) SetFragmentFormatter(f highlight.FragmentFormatter) {
|
||||||
s.formatter = f
|
s.formatter = f
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SimpleHighlighter) Separator() string {
|
func (s *Highlighter) Separator() string {
|
||||||
return s.sep
|
return s.sep
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SimpleHighlighter) SetSeparator(sep string) {
|
func (s *Highlighter) SetSeparator(sep string) {
|
||||||
s.sep = sep
|
s.sep = sep
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SimpleHighlighter) BestFragmentInField(dm *search.DocumentMatch, doc *document.Document, field string) string {
|
func (s *Highlighter) BestFragmentInField(dm *search.DocumentMatch, doc *document.Document, field string) string {
|
||||||
fragments := s.BestFragmentsInField(dm, doc, field, 1)
|
fragments := s.BestFragmentsInField(dm, doc, field, 1)
|
||||||
if len(fragments) > 0 {
|
if len(fragments) > 0 {
|
||||||
return fragments[0]
|
return fragments[0]
|
||||||
@ -68,10 +68,10 @@ func (s *SimpleHighlighter) BestFragmentInField(dm *search.DocumentMatch, doc *d
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SimpleHighlighter) BestFragmentsInField(dm *search.DocumentMatch, doc *document.Document, field string, num int) []string {
|
func (s *Highlighter) BestFragmentsInField(dm *search.DocumentMatch, doc *document.Document, field string, num int) []string {
|
||||||
tlm := dm.Locations[field]
|
tlm := dm.Locations[field]
|
||||||
orderedTermLocations := highlight.OrderTermLocations(tlm)
|
orderedTermLocations := highlight.OrderTermLocations(tlm)
|
||||||
scorer := NewSimpleFragmentScorer(dm.Locations[field])
|
scorer := NewFragmentScorer(dm.Locations[field])
|
||||||
|
|
||||||
// score the fragments and put them into a priority queue ordered by score
|
// score the fragments and put them into a priority queue ordered by score
|
||||||
fq := make(FragmentQueue, 0)
|
fq := make(FragmentQueue, 0)
|
||||||
@ -197,7 +197,7 @@ func Constructor(config map[string]interface{}, cache *registry.Cache) (highligh
|
|||||||
return nil, fmt.Errorf("error building fragment formatter: %v", err)
|
return nil, fmt.Errorf("error building fragment formatter: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewSimpleHighlighter(fragmenter, formatter, separator), nil
|
return NewHighlighter(fragmenter, formatter, separator), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -25,9 +25,9 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestSimpleHighlighter(t *testing.T) {
|
func TestSimpleHighlighter(t *testing.T) {
|
||||||
fragmenter := sfrag.NewSimpleFragmenter(100)
|
fragmenter := sfrag.NewFragmenter(100)
|
||||||
formatter := ansi.NewANSIFragmentFormatter()
|
formatter := ansi.NewFragmentFormatter()
|
||||||
highlighter := NewSimpleHighlighter(fragmenter, formatter, defaultSeparator)
|
highlighter := NewHighlighter(fragmenter, formatter, defaultSeparator)
|
||||||
|
|
||||||
docMatch := search.DocumentMatch{
|
docMatch := search.DocumentMatch{
|
||||||
ID: "a",
|
ID: "a",
|
||||||
@ -152,9 +152,9 @@ Etiam vel augue vel nisl commodo suscipit et ac nisl. Quisque eros diam, porttit
|
|||||||
"… accumsan. Vivamus eros felis, rhoncus vel " + DefaultAnsiHighlight + "interdum" + reset + " bibendum, imperdiet nec diam. Etiam sed eros sed…",
|
"… accumsan. Vivamus eros felis, rhoncus vel " + DefaultAnsiHighlight + "interdum" + reset + " bibendum, imperdiet nec diam. Etiam sed eros sed…",
|
||||||
}
|
}
|
||||||
|
|
||||||
fragmenter := sfrag.NewSimpleFragmenter(100)
|
fragmenter := sfrag.NewFragmenter(100)
|
||||||
formatter := ansi.NewANSIFragmentFormatter()
|
formatter := ansi.NewFragmentFormatter()
|
||||||
highlighter := NewSimpleHighlighter(fragmenter, formatter, defaultSeparator)
|
highlighter := NewHighlighter(fragmenter, formatter, defaultSeparator)
|
||||||
fragments := highlighter.BestFragmentsInField(&docMatch, doc, "full", 5)
|
fragments := highlighter.BestFragmentsInField(&docMatch, doc, "full", 5)
|
||||||
|
|
||||||
if !reflect.DeepEqual(fragments, expectedFragments) {
|
if !reflect.DeepEqual(fragments, expectedFragments) {
|
||||||
|
Loading…
Reference in New Issue
Block a user