Changeset 4a03b6fc


Ignore:
Timestamp:
Aug 25, 2018 4:35:41 PM (23 months ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
21fbcb8
Parents:
57ddc8e
Message:

i2psnark: Better comment deduping, fixes rating average

Location:
apps/i2psnark/java/src/org/klomp/snark/comments
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • apps/i2psnark/java/src/org/klomp/snark/comments/Comment.java

    r57ddc8e r4a03b6fc  
    1111
    1212/**
    13  * Store comments
     13 * Store a single comment and/or rating.
     14 * Unmodifiable except for marking as hidden.
     15 * Stores a one-second timestamp but designed so identical
     16 * comments within a certain time frame (bucket) are equal.
     17 * Don't store in a plain set - see equals().
    1418 *
    1519 * @since 0.9.31
     
    3135    private static final int BUCKET_SIZE = 10*60*1000;
    3236    private static final long TIME_SHRINK = 1000L;
     37    private static final int MAX_SKEW = (int) (BUCKET_SIZE / TIME_SHRINK);
    3338    // 1/1/2005
    3439    private static final long TIME_OFFSET = 1104537600000L;
     
    182187    }
    183188
    184 
     189    /**
     190     *  @return bucket number
     191     */
    185192    @Override
    186193    public int hashCode() {
    187         return time / (BUCKET_SIZE / (int) TIME_SHRINK);
    188     }
    189 
    190     /**
    191      *  Comments in the same 10-minute bucket and otherwise equal
    192      *  are considered equal. This will result in duplicates
    193      *  near the border.
     194        return time / MAX_SKEW;
     195    }
     196
     197    /**
     198     *  Comments within 10 minutes (not necessarily in same bucket)
     199     *  and otherwise equal are considered equal.
     200     *  Violates contract, as equal objects may have different hashcodes and
     201     *  be in adjacent buckets.
    194202     */
    195203    @Override
     
    198206        if (!(o instanceof Comment)) return false;
    199207        Comment c = (Comment) o;
    200         return rating == c.rating &&
    201                eq(text, c.text) &&
    202                eq(name, c.name) &&
    203                hashCode() == c.hashCode();
     208        int tdiff = time - c.time;
     209        if (tdiff > MAX_SKEW || tdiff < 0 - MAX_SKEW)
     210            return false;
     211        return equalsIgnoreTimestamp(c);
    204212    }
    205213
  • apps/i2psnark/java/src/org/klomp/snark/comments/CommentSet.java

    r57ddc8e r4a03b6fc  
    5959    private static final int MAX_TOTAL_TEXT_LEN = MAX_SIZE * 16;
    6060
    61     public CommentSet() {
     61    private CommentSet() {
    6262        super();
    6363        map = new HashMap<Integer, List<Comment>>(4);
     
    8181            br = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file)), "UTF-8"));
    8282            String line = null;
    83             while ( (line = br.readLine()) != null) {
     83            while ((line = br.readLine()) != null) {
    8484                Comment c = Comment.fromPersistentString(line);
    8585                if (c != null)
     
    130130        if (c.isMine() && c.getText() == null && c.getRating() == myRating)
    131131            return false;
    132         Integer hc = Integer.valueOf(c.hashCode());
     132        int hCode = c.hashCode();
     133        // check previous and next buckets
     134        Integer phc = Integer.valueOf(hCode - 1);
     135        List<Comment> plist = map.get(phc);
     136        if (plist != null && plist.contains(c))
     137            return false;
     138        Integer nhc = Integer.valueOf(hCode + 1);
     139        List<Comment> nxlist = map.get(nhc);
     140        if (nxlist != null && nxlist.contains(c))
     141            return false;
     142        // check this bucket
     143        Integer hc = Integer.valueOf(hCode);
    133144        List<Comment> list = map.get(hc);
    134145        if (list == null) {
Note: See TracChangeset for help on using the changeset viewer.