-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support searching with multiple indexes
Signed-off-by: yah01 <[email protected]>
- Loading branch information
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package indexmap | ||
|
||
import ( | ||
"github.com/yah01/container" | ||
"github.com/yah01/indexmap/utils" | ||
) | ||
|
||
const ( | ||
lookupTypeNone lookupType = iota | ||
lookupTypeAnd | ||
lookupTypeOr | ||
lookupTypeExclude | ||
) | ||
|
||
type lookupType int | ||
|
||
type indexLookup struct { | ||
lookupType lookupType | ||
indexName string | ||
keys []any | ||
} | ||
|
||
type SearchStream[K comparable, V any] struct { | ||
chain []*indexLookup | ||
|
||
imap *IndexMap[K, V] | ||
} | ||
|
||
func (stream *SearchStream[K, V]) And(indexName string, keys ...any) *SearchStream[K, V] { | ||
stream.chain = append(stream.chain, &indexLookup{ | ||
lookupType: lookupTypeAnd, | ||
indexName: indexName, | ||
keys: keys, | ||
}) | ||
|
||
return stream | ||
} | ||
|
||
func (stream *SearchStream[K, V]) Or(indexName string, keys ...any) *SearchStream[K, V] { | ||
stream.chain = append(stream.chain, &indexLookup{ | ||
lookupType: lookupTypeOr, | ||
indexName: indexName, | ||
keys: keys, | ||
}) | ||
|
||
return stream | ||
} | ||
|
||
func (stream *SearchStream[K, V]) Exclude(indexName string, keys ...any) *SearchStream[K, V] { | ||
stream.chain = append(stream.chain, &indexLookup{ | ||
lookupType: lookupTypeExclude, | ||
indexName: indexName, | ||
keys: keys, | ||
}) | ||
|
||
return stream | ||
} | ||
|
||
func (stream *SearchStream[K, V]) Excute() []*V { | ||
values := make(container.Set[*V]) | ||
|
||
for _, lookup := range stream.chain { | ||
result := stream.executeLookup(lookup) | ||
|
||
switch lookup.lookupType { | ||
case lookupTypeAnd: | ||
utils.IntersectSet(values, result) | ||
|
||
case lookupTypeOr: | ||
utils.UnionSet(values, result) | ||
|
||
case lookupTypeExclude: | ||
utils.ExceptSet(values, result) | ||
} | ||
} | ||
|
||
return values.Collect() | ||
} | ||
|
||
func (stream *SearchStream[K, V]) executeLookup(lookup *indexLookup) container.Set[*V] { | ||
result := make(container.Set[*V]) | ||
for i := range lookup.keys { | ||
utils.UnionSet(result, | ||
stream.imap.getAllBy(lookup.indexName, lookup.keys[i])) | ||
} | ||
|
||
return result | ||
} | ||
|
||
func (imap *IndexMap[K, V]) Search() *SearchStream[K, V] { | ||
return &SearchStream[K, V]{ | ||
chain: make([]*indexLookup, 0), | ||
imap: imap, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package indexmap | ||
|
||
import "testing" | ||
|
||
func TestSearch(t *testing.T) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package utils | ||
|
||
import "github.com/yah01/container" | ||
|
||
func UnionSet[T comparable](dst container.Set[T], src container.Set[T]) { | ||
for elem := range src { | ||
dst.Insert(elem) | ||
} | ||
} | ||
|
||
func IntersectSet[T comparable](dst container.Set[T], src container.Set[T]) { | ||
for elem := range dst { | ||
if !src.Contain(elem) { | ||
delete(dst, elem) | ||
} | ||
} | ||
} | ||
|
||
func ExceptSet[T comparable](dst container.Set[T], src container.Set[T]) { | ||
for elem := range src { | ||
delete(dst, elem) | ||
} | ||
} |