DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
IndexSetInfo.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.Collections;
19 using System.Collections.Generic;
20 using System.IO;
21 using System.Linq;
22 using System.Text;
23 
24 namespace Deveel.Data.Sql {
25  public sealed class IndexSetInfo : IEnumerable<IndexInfo> {
26  private readonly List<IndexInfo> indexes;
27 
28  private IndexSetInfo(ObjectName tableName, IEnumerable<IndexInfo> indexes, bool readOnly) {
29  if (tableName == null)
30  throw new ArgumentNullException("tableName");
31 
32  TableName = tableName;
33  this.indexes = new List<IndexInfo>();
34 
35  if (indexes != null)
36  this.indexes.AddRange(indexes);
37 
38  IsReadOnly = readOnly;
39  }
40 
41  public IndexSetInfo(ObjectName tableName)
42  : this(tableName, null, false) {
43  }
44 
45  public ObjectName TableName { get; private set; }
46 
47  public bool IsReadOnly { get; private set; }
48 
49  public int IndexCount {
50  get { return indexes.Count; }
51  }
52 
53  private void AssertNotReadOnly() {
54  if (IsReadOnly)
55  throw new ArgumentException("The set is read-only.");
56  }
57 
58  public void AddIndex(IndexInfo indexInfo) {
59  AssertNotReadOnly();
60 
61  indexes.Add(indexInfo);
62  indexInfo.Offset = indexes.Count;
63  }
64 
65  public IndexInfo GetIndex(int offset) {
66  return indexes.FirstOrDefault(x => x.Offset == offset);
67  }
68 
69  public void RemoveIndexAt(int offset) {
70  AssertNotReadOnly();
71 
72  indexes.RemoveAt(offset);
73  }
74 
75  public IndexInfo FindNamedIndex(string indexName) {
76  return FindNamedIndex(indexName, true);
77  }
78 
79  public IndexInfo FindNamedIndex(string indexName, bool ignoreCase) {
80  var compare = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
81  return indexes.FirstOrDefault(x => x.IndexName.Equals(indexName, compare));
82  }
83 
84  public int FindIndexOffset(string name) {
85  return FindIndexOffset(name, true);
86  }
87 
88  public int FindIndexOffset(string name, bool ignoreCase) {
89  var compare = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
90  for (int i = 0; i < indexes.Count; i++) {
91  var indexInfo = indexes[i];
92  if (indexInfo.IndexName.Equals(name, compare))
93  return i;
94  }
95 
96  return -1;
97  }
98 
99  public int FindIndexForColumns(string[] columnNames) {
100  int sz = IndexCount;
101  for (int i = 0; i < sz; ++i) {
102  string[] columns = indexes[i].ColumnNames;
103  if (columns.Length == columnNames.Length) {
104  bool passed = true;
105  for (int n = 0; n < columns.Length && passed; ++n) {
106  if (!columns[n].Equals(columnNames[n])) {
107  passed = false;
108  }
109  }
110  if (passed) {
111  return i;
112  }
113  }
114  }
115  return -1;
116  }
117 
118 
120  return new IndexSetInfo(TableName, indexes.ToArray(), true);
121  }
122 
123  public IEnumerator<IndexInfo> GetEnumerator() {
124  return indexes.GetEnumerator();
125  }
126 
127  IEnumerator IEnumerable.GetEnumerator() {
128  return GetEnumerator();
129  }
130 
131  public void SerialiazeTo(Stream stream) {
132  var schemaName = TableName.Parent;
133  var catName = schemaName != null && schemaName.Parent != null ? schemaName.Parent.Name : String.Empty;
134  var schema = schemaName != null ? schemaName.Name : String.Empty;
135 
136  var writer = new BinaryWriter(stream, Encoding.Unicode);
137  writer.Write(2); // Version
138  writer.Write(catName);
139  writer.Write(schema);
140  writer.Write(TableName.Name);
141 
142  int indexCount = indexes.Count;
143 
144  writer.Write(indexCount);
145 
146  for (int i = 0; i < indexCount; i++) {
147  var index = indexes[i];
148  index.SerializeTo(stream);
149  }
150  }
151 
152  public static IndexSetInfo DeserializeFrom(Stream stream) {
153  var reader = new BinaryReader(stream, Encoding.Unicode);
154 
155  var version = reader.ReadInt32();
156  if (version != 2)
157  throw new FormatException("Invalid version number of the Index-Set Info");
158 
159  var catName = reader.ReadString();
160  var schemaName = reader.ReadString();
161  var tableName = reader.ReadString();
162 
163  ObjectName objSchemaName;
164  if (String.IsNullOrEmpty(catName)) {
165  objSchemaName = new ObjectName(schemaName);
166  } else {
167  objSchemaName = new ObjectName(new ObjectName(catName), schemaName);
168  }
169 
170  var objTableName = new ObjectName(objSchemaName, tableName);
171 
172  var indexCount = reader.ReadInt32();
173 
174  var indices = new List<IndexInfo>();
175  for (int i = 0; i < indexCount; i++) {
176  var indexInfo = IndexInfo.DeserializeFrom(stream);
177  indices.Add(indexInfo);
178  }
179 
180  return new IndexSetInfo(objTableName, indices.ToArray(), false);
181  }
182  }
183 }
int FindIndexOffset(string name, bool ignoreCase)
Definition: IndexSetInfo.cs:88
readonly List< IndexInfo > indexes
Definition: IndexSetInfo.cs:26
IndexInfo FindNamedIndex(string indexName, bool ignoreCase)
Definition: IndexSetInfo.cs:79
void RemoveIndexAt(int offset)
Definition: IndexSetInfo.cs:69
int FindIndexForColumns(string[] columnNames)
Definition: IndexSetInfo.cs:99
Describes the name of an object within a database.
Definition: ObjectName.cs:44
IndexSetInfo(ObjectName tableName, IEnumerable< IndexInfo > indexes, bool readOnly)
Definition: IndexSetInfo.cs:28
int FindIndexOffset(string name)
Definition: IndexSetInfo.cs:84
IndexInfo GetIndex(int offset)
Definition: IndexSetInfo.cs:65
static IndexSetInfo DeserializeFrom(Stream stream)
void AddIndex(IndexInfo indexInfo)
Definition: IndexSetInfo.cs:58
static IndexInfo DeserializeFrom(Stream stream)
Definition: IndexInfo.cs:72
IndexInfo FindNamedIndex(string indexName)
Definition: IndexSetInfo.cs:75
IEnumerator< IndexInfo > GetEnumerator()
IndexSetInfo(ObjectName tableName)
Definition: IndexSetInfo.cs:41
void SerialiazeTo(Stream stream)