-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
520 additions
and
31 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,79 @@ | ||
/* | ||
* Copyright (c) 2024, VRAI Labs and/or its affiliates. All rights reserved. | ||
* | ||
* This software is licensed under the Apache License, Version 2.0 (the | ||
* "License") as published by the Apache Software Foundation. | ||
* | ||
* You may not use this file except in compliance with the License. You may | ||
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package io.supertokens.bulkimport; | ||
|
||
import io.supertokens.pluginInterface.bulkimport.BulkImportUser; | ||
import io.supertokens.pluginInterface.exceptions.StorageQueryException; | ||
import io.supertokens.pluginInterface.multitenancy.AppIdentifierWithStorage; | ||
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException; | ||
import io.supertokens.utils.Utils; | ||
|
||
import com.google.gson.JsonObject; | ||
|
||
import java.util.ArrayList; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
public class BulkImport { | ||
|
||
public static final int GET_USERS_PAGINATION_LIMIT = 500; | ||
public static final int GET_USERS_DEFAULT_LIMIT = 100; | ||
|
||
public static void addUsers(AppIdentifierWithStorage appIdentifierWithStorage, ArrayList<BulkImportUser> users) | ||
throws StorageQueryException, TenantOrAppNotFoundException { | ||
while (true) { | ||
try { | ||
appIdentifierWithStorage.getBulkImportStorage().addBulkImportUsers(appIdentifierWithStorage, users); | ||
break; | ||
} catch (io.supertokens.pluginInterface.bulkimport.exceptions.DuplicateUserIdException ignored) { | ||
// We re-generate the user id for every user and retry | ||
for (BulkImportUser user : users) { | ||
user.id = Utils.getUUID(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static BulkImportUserPaginationContainer getUsers(AppIdentifierWithStorage appIdentifierWithStorage, | ||
@Nonnull Integer limit, @Nullable String status, @Nullable String paginationToken) | ||
throws StorageQueryException, BulkImportUserPaginationToken.InvalidTokenException, | ||
TenantOrAppNotFoundException { | ||
JsonObject[] users; | ||
|
||
if (paginationToken == null) { | ||
users = appIdentifierWithStorage.getBulkImportStorage() | ||
.getBulkImportUsers(appIdentifierWithStorage, limit + 1, status, null); | ||
} else { | ||
BulkImportUserPaginationToken tokenInfo = BulkImportUserPaginationToken.extractTokenInfo(paginationToken); | ||
users = appIdentifierWithStorage.getBulkImportStorage() | ||
.getBulkImportUsers(appIdentifierWithStorage, limit + 1, status, tokenInfo.bulkImportUserId); | ||
} | ||
|
||
String nextPaginationToken = null; | ||
int maxLoop = users.length; | ||
if (users.length == limit + 1) { | ||
maxLoop = limit; | ||
nextPaginationToken = new BulkImportUserPaginationToken(users[limit].get("id").getAsString()) | ||
.generateToken(); | ||
} | ||
|
||
JsonObject[] resultUsers = new JsonObject[maxLoop]; | ||
System.arraycopy(users, 0, resultUsers, 0, maxLoop); | ||
return new BulkImportUserPaginationContainer(resultUsers, nextPaginationToken); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/io/supertokens/bulkimport/BulkImportUserPaginationContainer.java
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,32 @@ | ||
/* | ||
* Copyright (c) 2024, VRAI Labs and/or its affiliates. All rights reserved. | ||
* | ||
* This software is licensed under the Apache License, Version 2.0 (the | ||
* "License") as published by the Apache Software Foundation. | ||
* | ||
* You may not use this file except in compliance with the License. You may | ||
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package io.supertokens.bulkimport; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
import com.google.gson.JsonObject; | ||
|
||
public class BulkImportUserPaginationContainer { | ||
public final JsonObject[] users; | ||
public final String nextPaginationToken; | ||
|
||
public BulkImportUserPaginationContainer(@Nonnull JsonObject[] users, @Nullable String nextPaginationToken) { | ||
this.users = users; | ||
this.nextPaginationToken = nextPaginationToken; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/io/supertokens/bulkimport/BulkImportUserPaginationToken.java
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,45 @@ | ||
/* | ||
* Copyright (c) 2024, VRAI Labs and/or its affiliates. All rights reserved. | ||
* | ||
* This software is licensed under the Apache License, Version 2.0 (the | ||
* "License") as published by the Apache Software Foundation. | ||
* | ||
* You may not use this file except in compliance with the License. You may | ||
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package io.supertokens.bulkimport; | ||
|
||
import java.util.Base64; | ||
|
||
public class BulkImportUserPaginationToken { | ||
public final String bulkImportUserId; | ||
|
||
public BulkImportUserPaginationToken(String bulkImportUserId) { | ||
this.bulkImportUserId = bulkImportUserId; | ||
} | ||
|
||
public static BulkImportUserPaginationToken extractTokenInfo(String token) throws InvalidTokenException { | ||
try { | ||
String bulkImportUserId = new String(Base64.getDecoder().decode(token)); | ||
return new BulkImportUserPaginationToken(bulkImportUserId); | ||
} catch (Exception e) { | ||
throw new InvalidTokenException(); | ||
} | ||
} | ||
|
||
public String generateToken() { | ||
return new String(Base64.getEncoder().encode((this.bulkImportUserId).getBytes())); | ||
} | ||
|
||
public static class InvalidTokenException extends Exception { | ||
|
||
private static final long serialVersionUID = 6289026174830695478L; | ||
} | ||
} |
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
Oops, something went wrong.