Skip to content

Commit 6fbe145

Browse files
committed
update code with new sdk
1 parent 58d231c commit 6fbe145

File tree

2 files changed

+44
-42
lines changed

2 files changed

+44
-42
lines changed

src/TablestoreServiceProvider.php

+10-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Dew\TablestoreDriver;
66

7-
use Dew\Tablestore\Tablestore;
7+
use Dew\Acs\Tablestore\TablestoreInstance;
88
use Illuminate\Support\Facades\Cache;
99
use Illuminate\Support\ServiceProvider;
1010

@@ -29,18 +29,15 @@ public function register()
2929
private function registerCacheDriver(): void
3030
{
3131
Cache::extend('tablestore', function ($app, $config) {
32-
$client = new Tablestore(
33-
$config['key'], $config['secret'],
34-
$config['endpoint'], $config['instance'] ?? null
35-
);
36-
37-
if (isset($config['token'])) {
38-
$client->tokenUsing($config['token']);
39-
}
40-
41-
if (isset($config['http'])) {
42-
$client->optionsUsing($config['http']);
43-
}
32+
$client = new TablestoreInstance([
33+
'credentials' => [
34+
'key' => $config['key'],
35+
'secret' => $config['secret'],
36+
'token' => $config['token'] ?? null,
37+
],
38+
'instance' => $config['instance'] ?? null,
39+
'endpoint' => $config['endpoint'],
40+
]);
4441

4542
return Cache::repository(
4643
new TablestoreStore(

src/TablestoreStore.php

+34-29
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44

55
namespace Dew\TablestoreDriver;
66

7-
use Dew\Tablestore\Attribute;
8-
use Dew\Tablestore\Exceptions\TablestoreException;
9-
use Dew\Tablestore\PlainbufferWriter;
10-
use Dew\Tablestore\PrimaryKey;
11-
use Dew\Tablestore\Responses\RowDecodableResponse;
12-
use Dew\Tablestore\Tablestore;
7+
use Dew\Acs\Tablestore\Attribute;
8+
use Dew\Acs\Tablestore\InstanceException;
9+
use Dew\Acs\Tablestore\Messages\ComparatorType;
10+
use Dew\Acs\Tablestore\Messages\Filter;
11+
use Dew\Acs\Tablestore\Messages\FilterType;
12+
use Dew\Acs\Tablestore\Messages\SingleColumnValueFilter;
13+
use Dew\Acs\Tablestore\Plainbuf;
14+
use Dew\Acs\Tablestore\PlainBufferWriter;
15+
use Dew\Acs\Tablestore\PrimaryKey;
16+
use Dew\Acs\Tablestore\TablestoreInstance;
1317
use Illuminate\Contracts\Cache\LockProvider;
1418
use Illuminate\Contracts\Cache\Store;
1519
use Illuminate\Support\Carbon;
1620
use Illuminate\Support\InteractsWithTime;
1721
use InvalidArgumentException;
18-
use Protos\ComparatorType;
19-
use Protos\Filter;
20-
use Protos\FilterType;
21-
use Protos\SingleColumnValueFilter;
2222
use RuntimeException;
2323

2424
final class TablestoreStore implements LockProvider, Store
@@ -34,7 +34,7 @@ final class TablestoreStore implements LockProvider, Store
3434
* Create a Tablestore cache store.
3535
*/
3636
public function __construct(
37-
protected Tablestore $tablestore,
37+
protected TablestoreInstance $tablestore,
3838
protected string $table,
3939
protected string $keyAttribute = 'key',
4040
protected string $valueAttribute = 'value',
@@ -52,16 +52,19 @@ public function __construct(
5252
*/
5353
public function get($key)
5454
{
55-
$item = $this->tablestore->table($this->table)
55+
$buffer = $this->tablestore->table($this->table)
5656
->whereKey($this->keyAttribute, $this->prefix.$key)
5757
->where($this->expirationAttribute, '>', Carbon::now()->getTimestamp())
58-
->get()->getDecodedRow();
58+
->get()
59+
->getRow();
5960

60-
if ($item === null) {
61+
if ($buffer === '') {
6162
return;
6263
}
6364

64-
/** @var \Dew\Tablestore\Contracts\HasValue[] */
65+
$item = Plainbuf::decode($buffer);
66+
67+
/** @var \Dew\Acs\Tablestore\Cells\HasValue[] */
6568
$values = $item[$this->valueAttribute] ?? [];
6669

6770
return isset($values[0]) ? $this->unserialize($values[0]->value()) : null;
@@ -95,23 +98,25 @@ public function many(array $keys)
9598

9699
$result = array_fill_keys($keys, null);
97100

98-
/** @var \Protos\TableInBatchGetRowResponse[] */
101+
/** @var \Dew\Acs\Tablestore\Messages\TableInBatchGetRowResponse[] */
99102
$tables = $response->getTables();
100103

101-
/** @var \Protos\RowInBatchGetRowResponse[] */
104+
/** @var \Dew\Acs\Tablestore\Messages\RowInBatchGetRowResponse[] */
102105
$rows = $tables[0]->getRows();
103106

104107
foreach ($rows as $row) {
105-
$item = (new RowDecodableResponse($row))->getDecodedRow();
108+
$buffer = $row->getRow();
106109

107-
if ($item === null) {
110+
if ($buffer === '') {
108111
continue;
109112
}
110113

111-
/** @var \Dew\Tablestore\Cells\StringPrimaryKey */
114+
$item = Plainbuf::decode($buffer);
115+
116+
/** @var \Dew\Acs\Tablestore\Cells\StringPrimaryKey */
112117
$key = $item[$this->keyAttribute];
113118

114-
/** @var \Dew\Tablestore\Contracts\HasValue[] */
119+
/** @var \Dew\Acs\Tablestore\Cells\HasValue[] */
115120
$values = $item[$this->valueAttribute] ?? [];
116121

117122
if (isset($values[0])) {
@@ -183,7 +188,7 @@ public function add($key, $value, $seconds)
183188
{
184189
try {
185190
Attribute::integer($this->expirationAttribute, Carbon::now()->getTimestamp())
186-
->toFormattedValue($now = new PlainbufferWriter);
191+
->toFormattedValue($now = new PlainBufferWriter);
187192

188193
// Include only items that do not exist or that have expired
189194
// expression: expiration <= now
@@ -205,8 +210,8 @@ public function add($key, $value, $seconds)
205210
Attribute::createFromValue($this->valueAttribute, $this->serialize($value)),
206211
Attribute::integer($this->expirationAttribute, $this->toTimestamp($seconds)),
207212
]);
208-
} catch (TablestoreException $e) {
209-
if ($e->getError()->getCode() === 'OTSConditionCheckFail') {
213+
} catch (InstanceException $e) {
214+
if ($e->getError()?->getCode() === 'OTSConditionCheckFail') {
210215
return false;
211216
}
212217

@@ -235,8 +240,8 @@ public function increment($key, $value = 1)
235240
]);
236241

237242
return true;
238-
} catch (TablestoreException $e) {
239-
if ($e->getError()->getCode() === 'OTSConditionCheckFail') {
243+
} catch (InstanceException $e) {
244+
if ($e->getError()?->getCode() === 'OTSConditionCheckFail') {
240245
return false;
241246
}
242247

@@ -263,8 +268,8 @@ public function decrement($key, $value = 1)
263268
]);
264269

265270
return true;
266-
} catch (TablestoreException $e) {
267-
if ($e->getError()->getCode() === 'OTSConditionCheckFail') {
271+
} catch (InstanceException $e) {
272+
if ($e->getError()?->getCode() === 'OTSConditionCheckFail') {
268273
return false;
269274
}
270275

@@ -399,7 +404,7 @@ private function toTimestamp(int $seconds): int
399404
/**
400405
* The underlying Tablestore client.
401406
*/
402-
public function getClient(): Tablestore
407+
public function getClient(): TablestoreInstance
403408
{
404409
return $this->tablestore;
405410
}

0 commit comments

Comments
 (0)