@Override public void write(ObjectDataOutput out, Index xIndex) throws IOException { super.writeEntity(out, xIndex); out.writeUTF(xIndex.getName()); out.writeUTF(xIndex.getDocumentType()); out.writeUTF(xIndex.getTypePath()); out.writeUTF(xIndex.getPath()); out.writeUTF(xIndex.getDataType().toString()); out.writeBoolean(xIndex.isCaseSensitive()); out.writeBoolean(xIndex.isRange()); out.writeBoolean(xIndex.isUnique()); out.writeUTF(xIndex.getDescription()); out.writeBoolean(xIndex.isEnabled()); }
/** * * @param name the name of schema index to enable * @param enable the new enable value * @return true if the index enable flag has been changed, false otherwise */ public boolean enableIndex(String name, boolean enable) { for (Index index: indexes) { if (name.equals(index.getName())) { return index.setEnabled(enable); } } return false; }
public Collection<Integer> getTypeIndexes(String root, boolean uniqueOnly) { Collection<Integer> result = new HashSet<>(); //idxDict.keySet(p); for (Map.Entry<Integer, Index> e: idxDict.entrySet()) { if (root.equals(e.getValue().getTypePath())) { if (uniqueOnly) { if (!e.getValue().isUnique()) { continue; } } result.add(e.getKey()); } } logger.trace("getTypeIndexes.exit; returning {} path for root: {}, unique: {}", result.size(), root, uniqueOnly); return result; }
@Override public Path[] createIndex(Index index) throws BagriException { Set<Integer> paths = getPathsForIndex(index); Path[] result = new Path[paths.size()]; int idx = 0; for (Integer pathId: paths) { //idxDict.putIfAbsent(pathId, index); idxDict.put(pathId, index); //indexStats.initStats(index.getName()); if (index.isRange()) { rangeIndex.put(pathId, new TreeMap<Comparable, Integer>()); logger.trace("createIndex; registered range index path: {}", index.getName()); } result[idx] = mdlMgr.getPath(pathId); idx++; } if (isPatternIndex(index)) { //String path = mdlMgr.normalizePath(index.getPath()); String path = index.getPath(); path = path.replaceAll("\\{", Matcher.quoteReplacement("\\{")); path = path.replaceAll("\\}", Matcher.quoteReplacement("\\}")); patterns.put(index, Pattern.compile(PathBuilder.regexFromPath(path))); } return result; }
boolean first = false; IndexedValue xidx = entry.getValue(); if (idx.isUnique()) { long id = DocumentKey.toDocumentId(docKey); if (!checkUniquiness((UniqueDocument) xidx, id)) { throw new BagriException("unique index '" + idx.getName() + "' violated for docKey: " + docKey + ", pathId: " + pathId + ", value: " + value, BagriException.ecIndexUnique); xidx.addDocument(docKey, txId); if (idx.isRange()) { TreeMap<Comparable, Integer> range = rangeIndex.get(pathId); Integer count = range.get(value); updateStats(idx.getName(), true, first, xidx.getCount() - oldCount, xidx.getSize());
/** * * @param name the name of the index to search for * @return the XDM index instance if it is found, null otherwise */ public Index getIndex(String name) { for (Index index: indexes) { if (name.equals(index.getName())) { return index; } } return null; }
private Set<Integer> getPathsForIndex(Index index) throws BagriException { String path = index.getPath(); Set<Integer> result; if (PathBuilder.isRegexPath(path)) { // replace {} result = mdlMgr.translatePathFromRegex(index.getDocumentType(), PathBuilder.regexFromPath(path)); } else { int dataType = XQUtils.getBaseTypeForTypeName(index.getDataType()); Path xPath = mdlMgr.translatePath(index.getDocumentType(), path, NodeKind.fromPath(path), 0, dataType, Occurrence.zeroOrOne); result = new HashSet<>(1); result.add(xPath.getPathId()); } logger.trace("getPathsForIndex; returning {} for index {}", result, index); return result; }
@Override public Path[] dropIndex(Index index) throws BagriException { // we must not do translate here! Set<Integer> paths = getPathsForIndex(index); Path[] result = new Path[paths.size()]; int idx = 0; for (Integer pathId: paths) { idxDict.remove(pathId); //indexStats.deleteStats(index.getName()); if (index.isRange()) { rangeIndex.remove(pathId); logger.trace("deleteIndex; unregistered range index path: {}", index.getName()); } result[idx] = mdlMgr.getPath(pathId); idx++; } patterns.remove(index); return result; }
private Object getIndexedValue(Index idx, int pathId, Object value) { int baseType = getBaseTypeForTypeName(idx.getDataType()); if (isStringTypeCompatible(baseType)) { value = value.toString(); if (!idx.isCaseSensitive()) { value = ((String) value).toLowerCase(); } } else { Path xPath = mdlMgr.getPath(pathId); if (xPath.getDataType() != baseType) { logger.info("getIndexedValue; index [{}] and path [{}] types are not compatible; Index: {}; path: {}; value: {}({})", baseType, xPath.getDataType(), idx, pathId, value.getClass().getName(), value); try { // conversion from path type to index type value = getAtomicValue(baseType, value.toString()); } catch (Exception ex) { // just log error and use old value logger.error("getIndexedValue.error: " + ex, ex); } } } return value; }
public void addIndex(long docId, int pathId, String path, Object value) throws BagriException { // shouldn't we index NULL values too? create special NULL class for this.. if (value != null) { Collection<Index> indices = getPathIndices(pathId, path); if (indices.isEmpty()) { return; } IndexKey xid = factory.newIndexKey(pathId, value); for (Index idx: indices) { //indexPath(idx, docId, pathId, value); if (idx.isEnabled()) { long txId = idx.isUnique() ? txMgr.getCurrentTxId() : TX_NO; ValueIndexator indexator = new ValueIndexator(docId, txId); if (indexAsynch) { idxCache.submitToKey(xid, indexator); } else { // this does not work in transaction! idxCache.executeOnKey(xid, indexator); } logger.trace("addIndex; index submit for key {}", xid); } } } }
public Set<Long> getIndexedDocuments(int pathId, PathExpression pex, Object value) { logger.trace("getIndexedDocuments.enter; pathId: {}, PEx: {}, value: {}", pathId, pex, value); Set<Long> result = null; Index idx = idxDict.get(pathId); if (idx != null && idx.isEnabled()) { if (Comparison.EQ.equals(pex.getCompType())) { if (value instanceof Collection) { result = getIndexedDocuments(idx, pathId, (Iterable) value); } else { result = getIndexedDocuments(idx, pathId, value); } } else { value = getIndexedValue(idx, pathId, value); if (idx.isRange() && value instanceof Comparable) { result = getIndexedDocuments(idx, pathId, pex.getCompType(), (Comparable) value); } else { logger.debug("getIndexedDocuments; value is not comparable: {}", value.getClass()); } } } logger.trace("getIndexedDocuments.exit; returning: {}", result == null ? null : result.size()); return result; }
public boolean isIndexEnabled(int pathId) { Index idx = idxDict.get(pathId); if (idx != null) { return idx.isEnabled(); } return false; }
Index addIndex(String name, String docType, String path, String dataType, boolean caseSensitive, boolean range, boolean unique, String description) { // TODO: what it is for?! String typePath = path; //xdmRepo.getModelManagement().normalizePath(docType); Index index = new Index(1, new Date(), getCurrentUser(), name, docType, typePath, path, new QName(xs_ns, dataType, xs_prefix), caseSensitive, range, unique, description, true); Schema schema = getEntity(); if (schema.addIndex(index)) { // store schema! flushEntity(schema); return index; } return null; }
private boolean isPatternIndex(Index index) { return PathBuilder.isRegexPath(index.getPath()); }
/** * * @param name the name of the index to remove from schema * @return true if index has been removed, false otherwise */ public Index removeIndex(String name) { for (Index index: indexes) { if (name.equals(index.getName())) { if (indexes.remove(index)) { return index; } break; } } return null; }
if (idx != null && idx.isEnabled()) { IndexKey ik = factory.newIndexKey(dta.getPathId(), dta.getValue());
@Override public Index read(ObjectDataInput in) throws IOException { Object[] entity = super.readEntity(in); Index xIndex = new Index( (int) entity[0], (Date) entity[1], (String) entity[2], in.readUTF(), in.readUTF(), in.readUTF(), in.readUTF(), QName.valueOf(in.readUTF()), in.readBoolean(), in.readBoolean(), in.readBoolean(), in.readUTF(), in.readBoolean()); return xIndex; }
private Set<Long> getIndexedDocuments(Index idx, int pathId, Iterable values) { Set<IndexKey> keys = new HashSet<>(); for (Object value: values) { keys.add(factory.newIndexKey(pathId, value)); } Map<IndexKey, IndexedValue> xidv = idxCache.getAll(keys); Set<Long> ids = new HashSet<>(xidv.size()); for (IndexedValue value: xidv.values()) { ids.addAll(value.getDocumentKeys()); } updateStats(idx.getName(), true, xidv.size()); updateStats(idx.getName(), false, keys.size() - xidv.size()); return ids; }
private Set<Long> getIndexedDocuments(Index idx, int pathId, Object value) { IndexKey idxk = factory.newIndexKey(pathId, value); IndexedValue xidv = idxCache.get(idxk); if (xidv != null) { updateStats(idx.getName(), true, 1); return xidv.getDocumentKeys(); } updateStats(idx.getName(), false, 1); return Collections.emptySet(); }