Connect()) { return 0; } // Initialize $arr_results = array(); // Validate if ($start_from<1) $start_from = 1; if ($how_many<1) $how_many = 20; if ($how_many>200) $how_many = 200; // Set these for easier access $result_begin = ($start_from-1)*$how_many + 1; $result_end = $result_begin + $how_many - 1; // Set search start time $search_start_time = CassandraGetMicroTime(); // Sort arr_filters if (!empty($arr_filters)) foreach ($arr_filters as $filtername => $filtervalue) $arr_sorted_filters[] = $filtername; if (!empty($arr_sorted_filters)) sort($arr_sorted_filters); // Construct key if (!empty($arr_sorted_filters)) { foreach ($arr_sorted_filters as $filtername) { if (!empty($filter)) $filter .= ","; $filter .= $filtername.":".strtolower(trim($arr_filters[$filtername])); } } else $filter = ":"; // Get totals $keys = $cassandra->GetKeys("totals_by_filter", "$account_id-$tablename-$filter", "$account_id-$tablename-$filter-z"); if (!empty($keys)) foreach ($keys as $key) { $record = $cassandra->GetRecordByKey('totals_by_filter', $key); { $totals += $record['count']; } } // Prepare search key if (!empty($search)) { $searchkey = "-search-".trim(strtolower($search)); } else $searchkey = ""; // Get keys $keys = $cassandra->GetKeys("recordid_by_filter", "$account_id-$tablename-$filter$searchkey", "$account_id-$tablename-$filter$searchkey-z", null, 5000, !$sort_ascending); $cnt_keys = count($keys); // If we need to read records in descending order, rsort if (!$sort_ascending) rsort($keys); // Get record keys $cnt_records = 0; if (!empty($keys)) foreach ($keys as $key) { $record = $cassandra->GetRecordByKey("recordid_by_filter", $key); if (!empty($record)) { $cnt_records++; if ($cnt_records < $result_begin) continue; // Re-arrange the order of fields in the record, before adding it to arr_results // This makes it easier to display fields in a meaningful order when iterating through // all fields in the record with a foreach. By default Cassandra sorts fields alphabetically $result_record['table'] = $tablename; $result_record['record_id'] = $record['record_id']; $result_record['owner_id'] = $record['owner_id']; $result_record['content'] = $record['content']; foreach ($record as $loop_name => $loop_value) $result_record[$loop_name] = $loop_value; $arr_results[] = $result_record; if (count($arr_results)==$how_many) break; } if (count($arr_results)==$how_many) break; } // Set search End time and calculate how long it took $search_end_time = CassandraGetMicroTime(); $search_time = number_format($search_end_time - $search_start_time,3); // Setup stats record $max_page = number_format($totals/$how_many,2,'.',''); if (strlen($max_page)>3) $max_page = substr($max_page, 0, strlen($max_page)-3); $output['stats'] = array("SEARCH_QUERY_RAW"=>$search, "RESULT_BEGIN" => $result_begin, "RESULT_END" => $result_end, "TOTAL_RECORDS" => $totals, "NUMBER_OF_RESULTS" => count($arr_results), "FILTERS_COUNT" => count($arr_filters), "SEARCH_TOT_TIME" => $search_time, "TOTAL_FILTER_KEYS" => $cnt_keys, "PAGE" => ($start_from), "PER_PAGE" => $how_many, "MAX_PAGE" => $max_page); // Setup result record $output['items'] = $arr_results; // Return results return $output; } // Creates a column family for every table that the account has in mysql function CassandraCreateTables($account_id) { // Basic validation if(empty($account_id)) { return false; } // Initialize Cassandra $cassandra = GetCassandraDB("spi"); if (!$cassandra->Connect()) { return 0; } // Loop through all mysql tables for this account $result = mysql_query('show tables'); while($row=mysql_fetch_array($result)) { $sTableName = $row[0]; // Skip old tables that start with I_ // Skip Wordpress tables if(stristr($sTableName,'I_') !== FALSE || stristr($sTableName,'wp_') !== FALSE) { continue; } // Create the column family (table) in cassandra $cfdef = null; $record = array(); $record['keyspace'] = 'spi'; $record['name'] = strtolower($sTableName); $record['column_type'] = "Standard"; $record['comparator_type'] = "BytesType"; $cfdef = new cassandra_CfDef($record); $cassandra->AddColumnFamily($cfdef); } return true; } // Inserts the given record into the tablename (column family) using the specified key. function CassandraInsertRecord($account_id, $tablename, $key, $record) { // Basic validation if(empty($account_id) || empty($tablename) || empty($key) || empty($record)) { return false; } // Initialize Cassandra $cassandra = GetCassandraDB("spi"); if (!$cassandra->Connect()) { return 0; } // Insert Record $result = $cassandra->InsertRecord($tablename, $account_id."-".$key, $record); return $result; } function CassandraDeleteRecord($account_id,$tablename,$key) { // Basic validation if(empty($account_id) || empty($tablename) || empty($key)) { return false; } // Initialize Cassandra $cassandra = GetCassandraDB("spi"); if (!$cassandra->Connect()) { return 0; } // Delete the Record $result = $cassandra->DeleteRecord($tablename, $account_id."-".$key); // Return the result return $result; } // Delete a record from function CassandraGetRecord($account_id, $tablename, $key) { // Basic validation if(empty($account_id) || empty($tablename) || empty($key)) { return false; } // Initialize Cassandra $cassandra = GetCassandraDB("spi"); if (!$cassandra->Connect()) { return 0; } // Get the specified record $result = $cassandra->GetRecordByKey($tablename, $account_id."-".$key); // Return the result return $result; } ?>