Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

#!/usr/bin/env python 

 

 

""" 

@file ion/core/data/index_store_service.py 

@author Matt Rodriguez 

@author David Stuebe 

@brief Service which fronts the index store capability through the messaging to a single back end. 

""" 

 

import ion.util.ionlog 

log = ion.util.ionlog.getLogger(__name__) 

from twisted.internet import defer 

 

from ion.core.process.process import ProcessFactory 

from ion.core.process.service_process import ServiceProcess, ServiceClient 

from ion.core.object import object_utils 

 

from ion.core.data.store import Query 

 

from ion.core.data.store import IIndexStore, IndexStore, IndexStoreError 

from zope.interface import implements 

 

from ion.core import ioninit 

CONF = ioninit.config(__name__) 

 

 

 

QUERY_ATTRIBUTES_TYPE = object_utils.create_type_identifier(object_id=17, version=1) 

ROW_TYPE = object_utils.create_type_identifier(object_id=18, version=1) 

ROWS_TYPE = object_utils.create_type_identifier(object_id=19, version=1) 

INDEXED_ATTRIBUTES_TYPE = object_utils.create_type_identifier(object_id=20, version=1) 

ROW_INDEX_UPDATE_TYPE = object_utils.create_type_identifier(object_id=21, version=1) 

 

 

class IndexStoreServiceException(Exception): 

    """ 

    Exceptions that originate in the IndexStoreService class 

    """ 

 

class IndexStoreService(ServiceProcess): 

    """ 

    @brief IndexStoreService 

 

    This is not a ION service. It is part of a test harness to provide a pure, in memory backend for the data store 

    and the association service 

 

    TODO, this class does not catch any exceptions from the business logic class.  

    """ 

 

    # Declaration of service 

    declare = ServiceProcess.service_declare(name='index_store_service', version='0.1.0', dependencies=[]) 

 

    def __init__(self, *args, **kwargs): 

        # Service class initializer. Basic config, but no yields allowed. 

        ServiceProcess.__init__(self, *args, **kwargs) 

 

        log.info(self.spawn_args) 

        self.indices = self.spawn_args.get('indices',  []) 

        log.info(self.indices) 

 

    #@defer.inlineCallbacks 

    def slc_activate(self, *args): 

        """ 

        Activation can be automatic when the process is spawned or triggered by 

        a message from the client. 

         

        First, default to bootstrapping from the spawn args. Create all resources 

        by describing the cluster! 

         

        Second, add hooks to override the spawn args and take the storage resource 

        reference from a message in op_activate - connecting to an already active 

        system! 

         

        """ 

        IndexStore.kvs={} 

        IndexStore.indices={} 

        self._indexed_store = IndexStore(indices=self.indices) 

 

        log.info("Created Index Store Service") 

 

 

 

 

    @defer.inlineCallbacks 

    def op_query(self, request, headers, msg): 

        """ 

        @Note The goal is to return a dictionary of keys and resourceids. 

        @retval return a cassandra_rows type. The key attribute will be set and each row will contain one column  

        with the name value. 

        """ 

        log.debug("In op_query: request %s" % request) 

 

        query_predicates = Query() 

        for attr in request.attrs: 

            if attr.predicate_type == Query.EQ: 

                query_predicates.add_predicate_eq(attr.attribute_name, attr.attribute_value) 

            elif attr.predicate_type == Query.GT: 

                query_predicates.add_predicate_gt(attr.attribute_name, attr.attribute_value) 

            else: 

                raise IndexStoreServiceException("Unhandled predicate type: %s " % (attr.predicate_type,)) 

 

        results = yield self._indexed_store.query(query_predicates) 

        #Now we have to put these back into a response 

        response = yield self.message_client.create_instance(ROWS_TYPE) 

 

 

        #The GPB buffer object represents cassandra rows, we could probably get away with just making a dictionary like 

        #object, since that's what query returns. 

        for key,row in results.items(): 

            r = response.rows.add() 

            r.key = key 

 

 

            r.value = row.pop('value') 

 

            for name, val in row.items(): 

                col = r.cols.add() 

                col.column_name = name 

                col.column_value = val 

 

        log.debug("op_query Result %s" % response) 

 

        yield self.reply_ok(msg,response) 

 

    @defer.inlineCallbacks 

    def op_put(self, request, headers, msg): 

        """ 

        @note, puts a row into the Cassandra cluster.  

        @retval does not return anything 

        """ 

        key = request.key 

        value = request.value 

        index_attrs = {} 

        for col in request.cols: 

            index_attrs[col.column_name] = col.column_value 

        yield self._indexed_store.put(key=key,value=value,index_attributes=index_attrs) 

 

        yield self.reply_ok(msg) 

 

 

    @defer.inlineCallbacks 

    def op_update_index(self, request, headers, msg): 

        key = request.key 

        index_attrs = {} 

        for col in request.cols: 

            index_attrs[col.column_name] = col.column_value 

        yield self._indexed_store.update_index(key,index_attrs) 

        log.info("In op_update_index") 

        yield self.reply_ok(msg) 

 

    @defer.inlineCallbacks 

    def op_get(self, request, headers, msg): 

        """ 

        @note Gets a row from the Cassandra cluster 

        If the row does not exist then leave the value field in the CassandraIndexedRow empty. 

        @param request is a CassandraRow message object 

        @retval Returns a CassandraRow message in the response    

        """ 

 

        value = yield self._indexed_store.get(request.key) 

        response = yield self.message_client.create_instance(ROW_TYPE) 

        response.key = request.key 

 

        if value is not None: 

            response.value = value 

 

        # Consider using raise with a not found response? 

        yield self.reply_ok(msg, response) 

 

    @defer.inlineCallbacks 

    def op_remove(self, request, headers, msg): 

        """ 

        @note removes a row 

        @param request is a CassandraRow message object 

        @retval does not return anything 

        """ 

 

        yield self._indexed_store.remove(request.key) 

        yield self.reply_ok(msg) 

 

    @defer.inlineCallbacks 

    def op_has_key(self, request, headers, msg): 

        """ 

        @note sees if key exists in the cluster 

        @request is a CassandraRow message object 

        @retval return a string that is "True" or "False" in a CassandraRow message 

        """ 

        key_exists = yield self._indexed_store.has_key(request.key) 

        log.info("key_exists: " + str(key_exists)) 

        response = yield self.message_client.create_instance(ROW_TYPE) 

        response.value = str(int(key_exists)) 

        yield self.reply_ok(msg, response) 

 

    @defer.inlineCallbacks 

    def op_get_query_attributes(self, request, headers, msg): 

        """ 

        @note gets the names of the columns that are indexed in the column family 

        @retval returns the names of the columns in a CassandraRow message 

        """ 

        column_list = yield self._indexed_store.get_query_attributes() 

        response = yield self.message_client.create_instance(INDEXED_ATTRIBUTES_TYPE) 

        log.info(column_list) 

        response.attributes.extend(column_list) 

 

        log.info("replying for get_query_attributes") 

        yield self.reply_ok(msg, response) 

 

# Spawn of the process using the module name 

factory = ProcessFactory(IndexStoreService) 

 

 

class IndexStoreServiceClient(ServiceClient): 

    """ 

    This interface will change, because we have to define the ION resources. We probably want 

    convenience methods to query by name, type, etc... 

     

    TODO have this implement the Indexstore interface 

    """ 

    implements(IIndexStore) 

 

    def __init__(self, proc=None, **kwargs): 

        if not 'targetname' in kwargs: 

            kwargs['targetname'] = 'index_store_service' 

        ServiceClient.__init__(self, proc, **kwargs) 

 

        self.mc = proc.message_client 

 

 

    @defer.inlineCallbacks 

    def query(self, query_predicates): 

        log.info("Called Index Store Service client: Query") 

 

        request = yield self.mc.create_instance(QUERY_ATTRIBUTES_TYPE) 

 

        for attr_key,attr_value,pred_type in query_predicates.get_predicates(): 

            attr = request.attrs.add() 

            attr.attribute_name = str(attr_key) 

            attr.attribute_value = str(attr_value) 

            attr.predicate_type = str(pred_type) 

 

        (result, headers, msg) = yield self.rpc_send('query', request) 

 

 

        results ={} 

        for row in result.rows: 

 

            cols = {'value':row.value} 

 

            for col in row.cols: 

                cols[col.column_name] = col.column_value 

 

            results[row.key] = cols 

 

        defer.returnValue(results) 

 

    @defer.inlineCallbacks 

    def put(self, key, value, index_attributes=None): 

        log.info("Called Index Store Service client: put") 

 

        if index_attributes is None: 

            index_attributes = {} 

 

        row = yield self.mc.create_instance(ROW_TYPE) 

        row.key = key 

        row.value = value 

 

        for attr_key,attr_value in index_attributes.items(): 

            col = row.cols.add() 

            col.column_name = attr_key 

            col.column_value = str(attr_value) 

 

        (content, headers, msg) = yield self.rpc_send('put', row) 

 

 

        defer.returnValue(content) 

 

    @defer.inlineCallbacks 

    def update_index(self, key, index_attributes): 

        """ 

        use 

        """ 

 

        row = yield self.mc.create_instance(ROW_INDEX_UPDATE_TYPE) 

        row.key = key 

 

        for attr_key,attr_value in index_attributes.items(): 

 

            if attr_key == 'value': 

                raise IndexStoreError('Can not update the value column!') 

 

            col = row.cols.add() 

            col.column_name = attr_key 

            col.column_value = str(attr_value) 

 

        (content, headers, msg) = yield self.rpc_send('update_index', row) 

 

 

        defer.returnValue(None) 

 

 

 

    @defer.inlineCallbacks 

    def get(self, key): 

        log.info("Called Index Store Service client: get") 

        row = yield self.mc.create_instance(ROW_TYPE) 

        row.key = key 

 

        (result, headers, msg) = yield self.rpc_send('get',row) 

 

        if not result.value is '': 

            defer.returnValue(result.value) 

        else: 

            defer.returnValue(None) 

 

    @defer.inlineCallbacks 

    def remove(self, key): 

        log.info("Called Index Store Service client: remove") 

        row = yield self.mc.create_instance(ROW_TYPE) 

        row.key = key 

 

        (content, headers, msg) = yield self.rpc_send('remove', row) 

        defer.returnValue(content) 

 

    @defer.inlineCallbacks 

    def has_key(self, key): 

        log.info("Called Index Store Service client: has_key") 

        row = yield self.mc.create_instance(ROW_TYPE) 

        row.key = key 

        (result, headers, msg) = yield self.rpc_send('has_key', row) 

        ret = bool(int(result.value)) 

        log.info("%s" % (ret,)) 

        defer.returnValue(ret) 

 

    @defer.inlineCallbacks 

    def get_query_attributes(self): 

 

        log.info("Called Index Store Service client: get_query_attributes") 

 

        (result, headers, msg) = yield self.rpc_send('get_query_attributes','') 

 

        defer.returnValue(result.attributes)