public HashMap<String, String> getInitialStats() { HashMap<String, String> result = new HashMap<String, String>(); int userCnt = 0; int pendingFriends = 0; int confirmedFriends = 0; int resourceCnt = 0; HashSet<String> users = new HashSet<String>(); HqlResult queryRes; try { /* Get all unique users from user table */ HqlResultAsArrays result_as_arrays = client.hql_query_as_arrays(namespace, "SELECT * from users"); for (List<?> cell_as_array : result_as_arrays.cells) users.add(cell_as_array.get(0).toString()); userCnt = users.size(); /* Iterate all the users */ for (String user : users) { /* get resource cnt */ queryRes = client.hql_query( namespace, "SELECT * from " + ResourceTable + " where creatorid='" + user + "'"); resourceCnt += queryRes.cells.size(); /* get pending friends */ queryRes = client.hql_query( namespace, "SELECT * from " + PendingFriendsTable + " where row='" + user + "'"); pendingFriends += queryRes.cells.size(); /* get confirmed friends */ queryRes = client.hql_query( namespace, "SELECT * from " + ConfirmedFriendsTable + " where row='" + user + "'"); confirmedFriends += queryRes.cells.size(); } /* Calculate average count */ resourceCnt = resourceCnt > 0 ? resourceCnt / userCnt : 0; pendingFriends = pendingFriends > 0 ? pendingFriends / userCnt : 0; confirmedFriends = confirmedFriends > 0 ? confirmedFriends / userCnt : 0; /* Populate hash map */ result.put("usercount", String.valueOf(userCnt)); result.put("resourcesperuser", String.valueOf(resourceCnt)); result.put("avgfriendsperuser", String.valueOf(confirmedFriends)); result.put("avgpendingperuser", String.valueOf(pendingFriends)); return result; } catch (Exception e) { e.printStackTrace(System.out); return null; } }
/* init */ public boolean init() throws DBException { try { client = ThriftClient.create(HOST_IP, PORT_NUM, 16000000, true, 400 * 1024 * 1024); if (!client.namespace_exists(HTNAMESPACE)) { client.namespace_create(HTNAMESPACE); } namespace = client.namespace_open("bgclient"); } catch (Exception e) { e.printStackTrace(System.out); return false; } return true; }
public int viewProfile( int requesterID, int profileOwnerID, HashMap<String, ByteIterator> result, boolean insertImage, boolean testMode) { // TODO Auto-generated method stub int pendingFriends = 0; int confirmedFriends = 0; int resourceCnt = 0; HqlResult queryRes; try { result.put( "userid", new ObjectByteIterator(String.valueOf(profileOwnerID).getBytes("UTF-8"))); /* Get all unique users from user table */ HqlResultAsArrays result_as_arrays = client.hql_query_as_arrays( namespace, "SELECT * from users where row='" + profileOwnerID + "'"); for (List<?> cell_as_array : result_as_arrays.cells) { if (!insertImage && (cell_as_array.get(1).toString().equalsIgnoreCase("pic") || cell_as_array.get(1).toString().equalsIgnoreCase("tpic"))) { continue; } else { result.put( cell_as_array.get(1).toString(), new ObjectByteIterator(cell_as_array.get(3).toString().getBytes("UTF-8"))); } } /* get resource cnt */ queryRes = client.hql_query( namespace, "SELECT * from " + ResourceTable + " where walluserid='" + profileOwnerID + "'"); resourceCnt += queryRes.cells.size(); /* get pending friends */ if (requesterID == profileOwnerID) { queryRes = client.hql_query( namespace, "SELECT * from " + PendingFriendsTable + " where row='" + profileOwnerID + "'"); pendingFriends += queryRes.cells.size(); result.put( "pendingcount", new ObjectByteIterator(String.valueOf(pendingFriends).getBytes("UTF-8"))); } /* get confirmed friends */ queryRes = client.hql_query( namespace, "SELECT * from " + ConfirmedFriendsTable + " where row='" + profileOwnerID + "'"); confirmedFriends += queryRes.cells.size(); /* Populate hash map */ result.put( "resourcecount", new ObjectByteIterator(String.valueOf(resourceCnt).getBytes("UTF-8"))); result.put( "friendcount", new ObjectByteIterator(String.valueOf(pendingFriends).getBytes("UTF-8"))); } catch (Exception e) { e.printStackTrace(System.out); return -1; } return 0; }