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

Implement database iterator #47

Merged
merged 18 commits into from
Aug 8, 2018
Merged

Implement database iterator #47

merged 18 commits into from
Aug 8, 2018

Conversation

jjxtra
Copy link
Contributor

@jjxtra jjxtra commented Jul 16, 2018

I would appreciate code review on this. It passes all tests.

I would love to see this in a nuget package soon.

As a side note, some of the threading tests are failing. I did not investigate that.

@jjxtra
Copy link
Contributor Author

jjxtra commented Jul 16, 2018

I would suggest a squash and merge on this to avoid soiling the commit history

@jjxtra jjxtra mentioned this pull request Jul 17, 2018
jjxtra added 3 commits July 19, 2018 16:48
This allows any type to easily be iterated, at the cost of losing the convenience of a foreach loop.
Copy link
Member

@oschwald oschwald left a comment

Choose a reason for hiding this comment

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

This looks great! I apologize for taking so long to get back to you on this. I had a few minor comments.

/// <summary>
/// Get an enumerator that iterates all data nodes in the database
/// </summary>
/// <param name="cacheSize">The size of the data cache. This can greatly speed enumeration at the cost of memory usage.</param>
Copy link
Member

Choose a reason for hiding this comment

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

This should also probably warn about the dangers of mutating the returned object given that the object is in the cache.

}

[Fact]
public void TestEnumerateCitiesDatabaseSpeed()
Copy link
Member

Choose a reason for hiding this comment

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

This test is rather slow and would likely fail if run on a slow machine. Perhaps this could be refactored a bit and moved to be with the benchmarks in MaxMind.Db.Benchmark.

{
Stopwatch timer = Stopwatch.StartNew();
int count = 0;
using (var reader = new Reader(Path.Combine(_testDataRoot, "../../GeoLite2-Country.mmdb"), FileAccessMode.Memory))
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps this one could be moved to the benchmarks too. I noticed that you added this database to the repo. Although it is much smaller than the GeoLite2-City database that was added to the repo early on, I'd like to avoid adding new binary files to reduce repo bloat.

/// A dictionary that caches up to N values in memory. Once the dictionary reaches N count, the last item in the internal list is removed.
/// New items are always added to the start of the internal list.
/// </summary>
internal class CachedDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IDisposable
Copy link
Member

Choose a reason for hiding this comment

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

Nice! Was this based on another caching implementation or is it completely new code? I am just wondering for licensing reasons.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Something I use in other projects, can tag it with MIT license. All code written by me in my spare time for free.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks! If you are the owner of the code, would you be willing to place it under the Apache 2 license to match the other code in the repo and for the benefit of the patent grant?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure no problem

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is done.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Commented code eradicated

/// <summary>
/// The first node in the priority list
/// </summary>
protected LinkedListNode<KeyValuePair<TKey, TValue>> FirstNode
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is used.

/// <summary>
/// Current comparer
/// </summary>
public IEqualityComparer<TKey> Comparer
Copy link
Member

Choose a reason for hiding this comment

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

This and the following three are also not used.

/// </summary>
/// <param name="cacheSize">The size of the data cache. This can greatly speed enumeration at the cost of memory usage.</param>
/// <returns>Enumerator for all data nodes</returns>
public IEnumerable<Reader.ReaderIteratorNode<T>> FindAll<T>(int cacheSize = 16384) where T : class
Copy link
Member

Choose a reason for hiding this comment

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

A test that used something other than Dictionary<string, object> might be good. In particular, one using the TypeHolder class with the MaxMind-DB-test-decoder.mmdb database.

@jjxtra
Copy link
Contributor Author

jjxtra commented Aug 4, 2018

I've implemented most of these changes. I've moved the code to the benchmark project, but it is commented out for now. Not sure how useful it is at the moment, perhaps someone else can add it later if needed.

@oschwald
Copy link
Member

oschwald commented Aug 6, 2018

Looks good. Perhaps the benchmark code could be moved to a separate PR or an issue so that we don't have commented out code lying around in the codebase. Other than that and the license question, I am happy to merge this.

@jjxtra
Copy link
Contributor Author

jjxtra commented Aug 7, 2018

Ready to merge when you are.

@oschwald
Copy link
Member

oschwald commented Aug 8, 2018

Looks great! Thanks. I am squashing it so that the binary database does not end up in the Git history.

@oschwald oschwald merged commit db0f4c4 into maxmind:master Aug 8, 2018
oschwald pushed a commit that referenced this pull request Jul 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants