Skip to content
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

Use Grid instead of int as primary key. #2040

Closed
andy840119 opened this issue Jun 25, 2023 · 11 comments
Closed

Use Grid instead of int as primary key. #2040

andy840119 opened this issue Jun 25, 2023 · 11 comments
Labels
enhancement New feature or request
Milestone

Comments

@andy840119
Copy link
Member

andy840119 commented Jun 25, 2023

Instead of finding the largest index from the list of hit-object, like:

  • SingerInfo.getNewSingerId();
  • LyricsChangeHandler.getId();

maybe should be better to just use the GUID to prevent the duplicated id.

Another benefit:

  • There's no need to deal with logic to re-sort the id([1,3,4] -> [1,2,3])
  • Technically, there's no possible to have duplicated id.
  • Able to generate the id inside the class, follow how SkinInfo did in the osu.game
  • Easily to global search the id in the json file.

And note that:

  • We can change the algorithm for make the unique id shorter or not easier to duplicated at any time.
@andy840119 andy840119 added the enhancement New feature or request label Jun 25, 2023
@andy840119
Copy link
Member Author

https://dvoituron.com/2022/04/07/generate-small-unique-identifier/
Seems GUID is too large, maybe should find another way to shorter the unique id's length.

@andy840119
Copy link
Member Author

Those objects need the id:

Beatmap:

  • Singer -> max 20 singers
  • Lyric -> max 300 lyrics

Stage:

  • StageElement(i'm not very sure if skin element really need to unique key) -> max 200 elements
  • ClassicLyricTimingPoint(not really sure also.) -> max 2000 timing point.

Seems duplicated id is not the main issue.

@andy840119
Copy link
Member Author

andy840119 commented Jun 25, 2023

I think 7 characters as primary key is enough.
like how git generate the SHA1?

@andy840119
Copy link
Member Author

And we need to make sure that the generated singer or lyric's id should not be null in the test case.

@andy840119
Copy link
Member Author

Not very sure if it's over-design but we can follow how ResolvedAttribute did to assign the value to the primary key.

@andy840119
Copy link
Member Author

Or maybe make the id has it's own struct?
E.g. TinyUuid or NanoUuid
see: https://stackoverflow.com/a/61777945/4105113

Or ShortId
see: https://stackoverflow.com/a/45987192/4105113

@andy840119
Copy link
Member Author

Note:
Maybe Guid should accept the string format, so there will be no need to write it's own serializer.

See:
dotnet/runtime#31465

@andy840119
Copy link
Member Author

There's the sample code how serialize/deserialize the Guid without customized serializer.
see:

[Test]
public static void GuidSerializationTest()
{
    string str = JsonConvert.SerializeObject(new Foo
    {
        MyGuid = Guid.NewGuid()
    });
    Assert.NotNull(str, str);
}

[Test]
public static void GuidDeserializationTest()
{
    Foo? f = JsonConvert.DeserializeObject<Foo>("{\"MyGuid\":\"090bbc52-bfe3-4fa3-9b36-25d7e624e662\"}");
    Guid expected = new Guid("090bbc52-bfe3-4fa3-9b36-25d7e624e662");
    Assert.AreEqual(expected, f?.MyGuid);
}

public class Foo
{
    public Guid MyGuid { get; set; }
}

@andy840119
Copy link
Member Author

And note that we can make the "IHasPrimaryKey" interface in the Beatmap namespace.
Follow how IHasPrimaryKey and IHasGuidPrimaryKey did in the osu.Game.

@andy840119
Copy link
Member Author

image
Only remaining the skin element need to change the element type.
But it's not the first priority.

@andy840119
Copy link
Member Author

Guess should be able to close this issue because should not have the element id in the skin eventually.
It will let the skin in the karaoke beatmap hard to be replaced.

@andy840119 andy840119 added this to the 2023.0808 milestone Jul 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant