DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
IndexInfo.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.IO;
19 using System.Text;
20 
21 namespace Deveel.Data.Sql {
22  public sealed class IndexInfo {
23  public IndexInfo(string indexName, string indexType, string[] columnNames)
24  : this(indexName, indexType, columnNames, false) {
25  }
26 
27  public IndexInfo(string indexName, string indexType, string[] columnNames, bool isUnique, int offset) {
28  if (String.IsNullOrEmpty(indexType))
29  throw new ArgumentNullException("indexType");
30  if (String.IsNullOrEmpty(indexName))
31  throw new ArgumentNullException("indexName");
32  if (columnNames == null || columnNames.Length == 0)
33  throw new ArgumentNullException("columnNames");
34 
35  IndexName = indexName;
36  IndexType = indexType;
37  ColumnNames = columnNames;
38  IsUnique = isUnique;
39  Offset = offset;
40  }
41 
42  public IndexInfo(string indexName, string indexType, string[] columnNames, bool isUnique)
43  : this(indexName, indexType, columnNames, isUnique, -1) {
44  }
45 
46  public string IndexName { get; private set; }
47 
48  public string IndexType { get; private set; }
49 
50  public string[] ColumnNames { get; private set; }
51 
52  public bool IsUnique { get; private set; }
53 
54  public int Offset { get; internal set; }
55 
56  internal void SerializeTo(Stream stream) {
57  var writer = new BinaryWriter(stream, Encoding.Unicode);
58  writer.Write(2); // Version
59  writer.Write(IndexType);
60  writer.Write(IndexName);
61  writer.Write(IsUnique ? (byte) 1 : (byte) 0);
62  writer.Write(Offset);
63 
64  var colCount = ColumnNames.Length;
65  writer.Write(colCount);
66  for (int i = 0; i < colCount; i++) {
67  var colName = ColumnNames[i];
68  writer.Write(colName);
69  }
70  }
71 
72  internal static IndexInfo DeserializeFrom(Stream stream) {
73  var reader = new BinaryReader(stream, Encoding.Unicode);
74 
75  var version = reader.ReadInt32();
76  if (version != 2)
77  throw new FormatException("Invalid version number for Index-Info");
78 
79  var indexType = reader.ReadString();
80  var indexName = reader.ReadString();
81  var unique = reader.ReadByte() == 1;
82  var offset = reader.ReadInt32();
83 
84  var colCount = reader.ReadInt32();
85 
86  var columnNames = new string[colCount];
87  for (int i = 0; i < colCount; i++) {
88  var columnName = reader.ReadString();
89  columnNames[i] = columnName;
90  }
91 
92  return new IndexInfo(indexName, indexType, columnNames, unique, offset);
93  }
94  }
95 }
IndexInfo(string indexName, string indexType, string[] columnNames)
Definition: IndexInfo.cs:23
void SerializeTo(Stream stream)
Definition: IndexInfo.cs:56
static IndexInfo DeserializeFrom(Stream stream)
Definition: IndexInfo.cs:72
IndexInfo(string indexName, string indexType, string[] columnNames, bool isUnique, int offset)
Definition: IndexInfo.cs:27
IndexInfo(string indexName, string indexType, string[] columnNames, bool isUnique)
Definition: IndexInfo.cs:42