summaryrefslogtreecommitdiff
path: root/src/db/db_huffman.c
blob: ee5c2ccccc3140db9a6afa0e45a00e74ff85f58f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2008-2011 WiredTiger, Inc.
 *	All rights reserved.
 */

#include "wt_internal.h"

/*
 * 7-bit ASCII, with English language frequencies.
 *
 * Based on "Case-sensitive letter and bigram frequency counts from large-scale
 * English corpora"
 *	Michael N. Jones and D.J.K. Mewhort
 *	Queen's University, Kingston, Ontario, Canada
 * Behavior Research Methods, Instruments, & Computers 2004, 36 (3), 388-396
 *
 * Additionally supports space and tab characters; space is the most common
 * character in text where it occurs, and tab appears about as frequently as
 * 'a' and 'n' in text where it occurs.
 */
static uint8_t const __wt_huffman_ascii_english[256] = {
	1,	/* 000 nul */
	1,	/* 001 soh */
	1,	/* 002 stx */
	1,	/* 003 etx */
	1,	/* 004 eot */
	1,	/* 005 enq */
	1,	/* 006 ack */
	1,	/* 007 bel */
	1,	/* 010 bs  */
	251,	/* 011 ht  */
	1,	/* 012 nl  */
	1,	/* 013 vt  */
	1,	/* 014 np  */
	1,	/* 015 cr  */
	1,	/* 016 so  */
	1,	/* 017 si  */
	1,	/* 020 dle */
	1,	/* 021 dc1 */
	1,	/* 022 dc2 */
	1,	/* 023 dc3 */
	1,	/* 024 dc4 */
	1,	/* 025 nak */
	1,	/* 026 syn */
	1,	/* 027 etb */
	1,	/* 030 can */
	1,	/* 031 em  */
	1,	/* 032 sub */
	1,	/* 033 esc */
	1,	/* 034 fs  */
	1,	/* 035 gs  */
	1,	/* 036 rs  */
	1,	/* 037 us  */
	255,	/* 040 sp  */
	177,	/* 041  !  */
	223,	/* 042  "  */
	171,	/* 043  #  */
	188,	/* 044  $  */
	176,	/* 045  %  */
	179,	/* 046  &  */
	215,	/* 047  '  */
	189,	/* 050  (  */
	190,	/* 051  )  */
	184,	/* 052  *  */
	175,	/* 053  +  */
	234,	/* 054  ,  */
	219,	/* 055  -  */
	233,	/* 056  .  */
	181,	/* 057  /  */
	230,	/* 060  0  */
	229,	/* 061  1  */
	226,	/* 062  2  */
	213,	/* 063  3  */
	214,	/* 064  4  */
	227,	/* 065  5  */
	210,	/* 066  6  */
	203,	/* 067  7  */
	212,	/* 070  8  */
	222,	/* 071  9  */
	191,	/* 072  :  */
	186,	/* 073  ;  */
	173,	/* 074  <  */
	172,	/* 075  =  */
	174,	/* 076  >  */
	183,	/* 077  ?  */
	170,	/* 100  @  */
	221,	/* 101  A  */
	211,	/* 102  B  */
	218,	/* 103  C  */
	206,	/* 104  D  */
	207,	/* 105  E  */
	199,	/* 106  F  */
	197,	/* 107  G  */
	205,	/* 110  H  */
	217,	/* 111  I  */
	196,	/* 112  J  */
	187,	/* 113  K  */
	201,	/* 114  L  */
	220,	/* 115  M  */
	216,	/* 116  N  */
	200,	/* 117  O  */
	208,	/* 120  P  */
	182,	/* 121  Q  */
	209,	/* 122  R  */
	224,	/* 123  S  */
	225,	/* 124  T  */
	193,	/* 125  U  */
	185,	/* 126  V  */
	202,	/* 127  W  */
	180,	/* 130  X  */
	198,	/* 131  Y  */
	178,	/* 132  Z  */
	1,	/* 133  [  */
	1,	/* 134  \  */
	1,	/* 135  ]  */
	1,	/* 136  ^  */
	1,	/* 137  _  */
	1,	/* 140  `  */
	252,	/* 141  a  */
	232,	/* 142  b  */
	242,	/* 143  c  */
	243,	/* 144  d  */
	254,	/* 145  e  */
	239,	/* 146  f  */
	237,	/* 147  g  */
	245,	/* 150  h  */
	248,	/* 151  i  */
	194,	/* 152  j  */
	228,	/* 153  k  */
	244,	/* 154  l  */
	240,	/* 155  m  */
	249,	/* 156  n  */
	250,	/* 157  o  */
	238,	/* 160  p  */
	192,	/* 161  q  */
	246,	/* 162  r  */
	247,	/* 163  s  */
	253,	/* 164  t  */
	241,	/* 165  u  */
	231,	/* 166  v  */
	235,	/* 167  w  */
	204,	/* 170  x  */
	236,	/* 171  y  */
	195,	/* 172  z  */
	1,	/* 173  {  */
	1,	/* 174  |  */
	1,	/* 175  }  */
	1,	/* 176  ~  */
	1,	/* 177 del */
};

/*
 * __wt_btree_huffman_set --
 *	BTREE huffman configuration setter.
 */
int
__wt_btree_huffman_set(BTREE *btree,
    uint8_t const *huffman_table, u_int huffman_table_size, uint32_t flags)
{
	SESSION *session;
	uint8_t phone[256];

	session = &btree->conn->default_session;

	switch (LF_ISSET(WT_ASCII_ENGLISH | WT_TELEPHONE)) {
	case WT_ASCII_ENGLISH:
		if (huffman_table != NULL)
			goto err;
		huffman_table = __wt_huffman_ascii_english;
		huffman_table_size = sizeof(__wt_huffman_ascii_english);
		break;
	case WT_TELEPHONE:
		if (huffman_table != NULL)
			goto err;
		memset(phone, 0, sizeof(phone));
		phone['('] = 2;
		phone[')'] = 2;
		phone['+'] = 1;
		phone['-'] = 3;
		phone['0'] = 1;
		phone['1'] = 1;
		phone['2'] = 1;
		phone['3'] = 1;
		phone['4'] = 1;
		phone['5'] = 1;
		phone['6'] = 1;
		phone['7'] = 1;
		phone['8'] = 1;
		phone['9'] = 1;
		huffman_table = phone;
		huffman_table_size = sizeof(phone);
		break;
	default:
err:		return (__wt_api_args(session, "Db.huffman_set"));
	}

	/*
	 * If we're using an already-specified table, close it.   It's probably
	 * an application error to set the Huffman table twice, but hey, I just
	 * work here.
	 */
	if (LF_ISSET(WT_HUFFMAN_KEY) && btree->huffman_key != NULL) {
		/* Key and data may use the same table, only close it once. */
		if (btree->huffman_value == btree->huffman_key)
			btree->huffman_value = NULL;
		__wt_huffman_close(session, btree->huffman_key);
		btree->huffman_key = NULL;
	}
	if (LF_ISSET(WT_HUFFMAN_VALUE) && btree->huffman_value != NULL) {
		__wt_huffman_close(session, btree->huffman_value);
		btree->huffman_value = NULL;
	}
	if (LF_ISSET(WT_HUFFMAN_KEY)) {
		WT_RET(__wt_huffman_open(session,
		     huffman_table, huffman_table_size, &btree->huffman_key));
		/* Key and data may use the same table. */
		if (LF_ISSET(WT_HUFFMAN_VALUE)) {
			btree->huffman_value = btree->huffman_key;
			LF_CLR(WT_HUFFMAN_VALUE);
		}
	}
	if (LF_ISSET(WT_HUFFMAN_VALUE))
		WT_RET(__wt_huffman_open(session,
		    huffman_table, huffman_table_size, &btree->huffman_value));

	return (0);
}