summaryrefslogtreecommitdiff
path: root/src/libtracker-bus/tracker-bus-fd-cursor.vala
blob: 318d261cb8ac082918a5b3dd1988fc2bd7997b82 (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
/*
 * Copyright (C) 2010, Nokia <ivan.frade@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

class Tracker.Bus.FDCursor : Tracker.Sparql.Cursor {
	internal char* buffer;
	internal ulong buffer_index;
	internal ulong buffer_size;

	internal int _n_columns;
	internal int* offsets;
	internal int* types;
	internal char* data;
	internal string[] variable_names;
	internal bool cursor_finished;

	public FDCursor (char* buffer, ulong buffer_size, string[] variable_names) {
		Object ();
		this.buffer = buffer;
		this.buffer_size = buffer_size;
		this.variable_names = variable_names;
		this.cursor_finished = true;
		_n_columns = variable_names.length;
	}

	~FDCursor () {
		free (buffer);
	}

	inline int buffer_read_int () {
		int v = *((int*) (buffer + buffer_index));

		buffer_index += 4;

		return v;
	}

	public override int n_columns {
		get { return _n_columns; }
	}

	public override Sparql.ValueType get_value_type (int column)
	requires (types != null) {
		/* Cast from int to enum */
		return (Sparql.ValueType) types[column];
	}

	public override unowned string? get_variable_name (int column)
	requires (variable_names != null) {
		return variable_names[column];
	}

	public override unowned string? get_string (int column, out long length = null)
	requires (cursor_finished == false) {
		unowned string str = null;

		if (column >= n_columns) {
			length = 0;
			return null;
		}

		// return null instead of empty string for unbound values
		if (types[column] == Sparql.ValueType.UNBOUND) {
			length = 0;
			return null;
		}

		if (column == 0) {
			str = (string) data;
		} else {
			str = (string) (data + offsets[column - 1] + 1);
		}

		length = str.length;

		return str;
	}

	public override bool next (Cancellable? cancellable = null) throws GLib.Error {
		int last_offset;

		if (cancellable != null && cancellable.is_cancelled ()) {
			throw new IOError.CANCELLED ("Operation was cancelled");
		}

		if (buffer_index >= buffer_size) {
			cursor_finished = true;
			data = null;
			return false;
		}

		/* So, the make up on each cursor segment is:
		 *
		 * iteration = [4 bytes for number of columns,
		 *              columns x 4 bytes for types
		 *              columns x 4 bytes for offsets]
		 */

		_n_columns = buffer_read_int ();

		/* Storage of ints that will be cast to TrackerSparqlValueType enums,
		 * also see get_value_type */
		types = (int*) (buffer + buffer_index);
		buffer_index += sizeof (int) * n_columns;

		offsets = (int*) (buffer + buffer_index);
		buffer_index += sizeof (int) * (n_columns - 1);
		last_offset = buffer_read_int ();

		data = buffer + buffer_index;
		cursor_finished = false;

		buffer_index += last_offset + 1;

		return true;
	}

	public override async bool next_async (Cancellable? cancellable = null) throws GLib.Error {
		// next never blocks
		return next (cancellable);
	}

	public override void rewind () {
		buffer_index = 0;
		data = buffer;
		cursor_finished = false;
	}
}