Created
October 22, 2013 23:13
-
-
Save enachb/7109818 to your computer and use it in GitHub Desktop.
Cassandra tag implementation with substring matching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.datastax.driver.core.*; | |
| import java.util.HashSet; | |
| import java.util.Set; | |
| import java.util.UUID; | |
| /** | |
| * CREATE TABLE tag (id uuid, foreignId uuid ,name text, lowerName text,PRIMARY KEY (id) ); | |
| * | |
| * create index TagNameIdx on tag(name); | |
| * create index TagNameLowerIdx on tag(lowerName); | |
| * create index TagForeignIdIdx on tag(foreignId); | |
| */ | |
| public class TagManager { | |
| private Session _session = null; | |
| private PreparedStatement _updateSt = null; | |
| private PreparedStatement _matchSt = null; | |
| public TagManager(Session session) { | |
| _session = session; | |
| _updateSt = session.prepare("update tag set foreignId = ?, name = ?, lowerName = ? where id = ?"); | |
| _matchSt = session.prepare("select id, name from tag where foreignId = ? AND lowerName >= ? and lowerName <= ? allow filtering"); | |
| } | |
| public boolean add(UUID foreignId, String name) { | |
| BoundStatement stmnt = _updateSt.bind(foreignId, name, name.toLowerCase(), UUID.randomUUID()); | |
| _session.execute(stmnt); | |
| //TODO need to really return the proper execution state. Cassandra will thrown an exception if anything goes wrong. | |
| return true; | |
| } | |
| public Set<String> match(UUID foreignId, String subString) { | |
| HashSet<String> ids = new HashSet<String>(); | |
| // Page through results and refetch if required. | |
| BoundStatement stmt = _matchSt.bind(foreignId, subString.toLowerCase(), subString.toLowerCase() + Character.MAX_VALUE); | |
| ResultSet result = _session.execute(stmt); | |
| for (Row row : result.all()) { | |
| ids.add(row.getString("name")); | |
| } | |
| return ids; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.datastax.driver.core.Cluster; | |
| import com.datastax.driver.core.Session; | |
| import java.util.Random; | |
| import java.util.UUID; | |
| /** | |
| */ | |
| public class TagManagerTest { | |
| /** | |
| **/ | |
| private static Random rnd = new Random(); | |
| public static void main(String[] args) { | |
| Cluster cluster = Cluster.builder().addContactPoint("velum.io").build(); | |
| try { | |
| Session session = cluster.connect("rm"); | |
| TagManager mgr = new TagManager(session); | |
| UUID foreignId = UUID.randomUUID(); | |
| for (int i = 0; i < 100; i++) { | |
| UUID uuid = UUID.randomUUID(); | |
| mgr.add(foreignId, "tag" + i); | |
| } | |
| System.out.println("tag substr: " + mgr.match(foreignId, "tag0")); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } finally { | |
| cluster.shutdown(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment