DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
InsertSearchIndex.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.Generic;
19 
20 using Deveel.Data.Sql;
21 using Deveel.Data.Sql.Tables;
22 
23 namespace Deveel.Data.Index {
24  public sealed class InsertSearchIndex : CollatedSearchIndex {
25  private IIndex<int> list;
27 
28  private bool recordUid;
29 
30  private readonly bool readOnly;
31  private readonly int readOnlyCount;
32 
33  public InsertSearchIndex(ITable table, int columnOffset)
34  : base(table, columnOffset) {
35  comparer = new IndexComparerImpl(this);
36  list = new BlockIndex<int>();
37  }
38 
39  public InsertSearchIndex(ITable table, int columnOffset, IEnumerable<int> list)
40  : this(table, columnOffset) {
41  if (list != null) {
42  foreach (var item in list) {
43  this.list.Add(item);
44  }
45  }
46  }
47 
48  private InsertSearchIndex(ITable table, InsertSearchIndex source, bool readOnly)
49  : this(table, source.ColumnOffset, source.list) {
50  this.readOnly = readOnly;
51 
52  if (readOnly)
53  readOnlyCount = list.Count;
54 
55  // Do we generate lookup caches?
56  recordUid = source.recordUid;
57 
58  }
59 
60  public override bool IsReadOnly {
61  get { return readOnly; }
62  }
63 
64  protected override int Count {
65  get { return list.Count; }
66  }
67 
68  public override string IndexType {
69  get { return DefaultIndexTypes.InsertSearch; }
70  }
71 
72  protected override DataObject First {
73  get { return GetValue(list[0]); }
74  }
75 
76  protected override DataObject Last {
77  get { return GetValue(list[list.Count - 1]); }
78  }
79 
80  internal bool RecordUid { get; set; }
81 
82  public override void Insert(int rowNumber) {
83  if (IsReadOnly)
84  throw new InvalidOperationException("Tried to change an read-only index.");
85 
86  var value = GetValue(rowNumber);
87  list.InsertSort(value, rowNumber, comparer);
88  }
89 
90  public override void Remove(int rowNumber) {
91  if (IsReadOnly)
92  throw new InvalidOperationException("Tried to change an read-only index.");
93 
94  var value = GetValue(rowNumber);
95  var removed = list.RemoveSort(value, rowNumber, comparer);
96 
97  if (removed != rowNumber)
98  throw new InvalidOperationException(String.Format("Could not remove the requested row ({0})", rowNumber));
99  }
100 
101  protected override IEnumerable<int> AddRange(int start, int end, IEnumerable<int> input) {
102  var result = new List<int>();
103  if (input != null)
104  result.AddRange(input);
105 
106  var en = list.GetEnumerator(start, end);
107  while (en.MoveNext()) {
108  result.Add(en.Current);
109  }
110 
111  return result.ToArray();
112  }
113 
114  public override ColumnIndex Copy(ITable table, bool readOnly) {
115  // ASSERTION: If readOnly, check the size of the current set is equal to
116  // when the index was created.
117  if (IsReadOnly && readOnlyCount != list.Count)
118  throw new InvalidOperationException("Assert failed: read-only size is different from when created.");
119 
120  // We must create a new InsertSearch object and copy all the state
121  // information from this object to the new one.
122  return new InsertSearchIndex(table, this, readOnly);
123  }
124 
125  protected override int SearchFirst(DataObject value) {
126  return list.SearchFirst(value, comparer);
127  }
128 
129  protected override int SearchLast(DataObject value) {
130  return list.SearchLast(value, comparer);
131  }
132 
133  protected override void Dispose(bool disposing) {
134  list = null;
135  comparer = null;
136  }
137 
138  #region IndexComparerImpl
139 
140  private class IndexComparerImpl : IIndexComparer<int> {
141  private readonly InsertSearchIndex columnIndex;
142 
143  public IndexComparerImpl(InsertSearchIndex columnIndex) {
144  this.columnIndex = columnIndex;
145  }
146 
147  private int InternalCompare(int index, DataObject value) {
148  var cell = columnIndex.GetValue(index);
149  var cmp = cell.CompareTo(value);
150  return cmp;
151  }
152 
153  public int CompareValue(int index, DataObject val) {
154  return InternalCompare(index, val);
155  }
156 
157  public int Compare(int index1, int index2) {
158  var cell = columnIndex.GetValue(index2);
159  return InternalCompare(index1, cell);
160  }
161  }
162 
163  #endregion
164  }
165 }
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
InsertSearchIndex(ITable table, int columnOffset)
override IEnumerable< int > AddRange(int start, int end, IEnumerable< int > input)
override void Remove(int rowNumber)
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
InsertSearchIndex(ITable table, InsertSearchIndex source, bool readOnly)
override void Dispose(bool disposing)
override ColumnIndex Copy(ITable table, bool readOnly)
override int SearchFirst(DataObject value)
override void Insert(int rowNumber)
override int SearchLast(DataObject value)
InsertSearchIndex(ITable table, int columnOffset, IEnumerable< int > list)