dain / leveldb

Port of LevelDB to Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unnecessary to check levelnumber = = 0 in Level.java

hecenjie opened this issue · comments

// Version.java
    public LookupResult get(LookupKey key)
    {
        LookupResult lookupResult = level0.get(key, readStats);
        if (lookupResult == null) {
            for (Level level : levels) {
                lookupResult = level.get(key, readStats);
                ...
            }
        }
        ...
        return lookupResult;
    }

// Level0.java
    public LookupResult get(LookupKey key, ReadStats readStats)
    {
        ...
        List<FileMetaData> fileMetaDataList = new ArrayList<>(files.size());
        for (FileMetaData fileMetaData : files) {
            if (...) {
                fileMetaDataList.add(fileMetaData);
            }
        }
        Collections.sort(fileMetaDataList, NEWEST_FIRST);
        ...
    }

// Level.java
    public LookupResult get(LookupKey key, ReadStats readStats)
    {
        ...
        List<FileMetaData> fileMetaDataList = new ArrayList<>(files.size());
        if (levelNumber == 0) {    // Do we really need to judge this condition ?
            for (FileMetaData fileMetaData : files) {
                if (...) {
                    fileMetaDataList.add(fileMetaData);
                }
            }
        }
        else {
            // Binary search to find earliest index whose largest key >= ikey.
        }
        ...
    }

In the above function, level0.get() and level.get() are called respectively. However, In the implementation of the former, calls the sort function Collections.sort(fileMetaDataList, NEWEST_FIRST); to make the newer files in front of the list while the other call doesn't. So do we need this sort function call or not? And why get function in Level.java should consider the situation of level0?

Level 0 files are not compacted, so you have to process them in temporal order to ensure that old (deleted or over written) values are not returned. Later levels are compacted, so you don't need this.

I understand that. I mean, does the get method in Level.java need to check the first if-statement (levelnumber = = 0), I think it has been processed by the get method in Level0.java, which also means that the get method in Level.java doesn't need the sort as you said.

In other words, can we just delete the code fragment below in Level.java because this situation has been considered in Level0.java:

        if (levelNumber == 0) {    // Do we really need to judge this condition ?
            for (FileMetaData fileMetaData : files) {
                if (...) {
                    fileMetaDataList.add(fileMetaData);
                }
            }
        }