import os,sys,time maps = {} def count(key): k = str(key) if maps.has_key(k) : val = maps[k] maps[k]=val+1 else: maps[k]=1 def listDir(rootDir): list_dirs = os.walk(rootDir) year_sec = 60*60*24*365 for root, dirs, files in list_dirs: for d in dirs: year=int(os.path.getmtime(os.path.join(root, d))/year_sec+1970) count(year) for f in files: year=int(os.path.getctime(os.path.join(root, f))/year_sec+1970) count(year) print maps listDir(sys.argv[1])
package com.czp.opensource; import java.util.concurrent.ConcurrentHashMap; import redis.clients.jedis.Client; import redis.clients.jedis.Jedis; /** * Function:分布式Hashmap支持在线扩容<br> * * Date :2015年12月9日 <br> * Author :coder_czp@126.com<br> * Copyright (c) 2015,coder_czp@126.com All Rights Reserved. */ public class DistributedMap { private static final int BASE = 10000; private static final String SPLIT = "X"; private static final String SIZE = "SIZE"; private ConcurrentHashMap<Jedis, Integer> jedisArr = new ConcurrentHashMap<Jedis, Integer>(); /** * 格式:[host:port:pwd,...] * * @param redisGroup */ public DistributedMap(String[] redisGroup) { for (String string : redisGroup) { String[] arr = string.split(":"); addRedisServer(arr[0], Integer.valueOf(arr[1]), arr[2]); } } /** * 添加服务,如果有同名ip和端口,则返回false * * @param host * @param port * @param pwd * @return */ public synchronized boolean addRedisServer(String host, int port, String pwd) { if (getRedisServer(host, port) != null) { return false; } Jedis jedis = new Jedis(host, port); if (pwd != null && pwd.trim().length() > 0) jedis.auth(pwd.trim()); int redisKeyCount = getRedisKeyCount(jedis); jedisArr.put(jedis, redisKeyCount); return true; } /** * 返回key个数 * * @return */ public long size() { long size = 0; for (Jedis jedis : jedisArr.keySet()) { size += jedisArr.get(jedis); } return size; } /** * 是否包含某个key * * @param key * @return */ public boolean containsKey(String key) { for (Jedis jedis : jedisArr.keySet()) { if (jedis.exists(key)) return true; } return false; } /** * 获取key对应的value * * @param key * 必须是:/encode(host[h32],port[l32])/port/key * @return */ public String get(String key) { Jedis jedis = getRedisFromKey(key); return jedis == null ? null : jedis.get(key); } /** * 添加key-value,会将key包装为/encode(host,port)/key再保存 * * @param key * @param value * @return /encode(host[h23],port[l32])/key */ public String put(String key, String value) { if (!key.startsWith("/")) key = "/".concat(key); Jedis redis = chooseRedisServer(key); Client client = redis.getClient(); String prefex = endcodeIpPort(client.getHost(), client.getPort()); String wrapKey = String.format("/%s%s", prefex, key); redis.set(wrapKey, value); comareAndSetRedisSize(redis); return wrapKey; } /** * 选择Redis服务器,选择存储最少的服务 * * @param key * @return */ private Jedis chooseRedisServer(String key) { int minSize = -1; Jedis jedis = null; for (Jedis tmp : jedisArr.keySet()) { if (minSize == -1) { minSize = jedisArr.get(tmp); jedis = tmp; } else if (jedisArr.get(tmp) < minSize) { jedis = tmp; } } return jedis; } /** * 删除key-value * * @param key * @return */ public boolean remove(String key) { Jedis redis = getRedisFromKey(key); if (redis == null) return false; redis.del(key); return true; } /** * 关闭所有连接,释放资源 */ public synchronized void release() { for (Jedis tmp : jedisArr.keySet()) { tmp.quit(); } jedisArr.clear(); } /** * 更新redis的size值 * * @param redis */ private synchronized void comareAndSetRedisSize(Jedis redis) { int oldSize = getRedisKeyCount(redis); int curSize = jedisArr.get(redis); int max = Math.max(oldSize, curSize) + 1; redis.set(SIZE, String.valueOf(max)); jedisArr.put(redis, max); } private synchronized int getRedisKeyCount(Jedis jedis) { String ssize = jedis.get(SIZE); if (ssize == null) return 0; return Integer.valueOf(ssize); } /** * 选择匹配的redis * * @param ip * @param port * @return */ private Jedis getRedisServer(String redisIp, int port) { for (Jedis jedis : jedisArr.keySet()) { Client client = jedis.getClient(); String host = client.getHost(); int portx = client.getPort(); if (host.equals(redisIp) && portx == port) return jedis; } return null; } /** * key:endcode[ip,port]/key * * @param key * @return */ private Jedis getRedisFromKey(String key) { String[] arr = key.split("/"); String[] ipPort = decodeIpPort(arr[1]); Jedis jedis = getRedisServer(ipPort[0], Integer.valueOf(ipPort[1])); return jedis; } /** * @param string * @return */ private static String[] decodeIpPort(String string) { String[] arr = string.split(SPLIT); char frist = arr[0].charAt(0); int ipVal = Integer.valueOf(arr[0].substring(1)); if (frist == 'J') { ipVal = 0 - ipVal; } String port = String.valueOf(Integer.valueOf(arr[1]) ^ BASE); String ip = String.valueOf(PubTools.intToIP(ipVal)); return new String[] { ip, port }; } /** * 将ip转换为int 和port合并成int后用base64编码 * * @param ipstr * @param port * @return */ private static String endcodeIpPort(String ipstr, int port) { int ipToInt = PubTools.ipToInt(ipstr); String ip = null; if (ipToInt < 0) { ip = "J".concat(String.valueOf(0 - ipToInt)); } else { ip = "M".concat(String.valueOf(ipToInt)); } return String.valueOf(ip).concat(SPLIT) .concat(String.valueOf(port ^ BASE)); } public static void main(String[] args) { Jedis edis = new Jedis("192.168.20.78"); System.out.println(edis.get("mset2")); edis.quit(); } }