summaryrefslogtreecommitdiff
path: root/java/src/Freeze/MapInternal/Search.java
blob: a57dbed23d9df4ab54e6dcd5a19e1cb614ee6205 (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
230
231
232
233
// **********************************************************************
//
// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

package Freeze.MapInternal;

import Freeze.ConnectionI;
import Freeze.DatabaseException;
import Freeze.DeadlockException;

class Search
{
    enum Type
    {
        FIRST       // The first entry
        {
            Type descending() { return LAST; };
        },
        LAST        // The last entry
        {
            Type descending() { return FIRST; };
        },
        CEILING     // The entry with the smallest key greater than or equal to the target key
        {
            Type descending() { return FLOOR; };
        },
        FLOOR       // The entry with the greatest key less than or equal to the target key
        {
            Type descending() { return CEILING; };
        },
        HIGHER      // The entry with the smallest key greater than the target key
        {
            Type descending() { return LOWER; };
        },
        LOWER       // The entry with the greatest key less than the target key
        {
            Type descending() { return HIGHER; };
        };

        abstract Type descending(); // Returns the descending (opposite) value.
    }

    interface KeyValidator
    {
        boolean keyInRange(byte[] key);
    }

    static boolean
    search(Type type, ConnectionI connection, String dbName, com.sleepycat.db.Database db,
           com.sleepycat.db.DatabaseEntry key, com.sleepycat.db.DatabaseEntry value,
           java.util.Comparator<byte[]> comparator, KeyValidator validator, TraceLevels trace)
    {
        if(type != Type.FIRST && type != Type.LAST && comparator == null)
        {
            throw new UnsupportedOperationException("A comparator is required");
        }

        if(trace.level >= 2)
        {
            trace.logger.trace("Freeze.Map", "searching Db \"" + dbName + "\"");
        }

        if(value == null)
        {
            value = new com.sleepycat.db.DatabaseEntry();
            value.setPartial(true); // Not interested in the value.
        }

        try
        {
            for(;;)
            {
                com.sleepycat.db.Cursor dbc = null;
                try
                {
                    com.sleepycat.db.DatabaseEntry dbcKey =
                        new com.sleepycat.db.DatabaseEntry(key != null ? key.getData() : null);
                    dbcKey.setReuseBuffer(false);

                    dbc = db.openCursor(connection.dbTxn(), null);

                    com.sleepycat.db.OperationStatus status = null;

                    switch(type)
                    {
                    case FIRST:
                    {
                        status = dbc.getFirst(dbcKey, value, null);
                        break;
                    }
                    case LAST:
                    {
                        status = dbc.getLast(dbcKey, value, null);
                        break;
                    }
                    case CEILING:
                    {
                        //
                        // The semantics of getSearchKeyRange match CEILING.
                        //
                        status = dbc.getSearchKeyRange(dbcKey, value, null);
                        break;
                    }
                    case FLOOR:
                    {
                        status = dbc.getSearchKeyRange(dbcKey, value, null);
                        if(status == com.sleepycat.db.OperationStatus.SUCCESS)
                        {
                            //
                            // getSearchKeyRange returns the smallest key greater than or equal to
                            // the target key. If the matching key is greater than the target key
                            // then we need to get the previous entry.
                            //
                            int cmp = comparator.compare(dbcKey.getData(), key.getData());
                            assert(cmp >= 0);
                            if(cmp > 0)
                            {
                                status = dbc.getPrevNoDup(dbcKey, value, null);
                            }
                        }
                        else if(status == com.sleepycat.db.OperationStatus.NOTFOUND)
                        {
                            //
                            // All keys must be less than the target key so we pick the largest of
                            // all (the last one).
                            //
                            status = dbc.getLast(dbcKey, value, null);
                        }
                        break;
                    }
                    case HIGHER:
                    {
                        status = dbc.getSearchKeyRange(dbcKey, value, null);
                        if(status == com.sleepycat.db.OperationStatus.SUCCESS)
                        {
                            //
                            // getSearchKeyRange returns the smallest key greater than or equal to
                            // the target key. If the matching key is equal to the target key
                            // then we need to get the next entry.
                            //
                            int cmp = comparator.compare(dbcKey.getData(), key.getData());
                            assert(cmp >= 0);
                            if(cmp == 0)
                            {
                                status = dbc.getNextNoDup(dbcKey, value, null);
                            }
                        }
                        break;
                    }
                    case LOWER:
                    {
                        status = dbc.getSearchKeyRange(dbcKey, value, null);
                        if(status == com.sleepycat.db.OperationStatus.SUCCESS)
                        {
                            //
                            // getSearchKeyRange returns the smallest key greater than or equal to
                            // the target key. We move to the previous entry, whose key must be less
                            // than the target key.
                            //
                            status = dbc.getPrevNoDup(dbcKey, value, null);
                        }
                        else if(status == com.sleepycat.db.OperationStatus.NOTFOUND)
                        {
                            //
                            // All keys must be less than the target key so we pick the largest of
                            // all (the last one).
                            //
                            status = dbc.getLast(dbcKey, value, null);
                        }
                        break;
                    }
                    }

                    if(status == com.sleepycat.db.OperationStatus.SUCCESS)
                    {
                        if(validator == null || validator.keyInRange(dbcKey.getData()))
                        {
                            key.setData(dbcKey.getData());
                            return true;
                        }
                    }

                    return false;
                }
                catch(com.sleepycat.db.DeadlockException dx)
                {
                    if(connection.dbTxn() != null)
                    {
                        throw new DeadlockException(trace.errorPrefix + dx.getMessage(),
                                                    connection.currentTransaction(), dx);
                    }
                    else
                    {
                        if(trace.deadlockWarning)
                        {
                            trace.logger.warning("Deadlock in Freeze.Map while searching \"" + dbName +
                                                 "\"; retrying...");
                        }

                        //
                        // Retry
                        //
                    }
                }
                finally
                {
                    if(dbc != null)
                    {
                        try
                        {
                            dbc.close();
                        }
                        catch(com.sleepycat.db.DeadlockException dx)
                        {
                            //
                            // Ignored
                            //
                        }
                    }
                }
            }
        }
        catch(com.sleepycat.db.DatabaseException dx)
        {
            throw new DatabaseException(trace.errorPrefix + dx.getMessage(), dx);
        }
    }
}