📄 versionedstorage.java
字号:
this.defs = defs;
this.kind = kind;
this.timestamp = timestamp;
iterators = new Iterator[defs.length+1];
iterators[iterators.length-1] = iterator;
visited = new HashSet();
pos = iterators.length-1;
gotoNext();
}
public boolean hasNext() {
return currThing != null;
}
public Object next() {
if (currThing == null) {
throw new NoSuchElementException();
}
Thing thing = currThing;
gotoNext();
return thing;
}
public void remove() {
throw new UnsupportedOperationException();
}
private void gotoNext() {
while (true) {
while (pos < iterators.length && !iterators[pos].hasNext()) {
pos += 1;
}
if (pos == iterators.length) {
currThing = null;
return;
}
Thing thing = (Thing)iterators[pos].next();
if (kind == SearchKind.LatestVersion) {
if (!thing.isLatest()) {
continue;
}
} else if (kind == SearchKind.LatestBefore) {
if (thing.timestamp.compareTo(timestamp) > 0) {
continue;
}
} else if (kind == SearchKind.OldestAfter) {
if (thing.timestamp.compareTo(timestamp) < 0) {
continue;
}
}
if (pos == 0) {
Integer oid = new Integer(thing.getOid());
if (visited.contains(oid)) {
continue;
} else {
visited.add(oid);
}
currThing = thing;
return;
}
pos -= 1;
Key key = new Key(new Object[]{defs[pos], thing.vh});
iterators[pos] = root.refPropIndex.iterator(key, key, Index.ASCENT_ORDER);
}
}
}
private Iterator searchReferenceProperty(VersionHistory type, String uri, NameVal[] patterns, SearchKind kind, Date timestamp, NameVal prop, boolean compound, PropDef def, ArrayList refs)
{
refs.add(def);
NameVal[] restOfPatterns = compound ? patterns : subArray(patterns);
PropDef[] refProps = (PropDef[])refs.toArray(new PropDef[refs.size()]);
Object val = prop.val;
if (Symbols.Timestamp.equals(prop.name)) {
if (val instanceof Range) {
Range range = (Range)val;
if (range.from instanceof Date) {
Key fromKey = new Key((Date)range.from, range.fromInclusive);
Key tillKey = new Key((Date)range.till, range.tillInclusive);
return new SearchResult(root, type, uri, restOfPatterns, kind, timestamp,
new ReferenceIterator(root, refProps,
root.timeIndex.iterator(fromKey, tillKey, Index.ASCENT_ORDER),
kind, timestamp));
}
} else if (val instanceof Date) {
Key key = new Key((Date)val);
return new SearchResult(root, type, uri, restOfPatterns, kind, timestamp,
new ReferenceIterator(root, refProps,
root.timeIndex.iterator(key, key, Index.ASCENT_ORDER),
kind, timestamp));
}
return EmptyIterator;
} else if (Symbols.Rectangle.equals(prop.name)) {
if (val instanceof NameVal[]) {
NameVal[] coord = (NameVal[])val;
if (coord.length == 4) {
RectangleR2 r = new RectangleR2(((Number)coord[0].val).doubleValue(),
((Number)coord[1].val).doubleValue(),
((Number)coord[2].val).doubleValue(),
((Number)coord[3].val).doubleValue());
return new SearchResult(root, type, uri, restOfPatterns, kind, timestamp,
new ReferenceIterator(root, refProps,
root.spatialIndex.iterator(r), kind, timestamp));
}
}
} else if (Symbols.Point.equals(prop.name)) {
if (val instanceof NameVal[]) {
NameVal[] coord = (NameVal[])val;
if (coord.length == 2) {
double x = ((Number)coord[0].val).doubleValue();
double y = ((Number)coord[1].val).doubleValue();
RectangleR2 r = new RectangleR2(x, y, x, y);
return new SearchResult(root, type, uri, restOfPatterns, kind, timestamp,
new ReferenceIterator(root, refProps,
root.spatialIndex.iterator(r), kind, timestamp));
}
}
} else if (Symbols.Keyword.equals(prop.name)) {
if (val instanceof String) {
ArrayList keywords = getKeywords((String)val);
Iterator[] occurences = new Iterator[keywords.size()];
for (int i = 0; i < occurences.length; i++) {
Key key = new Key((String)keywords.get(i));
occurences[i] = root.inverseIndex.iterator(key, key, Index.ASCENT_ORDER);
}
return new SearchResult(root, type, uri, restOfPatterns, kind, timestamp,
new ReferenceIterator(root, refProps,
db.merge(occurences), kind, timestamp));
}
}
def = (PropDef)root.propDefIndex.get(prop.name);
if (def == null) {
return EmptyIterator;
}
if (val instanceof Range) {
Range range = (Range)val;
if (range.from instanceof Number) {
Key fromKey = new Key(new Object[]{def, range.from}, range.fromInclusive);
Key tillKey = new Key(new Object[]{def, range.till}, range.tillInclusive);
return new SearchResult(root, type, uri, restOfPatterns, kind, timestamp,
new ReferenceIterator(root, refProps,
root.numPropIndex.iterator(fromKey, tillKey, Index.ASCENT_ORDER),
kind, timestamp));
} else if (range.from instanceof Date) {
Key fromKey = new Key(new Object[]{def, range.from}, range.fromInclusive);
Key tillKey = new Key(new Object[]{def, range.till}, range.tillInclusive);
return new SearchResult(root, type, uri, restOfPatterns, kind, timestamp,
new ReferenceIterator(root, refProps,
root.timePropIndex.iterator(fromKey, tillKey, Index.ASCENT_ORDER),
kind, timestamp));
} else {
Key fromKey = new Key(new Object[]{def, range.from}, range.fromInclusive);
Key tillKey = new Key(new Object[]{def, range.till}, range.tillInclusive);
return new SearchResult(root, type, uri, restOfPatterns, kind, timestamp,
new ReferenceIterator(root, refProps,
root.strPropIndex.iterator(fromKey, tillKey, Index.ASCENT_ORDER),
kind, timestamp));
}
}
if (val instanceof String) {
String str = (String)prop.val;
int wc = str.indexOf('*');
if (wc < 0) {
Key key = new Key(new Object[]{def, str});
return new SearchResult(root, type, uri, restOfPatterns, kind, timestamp,
new ReferenceIterator(root, refProps,
root.strPropIndex.iterator(key, key, Index.ASCENT_ORDER), kind, timestamp));
} else if (wc > 0) {
String prefix = str.substring(0, wc);
Key fromKey = new Key(new Object[]{def, prefix});
Key tillKey = new Key(new Object[]{def, prefix + Character.MAX_VALUE}, false);
return new SearchResult(root, type, uri, wc == str.length()-1 ? restOfPatterns : patterns, kind, timestamp,
new ReferenceIterator(root, refProps,
root.strPropIndex.iterator(fromKey, tillKey, Index.ASCENT_ORDER),
kind, timestamp));
}
}
else if (val instanceof Number) {
Key key = new Key(new Object[]{def, val});
return new SearchResult(root, type, uri, restOfPatterns, kind, timestamp,
new ReferenceIterator(root, refProps,
root.numPropIndex.iterator(key, key, Index.ASCENT_ORDER),
kind, timestamp));
} else if (val instanceof Date) {
Key key = new Key(new Object[]{def, val});
return new SearchResult(root, type, uri, restOfPatterns, kind, timestamp,
new ReferenceIterator(root, refProps,
root.timePropIndex.iterator(key, key, Index.ASCENT_ORDER),
kind, timestamp));
} else if (val instanceof NameVal) {
return searchReferenceProperty(type, uri, patterns, kind, timestamp, (NameVal)val, compound, def, refs);
} else if (val instanceof NameVal[]) {
NameVal[] props = (NameVal[])val;
if (props.length > 0) {
return searchReferenceProperty(type, uri, patterns, kind, timestamp, props[0], true, def, refs);
}
}
return null;
}
/**
* Close database
*/
public void close() {
db.close();
}
/**
* Commit current transaction
*/
public void commit() {
db.commit();
root.unlock();
}
/**
* Rollback current transaction
*/
public void rollback() {
db.rollback();
root.unlock();
}
/**
* Begin new write transction: set exclusive lock
*/
public void beginTransaction() {
root.exclusiveLock();
}
static class SearchResult implements Iterator {
VersionHistory type;
String uri;
NameVal[] patterns;
SearchKind kind;
Date timestamp;
Iterator iterator;
Thing currThing;
int currVersion;
Link currHistory;
DatabaseRoot root;
public SearchResult(DatabaseRoot root, VersionHistory type, String uri, NameVal[] patterns, SearchKind kind, Date timestamp, Iterator iterator)
{
this.root = root;
this.type = type;
this.uri = uri;
this.patterns = patterns;
this.kind = kind;
this.timestamp = timestamp;
this.iterator = iterator;
gotoNext();
}
public boolean hasNext() {
return currThing != null;
}
public Object next() {
if (currThing == null) {
throw new NoSuchElementException();
}
Thing thing = currThing;
gotoNext();
return thing;
}
public void remove() {
throw new UnsupportedOperationException();
}
private void gotoNext() {
Repeat:
while (true) {
if (currHistory != null) {
while (currVersion < currHistory.size()) {
Thing thing = (Thing)currHistory.get(currVersion++);
if (match(thing)) {
return;
}
}
currHistory = null;
}
while (iterator.hasNext()) {
Object curr = iterator.next();
if (curr instanceof Thing) {
if (match((Thing)curr)) {
return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -