@Test public void testIndex() throws Exception { CMap cmap = mockCMap("default"); MapIndexService mapIndexService = new MapIndexService(false); Map<Expression, Index> indexes = mapIndexService.getIndexes(); assertFalse(mapIndexService.hasIndexedAttributes()); Expression nameExpression = Predicates.get("name"); Expression ageExpression = Predicates.get("age"); Expression salaryExpression = Predicates.get("salary"); mapIndexService.addIndex(nameExpression, false, 0); mapIndexService.addIndex(ageExpression, true, 1); mapIndexService.addIndex(salaryExpression, false, 2); assertTrue(indexes.containsKey(nameExpression)); assertTrue(indexes.containsKey(ageExpression)); assertTrue(indexes.containsKey(salaryExpression)); assertTrue(mapIndexService.hasIndexedAttributes()); for (int i = 0; i < 20000; i++) { Employee employee = new Employee(i + "Name", i % 80, (i % 2 == 0), 100 + (i % 1000)); Record record = newRecord(cmap, i, "key" + i, employee); record.setIndexes(mapIndexService.getIndexValues(employee), mapIndexService.getIndexTypes()); mapIndexService.index(record); } long total = Runtime.getRuntime().totalMemory(); long free = Runtime.getRuntime().freeMemory(); System.out.println("Used Memory:" + ((total - free) / 1024 / 1024)); for (int i = 0; i < 10000; i++) { long start = Clock.currentTimeMillis(); QueryContext queryContext = new QueryContext("default", new SqlPredicate("salary=161 and age >20 and age <23"), mapIndexService); Set<MapEntry> results = mapIndexService.doQuery(queryContext); assertEquals(10, results.size()); } cmap.getNode().connectionManager.shutdown(); }
public void index(Record record) { final Long recordId = record.getId(); if (record.isActive()) { final Record anotherRecord = records.putIfAbsent(recordId, record); if (anotherRecord != null) { record = anotherRecord; } else { size.incrementAndGet(); } } else { remove(record); } if (indexValue != null) { Long newValueIndex = -1L; if (record.isActive() && record.hasValueData()) { newValueIndex = (long) record.getValueData().hashCode(); } indexValue.index(newValueIndex, record); } Long[] indexValues = record.getIndexes(); if (indexValues != null && hasIndexedAttributes) { byte[] indexTypes = record.getIndexTypes(); if (indexTypes == null || indexValues.length != indexTypes.length) { throw new IllegalArgumentException("index and types don't match " + Arrays.toString(indexTypes)); } Collection<Index> indexes = mapIndexes.values(); for (Index index : indexes) { if (indexValues.length > index.getAttributeIndex()) { Long newValue = indexValues[index.getAttributeIndex()]; index.index(newValue, record); } } } }
MapIndexService { public void index(Record record) { final Long recordId = record.getId(); if (record.isActive()) { final Record anotherRecord = records.putIfAbsent(recordId, record); if (anotherRecord != null) { record = anotherRecord; } else { size.incrementAndGet(); } } else { remove(record); } if (indexValue != null) { Long newValueIndex = -1L; if (record.isActive() && record.hasValueData()) { newValueIndex = (long) record.getValueData().hashCode(); } indexValue.index(newValueIndex, record); } Long[] indexValues = record.getIndexes(); if (indexValues != null && hasIndexedAttributes) { byte[] indexTypes = record.getIndexTypes(); if (indexTypes == null || indexValues.length != indexTypes.length) { throw new IllegalArgumentException("index and types don't match " + Arrays.toString(indexTypes)); } Collection<Index> indexes = mapIndexes.values(); for (Index index : indexes) { if (indexValues.length > index.getAttributeIndex()) { Long newValue = indexValues[index.getAttributeIndex()]; index.index(newValue, record); } } } } }
MapIndexService { public void index(Record record) { final Long recordId = record.getId(); if (record.isActive()) { final Record anotherRecord = records.putIfAbsent(recordId, record); if (anotherRecord != null) { record = anotherRecord; } else { size.incrementAndGet(); } } else { remove(record); } if (indexValue != null) { Long newValueIndex = -1L; if (record.isActive() && record.hasValueData()) { newValueIndex = (long) record.getValueData().hashCode(); } indexValue.index(newValueIndex, record); } Long[] indexValues = record.getIndexes(); if (indexValues != null && hasIndexedAttributes) { byte[] indexTypes = record.getIndexTypes(); if (indexTypes == null || indexValues.length != indexTypes.length) { throw new IllegalArgumentException("index and types don't match " + Arrays.toString(indexTypes)); } Collection<Index> indexes = mapIndexes.values(); for (Index index : indexes) { if (indexValues.length > index.getAttributeIndex()) { Long newValue = indexValues[index.getAttributeIndex()]; index.index(newValue, record); } } } } MapIndexService(boolean valueIndexed); }
MapIndexService { public void index(Record record) { final Long recordId = record.getId(); if (record.isActive()) { final Record anotherRecord = records.putIfAbsent(recordId, record); if (anotherRecord != null) { record = anotherRecord; } else { size.incrementAndGet(); } } else { remove(record); } if (indexValue != null) { Long newValueIndex = -1L; if (record.isActive() && record.hasValueData()) { newValueIndex = (long) record.getValueData().hashCode(); } indexValue.index(newValueIndex, record); } Long[] indexValues = record.getIndexes(); if (indexValues != null && hasIndexedAttributes) { byte[] indexTypes = record.getIndexTypes(); if (indexTypes == null || indexValues.length != indexTypes.length) { throw new IllegalArgumentException("index and types don't match " + Arrays.toString(indexTypes)); } Collection<Index> indexes = mapIndexes.values(); for (Index index : indexes) { if (indexValues.length > index.getAttributeIndex()) { Long newValue = indexValues[index.getAttributeIndex()]; index.index(newValue, record); } } } } MapIndexService(boolean valueIndexed); void remove(Record record); void index(Record record); Collection<Record> getOwnedRecords(); Long[] getIndexValues(Object value); byte[] getIndexTypes(); Index addIndex(Expression expression, boolean ordered, int attributeIndex); Set<MapEntry> doQuery(QueryContext queryContext); Map<Expression, Index> getIndexes(); boolean hasIndexedAttributes(); Index getValueIndex(); Index[] getIndexesInOrder(); void appendState(StringBuffer sbState); void clear(); int size(); }
MapIndexService { public void index(Record record) { final Long recordId = record.getId(); if (record.isActive()) { final Record anotherRecord = records.putIfAbsent(recordId, record); if (anotherRecord != null) { record = anotherRecord; } else { size.incrementAndGet(); } } else { remove(record); } if (indexValue != null) { Long newValueIndex = -1L; if (record.isActive() && record.hasValueData()) { newValueIndex = (long) record.getValueData().hashCode(); } indexValue.index(newValueIndex, record); } Long[] indexValues = record.getIndexes(); if (indexValues != null && hasIndexedAttributes) { byte[] indexTypes = record.getIndexTypes(); if (indexTypes == null || indexValues.length != indexTypes.length) { throw new IllegalArgumentException("index and types don't match " + Arrays.toString(indexTypes)); } Collection<Index> indexes = mapIndexes.values(); for (Index index : indexes) { if (indexValues.length > index.getAttributeIndex()) { Long newValue = indexValues[index.getAttributeIndex()]; index.index(newValue, record); } } } } MapIndexService(boolean valueIndexed); void remove(Record record); void index(Record record); Collection<Record> getOwnedRecords(); Long[] getIndexValues(Object value); byte[] getIndexTypes(); Index addIndex(Expression expression, boolean ordered, int attributeIndex); Set<MapEntry> doQuery(QueryContext queryContext); Map<Expression, Index> getIndexes(); boolean hasIndexedAttributes(); Index getValueIndex(); Index[] getIndexesInOrder(); void appendState(StringBuffer sbState); void clear(); int size();  }