-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added: Implement AccountClient.CopyIndex
- Loading branch information
Showing
4 changed files
with
241 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package algoliasearch | ||
|
||
import "fmt" | ||
|
||
type accountClient struct{} | ||
|
||
func NewAccountClient() AccountClient { | ||
return &accountClient{} | ||
} | ||
|
||
func (a *accountClient) CopyIndex(src, dst Index) ([]int, error) { | ||
return a.CopyIndexWithRequestOptions(src, dst, nil) | ||
} | ||
|
||
func (a *accountClient) CopyIndexWithRequestOptions(src, dst Index, opts *RequestOptions) ([]int, error) { | ||
if src.GetAppID() == dst.GetAppID() { | ||
return nil, SameAppIDErr | ||
} | ||
|
||
if _, err := dst.GetSettingsWithRequestOptions(opts); err == nil { | ||
return nil, IndexAlreadyExistsErr | ||
} | ||
|
||
var taskIDs []int | ||
|
||
// Copy synonyms | ||
{ | ||
it := NewSynonymIterator(src) | ||
|
||
var synonyms []Synonym | ||
|
||
for { | ||
synonym, err := it.Next() | ||
if err != nil { | ||
if err == NoMoreSynonymsErr { | ||
break | ||
} else { | ||
return nil, fmt.Errorf("error while iterating source index synonyms: %v", err) | ||
} | ||
} | ||
synonyms = append(synonyms, *synonym) | ||
} | ||
|
||
if synonyms != nil { | ||
res, err := dst.ReplaceAllSynonymsWithRequestOptions(synonyms, opts) | ||
if err != nil { | ||
return nil, fmt.Errorf("error while replacing destination index synonyms: %v", err) | ||
} | ||
taskIDs = append(taskIDs, res.TaskID) | ||
} | ||
} | ||
|
||
// Copy rules | ||
{ | ||
it := NewRuleIterator(src) | ||
|
||
var rules []Rule | ||
|
||
for { | ||
rule, err := it.Next() | ||
if err != nil { | ||
if err == NoMoreRulesErr { | ||
break | ||
} else { | ||
return nil, fmt.Errorf("error while iterating source index rules: %v", err) | ||
} | ||
} | ||
rules = append(rules, *rule) | ||
} | ||
if rules != nil { | ||
res, err := dst.ReplaceAllRulesWithRequestOptions(rules, opts) | ||
if err != nil { | ||
return nil, fmt.Errorf("error while replacing destination index rules: %v", err) | ||
} | ||
taskIDs = append(taskIDs, res.TaskID) | ||
} | ||
} | ||
|
||
// Copy settings | ||
{ | ||
settings, err := src.GetSettingsWithRequestOptions(opts) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot retrieve source index settings: %v", err) | ||
} | ||
|
||
res, err := dst.SetSettingsWithRequestOptions(settings.ToMap(), opts) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot set destination index settings: %v", err) | ||
} | ||
taskIDs = append(taskIDs, res.TaskID) | ||
|
||
} | ||
|
||
// Copy objects | ||
{ | ||
it, err := src.BrowseAllWithRequestOptions(nil, opts) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot browse source index objects: %v", err) | ||
} | ||
|
||
var objects []Object | ||
batchSize := 1000 | ||
|
||
for { | ||
res, err := it.Next() | ||
if err != nil { | ||
if err == NoMoreHitsErr { | ||
break | ||
} else { | ||
return nil, fmt.Errorf("error while iterating source index objects: %v", err) | ||
} | ||
} | ||
objects = append(objects, Object(res)) | ||
|
||
if len(objects) >= batchSize { | ||
res, err := dst.AddObjectsWithRequestOptions(objects, opts) | ||
if err != nil { | ||
return nil, fmt.Errorf("error while saving batch of objects: %v", err) | ||
} | ||
taskIDs = append(taskIDs, res.TaskID) | ||
objects = []Object{} | ||
} | ||
} | ||
|
||
// Send the last batch | ||
res, err := dst.AddObjectsWithRequestOptions(objects, opts) | ||
if err != nil { | ||
return nil, fmt.Errorf("error while saving batch of objects: %v", err) | ||
} | ||
taskIDs = append(taskIDs, res.TaskID) | ||
objects = []Object{} | ||
} | ||
|
||
return taskIDs, nil | ||
} |
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,93 @@ | ||
package algoliasearch | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAccountClient(t *testing.T) { | ||
client1, index1 := initClientAndIndex(t, "TestAccountClient") | ||
|
||
index2 := client1.InitIndex("go-TestAccountClient") | ||
{ | ||
account := NewAccountClient() | ||
_, err := account.CopyIndex(index1, index2) | ||
require.Equal(t, SameAppIDErr, err) | ||
} | ||
|
||
client2 := initClient2(t) | ||
index2 = client2.InitIndex("go-TestAccountClient") | ||
|
||
{ | ||
_, err := index2.Delete() | ||
require.NoError(t, err) | ||
} | ||
|
||
var taskIDs []int | ||
|
||
{ | ||
res, err := index1.AddObject(Object{"objectID": "one"}) | ||
require.NoError(t, err) | ||
taskIDs = append(taskIDs, res.TaskID) | ||
} | ||
|
||
{ | ||
res, err := index1.SaveRule(Rule{ | ||
ObjectID: "one", | ||
Condition: NewSimpleRuleCondition(Contains, "pattern"), | ||
Consequence: RuleConsequence{ | ||
Params: Map{ | ||
"query": Map{ | ||
"edits": []Edit{DeleteEdit("pattern")}, | ||
}, | ||
}, | ||
}, | ||
}, false) | ||
require.NoError(t, err) | ||
taskIDs = append(taskIDs, res.TaskID) | ||
} | ||
|
||
{ | ||
res, err := index1.SaveSynonym(NewSynonym("one", []string{"one", "two"}), false) | ||
require.NoError(t, err) | ||
taskIDs = append(taskIDs, res.TaskID) | ||
} | ||
|
||
{ | ||
res, err := index1.SetSettings(Map{"searchableAttributes": []string{"objectID"}}) | ||
require.NoError(t, err) | ||
taskIDs = append(taskIDs, res.TaskID) | ||
} | ||
|
||
waitTasksAsync(t, index1, taskIDs) | ||
taskIDs = []int{} | ||
|
||
{ | ||
account := NewAccountClient() | ||
taskIDs, err := account.CopyIndex(index1, index2) | ||
require.NoError(t, err) | ||
waitTasksAsync(t, index2, taskIDs) | ||
} | ||
|
||
{ | ||
_, err := index2.GetObject("one", nil) | ||
require.NoError(t, err) | ||
|
||
_, err = index2.GetRule("one") | ||
require.NoError(t, err) | ||
|
||
_, err = index2.GetSynonym("one") | ||
require.NoError(t, err) | ||
|
||
settings, err := index2.GetSettings() | ||
require.NoError(t, err) | ||
require.Equal(t, []string{"objectID"}, settings.SearchableAttributes) | ||
} | ||
|
||
{ | ||
account := NewAccountClient() | ||
_, err := account.CopyIndex(index1, index2) | ||
require.Equal(t, IndexAlreadyExistsErr, err) | ||
} | ||
} |
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