{"action":"created","comment":{"url":"https://api.github.com/repos/17mon/java/pulls/comments/63387787","id":63387787,"diff_hunk":"@@ -0,0 +1,183 @@\n+import java.io.File;\n+import java.io.FileInputStream;\n+import java.io.IOException;\n+import java.nio.ByteBuffer;\n+import java.nio.ByteOrder;\n+import java.util.Arrays;\n+import java.nio.charset.Charset;\n+import java.util.Random;\n+import java.util.concurrent.Executors;\n+import java.util.concurrent.TimeUnit;\n+import java.util.concurrent.locks.ReentrantLock;\n+\n+class IPOld {\n+\n+    public static String randomIp() {\n+        Random r = new Random();\n+        StringBuffer str = new StringBuffer();\n+        str.append(r.nextInt(1000000) % 255);\n+        str.append(\".\");\n+        str.append(r.nextInt(1000000) % 255);\n+        str.append(\".\");\n+        str.append(r.nextInt(1000000) % 255);\n+        str.append(\".\");\n+        str.append(0);\n+\n+        return str.toString();\n+    }\n+\n+    public static void main(String[] args){\n+        IP.load(\"H:\\\\loveapp\\\\codebase\\\\17mon\\\\17monipdb.dat\");\n+\n+        Long st = System.nanoTime();\n+        for (int i = 0; i < 1000000; i++)\n+        {\n+            IP.find(randomIp());\n+        }\n+        Long et = System.nanoTime();\n+        System.out.println((et - st) / 1000 / 1000);\n+\n+        System.out.println(Arrays.toString(IP.find(\"118.28.8.8\")));\n+    }\n+\n+    public static boolean enableFileWatch = false;\n+\n+    private static int offset;\n+    private static int[] index = new int[256];\n+    private static ByteBuffer dataBuffer;\n+    private static ByteBuffer indexBuffer;\n+    private static Long lastModifyTime = 0L;\n+    private static File ipFile ;\n+    private static ReentrantLock lock = new ReentrantLock();\n+\n+    public static void load(String filename) {\n+        ipFile = new File(filename);\n+        load();\n+        if (enableFileWatch) {\n+            watch();\n+        }\n+    }\n+\n+    public static void load(String filename, boolean strict) throws Exception {\n+        ipFile = new File(filename);\n+        if (strict) {\n+            int contentLength = Long.valueOf(ipFile.length()).intValue();\n+            if (contentLength < 512 * 1024) {\n+                throw new Exception(\"ip data file error.\");\n+            }\n+        }\n+        load();\n+        if (enableFileWatch) {\n+            watch();\n+        }\n+    }\n+\n+    public static String[] find(String ip) {\n+        int ip_prefix_value = new Integer(ip.substring(0, ip.indexOf(\".\")));\n+        long ip2long_value  = ip2long(ip);\n+        int start = index[ip_prefix_value];\n+        int max_comp_len = offset - 1028;\n+        long index_offset = -1;\n+        int index_length = -1;\n+        byte b = 0;\n+        for (start = start * 8 + 1024; start < max_comp_len; start += 8) {\n+            if (int2long(indexBuffer.getInt(start)) >= ip2long_value) {\n+                index_offset = bytesToLong(b, indexBuffer.get(start + 6), indexBuffer.get(start + 5), indexBuffer.get(start + 4));\n+                index_length = 0xFF & indexBuffer.get(start + 7);\n+                break;\n+            }\n+        }\n+\n+        byte[] areaBytes;\n+\n+        lock.lock();\n+        try {\n+            dataBuffer.position(offset + (int) index_offset - 1024);\n+            areaBytes = new byte[index_length];\n+            dataBuffer.get(areaBytes, 0, index_length);\n+        } finally {\n+            lock.unlock();\n+        }\n+\n+        return new String(areaBytes, Charset.forName(\"UTF-8\")).split(\"\\t\", -1);\n+    }\n+\n+    private static void watch() {\n+        Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {\n+            @Override\n+            public void run() {\n+                long time = ipFile.lastModified();\n+                if (time > lastModifyTime) {\n+                    lastModifyTime = time;\n+                    load();\n+                }\n+            }\n+        }, 1000L, 5000L, TimeUnit.MILLISECONDS);\n+    }\n+\n+    private static void load() {\n+        lastModifyTime = ipFile.lastModified();\n+        FileInputStream fin = null;\n+        lock.lock();\n+        try {\n+            dataBuffer = ByteBuffer.allocate(Long.valueOf(ipFile.length()).intValue());\n+            fin = new FileInputStream(ipFile);\n+            int readBytesLength;\n+            byte[] chunk = new byte[4096];\n+            while (fin.available() > 0) {\n+                readBytesLength = fin.read(chunk);\n+                dataBuffer.put(chunk, 0, readBytesLength);\n+            }\n+            dataBuffer.position(0);\n+            int indexLength = dataBuffer.getInt();\n+            byte[] indexBytes = new byte[indexLength];\n+            dataBuffer.get(indexBytes, 0, indexLength - 4);\n+            indexBuffer = ByteBuffer.wrap(indexBytes);\n+            indexBuffer.order(ByteOrder.LITTLE_ENDIAN);\n+            offset = indexLength;\n+\n+            int loop = 0;\n+            while (loop++ < 256) {\n+                index[loop - 1] = indexBuffer.getInt();\n+            }\n+            indexBuffer.order(ByteOrder.BIG_ENDIAN);\n+        } catch (IOException ioe) {\n+            ioe.printStackTrace();\n+        } finally {\n+            try {\n+                if (fin != null) {\n+                    fin.close();\n+                }\n+            } catch (IOException e){\n+                e.printStackTrace();\n+            }\n+            lock.unlock();\n+        }\n+    }\n+\n+    private static long bytesToLong(byte a, byte b, byte c, byte d) {\n+        return int2long((((a & 0xff) << 24) | ((b & 0xff) << 16) | ((c & 0xff) << 8) | (d & 0xff)));\n+    }\n+\n+    private static int str2Ip(String ip)  {\n+        String[] ss = ip.split(\"\\\\.\");\n+        int a, b, c, d;\n+        a = Integer.parseInt(ss[0]);\n+        b = Integer.parseInt(ss[1]);\n+        c = Integer.parseInt(ss[2]);\n+        d = Integer.parseInt(ss[3]);\n+        return (a << 24) | (b << 16) | (c << 8) | d;\n+    }\n+\n+    private static long ip2long(String ip)  {\n+        return int2long(str2Ip(ip));\n+    }\n+\n+    private static long int2long(int i) {","path":"IPOld.java","position":176,"original_position":176,"commit_id":"697a6574797d14dd97e44530fd371ce31263f1b2","original_commit_id":"697a6574797d14dd97e44530fd371ce31263f1b2","user":{"login":"Feng-Zihao","id":2885070,"avatar_url":"https://avatars.githubusercontent.com/u/2885070?v=3","gravatar_id":"","url":"https://api.github.com/users/Feng-Zihao","html_url":"https://github.com/Feng-Zihao","followers_url":"https://api.github.com/users/Feng-Zihao/followers","following_url":"https://api.github.com/users/Feng-Zihao/following{/other_user}","gists_url":"https://api.github.com/users/Feng-Zihao/gists{/gist_id}","starred_url":"https://api.github.com/users/Feng-Zihao/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Feng-Zihao/subscriptions","organizations_url":"https://api.github.com/users/Feng-Zihao/orgs","repos_url":"https://api.github.com/users/Feng-Zihao/repos","events_url":"https://api.github.com/users/Feng-Zihao/events{/privacy}","received_events_url":"https://api.github.com/users/Feng-Zihao/received_events","type":"User","site_admin":false},"body":"(long)i   ??","created_at":"2016-05-16T17:09:02Z","updated_at":"2016-05-16T17:09:02Z","html_url":"https://github.com/17mon/java/pull/9#discussion_r63387787","pull_request_url":"https://api.github.com/repos/17mon/java/pulls/9","_links":{"self":{"href":"https://api.github.com/repos/17mon/java/pulls/comments/63387787"},"html":{"href":"https://github.com/17mon/java/pull/9#discussion_r63387787"},"pull_request":{"href":"https://api.github.com/repos/17mon/java/pulls/9"}}},"pull_request":{"url":"https://api.github.com/repos/17mon/java/pulls/9","id":67992035,"html_url":"https://github.com/17mon/java/pull/9","diff_url":"https://github.com/17mon/java/pull/9.diff","patch_url":"https://github.com/17mon/java/pull/9.patch","issue_url":"https://api.github.com/repos/17mon/java/issues/9","number":9,"state":"open","locked":false,"title":"improve performance","user":{"login":"starfalling","id":532951,"avatar_url":"https://avatars.githubusercontent.com/u/532951?v=3","gravatar_id":"","url":"https://api.github.com/users/starfalling","html_url":"https://github.com/starfalling","followers_url":"https://api.github.com/users/starfalling/followers","following_url":"https://api.github.com/users/starfalling/following{/other_user}","gists_url":"https://api.github.com/users/starfalling/gists{/gist_id}","starred_url":"https://api.github.com/users/starfalling/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/starfalling/subscriptions","organizations_url":"https://api.github.com/users/starfalling/orgs","repos_url":"https://api.github.com/users/starfalling/repos","events_url":"https://api.github.com/users/starfalling/events{/privacy}","received_events_url":"https://api.github.com/users/starfalling/received_events","type":"User","site_admin":false},"body":"耗时降低至原算法的 1/3 左右","created_at":"2016-04-27T03:36:14Z","updated_at":"2016-05-16T17:09:02Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d8a5b31553f17b33356d7acd6e8f73b1911bd551","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/17mon/java/pulls/9/commits","review_comments_url":"https://api.github.com/repos/17mon/java/pulls/9/comments","review_comment_url":"https://api.github.com/repos/17mon/java/pulls/comments{/number}","comments_url":"https://api.github.com/repos/17mon/java/issues/9/comments","statuses_url":"https://api.github.com/repos/17mon/java/statuses/697a6574797d14dd97e44530fd371ce31263f1b2","head":{"label":"starfalling:master","ref":"master","sha":"697a6574797d14dd97e44530fd371ce31263f1b2","user":{"login":"starfalling","id":532951,"avatar_url":"https://avatars.githubusercontent.com/u/532951?v=3","gravatar_id":"","url":"https://api.github.com/users/starfalling","html_url":"https://github.com/starfalling","followers_url":"https://api.github.com/users/starfalling/followers","following_url":"https://api.github.com/users/starfalling/following{/other_user}","gists_url":"https://api.github.com/users/starfalling/gists{/gist_id}","starred_url":"https://api.github.com/users/starfalling/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/starfalling/subscriptions","organizations_url":"https://api.github.com/users/starfalling/orgs","repos_url":"https://api.github.com/users/starfalling/repos","events_url":"https://api.github.com/users/starfalling/events{/privacy}","received_events_url":"https://api.github.com/users/starfalling/received_events","type":"User","site_admin":false},"repo":{"id":57180454,"name":"java","full_name":"starfalling/java","owner":{"login":"starfalling","id":532951,"avatar_url":"https://avatars.githubusercontent.com/u/532951?v=3","gravatar_id":"","url":"https://api.github.com/users/starfalling","html_url":"https://github.com/starfalling","followers_url":"https://api.github.com/users/starfalling/followers","following_url":"https://api.github.com/users/starfalling/following{/other_user}","gists_url":"https://api.github.com/users/starfalling/gists{/gist_id}","starred_url":"https://api.github.com/users/starfalling/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/starfalling/subscriptions","organizations_url":"https://api.github.com/users/starfalling/orgs","repos_url":"https://api.github.com/users/starfalling/repos","events_url":"https://api.github.com/users/starfalling/events{/privacy}","received_events_url":"https://api.github.com/users/starfalling/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/starfalling/java","description":"17mon IP库解析代码","fork":true,"url":"https://api.github.com/repos/starfalling/java","forks_url":"https://api.github.com/repos/starfalling/java/forks","keys_url":"https://api.github.com/repos/starfalling/java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/starfalling/java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/starfalling/java/teams","hooks_url":"https://api.github.com/repos/starfalling/java/hooks","issue_events_url":"https://api.github.com/repos/starfalling/java/issues/events{/number}","events_url":"https://api.github.com/repos/starfalling/java/events","assignees_url":"https://api.github.com/repos/starfalling/java/assignees{/user}","branches_url":"https://api.github.com/repos/starfalling/java/branches{/branch}","tags_url":"https://api.github.com/repos/starfalling/java/tags","blobs_url":"https://api.github.com/repos/starfalling/java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/starfalling/java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/starfalling/java/git/refs{/sha}","trees_url":"https://api.github.com/repos/starfalling/java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/starfalling/java/statuses/{sha}","languages_url":"https://api.github.com/repos/starfalling/java/languages","stargazers_url":"https://api.github.com/repos/starfalling/java/stargazers","contributors_url":"https://api.github.com/repos/starfalling/java/contributors","subscribers_url":"https://api.github.com/repos/starfalling/java/subscribers","subscription_url":"https://api.github.com/repos/starfalling/java/subscription","commits_url":"https://api.github.com/repos/starfalling/java/commits{/sha}","git_commits_url":"https://api.github.com/repos/starfalling/java/git/commits{/sha}","comments_url":"https://api.github.com/repos/starfalling/java/comments{/number}","issue_comment_url":"https://api.github.com/repos/starfalling/java/issues/comments{/number}","contents_url":"https://api.github.com/repos/starfalling/java/contents/{+path}","compare_url":"https://api.github.com/repos/starfalling/java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/starfalling/java/merges","archive_url":"https://api.github.com/repos/starfalling/java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/starfalling/java/downloads","issues_url":"https://api.github.com/repos/starfalling/java/issues{/number}","pulls_url":"https://api.github.com/repos/starfalling/java/pulls{/number}","milestones_url":"https://api.github.com/repos/starfalling/java/milestones{/number}","notifications_url":"https://api.github.com/repos/starfalling/java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/starfalling/java/labels{/name}","releases_url":"https://api.github.com/repos/starfalling/java/releases{/id}","deployments_url":"https://api.github.com/repos/starfalling/java/deployments","created_at":"2016-04-27T03:26:23Z","updated_at":"2016-04-27T03:26:24Z","pushed_at":"2016-05-11T09:55:46Z","git_url":"git://github.com/starfalling/java.git","ssh_url":"git@github.com:starfalling/java.git","clone_url":"https://github.com/starfalling/java.git","svn_url":"https://github.com/starfalling/java","homepage":null,"size":17,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"17mon:master","ref":"master","sha":"17a7457be3c2d1800c35af81be7987556896b8ea","user":{"login":"17mon","id":7857663,"avatar_url":"https://avatars.githubusercontent.com/u/7857663?v=3","gravatar_id":"","url":"https://api.github.com/users/17mon","html_url":"https://github.com/17mon","followers_url":"https://api.github.com/users/17mon/followers","following_url":"https://api.github.com/users/17mon/following{/other_user}","gists_url":"https://api.github.com/users/17mon/gists{/gist_id}","starred_url":"https://api.github.com/users/17mon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/17mon/subscriptions","organizations_url":"https://api.github.com/users/17mon/orgs","repos_url":"https://api.github.com/users/17mon/repos","events_url":"https://api.github.com/users/17mon/events{/privacy}","received_events_url":"https://api.github.com/users/17mon/received_events","type":"User","site_admin":false},"repo":{"id":26843438,"name":"java","full_name":"17mon/java","owner":{"login":"17mon","id":7857663,"avatar_url":"https://avatars.githubusercontent.com/u/7857663?v=3","gravatar_id":"","url":"https://api.github.com/users/17mon","html_url":"https://github.com/17mon","followers_url":"https://api.github.com/users/17mon/followers","following_url":"https://api.github.com/users/17mon/following{/other_user}","gists_url":"https://api.github.com/users/17mon/gists{/gist_id}","starred_url":"https://api.github.com/users/17mon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/17mon/subscriptions","organizations_url":"https://api.github.com/users/17mon/orgs","repos_url":"https://api.github.com/users/17mon/repos","events_url":"https://api.github.com/users/17mon/events{/privacy}","received_events_url":"https://api.github.com/users/17mon/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/17mon/java","description":"17mon IP库解析代码","fork":false,"url":"https://api.github.com/repos/17mon/java","forks_url":"https://api.github.com/repos/17mon/java/forks","keys_url":"https://api.github.com/repos/17mon/java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/17mon/java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/17mon/java/teams","hooks_url":"https://api.github.com/repos/17mon/java/hooks","issue_events_url":"https://api.github.com/repos/17mon/java/issues/events{/number}","events_url":"https://api.github.com/repos/17mon/java/events","assignees_url":"https://api.github.com/repos/17mon/java/assignees{/user}","branches_url":"https://api.github.com/repos/17mon/java/branches{/branch}","tags_url":"https://api.github.com/repos/17mon/java/tags","blobs_url":"https://api.github.com/repos/17mon/java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/17mon/java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/17mon/java/git/refs{/sha}","trees_url":"https://api.github.com/repos/17mon/java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/17mon/java/statuses/{sha}","languages_url":"https://api.github.com/repos/17mon/java/languages","stargazers_url":"https://api.github.com/repos/17mon/java/stargazers","contributors_url":"https://api.github.com/repos/17mon/java/contributors","subscribers_url":"https://api.github.com/repos/17mon/java/subscribers","subscription_url":"https://api.github.com/repos/17mon/java/subscription","commits_url":"https://api.github.com/repos/17mon/java/commits{/sha}","git_commits_url":"https://api.github.com/repos/17mon/java/git/commits{/sha}","comments_url":"https://api.github.com/repos/17mon/java/comments{/number}","issue_comment_url":"https://api.github.com/repos/17mon/java/issues/comments{/number}","contents_url":"https://api.github.com/repos/17mon/java/contents/{+path}","compare_url":"https://api.github.com/repos/17mon/java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/17mon/java/merges","archive_url":"https://api.github.com/repos/17mon/java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/17mon/java/downloads","issues_url":"https://api.github.com/repos/17mon/java/issues{/number}","pulls_url":"https://api.github.com/repos/17mon/java/pulls{/number}","milestones_url":"https://api.github.com/repos/17mon/java/milestones{/number}","notifications_url":"https://api.github.com/repos/17mon/java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/17mon/java/labels{/name}","releases_url":"https://api.github.com/repos/17mon/java/releases{/id}","deployments_url":"https://api.github.com/repos/17mon/java/deployments","created_at":"2014-11-19T03:52:56Z","updated_at":"2016-05-13T02:47:55Z","pushed_at":"2016-05-11T09:55:46Z","git_url":"git://github.com/17mon/java.git","ssh_url":"git@github.com:17mon/java.git","clone_url":"https://github.com/17mon/java.git","svn_url":"https://github.com/17mon/java","homepage":null,"size":21,"stargazers_count":78,"watchers_count":78,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":81,"mirror_url":null,"open_issues_count":6,"forks":81,"open_issues":6,"watchers":78,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/17mon/java/pulls/9"},"html":{"href":"https://github.com/17mon/java/pull/9"},"issue":{"href":"https://api.github.com/repos/17mon/java/issues/9"},"comments":{"href":"https://api.github.com/repos/17mon/java/issues/9/comments"},"review_comments":{"href":"https://api.github.com/repos/17mon/java/pulls/9/comments"},"review_comment":{"href":"https://api.github.com/repos/17mon/java/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/17mon/java/pulls/9/commits"},"statuses":{"href":"https://api.github.com/repos/17mon/java/statuses/697a6574797d14dd97e44530fd371ce31263f1b2"}}}}