📄 versionedstorage.cs
字号:
}
}
currThing = thing;
return true;
}
public object Current
{
get
{
if(currThing == null)
{
throw new InvalidOperationException("No current element");
}
return currThing;
}
}
private bool MatchProperty(NameVal prop, Thing thing)
{
switch (prop.name)
{
case Symbols.Point:
if (prop.val is NameVal[])
{
NameVal[] coord = (NameVal[])prop.val;
if (coord.Length == 2)
{
double x = (double)coord[0].val;
double y = (double)coord[1].val;
RectangleR2 r = new RectangleR2(x, y, x, y);
foreach (Thing t in root.spatialIndex.Overlaps(r))
{
if (t == thing)
{
return true;
}
}
return false;
}
}
break;
case Symbols.Rectangle:
if (prop.val is NameVal[])
{
NameVal[] coord = (NameVal[])prop.val;
if (coord.Length == 4)
{
RectangleR2 r = new RectangleR2((double)coord[0].val,
(double)coord[1].val,
(double)coord[2].val,
(double)coord[3].val);
foreach (Thing t in root.spatialIndex.Overlaps(r))
{
if (t == thing)
{
return true;
}
}
return false;
}
}
break;
case Symbols.Keyword:
if (prop.val is string)
{
Hashtable keywords = new Hashtable();
foreach (PropVal pv in thing.props)
{
object val = pv.val;
if (val is string)
{
foreach (string keyword in ((string)val).ToLower().Split(keywordSeparators))
{
if (keyword.Length > 0 && !keywordStopList.ContainsKey(keyword))
{
keywords[keyword] = this;
}
}
}
}
foreach (string keyword in ((string)prop.val).ToLower().Split(keywordSeparators))
{
if (keyword.Length > 0 && !keywordStopList.ContainsKey(keyword) && !keywords.ContainsKey(keyword))
{
return false;
}
}
return true;
}
break;
}
NextItem:
foreach (object val in thing[prop.name])
{
object pattern = prop.val;
if (val is string && pattern is string)
{
if (MatchString((string)val, (string)pattern))
{
return true;
}
}
else if (pattern is NameVal)
{
if (FollowReference((NameVal)pattern, val as VersionHistory))
{
return true;
}
}
else if (pattern is NameVal[])
{
foreach (NameVal p in (NameVal[])prop.val)
{
if (!FollowReference(p, val as VersionHistory))
{
goto NextItem;
}
}
return true;
}
else if (pattern is Range && val is IComparable)
{
try
{
Range range = (Range)pattern;
IComparable cmp = (IComparable)val;
return cmp.CompareTo(range.from) >= (range.fromInclusive ? 0 : 1) &&
cmp.CompareTo(range.till) <= (range.tillInclusive ? 0 : -1);
}
catch (ArgumentException) {}
}
else if (pattern != null && pattern.Equals(val))
{
return true;
}
}
return false;
}
private bool FollowReference(NameVal prop, VersionHistory vh)
{
if (vh != null)
{
if (kind == SearchKind.AllVersions)
{
foreach (Thing v in vh.versions)
{
if (MatchProperty(prop, v))
{
return true;
}
}
}
else
{
Thing thing = vh.GetVersion(kind, timestamp);
return thing != null && MatchProperty(prop, thing);
}
}
return false;
}
}
private void CreateMetaType()
{
VersionHistory vh = CreateVersionHistory(Symbols.Metatype, null);
vh.type = vh;
Thing metatype = CreateObject(null, vh, new NameVal[0]);
metatype.type = metatype;
root.metatype = vh;
}
private static string ReverseString(string s)
{
int len = s.Length;
char[] chars = new char[len];
for (int i = 0; i < len; i++)
{
chars[i] = s[len-i-1];
}
return new string(chars);
}
private VersionHistory CreateVersionHistory(String uri, VersionHistory type)
{
VersionHistory vh = new VersionHistory();
vh.uri = uri;
vh.type = type;
vh.versions = db.CreateLink();
root.prefixUriIndex.Put(uri, vh);
root.suffixUriIndex.Put(ReverseString(uri), vh);
return vh;
}
private static NameVal[] SubArray(NameVal[] arr)
{
NameVal[] newArr = new NameVal[arr.Length-1];
Array.Copy(arr, 1, newArr, 0, newArr.Length);
return newArr;
}
private Thing CreateObject(Thing type, VersionHistory vh, NameVal[] props)
{
Thing thing = new Thing();
thing.vh = vh;
thing.type = type;
thing.timestamp = DateTime.Now;
thing.props = new PropVal[props.Length];
for (int i = 0; i < props.Length; i++)
{
NameVal prop = props[i];
PropDef def = (PropDef)root.propDefIndex[prop.name];
if (def == null)
{
def = new PropDef();
def.name = prop.name;
root.propDefIndex.Put(def);
}
object val = prop.val;
PropVal pv = new PropVal(def, val);
Key key = new Key(new object[]{def, val});
if (val is string)
{
root.strPropIndex.Put(key, thing);
foreach (string keyword in ((string)val).ToLower().Split(keywordSeparators))
{
if (keyword.Length > 0 && !keywordStopList.ContainsKey(keyword))
{
root.inverseIndex.Put(keyword, thing);
}
}
}
else if (val is double)
{
root.numPropIndex.Put(key, thing);
}
else if (val is DateTime)
{
root.timePropIndex.Put(key, thing);
}
else if (val is VersionHistory || val == null)
{
root.refPropIndex.Put(key, thing);
if (prop.name == Symbols.Rectangle)
{
PropVal[] coord = ((VersionHistory)val).Latest.props;
RectangleR2 r = new RectangleR2((double)coord[0].val,
(double)coord[1].val,
(double)coord[2].val,
(double)coord[3].val);
root.spatialIndex.Put(r, thing);
}
else if (prop.name == Symbols.Point)
{
PropVal[] coord = ((VersionHistory)val).Latest.props;
double x = (double)coord[0].val;
double y = (double)coord[1].val;
RectangleR2 r = new RectangleR2(x, y, x, y);
root.spatialIndex.Put(r, thing);
}
}
else
{
throw new InvalidOperationException("Invalid propery value type " + prop.val.GetType());
}
thing.props[i] = pv;
}
thing.Modify();
vh.versions.Add(thing);
root.timeIndex.Put(thing);
root.latest.Add(thing);
return thing;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -