Skip to content

Fix tons of bugs relating to null snapshot.values and improve perf for good citizens #871

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

Merged
merged 6 commits into from
Sep 1, 2017

Conversation

SUPERCILEX
Copy link
Collaborator

@SUPERCILEX SUPERCILEX commented Aug 18, 2017

protected void onKeyAdded(DataSnapshot data) {}

protected void onKeyAdded(DataSnapshot data, int index) {
onKeyAdded(data);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this isn't backwards compatible if people call super on the old method. 😢 I totally understand if you don't want to proceed with this change though we would be missing out on some significant perf gains: O(n) with the old method, O(1) with the new method on simple additions.

int dataCount = size();
int dataIndex = 0;
for (int keyIndex = 0; dataIndex < dataCount; keyIndex++) {
if (keyIndex == mKeySnapshots.size()) { break; }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this line to the old method to handle being called by a deletion at the last index.

mChatIndicesRef = FirebaseDatabase.getInstance()
.getReference()
.child("chatIndices")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a uid to the indices to make them more realistic.

@@ -58,6 +71,10 @@ private void init(Query query) {
mQuery = query;
}

void setPreChangeEventListener(PreChangeEventListener listener) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what to do about this api... seems like other devs might want to use it, but it's kinda gross.

@SUPERCILEX SUPERCILEX changed the title Fix tons of bugs relating to null snapshot.values Fix tons of bugs relating to null snapshot.values and improve perf for good citizens Aug 18, 2017
@samtstern
Copy link
Contributor

@SUPERCILEX sorry I never responded here. I think we're both on the same page about this one. It's definitely an improvement in some ways, but PreChangeListener seems like a hacky API change and losing backwards compatibility isn't really worth it here.

If there are any small improvements from this you'd like to keep we can move forward with those, otherwise I'd just say we document the limitations of the indexed adapters.

@SUPERCILEX SUPERCILEX closed this Aug 22, 2017
@SUPERCILEX
Copy link
Collaborator Author

Oops, didn't mean to close this PR.

@samtstern yep, I'll keep as many changes as I can as long as they're backwards compatible.

@SUPERCILEX SUPERCILEX reopened this Aug 24, 2017
@SUPERCILEX
Copy link
Collaborator Author

@samtstern Could you please restart the build? My Travis is passing. Thanks!

@samtstern samtstern changed the base branch from version-2.2.1-dev to version-2.3.1-dev August 30, 2017 14:55
Signed-off-by: Alex Saveau <[email protected]>
} else {
int dataCount = size();
int dataIndex = 0;
for (int keyIndex = 0; dataIndex < dataCount; keyIndex++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very strange for loop. It initializes keyIndex but then waits for a condition on dataIndex, all while incrementing keyIndex on each pass.

Took me a very long time to decipher. I'd suggest converting it to something like this:

int dataIndex = 0;
int keyIndex = 0;

while (dataIndex < dataCount) {
   // ... stuff

   keyIndex++;
}

Since the first line is if (keyIndex == mKeySnapshots.size()) { break; } you may even want to make the main condition while(dataIndex < dataCount && keyIndex < mKeySnapshots.size())

Also add comments throughout the method please!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samtstern Same! Another contributor who worked on my original PR wrote that for loop and it always takes me 5+ mins to get my head around it again. I thought that was just me! 😆 Anyway, your suggestions sound like great improvements!

Copy link
Contributor

@samtstern samtstern left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small comment remaining, but LGTM otherwise. This is much clearer!

} else if (mDataSnapshots.get(index).getKey().equals(superKey)) {
index++;
private int returnOrFindIndexForKey(int index, String key) {
int realIndex;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we initialize realIndex to 0 for clarity? If key.equals(superKey) is true on the first loop then we will break and return realIndex without ever having initialized it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, we could but intellij complains because we do initialize it to dataIndex while out of the loop. Do you still want to proceed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh oops I missed that, thanks!

@samtstern samtstern merged commit 2f8ee50 into firebase:version-2.3.1-dev Sep 1, 2017
@samtstern samtstern added this to the 2.3.1 milestone Sep 1, 2017
@SUPERCILEX SUPERCILEX deleted the null-value branch September 1, 2017 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants