-
Notifications
You must be signed in to change notification settings - Fork 198
feat: Create and alias index #813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SoulPancake
wants to merge
7
commits into
redis:main
Choose a base branch
from
SoulPancake:ab/create-and-alias-index
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
536212e
feat: Create and aliax index
SoulPancake df86e86
feat: create alias inside
SoulPancake 763bc07
feat: check if alias exists
SoulPancake 3f13337
feat: address comment
SoulPancake f06f019
feat: address from suggestion
SoulPancake b0453cf
feat: address comment
SoulPancake 524e46e
feat: update test
SoulPancake File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -2,9 +2,11 @@ package om | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/oklog/ulid/v2" | ||
"reflect" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/redis/rueidis" | ||
|
@@ -146,6 +148,77 @@ func (r *HashRepository[T]) CreateIndex(ctx context.Context, cmdFn func(schema F | |
return r.client.Do(ctx, cmdFn(r.client.B().FtCreate().Index(r.idx).OnHash().Prefix(1).Prefix(r.prefix+":").Schema())).Error() | ||
} | ||
|
||
// CreateAndAliasIndex creates a new index, aliases it, and drops the old index if needed. | ||
func (r *HashRepository[T]) CreateAndAliasIndex(ctx context.Context, cmdFn func(schema FtCreateSchema) rueidis.Completed) error { | ||
alias := r.idx | ||
|
||
var currentIndex string | ||
aliasExists := false | ||
infoCmd := r.client.B().FtInfo().Index(alias).Build() | ||
infoResp, err := r.client.Do(ctx, infoCmd).ToMap() | ||
if err != nil { | ||
if strings.Contains(err.Error(), "Unknown index name") { | ||
// This is expected when the alias doesn't exist yet | ||
aliasExists = false | ||
} else { | ||
// This is an unexpected error (network, connection, etc.) | ||
return fmt.Errorf("failed to check if index exists: %w", err) | ||
} | ||
} else { | ||
aliasExists = true | ||
} | ||
|
||
if aliasExists { | ||
message, ok := infoResp["index_name"] | ||
if !ok { | ||
return fmt.Errorf("index_name not found in FT.INFO response") | ||
} | ||
|
||
currentIndex, err = message.ToString() | ||
if err != nil { | ||
return fmt.Errorf("failed to convert index_name to string: %w", err) | ||
} | ||
} | ||
|
||
newIndex := alias + "_v1" | ||
if aliasExists && currentIndex != "" { | ||
// Find the last occurrence of "_v" followed by digits | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about this? @rueian There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM. Please update for the JSONRepository as well. |
||
lastVersionIndex := strings.LastIndex(currentIndex, "_v") | ||
if lastVersionIndex != -1 && lastVersionIndex+2 < len(currentIndex) { | ||
versionStr := currentIndex[lastVersionIndex+2:] | ||
if version, err := strconv.Atoi(versionStr); err == nil { | ||
newIndex = fmt.Sprintf("%s_v%d", alias, version+1) | ||
} | ||
} | ||
} | ||
|
||
// Create the new index | ||
if err := r.client.Do(ctx, cmdFn(r.client.B().FtCreate().Index(newIndex).OnHash().Prefix(1).Prefix(r.prefix+":").Schema())).Error(); err != nil { | ||
return err | ||
} | ||
|
||
// Update or add the alias | ||
var aliasErr error | ||
if aliasExists { | ||
aliasErr = r.client.Do(ctx, r.client.B().FtAliasupdate().Alias(alias).Index(newIndex).Build()).Error() | ||
} else { | ||
aliasErr = r.client.Do(ctx, r.client.B().FtAliasadd().Alias(alias).Index(newIndex).Build()).Error() | ||
} | ||
|
||
if aliasErr != nil { | ||
return fmt.Errorf("failed to update alias: %w", aliasErr) | ||
} | ||
|
||
// Drop the old index if it exists and differs from the new one | ||
if aliasExists && currentIndex != "" && currentIndex != newIndex { | ||
if err := r.client.Do(ctx, r.client.B().FtDropindex().Index(currentIndex).Build()).Error(); err != nil { | ||
return fmt.Errorf("failed to drop old index: %w", err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// DropIndex uses FT.DROPINDEX from the RediSearch module to drop index whose name is `hashidx:{prefix}` | ||
func (r *HashRepository[T]) DropIndex(ctx context.Context) error { | ||
return r.client.Do(ctx, r.client.B().FtDropindex().Index(r.idx).Build()).Error() | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should work @rueian
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok. please write tests for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, will do the changes in JSON as well and then write UTs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @SoulPancake, any update on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @rueian Sorry been busy with work, Will do this tomorrow