@Override public boolean equals(Object o) { if (o instanceof FastIndex) { FastIndex comp = (FastIndex) o; if (nonZeroEntries != comp.nonZeroEntries) { return false; } else { return Arrays.equals(entryNums, comp.entryNums) && Arrays.equals(entryVals, comp.entryVals); } } else if (o instanceof Index) { Index comp = (Index) o; if (nonZeroEntries() != comp.nonZeroEntries()) { return false; } else { for (int i = 0; i < nonZeroEntries; i++) { if (entryVals[i] != comp.get(entryNums[i])) { return false; } } return true; } } return false; }
@Test public void testRemove() { Index<String> idx = new Index<String>(String.class, "in", Predicates.containsPattern("ok")); idx.update(Update(String.class, 1, "in", "ok")); idx.update(Update(String.class, 2, "in", "ok")); idx.update(Update(String.class, 1, "in", "nope")); idx.update(Update(Integer.class, 2, "in", "nope")); // wrong type Assert.assertEquals(idx.get(), ArrayIdSet.ofSorted(2)); }
@Test public void testAdd() { Index<String> idx = new Index<String>(String.class, "in", Predicates.containsPattern("ok")); idx.update(Update(String.class, 1, "in", "ok")); idx.update(Update(Integer.class, 2, "in", "ok")); idx.update(Update(String.class, 3, "x", "ok")); idx.update(Update(String.class, 3, "in", "x")); Assert.assertEquals(idx.get(), ArrayIdSet.ofSorted(1)); }
public void addAssignmentToIndex(Assignment assignment) { for (int i = 0; i < assignment.size(); i++) { GdlConstant c = assignment.get(i); if (indices.size() <= i) indices.add(new Index()); Index index = indices.get(i); if (!index.containsKey(c)) index.put(c, new Assignments()); Assignments val = index.get(c); val.add(assignment); } }
private CustomObject load(long id) { Transaction tx = m_pageFile.tx(); try { Index<Long, Integer> idx = m_indexFactory.open(tx); Integer pageNumber = idx.get(id); // see if the index contains this object if (pageNumber == null) { return null; } else { return load(tx, pageNumber); } } finally { tx.commit(); tx.close(); } }
private void store(CustomObject c) { Transaction tx = m_pageFile.tx(); try { Index<Long, Integer> idx = m_indexFactory.open(tx); Integer pageNumber = idx.get(c.id); // see if the index contains this object if (pageNumber == null) { pageNumber = tx.alloc(); store(tx, pageNumber, c); idx.put(c.id, pageNumber); } else { store(tx, pageNumber, c); } tx.commit(); } finally { tx.close(); } }
// Coolest method in this whole thing, does the real work of the JOIN stuff private Set<Map<GdlVariable, GdlConstant>> findSatisfyingInstantiations( List<Condition> conditions, int idx, Map<GdlVariable, GdlConstant> instantiation) { Set<Map<GdlVariable, GdlConstant>> rval = new HashSet<>(); if (idx == conditions.size()) { rval.add(instantiation); return rval; } Condition cond = conditions.get(idx); Domain dom = cond.dom; Assignments assignments = null; for (int i = 0; i < cond.template.size(); i++) { GdlTerm t = cond.template.get(i); GdlConstant c = null; if (t instanceof GdlVariable) { GdlVariable v = (GdlVariable) t; if (instantiation.containsKey(v)) c = instantiation.get(v); } else if (t instanceof GdlConstant) c = (GdlConstant) t; if (c != null) { if (assignments == null) { assignments = new Assignments(); if (dom.indices.size() > i) // if this doesn't hold it is because there are no assignments and the indices // haven't been set up yet { Index index = dom.indices.get(i); if (index.containsKey(c)) // Might be no assignment which satisfies this condition assignments.addAll(index.get(c)); } } else { if (dom.indices.size() > i) { Index index = dom.indices.get(i); if (index.containsKey(c)) // Might be no assignment which satisfies this condition assignments.retainAll(index.get(c)); } else // This is when we've tried to find an assignment for a form that doesn't have any // assignments yet. Pretend it returned an empty set assignments.clear(); } } } if (assignments == null) // case where there are no constants to be consistent with { assignments = dom.assignments; } for (Assignment a : assignments) { Map<GdlVariable, GdlConstant> newInstantiation = new HashMap<>(instantiation); for (int i = 0; i < a.size(); i++) { GdlTerm t = cond.template.get(i); if (t instanceof GdlVariable) { GdlVariable var = (GdlVariable) t; if (!instantiation.containsKey(var)) newInstantiation.put(var, a.get(i)); } } rval.addAll(findSatisfyingInstantiations(conditions, idx + 1, newInstantiation)); } return rval; }